From 5d8ba1b90dbe7d6f1c0fec9fb69eafdb8aec0805 Mon Sep 17 00:00:00 2001 From: Steve Kriss Date: Fri, 21 Jun 2019 12:38:54 -0600 Subject: [PATCH] migrate namespace mapping tests Signed-off-by: Steve Kriss --- pkg/restore/restore_new_test.go | 59 +++++++++++++++++++++++++++ pkg/restore/restore_test.go | 72 --------------------------------- 2 files changed, 59 insertions(+), 72 deletions(-) diff --git a/pkg/restore/restore_new_test.go b/pkg/restore/restore_new_test.go index d0541292c..9cf081cf5 100644 --- a/pkg/restore/restore_new_test.go +++ b/pkg/restore/restore_new_test.go @@ -508,6 +508,65 @@ func TestRestoreResourceFiltering(t *testing.T) { } } +// TestRestoreNamespaceMapping runs restores with namespace mappings specified, +// and verifies that the set of items created in the API are in the correct +// namespaces. Validation is done by looking at the namespaces/names of the items +// in the API; contents are not checked. +func TestRestoreNamespaceMapping(t *testing.T) { + tests := []struct { + name string + restore *velerov1api.Restore + backup *velerov1api.Backup + apiResources []*test.APIResource + tarball io.Reader + want map[*test.APIResource][]string + }{ + { + name: "namespace mappings are applied", + restore: defaultRestore().NamespaceMappings("ns-1", "mapped-ns-1", "ns-2", "mapped-ns-2").Restore(), + backup: defaultBackup().Backup(), + apiResources: []*test.APIResource{ + test.Pods(), + }, + tarball: newTarWriter(t). + addItems("pods", + test.NewPod("ns-1", "pod-1"), + test.NewPod("ns-2", "pod-2"), + test.NewPod("ns-3", "pod-3"), + ). + done(), + want: map[*test.APIResource][]string{ + test.Pods(): {"mapped-ns-1/pod-1", "mapped-ns-2/pod-2", "ns-3/pod-3"}, + }, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + h := newHarness(t) + + for _, r := range tc.apiResources { + h.DiscoveryClient.WithAPIResource(r) + } + require.NoError(t, h.restorer.discoveryHelper.Refresh()) + + warnings, errs := h.restorer.Restore( + h.log, + tc.restore, + tc.backup, + nil, // volume snapshots + tc.tarball, + nil, // actions + nil, // snapshot location lister + nil, // volume snapshotter getter + ) + + assertEmptyResults(t, warnings, errs) + assertAPIContents(t, h, tc.want) + }) + } +} + func defaultRestore() *Builder { return NewNamedBuilder(velerov1api.DefaultNamespace, "restore-1").Backup("backup-1") } diff --git a/pkg/restore/restore_test.go b/pkg/restore/restore_test.go index 7d8158af0..f594a1a57 100644 --- a/pkg/restore/restore_test.go +++ b/pkg/restore/restore_test.go @@ -219,70 +219,6 @@ func TestRestorePriority(t *testing.T) { } } -func TestNamespaceRemapping(t *testing.T) { - var ( - baseDir = "bak" - restore = &api.Restore{Spec: api.RestoreSpec{IncludedNamespaces: []string{"*"}, NamespaceMapping: map[string]string{"ns-1": "ns-2"}}} - prioritizedResources = []schema.GroupResource{{Resource: "namespaces"}, {Resource: "configmaps"}} - labelSelector = labels.NewSelector() - fileSystem = velerotest.NewFakeFileSystem(). - WithFile("bak/resources/configmaps/namespaces/ns-1/cm-1.json", newTestConfigMap().WithNamespace("ns-1").ToJSON()). - WithFile("bak/resources/namespaces/cluster/ns-1.json", newTestNamespace("ns-1").ToJSON()) - expectedNS = "ns-2" - expectedObjs = toUnstructured(newTestConfigMap().WithNamespace("ns-2").ConfigMap) - ) - - resourceClient := &velerotest.FakeDynamicClient{} - for i := range expectedObjs { - addRestoreLabels(&expectedObjs[i], "", "") - resourceClient.On("Create", &expectedObjs[i]).Return(&expectedObjs[i], nil) - } - - dynamicFactory := &velerotest.FakeDynamicFactory{} - resource := metav1.APIResource{Name: "configmaps", Namespaced: true} - gv := schema.GroupVersion{Group: "", Version: "v1"} - dynamicFactory.On("ClientForGroupVersionResource", gv, resource, expectedNS).Return(resourceClient, nil) - - nsClient := &velerotest.FakeNamespaceClient{} - - ctx := &context{ - dynamicFactory: dynamicFactory, - fileSystem: fileSystem, - selector: labelSelector, - namespaceClient: nsClient, - prioritizedResources: prioritizedResources, - restore: restore, - backup: &api.Backup{}, - log: velerotest.NewLogger(), - applicableActions: make(map[schema.GroupResource][]resolvedAction), - resourceClients: make(map[resourceClientKey]pkgclient.Dynamic), - restoredItems: make(map[velero.ResourceIdentifier]struct{}), - restoreDir: baseDir, - } - - nsClient.On("Get", "ns-2", metav1.GetOptions{}).Return(&v1.Namespace{}, k8serrors.NewNotFound(schema.GroupResource{Resource: "namespaces"}, "ns-2")) - ns := newTestNamespace("ns-2").Namespace - nsClient.On("Create", ns).Return(ns, nil) - - warnings, errors := ctx.restoreFromDir() - - assert.Empty(t, warnings.Velero) - assert.Empty(t, warnings.Cluster) - assert.Empty(t, warnings.Namespaces) - assert.Empty(t, errors.Velero) - assert.Empty(t, errors.Cluster) - assert.Empty(t, errors.Namespaces) - - // ensure the remapped NS (only) was created via the namespaceClient - nsClient.AssertExpectations(t) - - // ensure that we did not try to create namespaces via dynamic client - dynamicFactory.AssertNotCalled(t, "ClientForGroupVersionResource", gv, metav1.APIResource{Name: "namespaces", Namespaced: true}, "") - - dynamicFactory.AssertExpectations(t) - resourceClient.AssertExpectations(t) -} - func TestRestoreResourceForNamespace(t *testing.T) { tests := []struct { name string @@ -327,14 +263,6 @@ func TestRestoreResourceForNamespace(t *testing.T) { }, expectedObjs: toUnstructured(newNamedTestConfigMap("cm-2").ConfigMap), }, - { - name: "namespace is remapped", - namespace: "ns-2", - resourcePath: "configmaps", - labelSelector: labels.NewSelector(), - fileSystem: velerotest.NewFakeFileSystem().WithFile("configmaps/cm-1.json", newTestConfigMap().WithNamespace("ns-1").ToJSON()), - expectedObjs: toUnstructured(newTestConfigMap().WithNamespace("ns-2").ConfigMap), - }, { name: "custom restorer is correctly used", namespace: "ns-1",