Merge pull request #485 from skriss/fix-cohab

use new cohabitatingResources map for each backup
This commit is contained in:
Andy Goldstein
2018-05-14 15:02:33 -04:00
committed by GitHub
2 changed files with 83 additions and 8 deletions

View File

@@ -73,12 +73,14 @@ func (i *itemKey) String() string {
return fmt.Sprintf("resource=%s,namespace=%s,name=%s", i.resource, i.namespace, i.name)
}
var cohabitatingResources = map[string]*cohabitatingResource{
"deployments": newCohabitatingResource("deployments", "extensions", "apps"),
"daemonsets": newCohabitatingResource("daemonsets", "extensions", "apps"),
"replicasets": newCohabitatingResource("replicasets", "extensions", "apps"),
"networkpolicies": newCohabitatingResource("networkpolicies", "extensions", "networking.k8s.io"),
"events": newCohabitatingResource("events", "", "events.k8s.io"),
func cohabitatingResources() map[string]*cohabitatingResource {
return map[string]*cohabitatingResource{
"deployments": newCohabitatingResource("deployments", "extensions", "apps"),
"daemonsets": newCohabitatingResource("daemonsets", "extensions", "apps"),
"replicasets": newCohabitatingResource("replicasets", "extensions", "apps"),
"networkpolicies": newCohabitatingResource("networkpolicies", "extensions", "networking.k8s.io"),
"events": newCohabitatingResource("events", "", "events.k8s.io"),
}
}
// NewKubernetesBackupper creates a new kubernetesBackupper.
@@ -252,7 +254,7 @@ func (kb *kubernetesBackupper) Backup(backup *api.Backup, backupFile, logFile io
kb.dynamicFactory,
kb.discoveryHelper,
backedUpItems,
cohabitatingResources,
cohabitatingResources(),
resolvedActions,
kb.podCommandExecutor,
tw,

View File

@@ -533,7 +533,7 @@ func TestBackup(t *testing.T) {
dynamicFactory,
discoveryHelper,
map[itemKey]struct{}{}, // backedUpItems
cohabitatingResources,
cohabitatingResources(),
mock.Anything,
kb.podCommandExecutor,
mock.Anything, // tarWriter
@@ -571,6 +571,79 @@ func TestBackup(t *testing.T) {
}
}
func TestBackupUsesNewCohabitatingResourcesForEachBackup(t *testing.T) {
discoveryHelper := &arktest.FakeDiscoveryHelper{
Mapper: &arktest.FakeMapper{
Resources: map[schema.GroupVersionResource]schema.GroupVersionResource{},
},
}
b, err := NewKubernetesBackupper(discoveryHelper, nil, nil, nil)
require.NoError(t, err)
kb := b.(*kubernetesBackupper)
groupBackupperFactory := &mockGroupBackupperFactory{}
kb.groupBackupperFactory = groupBackupperFactory
// assert that newGroupBackupper() is called with the result of cohabitatingResources()
// passed as an argument.
firstCohabitatingResources := cohabitatingResources()
groupBackupperFactory.On("newGroupBackupper",
mock.Anything,
mock.Anything,
mock.Anything,
mock.Anything,
mock.Anything,
mock.Anything,
discoveryHelper,
mock.Anything,
firstCohabitatingResources,
mock.Anything,
mock.Anything,
mock.Anything,
mock.Anything,
mock.Anything,
).Return(&mockGroupBackupper{})
assert.NoError(t, b.Backup(&v1.Backup{}, &bytes.Buffer{}, &bytes.Buffer{}, nil))
groupBackupperFactory.AssertExpectations(t)
// mutate the cohabitatingResources map that was used in the first backup to simulate
// the first backup process having done so.
for _, value := range firstCohabitatingResources {
value.seen = true
}
// assert that on a second backup, newGroupBackupper() is called with the result of
// cohabitatingResources() passed as an argument, that the value is not the
// same as the mutated firstCohabitatingResources value, and that all of the `seen`
// flags are false as they should be for a new instance
secondCohabitatingResources := cohabitatingResources()
groupBackupperFactory.On("newGroupBackupper",
mock.Anything,
mock.Anything,
mock.Anything,
mock.Anything,
mock.Anything,
mock.Anything,
discoveryHelper,
mock.Anything,
secondCohabitatingResources,
mock.Anything,
mock.Anything,
mock.Anything,
mock.Anything,
mock.Anything,
).Return(&mockGroupBackupper{})
assert.NoError(t, b.Backup(&v1.Backup{}, &bytes.Buffer{}, &bytes.Buffer{}, nil))
assert.NotEqual(t, firstCohabitatingResources, secondCohabitatingResources)
for _, resource := range secondCohabitatingResources {
assert.False(t, resource.seen)
}
groupBackupperFactory.AssertExpectations(t)
}
type mockGroupBackupperFactory struct {
mock.Mock
}