Skip to content

Commit

Permalink
Add unit tests to utils (#192)
Browse files Browse the repository at this point in the history
  • Loading branch information
weekface authored and tennix committed Nov 23, 2018
1 parent a4ed119 commit a4090a6
Show file tree
Hide file tree
Showing 8 changed files with 551 additions and 35 deletions.
11 changes: 4 additions & 7 deletions pkg/apis/pingcap.com/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ import (
)

const (
// AnnotationStorageSize is a storage size annotation key
AnnotationStorageSize string = "storage.pingcap.com/size"

// TiKVStateUp represents status of Up of TiKV
TiKVStateUp string = "Up"
// TiKVStateDown represents status of Down of TiKV
TiKVStateDown string = "Down"
// TiKVStateOffline represents status of Offline of TiKV
TiKVStateOffline string = "Offline"
// TiKVStateTombstone represents status of Tombstone of TiKV
TiKVStateTombstone string = "Tombstone"
)

// MemberType represents member type
Expand All @@ -37,16 +38,12 @@ type MemberType string
const (
// PDMemberType is pd container type
PDMemberType MemberType = "pd"

// TiDBMemberType is tidb container type
TiDBMemberType MemberType = "tidb"

// TiKVMemberType is tikv container type
TiKVMemberType MemberType = "tikv"

//PushGatewayMemberType is pushgateway container type
PushGatewayMemberType MemberType = "pushgateway"

// UnknownMemberType is unknown container type
UnknownMemberType MemberType = "unknown"
)
Expand Down
4 changes: 3 additions & 1 deletion pkg/controller/pd_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ import (
)

const (
timeout = 5 * time.Second
timeout = 5 * time.Second

// https://github.com/pingcap/pd/blob/master/server/coordinator.go#L42-L45
schedulerExisted = "scheduler existed"
schedulerNotFound = "scheduler not found"
)
Expand Down
3 changes: 1 addition & 2 deletions pkg/controller/tidbcluster/tidb_cluster_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/pingcap/tidb-operator/pkg/apis/pingcap.com/v1alpha1"
"github.com/pingcap/tidb-operator/pkg/controller"
"github.com/pingcap/tidb-operator/pkg/manager"
"github.com/pingcap/tidb-operator/pkg/util"
apiequality "k8s.io/apimachinery/pkg/api/equality"
errorutils "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/client-go/tools/record"
Expand Down Expand Up @@ -161,7 +160,7 @@ func (tcc *defaultTidbClusterControl) IsTiKVAvailable(tc *v1alpha1.TidbCluster)

var availableNum int32
for _, store := range tc.Status.TiKV.Stores {
if store.State == util.StoreUpState {
if store.State == v1alpha1.TiKVStateUp {
availableNum++
}
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/manager/member/tikv_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/pingcap/tidb-operator/pkg/apis/pingcap.com/v1alpha1"
"github.com/pingcap/tidb-operator/pkg/controller"
"github.com/pingcap/tidb-operator/pkg/label"
"github.com/pingcap/tidb-operator/pkg/util"
apps "k8s.io/api/apps/v1beta1"
corelisters "k8s.io/client-go/listers/core/v1"
)
Expand Down Expand Up @@ -86,7 +85,7 @@ func (tsd *tikvScaler) ScaleIn(tc *v1alpha1.TidbCluster, oldSet *apps.StatefulSe
resetReplicas(newSet, oldSet)
return err
}
if state != util.StoreOfflineState {
if state != v1alpha1.TiKVStateOffline {
if err := tsd.pdControl.GetPDClient(tc).DeleteStore(id); err != nil {
resetReplicas(newSet, oldSet)
return err
Expand Down
7 changes: 3 additions & 4 deletions pkg/manager/member/tikv_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/pingcap/tidb-operator/pkg/apis/pingcap.com/v1alpha1"
"github.com/pingcap/tidb-operator/pkg/controller"
"github.com/pingcap/tidb-operator/pkg/label"
"github.com/pingcap/tidb-operator/pkg/util"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -284,7 +283,7 @@ func TestTiKVScalerScaleIn(t *testing.T) {
storeFun: func(tc *v1alpha1.TidbCluster) {
normalStoreFun(tc)
store := tc.Status.TiKV.Stores["1"]
store.State = util.StoreOfflineState
store.State = v1alpha1.TiKVStateOffline
tc.Status.TiKV.Stores["1"] = store
},
delStoreErr: false,
Expand Down Expand Up @@ -379,7 +378,7 @@ func normalStoreFun(tc *v1alpha1.TidbCluster) {
"1": {
ID: "1",
PodName: ordinalPodName(v1alpha1.TiKVMemberType, tc.GetName(), 4),
State: util.StoreUpState,
State: v1alpha1.TiKVStateUp,
},
}
}
Expand All @@ -389,7 +388,7 @@ func tombstoneStoreFun(tc *v1alpha1.TidbCluster) {
"1": {
ID: "1",
PodName: ordinalPodName(v1alpha1.TiKVMemberType, tc.GetName(), 4),
State: util.StoreTombstoneState,
State: v1alpha1.TiKVStateTombstone,
},
}
}
Expand Down
21 changes: 5 additions & 16 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,6 @@ var (
}
)

const (
// StoreUpState is state when tikv store is normal
StoreUpState = "Up"
// StoreOfflineState is state when tikv store is offline
StoreOfflineState = "Offline"
// StoreDownState is state when tikv store is down
StoreDownState = "Down"
// StoreTombstoneState is state when tikv store is tombstone
StoreTombstoneState = "Tombstone"
)

// AntiAffinityForPod creates a PodAntiAffinity with antiLabels
func AntiAffinityForPod(namespace string, antiLabels map[string]string) *corev1.PodAntiAffinity {
keys := []string{}
Expand Down Expand Up @@ -114,12 +103,12 @@ func AffinityForNodeSelector(namespace string, required bool, antiLabels, select
preferredTerms := []corev1.PreferredSchedulingTerm{}
exps := []corev1.NodeSelectorRequirement{}
for _, key := range keys {
if selector[key] == "" {
continue
}
values := strings.Split(selector[key], ",")
// region,zone,rack,host are preferred labels, others are must match labels
if weight, ok := topologySchedulingWeight[key]; ok {
if selector[key] == "" {
continue
}
values := strings.Split(selector[key], ",")
t := corev1.PreferredSchedulingTerm{
Weight: weight,
Preference: corev1.NodeSelectorTerm{
Expand All @@ -137,7 +126,7 @@ func AffinityForNodeSelector(namespace string, required bool, antiLabels, select
requirement := corev1.NodeSelectorRequirement{
Key: key,
Operator: corev1.NodeSelectorOpIn,
Values: []string{selector[key]},
Values: values,
}
// NodeSelectorRequirement in the same MatchExpressions are ANDed otherwise ORed
exps = append(exps, requirement)
Expand Down
Loading

0 comments on commit a4090a6

Please sign in to comment.