Skip to content

Commit

Permalink
Merge pull request #42 from carolynvs/config-schema
Browse files Browse the repository at this point in the history
Define configuration in the mixin schema
  • Loading branch information
carolynvs authored Sep 2, 2022
2 parents 2bc57d6 + 75c9040 commit f47ca24
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ replace (
require (
get.porter.sh/porter v1.0.0-alpha.13
github.com/Masterminds/semver v1.5.0
github.com/PaesslerAG/jsonpath v0.1.1
github.com/ghodss/yaml v1.0.0
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.1.3
Expand All @@ -29,7 +30,6 @@ require (
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/Masterminds/semver/v3 v3.1.1 // indirect
github.com/PaesslerAG/gval v1.0.0 // indirect
github.com/PaesslerAG/jsonpath v0.1.1 // indirect
github.com/PuerkitoBio/goquery v1.5.0 // indirect
github.com/andybalholm/brotli v1.0.0 // indirect
github.com/andybalholm/cascadia v1.0.0 // indirect
Expand Down
33 changes: 33 additions & 0 deletions pkg/kubernetes/schema/schema.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,35 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"declaration": {
"oneOf": [
{
"description": "Declare the kubernetes mixin without configuration",
"type": "string",
"enum": ["kubernetes"]
},
{"$ref": "#/definitions/config"}
]
},
"config": {
"description": "Declare the kubernetes mixin with additional configuration",
"type": "object",
"properties": {
"kubernetes": {
"description": "kubernetes mixin configuration",
"type": "object",
"properties": {
"clientVersion": {
"description": "Version of kubectl to install in the bundle",
"type": "string"
}
},
"additionalProperties": false
}
},
"additionalProperties": false,
"required": ["kubernetes"]
},
"installStep": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -299,6 +328,10 @@
"items": {
"$ref": "#/definitions/uninstallStep"
}
},
"mixins": {
"type": "array",
"items": { "$ref": "#/definitions/declaration" }
}
},
"additionalProperties": {
Expand Down
38 changes: 37 additions & 1 deletion pkg/kubernetes/schema_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package kubernetes

import (
"encoding/json"
"fmt"
"io/ioutil"
"testing"

// We are not using go-yaml because of serialization problems with jsonschema, don't use this library elsewhere
"github.com/PaesslerAG/jsonpath"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand All @@ -23,6 +25,40 @@ func TestMixin_PrintSchema(t *testing.T) {
assert.Equal(t, string(wantSchema), gotSchema)
}

func TestMixin_CheckSchema(t *testing.T) {
// Long term it would be great to have a helper function in Porter that a mixin can use to check that it meets certain interfaces
// check that certain characteristics of the schema that Porter expects are present
// Once we have a mixin library, that would be a good place to package up this type of helper function
var schemaMap map[string]interface{}
err := json.Unmarshal([]byte(schema), &schemaMap)
require.NoError(t, err, "could not unmarshal the schema into a map")

t.Run("mixin configuration", func(t *testing.T) {
// Check that mixin config is defined, and has all the supported fields
configSchema, err := jsonpath.Get("$.definitions.config", schemaMap)
require.NoError(t, err, "could not find the mixin config schema declaration")
_, err = jsonpath.Get("$.properties.kubernetes.properties.clientVersion", configSchema)
require.NoError(t, err, "client version was not included in the mixin config schema")
})

// Check that schema are defined for each action
actions := []string{"install", "upgrade", "invoke", "uninstall"}
for _, action := range actions {
t.Run("supports "+action, func(t *testing.T) {
actionPath := fmt.Sprintf("$.definitions.%sStep", action)
_, err := jsonpath.Get(actionPath, schemaMap)
require.NoErrorf(t, err, "could not find the %sStep declaration", action)
})
}

// Check that the invoke action is registered
additionalSchema, err := jsonpath.Get("$.additionalProperties.items", schemaMap)
require.NoError(t, err, "the invoke action was not registered in the schema")
require.Contains(t, additionalSchema, "$ref")
invokeRef := additionalSchema.(map[string]interface{})["$ref"]
require.Equal(t, "#/definitions/invokeStep", invokeRef, "the invoke action was not registered correctly")
}

func TestMixin_ValidatePayload(t *testing.T) {
testcases := []struct {
name string
Expand Down

0 comments on commit f47ca24

Please sign in to comment.