Skip to content

Commit

Permalink
ut for TiFlash API
Browse files Browse the repository at this point in the history
  • Loading branch information
csuzhangxc committed Dec 25, 2024
1 parent e63568e commit a3f836c
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 5 deletions.
40 changes: 40 additions & 0 deletions pkg/tiflashapi/v1/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2024 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package tiflashapi

import (
"context"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestTiFlash_GetStoreStatus(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "/tiflash/store-status", r.URL.Path)
assert.Equal(t, http.MethodGet, r.Method)
w.Write([]byte("Running"))
}))
defer server.Close()

client := NewTiFlashClient(server.URL, 5*time.Second, nil, false)
status, err := client.GetStoreStatus(context.Background())
require.NoError(t, err)
assert.Equal(t, Running, status)
}
7 changes: 2 additions & 5 deletions pkg/tiflashapi/v1/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
"fmt"
"time"

corelisterv1 "k8s.io/client-go/listers/core/v1"

"github.com/pingcap/tidb-operator/apis/core/v1alpha1"
"github.com/pingcap/tidb-operator/pkg/client"
tlsutil "github.com/pingcap/tidb-operator/pkg/utils/tls"
Expand All @@ -41,12 +39,11 @@ type TiFlashControlInterface interface {

// defaultTiFlashControl is the default implementation of TiFlashControlInterface.
type defaultTiFlashControl struct {
secretLister corelisterv1.SecretLister
}

// NewDefaultTiFlashControl returns a defaultTiFlashControl instance
func NewDefaultTiFlashControl(secretLister corelisterv1.SecretLister) TiFlashControlInterface {
return &defaultTiFlashControl{secretLister: secretLister}
func NewDefaultTiFlashControl() TiFlashControlInterface {
return &defaultTiFlashControl{}
}

// GetTiFlashPodClient provides TiFlashClient of a TiFlash pod.
Expand Down
42 changes: 42 additions & 0 deletions pkg/tiflashapi/v1/control_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2024 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package tiflashapi

import (
"context"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestTiFlashControl(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "/tiflash/store-status", r.URL.Path)
assert.Equal(t, http.MethodGet, r.Method)
w.Write([]byte("Ready"))
}))
defer server.Close()

// only test non-tls
client, err := NewDefaultTiFlashControl().GetTiFlashPodClient(context.Background(), nil, "", "", "", false)
require.NoError(t, err)
client.(*tiflashClient).url = server.URL
status, err := client.GetStoreStatus(context.Background())
require.NoError(t, err)
assert.Equal(t, Ready, status)
}

0 comments on commit a3f836c

Please sign in to comment.