tests for label migration

This commit is contained in:
Xander Flood 2019-11-16 09:11:17 -05:00
parent e990b2db03
commit 4d158b4ba0
2 changed files with 105 additions and 3 deletions

View file

@ -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{})

View file

@ -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
}
}