Skip to content

Commit

Permalink
Fixes bug in dry-run for kpt live migrate
Browse files Browse the repository at this point in the history
  • Loading branch information
seans3 committed Dec 11, 2020
1 parent c1460eb commit 35b3e85
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 16 deletions.
23 changes: 16 additions & 7 deletions commands/migratecmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,11 @@ func (mr *MigrateRunner) retrieveInvObjs(invObj inventory.InventoryInfo) ([]obje
func (mr *MigrateRunner) migrateObjs(cmObjs []object.ObjMetadata, reader io.Reader, args []string) error {
fmt.Fprint(mr.ioStreams.Out, " migrate inventory to ResourceGroup...")
if len(cmObjs) == 0 {
fmt.Fprint(mr.ioStreams.Out, "no inventory objects found\n")
fmt.Fprintln(mr.ioStreams.Out, "no inventory objects found")
return nil
}
if mr.dryRun {
fmt.Fprintln(mr.ioStreams.Out, "success")
return nil
}
rgReader, err := mr.rgLoader.ManifestReader(reader, args)
Expand All @@ -246,7 +250,7 @@ func (mr *MigrateRunner) migrateObjs(cmObjs []object.ObjMetadata, reader io.Read
if err != nil {
return err
}
fmt.Fprint(mr.ioStreams.Out, "success\n")
fmt.Fprintln(mr.ioStreams.Out, "success")
return nil
}

Expand All @@ -258,6 +262,9 @@ func (mr *MigrateRunner) deleteConfigMapInv(invObj inventory.InventoryInfo) erro
if err != nil {
return err
}
if mr.dryRun {
cmInvClient.SetDryRunStrategy(common.DryRunClient)
}
if err = cmInvClient.DeleteInventoryObj(invObj); err != nil {
return err
}
Expand All @@ -279,12 +286,14 @@ func (mr *MigrateRunner) deleteConfigMapFile() error {
}
if len(cmFilename) > 0 {
fmt.Fprintf(mr.ioStreams.Out, "deleting inventory template file: %s...", cmFilename)
err = os.Remove(cmFilename)
if err != nil {
fmt.Fprint(mr.ioStreams.Out, "failed\n")
return err
if !mr.dryRun {
err = os.Remove(cmFilename)
if err != nil {
fmt.Fprintln(mr.ioStreams.Out, "failed")
return err
}
}
fmt.Fprint(mr.ioStreams.Out, "success\n")
fmt.Fprintln(mr.ioStreams.Out, "success")
}
}
return nil
Expand Down
38 changes: 29 additions & 9 deletions e2e/live/end-to-end-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -437,14 +437,6 @@ assertPodNotExists "pod-c" "test-namespace"
assertPodNotExists "pod-d" "test-namespace"
printResult

# Creates new inventory-template.yaml for "migrate-case-1a" directory.
echo "kpt live init e2e/live/testdata/migrate-case-1a"
rm -f e2e/live/testdata/migrate-case-1a/inventory-template.yaml
${BIN_DIR}/kpt live init e2e/live/testdata/migrate-case-1a > $OUTPUT_DIR/status
assertContains "namespace: test-rg-namespace is used for inventory object"
assertContains "live/testdata/migrate-case-1a/inventory-template.yaml"
printResult


###########################################################################
# Tests with RESOURCE_GROUP_INVENTORY env var set
Expand All @@ -458,6 +450,7 @@ echo "Testing kpt live apply with ConfigMap inventory"
echo "kpt live apply e2e/live/testdata/migrate-case-1a"
# Copy Kptfile into "migrate-case-1a" WITHOUT inventory information. This ensures
# the apply uses the ConfigMap inventory-template.yaml during the apply.
cp -f e2e/live/testdata/inventory-template.yaml e2e/live/testdata/migrate-case-1a
cp -f e2e/live/testdata/Kptfile e2e/live/testdata/migrate-case-1a
${BIN_DIR}/kpt live apply e2e/live/testdata/migrate-case-1a > $OUTPUT_DIR/status
assertContains "namespace/test-rg-namespace unchanged"
Expand All @@ -475,10 +468,26 @@ printResult

# Test 8: kpt live migrate from ConfigMap to ResourceGroup inventory
# Migrates resources in "migrate-case-1a" directory.
echo "Testing migrate dry-run from ConfigMap to ResourceGroup inventory"
echo "kpt live migrate --dry-run e2e/live/testdata/migrate-case-1a"
# Run migrate dry-run and verify that the migrate did not actually happen
${BIN_DIR}/kpt live migrate --dry-run e2e/live/testdata/migrate-case-1a > $OUTPUT_DIR/status
assertContains "ensuring ResourceGroup CRD exists in cluster...success"
assertContains "updating Kptfile inventory values...success"
assertContains "retrieve the current ConfigMap inventory...success (4 inventory objects)"
assertContains "migrate inventory to ResourceGroup...success"
assertContains "deleting old ConfigMap inventory object...success"
assertContains "deleting inventory template file"
assertContains "inventory migration...success"
# Migrate did not actually happen in dry-run, so ConfigMap inventory still exists
assertCMInventory "test-rg-namespace" "4"
printResult

# Now actually run the migrate and verify the new ResourceGroup inventory exists
echo "Testing migrate from ConfigMap to ResourceGroup inventory"
echo "kpt live migrate e2e/live/testdata/migrate-case-1a"
${BIN_DIR}/kpt live migrate e2e/live/testdata/migrate-case-1a > $OUTPUT_DIR/status
assertContains "ensuring ResourceGroup CRD exists in cluster...success"
assertContains "ensuring ResourceGroup CRD exists in cluster...already installed...success"
assertContains "updating Kptfile inventory values...success"
assertContains "retrieve the current ConfigMap inventory...success (4 inventory objects)"
assertContains "migrate inventory to ResourceGroup...success"
Expand Down Expand Up @@ -583,6 +592,17 @@ assertContains "e2e/live/testdata/migrate-error/inventory-template.yaml...succes
assertContains "inventory migration...success"
printResult

# Now test kpt live migrate with --force flag, which overwrites inventory
# info in the Kptfile.
cp -f e2e/live/testdata/inventory-template.yaml e2e/live/testdata/migrate-error
echo "Testing kpt live migrate with --force flag"
echo "kpt live migrate --force e2e/live/testdata/migrate-error"
${BIN_DIR}/kpt live migrate --force e2e/live/testdata/migrate-error > $OUTPUT_DIR/status 2>&1
# Does not contain "values already exist"
assertContains "updating Kptfile inventory values...success"
assertContains "inventory migration...success"
printResult

# Test 15: kpt live initial apply ResourceGroup inventory
echo "Testing kpt apply ResourceGroup inventory"
echo "kpt live apply e2e/live/testdata/migrate-error"
Expand Down

0 comments on commit 35b3e85

Please sign in to comment.