Skip to content

Commit

Permalink
chore: add some UT for some pkgs (#6009)
Browse files Browse the repository at this point in the history
  • Loading branch information
csuzhangxc authored Dec 24, 2024
1 parent 43de8f5 commit a716b34
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ lint: bin/golangci-lint
unit:
go test $$(go list -e ./... | grep -v cmd | grep -v tools | grep -v tests | grep -v third_party) \
-cover -coverprofile=coverage.txt -covermode=atomic
sed -i.bak '/generated/d' coverage.txt && rm coverage.txt.bak
sed -i.bak '/generated/d;/fake.go/d' coverage.txt && rm coverage.txt.bak

.PHONY: check
check: lint unit verify
Expand Down
86 changes: 86 additions & 0 deletions pkg/action/upgrader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import (
"reflect"
"testing"

"github.com/go-logr/logr"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/pingcap/tidb-operator/apis/core/v1alpha1"
"github.com/pingcap/tidb-operator/pkg/client"
"github.com/pingcap/tidb-operator/pkg/utils/fake"
Expand Down Expand Up @@ -183,3 +186,86 @@ func Test_getDependentGroups(t *testing.T) {
})
}
}

func TestUpgradePolicy(t *testing.T) {
pdg := &v1alpha1.PDGroup{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test",
Name: "pd",
Labels: map[string]string{
v1alpha1.LabelKeyCluster: "tc",
v1alpha1.LabelKeyComponent: v1alpha1.LabelValComponentPD,
},
},
Spec: v1alpha1.PDGroupSpec{
Cluster: v1alpha1.ClusterReference{
Name: "tc",
},
Version: "v8.5.0",
},
Status: v1alpha1.PDGroupStatus{
GroupStatus: v1alpha1.GroupStatus{
Version: "v8.1.0", // not upgraded
},
},
}
kvg := &v1alpha1.TiKVGroup{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test",
Name: "tikv",
Labels: map[string]string{
v1alpha1.LabelKeyCluster: "tc",
v1alpha1.LabelKeyComponent: v1alpha1.LabelValComponentTiKV,
},
},
Spec: v1alpha1.TiKVGroupSpec{
Cluster: v1alpha1.ClusterReference{
Name: "tc",
},
Version: "v8.5.0",
},
Status: v1alpha1.TiKVGroupStatus{
GroupStatus: v1alpha1.GroupStatus{
Version: "v8.1.0", // not upgraded
},
},
}

tests := []struct {
name string
policy v1alpha1.UpgradePolicy
canUpgrade bool
}{
{
name: "no constraints",
policy: v1alpha1.UpgradePolicyNoConstraints,
canUpgrade: true,
},
{
name: "default policy",
policy: v1alpha1.UpgradePolicyDefault,
canUpgrade: false,
},
{
name: "unknown policy",
policy: "unknown",
canUpgrade: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cluster := v1alpha1.Cluster{
Spec: v1alpha1.ClusterSpec{
UpgradePolicy: tt.policy,
},
}
cli := client.NewFakeClient(pdg, kvg)
checker := NewUpgradeChecker(cli, &cluster, logr.Discard())
got := checker.CanUpgrade(context.TODO(), kvg)
if got != tt.canUpgrade {
t.Errorf("CanUpgrade() got = %v, want %v", got, tt.canUpgrade)
}
})
}
}
61 changes: 61 additions & 0 deletions pkg/overlay/overlay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ import (
"testing"

"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"

"github.com/pingcap/tidb-operator/apis/core/v1alpha1"
"github.com/pingcap/tidb-operator/pkg/utils/random"
)

Expand All @@ -43,6 +46,64 @@ const (
NoNotEqual
)

func TestOverlayPod(t *testing.T) {
base := corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"aa": "aa",
"zz": "123",
},
Labels: map[string]string{
"aa": "aa",
"zz": "123",
},
},
Spec: corev1.PodSpec{
NodeSelector: map[string]string{
"aa": "aa",
"zz": "123",
},
},
}
overlay := v1alpha1.PodOverlay{
ObjectMeta: v1alpha1.ObjectMeta{
Annotations: map[string]string{
"bb": "bb",
"zz": "456",
},
Labels: map[string]string{
"bb": "bb",
"zz": "456",
},
},
Spec: &corev1.PodSpec{
NodeSelector: map[string]string{
"bb": "bb",
"zz": "456",
},
TerminationGracePeriodSeconds: ptr.To[int64](100),
},
}
OverlayPod(&base, &overlay)

assert.Equal(t, map[string]string{
"aa": "aa",
"bb": "bb",
"zz": "456",
}, base.ObjectMeta.Annotations)
assert.Equal(t, map[string]string{
"aa": "aa",
"bb": "bb",
"zz": "456",
}, base.ObjectMeta.Labels)
assert.Equal(t, map[string]string{
"aa": "aa",
"bb": "bb",
"zz": "123", // special case
}, base.Spec.NodeSelector)
assert.Equal(t, int64(100), *base.Spec.TerminationGracePeriodSeconds)
}

func randString() string {
return random.Random(10)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/pdapi/pd/duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func NewDuration(duration time.Duration) Duration {

// MarshalJSON returns the duration as a JSON string.
func (d *Duration) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%q"`, d.String())), nil
return []byte(fmt.Sprintf(`%q`, d.String())), nil
}

// UnmarshalJSON parses a JSON string into the duration.
Expand Down
42 changes: 42 additions & 0 deletions pkg/pdapi/pd/duration_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 pd

import (
"testing"
"time"

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

func TestDuration(t *testing.T) {
du := NewDuration(72*time.Hour + 3*time.Minute + 500*time.Millisecond)
data, err := du.MarshalJSON()
require.NoError(t, err)
assert.Equal(t, `"72h3m0.5s"`, string(data))
data2, err := du.MarshalText()
require.NoError(t, err)
assert.Equal(t, "72h3m0.5s", string(data2))

var du2 Duration
err = du2.UnmarshalJSON(data)
require.NoError(t, err)
assert.Equal(t, du, du2)

err = du2.UnmarshalText(data2)
require.NoError(t, err)
assert.Equal(t, du, du2)
}
48 changes: 48 additions & 0 deletions pkg/pdapi/pd/size_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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 pd

import (
"testing"

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

func TestParseMBFromText(t *testing.T) {
value := uint64(512)
result := ParseMBFromText("1.0 MiB", value)
assert.Equal(t, uint64(1), result)

result = ParseMBFromText("invalid", value)
assert.Equal(t, value, result)
}

func TestByteSize(t *testing.T) {
b := ByteSize(17 * 1024 * 1024) // 17 MiB
data, err := b.MarshalJSON()
require.NoError(t, err)
assert.Equal(t, `"17MiB"`, string(data))

var b2 ByteSize
err = b2.UnmarshalJSON(data)
require.NoError(t, err)
assert.Equal(t, b, b2)

data2 := []byte("17MiB")
err = b2.UnmarshalText(data2)
require.NoError(t, err)
assert.Equal(t, b, b2)
}

0 comments on commit a716b34

Please sign in to comment.