Skip to content

Commit

Permalink
add/update schema, add schema validation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vdice committed Aug 23, 2019
1 parent 0dc48e9 commit a792ae6
Show file tree
Hide file tree
Showing 48 changed files with 7,281 additions and 145 deletions.
39 changes: 35 additions & 4 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/terraform/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func TestMixin_Install(t *testing.T) {
}, "\n"),
installStep: InstallStep{
InstallArguments: InstallArguments{
Step: Step{Description: "Install"},
AutoApprove: true,
LogLevel: "TRACE",
Vars: map[string]string{
Expand Down
6 changes: 1 addition & 5 deletions pkg/terraform/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package terraform

import (
"fmt"

packr "github.com/gobuffalo/packr/v2"
)

func (m *Mixin) PrintSchema() error {
Expand All @@ -18,9 +16,7 @@ func (m *Mixin) PrintSchema() error {
}

func (m *Mixin) GetSchema() (string, error) {
t := packr.New("schema", "./schema")

b, err := t.Find("mixin.json")
b, err := m.schema.Find("terraform.json")
if err != nil {
return "", err
}
Expand Down
7 changes: 0 additions & 7 deletions pkg/terraform/schema/mixin.json

This file was deleted.

208 changes: 208 additions & 0 deletions pkg/terraform/schema/terraform.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"installStep": {
"type": "object",
"properties": {
"terraform": {
"type": "object",
"properties": {
"autoApprove": {
"type": "boolean"
},
"backendConfig": {
"type": "object"
},
"description": {
"$ref": "#/definitions/stepDescription"
},
"input": {
"type": "boolean"
},
"logLevel": {
"type": "string"
},
"outputs": {
"$ref": "#/definitions/outputs"
},
"vars": {
"type": "object"
}
},
"additionalProperties": false,
"required": [
"description"
]
}
},
"required": [
"terraform"
],
"additionalProperties": false
},
"invokeStep": {
"type": "object",
"properties": {
"terraform": {
"type": "object",
"properties": {
"autoApprove": {
"type": "boolean"
},
"backendConfig": {
"type": "object"
},
"command": {
"type": "string"
},
"description": {
"$ref": "#/definitions/stepDescription"
},
"input": {
"type": "boolean"
},
"logLevel": {
"type": "string"
},
"outputs": {
"$ref": "#/definitions/outputs"
},
"vars": {
"type": "object"
}
},
"additionalProperties": false,
"required": [
"description"
]
}
},
"additionalProperties": false,
"required": [
"terraform"
]
},
"upgradeStep": {
"type": "object",
"properties": {
"terraform": {
"type": "object",
"properties": {
"autoApprove": {
"type": "boolean"
},
"backendConfig": {
"type": "object"
},
"description": {
"$ref": "#/definitions/stepDescription"
},
"input": {
"type": "boolean"
},
"logLevel": {
"type": "string"
},
"outputs": {
"$ref": "#/definitions/outputs"
},
"vars": {
"type": "object"
}
},
"additionalProperties": false,
"required": [
"description"
]
}
},
"required": [
"terraform"
],
"additionalProperties": false
},
"uninstallStep": {
"type": "object",
"properties": {
"terraform": {
"type": "object",
"properties": {
"autoApprove": {
"type": "boolean"
},
"backendConfig": {
"type": "object"
},
"description": {
"$ref": "#/definitions/stepDescription"
},
"logLevel": {
"type": "string"
},
"outputs": {
"$ref": "#/definitions/outputs"
},
"vars": {
"type": "object"
}
},
"additionalProperties": false,
"required": [
"description"
]
}
},
"required": [
"terraform"
],
"additionalProperties": false
},
"stepDescription": {
"type": "string",
"minLength": 1
},
"outputs": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
},
"additionalProperties": false,
"required": ["name"]
}
}
},
"type": "object",
"properties": {
"install": {
"type": "array",
"items": {
"$ref": "#/definitions/installStep"
}
},
"upgrade": {
"type": "array",
"items": {
"$ref": "#/definitions/upgradeStep"
}
},
"uninstall": {
"type": "array",
"items": {
"$ref": "#/definitions/uninstallStep"
}
}
},
"patternProperties": {
".*": {
"type": "array",
"items": {
"$ref": "#/definitions/invokeStep"
}
}
},
"additionalProperties": false
}
55 changes: 55 additions & 0 deletions pkg/terraform/schema_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package terraform

import (
"io/ioutil"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestMixin_PrintSchema(t *testing.T) {
m := NewTestMixin(t)

err := m.PrintSchema()
require.NoError(t, err)

gotSchema := m.TestContext.GetOutput()

wantSchema, err := ioutil.ReadFile("testdata/schema.json")
require.NoError(t, err)

assert.Equal(t, string(wantSchema), gotSchema)
}

func TestMixin_ValidatePayload(t *testing.T) {
testcases := []struct {
name string
step string
pass bool
error string
}{
{"install", "testdata/install-input.yaml", true, ""},
{"invoke", "testdata/invoke-input.yaml", true, ""},
{"upgrade", "testdata/upgrade-input.yaml", true, ""},
{"uninstall", "testdata/uninstall-input.yaml", true, ""},
{"install.missing-desc", "testdata/bad-install-input.missing-desc.yaml", false, "install.0.terraform: Invalid type. Expected: object, given: null"},
{"install.desc-empty", "testdata/bad-install-input.desc-empty.yaml", false, "install.0.terraform.description: String length must be greater than or equal to 1"},
{"uninstall.input-not-valid", "testdata/bad-uninstall-input.input-not-valid.yaml", false, "uninstall.0.terraform: Additional property input is not allowed"},
}

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
m := NewTestMixin(t)
b, err := ioutil.ReadFile(tc.step)
require.NoError(t, err)

err = m.ValidatePayload(b)
if tc.pass {
require.NoError(t, err)
} else {
require.EqualError(t, err, tc.error)
}
})
}
}
Loading

0 comments on commit a792ae6

Please sign in to comment.