|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" |
| 7 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 8 | + |
| 9 | + "github.com/kro-run/kro/pkg/metadata" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +func TestCRDWrapper_verifyNoConflict(t *testing.T) { |
| 14 | + tests := []struct { |
| 15 | + name string |
| 16 | + existingCRD *v1.CustomResourceDefinition |
| 17 | + newCRD v1.CustomResourceDefinition |
| 18 | + wantErr bool |
| 19 | + errMsg string |
| 20 | + }{ |
| 21 | + { |
| 22 | + name: "successful verification - same owner", |
| 23 | + existingCRD: &v1.CustomResourceDefinition{ |
| 24 | + ObjectMeta: metav1.ObjectMeta{ |
| 25 | + Name: "test.crd", |
| 26 | + Labels: map[string]string{ |
| 27 | + metadata.OwnedLabel: "true", |
| 28 | + metadata.ResourceGraphDefinitionNameLabel: "test-rgd", |
| 29 | + metadata.ResourceGraphDefinitionIDLabel: "test-id", |
| 30 | + }, |
| 31 | + }, |
| 32 | + }, |
| 33 | + newCRD: v1.CustomResourceDefinition{ |
| 34 | + ObjectMeta: metav1.ObjectMeta{ |
| 35 | + Name: "test.crd", |
| 36 | + Labels: map[string]string{ |
| 37 | + metadata.OwnedLabel: "true", |
| 38 | + metadata.ResourceGraphDefinitionNameLabel: "test-rgd", |
| 39 | + metadata.ResourceGraphDefinitionIDLabel: "test-id", |
| 40 | + }, |
| 41 | + }, |
| 42 | + }, |
| 43 | + wantErr: false, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "conflict - not owned by KRO", |
| 47 | + existingCRD: &v1.CustomResourceDefinition{ |
| 48 | + ObjectMeta: metav1.ObjectMeta{ |
| 49 | + Name: "test.crd", |
| 50 | + Labels: map[string]string{ |
| 51 | + metadata.ResourceGraphDefinitionNameLabel: "test-rgd", |
| 52 | + metadata.ResourceGraphDefinitionIDLabel: "test-id", |
| 53 | + }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + newCRD: v1.CustomResourceDefinition{ |
| 57 | + ObjectMeta: metav1.ObjectMeta{ |
| 58 | + Name: "test.crd", |
| 59 | + Labels: map[string]string{ |
| 60 | + metadata.OwnedLabel: "true", |
| 61 | + metadata.ResourceGraphDefinitionNameLabel: "test-rgd", |
| 62 | + metadata.ResourceGraphDefinitionIDLabel: "test-id", |
| 63 | + }, |
| 64 | + }, |
| 65 | + }, |
| 66 | + wantErr: true, |
| 67 | + errMsg: "conflict detected: CRD test.crd already exists and is not owned by KRO", |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "conflict - different RGD name", |
| 71 | + existingCRD: &v1.CustomResourceDefinition{ |
| 72 | + ObjectMeta: metav1.ObjectMeta{ |
| 73 | + Name: "test.crd", |
| 74 | + Labels: map[string]string{ |
| 75 | + metadata.OwnedLabel: "true", |
| 76 | + metadata.ResourceGraphDefinitionNameLabel: "existing-rgd", |
| 77 | + metadata.ResourceGraphDefinitionIDLabel: "test-id", |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + newCRD: v1.CustomResourceDefinition{ |
| 82 | + ObjectMeta: metav1.ObjectMeta{ |
| 83 | + Name: "test.crd", |
| 84 | + Labels: map[string]string{ |
| 85 | + metadata.OwnedLabel: "true", |
| 86 | + metadata.ResourceGraphDefinitionNameLabel: "new-rgd", |
| 87 | + metadata.ResourceGraphDefinitionIDLabel: "test-id", |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + wantErr: true, |
| 92 | + errMsg: "conflict detected: CRD test.crd has ownership by another ResourceGraphDefinition", |
| 93 | + }, |
| 94 | + { |
| 95 | + name: "conflict - different RGD ID", |
| 96 | + existingCRD: &v1.CustomResourceDefinition{ |
| 97 | + ObjectMeta: metav1.ObjectMeta{ |
| 98 | + Name: "test.crd", |
| 99 | + Labels: map[string]string{ |
| 100 | + metadata.OwnedLabel: "true", |
| 101 | + metadata.ResourceGraphDefinitionNameLabel: "test-rgd", |
| 102 | + metadata.ResourceGraphDefinitionIDLabel: "existing-id", |
| 103 | + }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + newCRD: v1.CustomResourceDefinition{ |
| 107 | + ObjectMeta: metav1.ObjectMeta{ |
| 108 | + Name: "test.crd", |
| 109 | + Labels: map[string]string{ |
| 110 | + metadata.OwnedLabel: "true", |
| 111 | + metadata.ResourceGraphDefinitionNameLabel: "test-rgd", |
| 112 | + metadata.ResourceGraphDefinitionIDLabel: "new-id", |
| 113 | + }, |
| 114 | + }, |
| 115 | + }, |
| 116 | + wantErr: true, |
| 117 | + errMsg: "conflict detected: CRD test.crd has ownership by another ResourceGraphDefinition", |
| 118 | + }, |
| 119 | + } |
| 120 | + |
| 121 | + for _, tt := range tests { |
| 122 | + t.Run(tt.name, func(t *testing.T) { |
| 123 | + w := &CRDWrapper{} |
| 124 | + err := w.verifyNoConflict(tt.existingCRD, tt.newCRD) |
| 125 | + |
| 126 | + if tt.wantErr { |
| 127 | + assert.Error(t, err) |
| 128 | + assert.Equal(t, tt.errMsg, err.Error()) |
| 129 | + } else { |
| 130 | + assert.NoError(t, err) |
| 131 | + } |
| 132 | + }) |
| 133 | + } |
| 134 | +} |
0 commit comments