Skip to content

Commit

Permalink
fix restore namespace remapping bug
Browse files Browse the repository at this point in the history
Signed-off-by: Steve Kriss <[email protected]>
  • Loading branch information
skriss committed Nov 2, 2017
1 parent 7f959f0 commit 55c038a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/restore/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ func (ctx *context) restoreFromDir(dir string) (api.RestoreResult, api.RestoreRe
continue
}

w, e := ctx.restoreResource(resource.String(), nsName, nsPath)
w, e := ctx.restoreResource(resource.String(), mappedNsName, nsPath)
merge(&warnings, &w)
merge(&errs, &e)
}
Expand Down
47 changes: 47 additions & 0 deletions pkg/restore/restore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,53 @@ 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: "configmaps"}}
labelSelector = labels.NewSelector()
fileSystem = newFakeFileSystem().WithFile("bak/resources/configmaps/namespaces/ns-1/cm-1.json", newTestConfigMap().WithNamespace("ns-1").ToJSON())
expectedNS = "ns-2"
expectedObjs = toUnstructured(newTestConfigMap().WithNamespace("ns-2").WithArkLabel("").ConfigMap)
)

resourceClient := &FakeDynamicClient{}
for i := range expectedObjs {
resourceClient.On("Create", &expectedObjs[i]).Return(&expectedObjs[i], nil)
}

dynamicFactory := &FakeDynamicFactory{}
resource := metav1.APIResource{Name: "configmaps", Namespaced: true}
gv := schema.GroupVersion{Group: "", Version: "v1"}
dynamicFactory.On("ClientForGroupVersionResource", gv, resource, expectedNS).Return(resourceClient, nil)

log, _ := testlogger.NewNullLogger()

ctx := &context{
dynamicFactory: dynamicFactory,
fileSystem: fileSystem,
selector: labelSelector,
namespaceClient: &fakeNamespaceClient{},
prioritizedResources: prioritizedResources,
restore: restore,
backup: &api.Backup{},
logger: log,
}

warnings, errors := ctx.restoreFromDir(baseDir)

assert.Empty(t, warnings.Ark)
assert.Empty(t, warnings.Cluster)
assert.Empty(t, warnings.Namespaces)
assert.Empty(t, errors.Ark)
assert.Empty(t, errors.Cluster)
assert.Empty(t, errors.Namespaces)

dynamicFactory.AssertExpectations(t)
resourceClient.AssertExpectations(t)
}

func TestRestoreResourceForNamespace(t *testing.T) {
var (
trueVal = true
Expand Down

0 comments on commit 55c038a

Please sign in to comment.