|
| 1 | +package storageclasses |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/loft-sh/vcluster/pkg/syncer/synccontext" |
| 7 | + syncertesting "github.com/loft-sh/vcluster/pkg/syncer/testing" |
| 8 | + testingutil "github.com/loft-sh/vcluster/pkg/util/testing" |
| 9 | + "github.com/loft-sh/vcluster/pkg/util/translate" |
| 10 | + "gotest.tools/assert" |
| 11 | + storagev1 "k8s.io/api/storage/v1" |
| 12 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 13 | + "k8s.io/apimachinery/pkg/runtime" |
| 14 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 15 | +) |
| 16 | + |
| 17 | +func TestFromHostSync(t *testing.T) { |
| 18 | + translate.Default = translate.NewSingleNamespaceTranslator(testingutil.DefaultTestTargetNamespace) |
| 19 | + const storageClassName = "test-storageclass" |
| 20 | + |
| 21 | + pObject := &storagev1.StorageClass{ |
| 22 | + ObjectMeta: metav1.ObjectMeta{ |
| 23 | + Name: storageClassName, |
| 24 | + ResourceVersion: syncertesting.FakeClientResourceVersion, |
| 25 | + Labels: map[string]string{ |
| 26 | + "example.com/label-a": "test-1", |
| 27 | + "example.com/label-b": "test-2", |
| 28 | + }, |
| 29 | + Annotations: map[string]string{ |
| 30 | + "example.com/annotation-a": "test-1", |
| 31 | + "example.com/annotation-b": "test-2", |
| 32 | + }, |
| 33 | + }, |
| 34 | + Provisioner: "my-provisioner", |
| 35 | + } |
| 36 | + vObject := &storagev1.StorageClass{ |
| 37 | + ObjectMeta: metav1.ObjectMeta{ |
| 38 | + Name: storageClassName, |
| 39 | + ResourceVersion: syncertesting.FakeClientResourceVersion, |
| 40 | + Labels: map[string]string{ |
| 41 | + "example.com/label-a": "test-1", |
| 42 | + "example.com/label-b": "test-2", |
| 43 | + }, |
| 44 | + Annotations: map[string]string{ |
| 45 | + "example.com/annotation-a": "test-1", |
| 46 | + "example.com/annotation-b": "test-2", |
| 47 | + }, |
| 48 | + }, |
| 49 | + Provisioner: "my-provisioner", |
| 50 | + } |
| 51 | + pObjectUpdated := pObject.DeepCopy() |
| 52 | + pObjectUpdated.Labels["example.com/label-c"] = "test-3" |
| 53 | + pObjectUpdated.Annotations["example.com/annotation-c"] = "test-3" |
| 54 | + pObjectUpdated.Parameters = map[string]string{ |
| 55 | + "test": "value", |
| 56 | + } |
| 57 | + vObjectUpdated := vObject.DeepCopy() |
| 58 | + vObjectUpdated.Labels["example.com/label-c"] = "test-3" |
| 59 | + vObjectUpdated.Annotations["example.com/annotation-c"] = "test-3" |
| 60 | + vObjectUpdated.Parameters = map[string]string{ |
| 61 | + "test": "value", |
| 62 | + } |
| 63 | + |
| 64 | + syncertesting.RunTests(t, []*syncertesting.SyncTest{ |
| 65 | + { |
| 66 | + Name: "Sync new host resource to virtual", |
| 67 | + InitialPhysicalState: []runtime.Object{pObject.DeepCopy()}, |
| 68 | + ExpectedPhysicalState: map[schema.GroupVersionKind][]runtime.Object{ |
| 69 | + storagev1.SchemeGroupVersion.WithKind("StorageClass"): {pObject}, |
| 70 | + }, |
| 71 | + ExpectedVirtualState: map[schema.GroupVersionKind][]runtime.Object{ |
| 72 | + storagev1.SchemeGroupVersion.WithKind("StorageClass"): {vObject}, |
| 73 | + }, |
| 74 | + Sync: func(ctx *synccontext.RegisterContext) { |
| 75 | + syncerCtx, syncer := newFakeSyncer(t, ctx) |
| 76 | + _, err := syncer.SyncToVirtual(syncerCtx, synccontext.NewSyncToVirtualEvent(pObject)) |
| 77 | + assert.NilError(t, err) |
| 78 | + }, |
| 79 | + }, |
| 80 | + { |
| 81 | + Name: "Sync host changes to virtual", |
| 82 | + InitialPhysicalState: []runtime.Object{pObjectUpdated.DeepCopy()}, // host resource has been updated |
| 83 | + InitialVirtualState: []runtime.Object{vObject.DeepCopy()}, // virtual resource has old values |
| 84 | + ExpectedPhysicalState: map[schema.GroupVersionKind][]runtime.Object{ |
| 85 | + storagev1.SchemeGroupVersion.WithKind("StorageClass"): {pObjectUpdated}, // host resource did not change |
| 86 | + }, |
| 87 | + ExpectedVirtualState: map[schema.GroupVersionKind][]runtime.Object{ |
| 88 | + storagev1.SchemeGroupVersion.WithKind("StorageClass"): {vObjectUpdated}, // virtual resource has been updated after syncing |
| 89 | + }, |
| 90 | + Sync: func(ctx *synccontext.RegisterContext) { |
| 91 | + syncerCtx, syncer := newFakeSyncer(t, ctx) |
| 92 | + _, err := syncer.Sync(syncerCtx, synccontext.NewSyncEvent(pObjectUpdated, vObject.DeepCopy())) |
| 93 | + assert.NilError(t, err) |
| 94 | + }, |
| 95 | + }, |
| 96 | + { |
| 97 | + Name: "Delete virtual resources after host resource has been deleted", |
| 98 | + InitialPhysicalState: []runtime.Object{}, // host resource has been deleted |
| 99 | + InitialVirtualState: []runtime.Object{vObject.DeepCopy()}, // virtual resource exists, since it was previously synced |
| 100 | + ExpectedVirtualState: map[schema.GroupVersionKind][]runtime.Object{}, // virtual resource has been deleted after syncing |
| 101 | + Sync: func(ctx *synccontext.RegisterContext) { |
| 102 | + syncerCtx, syncer := newFakeSyncer(t, ctx) |
| 103 | + _, err := syncer.SyncToHost(syncerCtx, synccontext.NewSyncToHostEvent(vObject.DeepCopy())) |
| 104 | + assert.NilError(t, err) |
| 105 | + }, |
| 106 | + }, |
| 107 | + }) |
| 108 | +} |
| 109 | + |
| 110 | +func newFakeSyncer(t *testing.T, ctx *synccontext.RegisterContext) (*synccontext.SyncContext, *hostStorageClassSyncer) { |
| 111 | + syncContext, object := syncertesting.FakeStartSyncer(t, ctx, NewHostStorageClassSyncer) |
| 112 | + return syncContext, object.(*hostStorageClassSyncer) |
| 113 | +} |
0 commit comments