From 4d158b4ba005b5cb397e25a11643fa04e52660a9 Mon Sep 17 00:00:00 2001 From: Xander Flood Date: Sat, 16 Nov 2019 09:11:17 -0500 Subject: [PATCH] tests for label migration --- docker/label_migration.go | 6 +- docker/label_migration_test.go | 102 +++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+), 3 deletions(-) create mode 100644 docker/label_migration_test.go diff --git a/docker/label_migration.go b/docker/label_migration.go index 8b96f151..fff03a14 100644 --- a/docker/label_migration.go +++ b/docker/label_migration.go @@ -13,7 +13,7 @@ func replaceLabelsMapFieldWithSetField(rawState map[string]interface{}) map[stri } func migrateContainerLabels(rawState map[string]interface{}) map[string]interface{} { - rawState = replaceLabelsMapFieldWithSetField(rawState) + replaceLabelsMapFieldWithSetField(rawState) mounts := rawState["mounts"].([]interface{}) newMounts := make([]interface{}, len(mounts)) @@ -22,7 +22,7 @@ func migrateContainerLabels(rawState map[string]interface{}) map[string]interfac volumeOptionsList := mount["volume_options"].([]interface{}) if len(volumeOptionsList) != 0 { - mount["volume_options"] = replaceLabelsMapFieldWithSetField(volumeOptionsList[0].(map[string]interface{})) + replaceLabelsMapFieldWithSetField(volumeOptionsList[0].(map[string]interface{})) } newMounts[i] = mount } @@ -32,7 +32,7 @@ func migrateContainerLabels(rawState map[string]interface{}) map[string]interfac } func migrateServiceLabels(rawState map[string]interface{}) map[string]interface{} { - rawState = replaceLabelsMapFieldWithSetField(rawState) + replaceLabelsMapFieldWithSetField(rawState) taskSpec := rawState["task_spec"].([]interface{})[0].(map[string]interface{}) containerSpec := taskSpec["container_spec"].([]interface{})[0].(map[string]interface{}) diff --git a/docker/label_migration_test.go b/docker/label_migration_test.go new file mode 100644 index 00000000..a5d7d680 --- /dev/null +++ b/docker/label_migration_test.go @@ -0,0 +1,102 @@ +package docker + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/terraform" +) + +func TestMigrateServiceLabelState_empty_labels(t *testing.T) { + v0State := map[string]interface{}{ + "name": "volume-name", + "task_spec": []interface{}{ + map[string]interface{}{ + "container_spec": []interface{}{ + map[string]interface{}{ + "image": "repo:tag", + "mounts": []interface{}{ + map[string]interface{}{ + "target": "path/to/target", + "type": "bind", + "volume_options": []interface{}{ + map[string]interface{}{}, + }, + }, + }, + }, + }, + }, + }, + } + + //first validate that we build that correctly + v0Config := terraform.NewResourceConfigRaw(v0State) + warns, errs := resourceDockerServiceV0().Validate(v0Config) + if len(warns) > 0 || len(errs) > 0 { + t.Error("test precondition failed - attempt to migrate an invalid v0 config") + return + } + + v1State := migrateServiceLabels(v0State) + v1Config := terraform.NewResourceConfigRaw(v1State) + warns, errs = resourceDockerService().Validate(v1Config) + if len(warns) > 0 || len(errs) > 0 { + fmt.Println(warns, errs) + t.Error("migrated service config is invalid") + return + } +} + +func TestMigrateServiceLabelState_with_labels(t *testing.T) { + v0State := map[string]interface{}{ + "name": "volume-name", + "task_spec": []interface{}{ + map[string]interface{}{ + "container_spec": []interface{}{ + map[string]interface{}{ + "image": "repo:tag", + "labels": map[string]interface{}{ + "type": "container", + "env": "dev", + }, + "mounts": []interface{}{ + map[string]interface{}{ + "target": "path/to/target", + "type": "bind", + "volume_options": []interface{}{ + map[string]interface{}{ + "labels": map[string]interface{}{ + "type": "mount", + }, + }, + }, + }, + }, + }, + }, + }, + }, + "labels": map[string]interface{}{ + "foo": "bar", + "env": "dev", + }, + } + + //first validate that we build that correctly + v0Config := terraform.NewResourceConfigRaw(v0State) + warns, errs := resourceDockerServiceV0().Validate(v0Config) + if len(warns) > 0 || len(errs) > 0 { + t.Error("test precondition failed - attempt to migrate an invalid v0 config") + return + } + + v1State := migrateServiceLabels(v0State) + v1Config := terraform.NewResourceConfigRaw(v1State) + warns, errs = resourceDockerService().Validate(v1Config) + if len(warns) > 0 || len(errs) > 0 { + fmt.Println(warns, errs) + t.Error("migrated service config is invalid") + return + } +}