From de80204caf62eedc7aabd20ec93c47baa1c1c27a Mon Sep 17 00:00:00 2001 From: Vaughn Dice Date: Mon, 12 Aug 2019 11:13:58 -0600 Subject: [PATCH] feat(bundle_test.go): add yaml marshal test for bundle (#92) --- bundle/bundle_test.go | 199 ++++++++++++++----------- testdata/bundles/bundle.yaml | 68 +++++++++ testdata/bundles/canonical-bundle.json | 2 +- 3 files changed, 179 insertions(+), 90 deletions(-) create mode 100644 testdata/bundles/bundle.yaml diff --git a/bundle/bundle_test.go b/bundle/bundle_test.go index e47f75b4..537fd61d 100644 --- a/bundle/bundle_test.go +++ b/bundle/bundle_test.go @@ -8,6 +8,8 @@ import ( "github.com/deislabs/cnab-go/bundle/definition" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + + "gopkg.in/yaml.v2" ) func TestReadTopLevelProperties(t *testing.T) { @@ -420,119 +422,138 @@ func TestOutputs_Marshall(t *testing.T) { assert.Equal(t, "/cnab/app/outputs/port", port.Path, "port path was not the expected value") } -func TestBundleMarshallAllThings(t *testing.T) { - cred := Credential{ - Description: "a password", - } - cred.EnvironmentVariable = "PASSWORD" - cred.Path = "/cnab/app/path" +var exampleCred = Credential{ + Description: "a password", + Location: Location{ + EnvironmentVariable: "PASSWORD", + Path: "/cnab/app/path", + }, +} - b := &Bundle{ - SchemaVersion: "v1.0.0-WD", - Name: "testBundle", - Description: "something", - Version: "1.0", - License: "MIT License", - Credentials: map[string]Credential{ - "password": cred, - }, - Images: map[string]Image{ - "server": { - BaseImage: BaseImage{ - Image: "nginx:1.0", - ImageType: "docker", - }, - Description: "complicated", +var exampleBundle = &Bundle{ + SchemaVersion: "v1.0.0-WD", + Name: "testBundle", + Description: "something", + Version: "1.0", + License: "MIT License", + Credentials: map[string]Credential{ + "password": exampleCred, + }, + Images: map[string]Image{ + "server": { + BaseImage: BaseImage{ + Image: "nginx:1.0", + ImageType: "docker", }, + Description: "complicated", }, - InvocationImages: []InvocationImage{ - { - BaseImage: BaseImage{ - Image: "deislabs/invocation-image:1.0", - ImageType: "docker", - Labels: map[string]string{ - "os": "Linux", - }, + }, + InvocationImages: []InvocationImage{ + { + BaseImage: BaseImage{ + Image: "deislabs/invocation-image:1.0", + ImageType: "docker", + Labels: map[string]string{ + "os": "Linux", }, }, }, - Definitions: map[string]*definition.Schema{ - "portType": { - Type: "integer", - Default: 1234, - }, - "hostType": { - Type: "string", - Default: "locahost.localdomain", - }, - "replicaCountType": { - Type: "integer", - Default: 3, - }, - "enabledType": { - Type: "boolean", - Default: false, - }, - "clientCert": { - Type: "string", - ContentEncoding: "base64", - }, - "productKeyType": { - Type: "string", - }, + }, + Definitions: map[string]*definition.Schema{ + "portType": { + Type: "integer", + Default: 1234, }, - Parameters: map[string]Parameter{ - "port": { - Definition: "portType", - Destination: &Location{ - EnvironmentVariable: "PORT", - }, - Required: true, - }, - "host": { - Definition: "hostType", - Destination: &Location{ - EnvironmentVariable: "HOST", - }, - Required: true, + "hostType": { + Type: "string", + Default: "locahost.localdomain", + }, + "replicaCountType": { + Type: "integer", + Default: 3, + }, + "enabledType": { + Type: "boolean", + Default: false, + }, + "clientCert": { + Type: "string", + ContentEncoding: "base64", + }, + "productKeyType": { + Type: "string", + }, + }, + Parameters: map[string]Parameter{ + "port": { + Definition: "portType", + Destination: &Location{ + EnvironmentVariable: "PORT", + Path: "/path/to/port", }, - "enabled": { - Definition: "enabledType", - Destination: &Location{ - EnvironmentVariable: "ENABLED", - }, + Required: true, + }, + "host": { + Definition: "hostType", + Destination: &Location{ + EnvironmentVariable: "HOST", }, - "replicaCount": { - Definition: "replicaCountType", - Destination: &Location{ - EnvironmentVariable: "REPLICA_COUNT", - }, + Required: true, + }, + "enabled": { + Definition: "enabledType", + Destination: &Location{ + EnvironmentVariable: "ENABLED", }, - "productKey": { - Definition: "productKeyType", - Destination: &Location{ - EnvironmentVariable: "PRODUCT_KEY", - }, + }, + "replicaCount": { + Definition: "replicaCountType", + Destination: &Location{ + EnvironmentVariable: "REPLICA_COUNT", }, }, - Outputs: map[string]Output{ - "clientCert": { - Path: "/cnab/app/outputs/blah", - Definition: "clientCert", + "productKey": { + Definition: "productKeyType", + Destination: &Location{ + EnvironmentVariable: "PRODUCT_KEY", }, }, - } + }, + Outputs: map[string]Output{ + "clientCert": { + Path: "/cnab/app/outputs/blah", + Definition: "clientCert", + }, + }, +} +func TestBundleMarshallAllThings(t *testing.T) { expectedJSON, err := ioutil.ReadFile("../testdata/bundles/canonical-bundle.json") require.NoError(t, err, "couldn't read test data") var buf bytes.Buffer - _, err = b.WriteTo(&buf) + _, err = exampleBundle.WriteTo(&buf) require.NoError(t, err, "test requires output") assert.Equal(t, string(expectedJSON), buf.String(), "output should match expected canonical json") } +func TestBundleYamlRoundtrip(t *testing.T) { + bytes, err := yaml.Marshal(exampleBundle) + require.NoError(t, err, "should have been able to yaml.Marshal bundle") + + expectedYAML, err := ioutil.ReadFile("../testdata/bundles/bundle.yaml") + require.NoError(t, err, "couldn't read test data") + + assert.Equal(t, string(expectedYAML), string(bytes), "marshaled bytes should match expected yaml representation") + + var roundTripBun Bundle + err = yaml.UnmarshalStrict(bytes, &roundTripBun) + require.NoError(t, err, "should have been able to yaml.UnmarshalStrict bundle") + + assert.Equal(t, exampleBundle, &roundTripBun, "after a roundtrip yaml marshal/unmarshal, the bundle does not match expected") +} + func TestValidateABundleAndParams(t *testing.T) { bun, err := ioutil.ReadFile("../testdata/bundles/foo.json") diff --git a/testdata/bundles/bundle.yaml b/testdata/bundles/bundle.yaml new file mode 100644 index 00000000..6567210e --- /dev/null +++ b/testdata/bundles/bundle.yaml @@ -0,0 +1,68 @@ +schemaVersion: v1.0.0-WD +name: testBundle +version: "1.0" +description: something +invocationImages: +- imageType: docker + image: deislabs/invocation-image:1.0 + contentDigest: "" + labels: + os: Linux +images: + server: + imageType: docker + image: nginx:1.0 + contentDigest: "" + description: complicated +parameters: + enabled: + definition: enabledType + destination: + env: ENABLED + host: + definition: hostType + destination: + env: HOST + required: true + port: + definition: portType + destination: + path: /path/to/port + env: PORT + required: true + productKey: + definition: productKeyType + destination: + env: PRODUCT_KEY + replicaCount: + definition: replicaCountType + destination: + env: REPLICA_COUNT +credentials: + password: + path: /cnab/app/path + env: PASSWORD + description: a password +outputs: + clientCert: + definition: clientCert + path: /cnab/app/outputs/blah +definitions: + clientCert: + contentEncoding: base64 + type: string + enabledType: + default: false + type: boolean + hostType: + default: locahost.localdomain + type: string + portType: + default: 1234 + type: integer + productKeyType: + type: string + replicaCountType: + default: 3 + type: integer +license: MIT License diff --git a/testdata/bundles/canonical-bundle.json b/testdata/bundles/canonical-bundle.json index a3ae0686..d74dd3cb 100644 --- a/testdata/bundles/canonical-bundle.json +++ b/testdata/bundles/canonical-bundle.json @@ -1 +1 @@ -{"credentials":{"password":{"description":"a password","env":"PASSWORD","path":"/cnab/app/path"}},"definitions":{"clientCert":{"contentEncoding":"base64","type":"string"},"enabledType":{"default":false,"type":"boolean"},"hostType":{"default":"locahost.localdomain","type":"string"},"portType":{"default":1234,"type":"integer"},"productKeyType":{"type":"string"},"replicaCountType":{"default":3,"type":"integer"}},"description":"something","images":{"server":{"description":"complicated","image":"nginx:1.0","imageType":"docker"}},"invocationImages":[{"image":"deislabs/invocation-image:1.0","imageType":"docker","labels":{"os":"Linux"}}],"license":"MIT License","name":"testBundle","outputs":{"clientCert":{"definition":"clientCert","path":"/cnab/app/outputs/blah"}},"parameters":{"enabled":{"definition":"enabledType","destination":{"env":"ENABLED"}},"host":{"definition":"hostType","destination":{"env":"HOST"},"required":true},"port":{"definition":"portType","destination":{"env":"PORT"},"required":true},"productKey":{"definition":"productKeyType","destination":{"env":"PRODUCT_KEY"}},"replicaCount":{"definition":"replicaCountType","destination":{"env":"REPLICA_COUNT"}}},"schemaVersion":"v1.0.0-WD","version":"1.0"} \ No newline at end of file +{"credentials":{"password":{"description":"a password","env":"PASSWORD","path":"/cnab/app/path"}},"definitions":{"clientCert":{"contentEncoding":"base64","type":"string"},"enabledType":{"default":false,"type":"boolean"},"hostType":{"default":"locahost.localdomain","type":"string"},"portType":{"default":1234,"type":"integer"},"productKeyType":{"type":"string"},"replicaCountType":{"default":3,"type":"integer"}},"description":"something","images":{"server":{"description":"complicated","image":"nginx:1.0","imageType":"docker"}},"invocationImages":[{"image":"deislabs/invocation-image:1.0","imageType":"docker","labels":{"os":"Linux"}}],"license":"MIT License","name":"testBundle","outputs":{"clientCert":{"definition":"clientCert","path":"/cnab/app/outputs/blah"}},"parameters":{"enabled":{"definition":"enabledType","destination":{"env":"ENABLED"}},"host":{"definition":"hostType","destination":{"env":"HOST"},"required":true},"port":{"definition":"portType","destination":{"env":"PORT","path":"/path/to/port"},"required":true},"productKey":{"definition":"productKeyType","destination":{"env":"PRODUCT_KEY"}},"replicaCount":{"definition":"replicaCountType","destination":{"env":"REPLICA_COUNT"}}},"schemaVersion":"v1.0.0-WD","version":"1.0"} \ No newline at end of file