|
| 1 | +// Copyright 2024 PingCAP, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package common |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/prometheus/client_golang/prometheus" |
| 22 | + dto "github.com/prometheus/client_model/go" |
| 23 | + "github.com/stretchr/testify/assert" |
| 24 | + "github.com/stretchr/testify/require" |
| 25 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | + |
| 27 | + "github.com/pingcap/tidb-operator/api/v2/core/v1alpha1" |
| 28 | + "github.com/pingcap/tidb-operator/v2/pkg/metrics" |
| 29 | + "github.com/pingcap/tidb-operator/v2/pkg/runtime/scope" |
| 30 | + "github.com/pingcap/tidb-operator/v2/pkg/utils/fake" |
| 31 | + "github.com/pingcap/tidb-operator/v2/pkg/utils/task/v3" |
| 32 | +) |
| 33 | + |
| 34 | +// hasGaugeSample returns true if the AbnormalInstance gauge has any sample |
| 35 | +// whose (namespace, instance) labels match the inputs. |
| 36 | +func hasGaugeSample(t *testing.T, namespace, instance string) bool { |
| 37 | + t.Helper() |
| 38 | + ch := make(chan prometheus.Metric, 32) |
| 39 | + metrics.AbnormalInstance.Collect(ch) |
| 40 | + close(ch) |
| 41 | + for m := range ch { |
| 42 | + dm := &dto.Metric{} |
| 43 | + if err := m.Write(dm); err != nil { |
| 44 | + continue |
| 45 | + } |
| 46 | + labels := map[string]string{} |
| 47 | + for _, lp := range dm.GetLabel() { |
| 48 | + labels[lp.GetName()] = lp.GetValue() |
| 49 | + } |
| 50 | + if labels["namespace"] == namespace && labels["instance"] == instance { |
| 51 | + return true |
| 52 | + } |
| 53 | + } |
| 54 | + return false |
| 55 | +} |
| 56 | + |
| 57 | +func TestTaskObserveInstance_ObservesConditions(t *testing.T) { |
| 58 | + const ns, name = "ns-observe", "pd-observe" |
| 59 | + defer metrics.ClearInstanceConditionMetricsByKey(ns, v1alpha1.LabelValComponentPD, name) |
| 60 | + |
| 61 | + obj := fake.FakeObj(name, func(o *v1alpha1.PD) *v1alpha1.PD { |
| 62 | + o.Namespace = ns |
| 63 | + o.Labels = map[string]string{ |
| 64 | + v1alpha1.LabelKeyCluster: "c", |
| 65 | + v1alpha1.LabelKeyComponent: "pd", |
| 66 | + v1alpha1.LabelKeyGroup: "g", |
| 67 | + } |
| 68 | + o.Status.Conditions = []metav1.Condition{ |
| 69 | + {Type: v1alpha1.CondReady, Status: metav1.ConditionFalse}, |
| 70 | + {Type: v1alpha1.CondSynced, Status: metav1.ConditionTrue}, |
| 71 | + } |
| 72 | + return o |
| 73 | + }) |
| 74 | + state := &fakeState[v1alpha1.PD]{ns: ns, name: name, obj: obj} |
| 75 | + |
| 76 | + res, done := task.RunTask(context.Background(), TaskObserveInstance[scope.PD](state)) |
| 77 | + require.Equal(t, task.SComplete, res.Status()) |
| 78 | + require.False(t, done) |
| 79 | + assert.True(t, hasGaugeSample(t, ns, name), |
| 80 | + "ObserveInstance must write at least one series for the instance") |
| 81 | +} |
| 82 | + |
| 83 | +func TestTaskObserveInstance_ClearsOnMissingObject(t *testing.T) { |
| 84 | + const ns, name = "ns-clear", "pd-clear" |
| 85 | + |
| 86 | + // Pre-populate a series as if a previous reconcile had observed the instance. |
| 87 | + seed := fake.FakeObj(name, func(o *v1alpha1.PD) *v1alpha1.PD { |
| 88 | + o.Namespace = ns |
| 89 | + o.Labels = map[string]string{ |
| 90 | + v1alpha1.LabelKeyCluster: "c", |
| 91 | + v1alpha1.LabelKeyComponent: "pd", |
| 92 | + v1alpha1.LabelKeyGroup: "g", |
| 93 | + } |
| 94 | + return o |
| 95 | + }) |
| 96 | + metrics.ObserveConditions(seed, []metav1.Condition{ |
| 97 | + {Type: v1alpha1.CondReady, Status: metav1.ConditionFalse}, |
| 98 | + }) |
| 99 | + require.True(t, hasGaugeSample(t, ns, name), "test precondition: seed series exists") |
| 100 | + |
| 101 | + // Simulate a reconcile where TaskContextObject saw NotFound. |
| 102 | + state := &fakeState[v1alpha1.PD]{ns: ns, name: name, obj: nil} |
| 103 | + |
| 104 | + res, done := task.RunTask(context.Background(), TaskObserveInstance[scope.PD](state)) |
| 105 | + require.Equal(t, task.SComplete, res.Status()) |
| 106 | + require.False(t, done) |
| 107 | + assert.False(t, hasGaugeSample(t, ns, name), |
| 108 | + "ObserveInstance must clear every series for the instance when the object is gone") |
| 109 | +} |
0 commit comments