|
| 1 | +package utils |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 7 | + "k8s.io/apimachinery/pkg/runtime" |
| 8 | + addonsv1alpha1 "sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/addon/pkg/apis/v1alpha1" |
| 9 | +) |
| 10 | + |
| 11 | +type mockCommonObject struct { |
| 12 | + addonsv1alpha1.CommonObject |
| 13 | + name string |
| 14 | +} |
| 15 | + |
| 16 | +func (m *mockCommonObject) ComponentName() string { |
| 17 | + return m.name |
| 18 | +} |
| 19 | + |
| 20 | +func TestGetCommonName(t *testing.T) { |
| 21 | + type args struct { |
| 22 | + instance runtime.Object |
| 23 | + } |
| 24 | + tests := []struct { |
| 25 | + name string |
| 26 | + args args |
| 27 | + want string |
| 28 | + wantErr bool |
| 29 | + }{ |
| 30 | + { |
| 31 | + name: "CommonObject instance", |
| 32 | + args: args{ |
| 33 | + instance: &mockCommonObject{name: "test-component"}, |
| 34 | + }, |
| 35 | + want: "test-component", |
| 36 | + wantErr: false, |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "Unstructured instance", |
| 40 | + args: args{ |
| 41 | + instance: &unstructured.Unstructured{ |
| 42 | + Object: map[string]interface{}{ |
| 43 | + "kind": "TestKind", |
| 44 | + }, |
| 45 | + }, |
| 46 | + }, |
| 47 | + want: "testkind", |
| 48 | + wantErr: false, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "Invalid instance", |
| 52 | + args: args{ |
| 53 | + instance: &runtime.Unknown{}, |
| 54 | + }, |
| 55 | + want: "", |
| 56 | + wantErr: true, |
| 57 | + }, |
| 58 | + } |
| 59 | + for _, tt := range tests { |
| 60 | + t.Run(tt.name, func(t *testing.T) { |
| 61 | + got, err := GetCommonName(tt.args.instance) |
| 62 | + if (err != nil) != tt.wantErr { |
| 63 | + t.Errorf("GetCommonName() error = %v, wantErr %v", err, tt.wantErr) |
| 64 | + return |
| 65 | + } |
| 66 | + if got != tt.want { |
| 67 | + t.Errorf("GetCommonName() = %v, want %v", got, tt.want) |
| 68 | + } |
| 69 | + }) |
| 70 | + } |
| 71 | +} |
0 commit comments