|
| 1 | +package cache |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/argoproj/gitops-engine/pkg/utils/kube" |
| 5 | + "github.com/stretchr/testify/assert" |
| 6 | + appsv1 "k8s.io/api/apps/v1" |
| 7 | + corev1 "k8s.io/api/core/v1" |
| 8 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 9 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 10 | + "testing" |
| 11 | +) |
| 12 | + |
| 13 | +func Test_isStatefulSetChild(t *testing.T) { |
| 14 | + type args struct { |
| 15 | + un *unstructured.Unstructured |
| 16 | + } |
| 17 | + |
| 18 | + statefulSet := &appsv1.StatefulSet{ |
| 19 | + TypeMeta: metav1.TypeMeta{ |
| 20 | + APIVersion: "apps/v1", |
| 21 | + Kind: "StatefulSet", |
| 22 | + }, |
| 23 | + ObjectMeta: metav1.ObjectMeta{ |
| 24 | + Name: "sw-broker", |
| 25 | + }, |
| 26 | + Spec: appsv1.StatefulSetSpec{ |
| 27 | + VolumeClaimTemplates: []corev1.PersistentVolumeClaim{ |
| 28 | + { |
| 29 | + ObjectMeta: metav1.ObjectMeta{ |
| 30 | + Name: "emqx-data", |
| 31 | + }, |
| 32 | + }, |
| 33 | + }, |
| 34 | + }, |
| 35 | + } |
| 36 | + |
| 37 | + // Create a new unstructured object from the JSON string |
| 38 | + un, err := kube.ToUnstructured(statefulSet) |
| 39 | + if err != nil { |
| 40 | + t.Errorf("Failed to convert StatefulSet to unstructured: %v", err) |
| 41 | + } |
| 42 | + |
| 43 | + tests := []struct { |
| 44 | + name string |
| 45 | + args args |
| 46 | + wantErr bool |
| 47 | + checkFunc func(func(kube.ResourceKey) bool) bool |
| 48 | + }{ |
| 49 | + { |
| 50 | + name: "Valid PVC for sw-broker", |
| 51 | + args: args{un: un}, |
| 52 | + wantErr: false, |
| 53 | + checkFunc: func(fn func(kube.ResourceKey) bool) bool { |
| 54 | + // Check a valid PVC name for "sw-broker" |
| 55 | + return fn(kube.ResourceKey{Kind: "PersistentVolumeClaim", Name: "emqx-data-sw-broker-0"}) |
| 56 | + }, |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "Invalid PVC for sw-broker", |
| 60 | + args: args{un: un}, |
| 61 | + wantErr: false, |
| 62 | + checkFunc: func(fn func(kube.ResourceKey) bool) bool { |
| 63 | + // Check an invalid PVC name that should belong to "sw-broker-internal" |
| 64 | + return !fn(kube.ResourceKey{Kind: "PersistentVolumeClaim", Name: "emqx-data-sw-broker-internal-0"}) |
| 65 | + }, |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "Mismatch PVC for sw-broker", |
| 69 | + args: args{un: &unstructured.Unstructured{ |
| 70 | + Object: map[string]interface{}{ |
| 71 | + "apiVersion": "apps/v1", |
| 72 | + "kind": "StatefulSet", |
| 73 | + "metadata": map[string]interface{}{ |
| 74 | + "name": "sw-broker", |
| 75 | + }, |
| 76 | + "spec": map[string]interface{}{ |
| 77 | + "volumeClaimTemplates": []interface{}{ |
| 78 | + map[string]interface{}{ |
| 79 | + "metadata": map[string]interface{}{ |
| 80 | + "name": "volume-2", |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + }}, |
| 87 | + wantErr: false, |
| 88 | + checkFunc: func(fn func(kube.ResourceKey) bool) bool { |
| 89 | + // Check an invalid PVC name for "api-test" |
| 90 | + return !fn(kube.ResourceKey{Kind: "PersistentVolumeClaim", Name: "volume-2"}) |
| 91 | + }, |
| 92 | + }, |
| 93 | + } |
| 94 | + |
| 95 | + // Execute test cases |
| 96 | + for _, tt := range tests { |
| 97 | + t.Run(tt.name, func(t *testing.T) { |
| 98 | + got, err := isStatefulSetChild(tt.args.un) |
| 99 | + assert.Equal(t, tt.wantErr, err != nil, "isStatefulSetChild() error = %v, wantErr %v", err, tt.wantErr) |
| 100 | + if err == nil { |
| 101 | + assert.True(t, tt.checkFunc(got), "Check function failed for %v", tt.name) |
| 102 | + } |
| 103 | + }) |
| 104 | + } |
| 105 | +} |
0 commit comments