From 0256a16681eb211d094de6a952ea0887c4d4e2c4 Mon Sep 17 00:00:00 2001 From: csuzhangxc Date: Mon, 23 Dec 2024 18:00:12 +0800 Subject: [PATCH 1/4] ut for action --- pkg/action/upgrader_test.go | 86 +++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/pkg/action/upgrader_test.go b/pkg/action/upgrader_test.go index f59a0fae66..fd7c1fe219 100644 --- a/pkg/action/upgrader_test.go +++ b/pkg/action/upgrader_test.go @@ -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" @@ -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) + } + }) + } +} From f5f1e29f94da0bd021752c29b496876600c243b8 Mon Sep 17 00:00:00 2001 From: csuzhangxc Date: Mon, 23 Dec 2024 18:06:18 +0800 Subject: [PATCH 2/4] ignore coverage for fake.go --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index f656a65eef..990fd026cc 100644 --- a/Makefile +++ b/Makefile @@ -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 From 96ede0bcd94b346ec99a820f6b80eb51917ac342 Mon Sep 17 00:00:00 2001 From: csuzhangxc Date: Tue, 24 Dec 2024 11:01:05 +0800 Subject: [PATCH 3/4] ut for overlay --- pkg/overlay/overlay_test.go | 61 +++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/pkg/overlay/overlay_test.go b/pkg/overlay/overlay_test.go index 5bcc06e407..416ac7cc53 100644 --- a/pkg/overlay/overlay_test.go +++ b/pkg/overlay/overlay_test.go @@ -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" ) @@ -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) } From 222d7e58dd4e19ab0a54cd53c966133b3cd66445 Mon Sep 17 00:00:00 2001 From: csuzhangxc Date: Tue, 24 Dec 2024 11:56:45 +0800 Subject: [PATCH 4/4] UT for PD API struct --- pkg/pdapi/pd/duration.go | 2 +- pkg/pdapi/pd/duration_test.go | 42 ++++++++++++++++++++++++++++++ pkg/pdapi/pd/size_test.go | 48 +++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 pkg/pdapi/pd/duration_test.go create mode 100644 pkg/pdapi/pd/size_test.go diff --git a/pkg/pdapi/pd/duration.go b/pkg/pdapi/pd/duration.go index d7cc8c5963..a2b620f37f 100644 --- a/pkg/pdapi/pd/duration.go +++ b/pkg/pdapi/pd/duration.go @@ -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. diff --git a/pkg/pdapi/pd/duration_test.go b/pkg/pdapi/pd/duration_test.go new file mode 100644 index 0000000000..6178954268 --- /dev/null +++ b/pkg/pdapi/pd/duration_test.go @@ -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) +} diff --git a/pkg/pdapi/pd/size_test.go b/pkg/pdapi/pd/size_test.go new file mode 100644 index 0000000000..a1e9224e33 --- /dev/null +++ b/pkg/pdapi/pd/size_test.go @@ -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) +}