Skip to content

Commit

Permalink
feat(bundle_test.go): add yaml marshal test for bundle (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
vdice authored and Radu M committed Aug 12, 2019
1 parent b47ce64 commit de80204
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 90 deletions.
199 changes: 110 additions & 89 deletions bundle/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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")
Expand Down
68 changes: 68 additions & 0 deletions testdata/bundles/bundle.yaml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion testdata/bundles/canonical-bundle.json
Original file line number Diff line number Diff line change
@@ -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"}
{"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"}

0 comments on commit de80204

Please sign in to comment.