Skip to content

Commit

Permalink
Merge pull request #10 from fabi200123/generate-jsonschema
Browse files Browse the repository at this point in the history
Add json-schema generator
  • Loading branch information
gabriel-samfira authored Sep 20, 2024
2 parents edf74f2 + 2b298bc commit 221162c
Show file tree
Hide file tree
Showing 44 changed files with 6,298 additions and 130 deletions.
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ toolchain go1.22.3

require (
github.com/BurntSushi/toml v1.3.2
github.com/cloudbase/garm-provider-common v0.1.4-0.20240906095211-57315d4ac8ae
github.com/cloudbase/garm-provider-common v0.1.4-0.20240912084949-899c120c80ce
github.com/gorilla/websocket v1.5.4-0.20240702125206-a62d9d2a8413
github.com/invopop/jsonschema v0.12.0
github.com/juju/clock v1.0.3
github.com/juju/retry v1.0.0
github.com/lxc/incus v0.7.0
Expand All @@ -17,6 +18,8 @@ require (
)

require (
github.com/bahlo/generic-list-go v0.2.0 // indirect
github.com/buger/jsonparser v1.1.1 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/google/uuid v1.6.0 // indirect
Expand All @@ -28,6 +31,7 @@ require (
github.com/juju/testing v1.0.2 // indirect
github.com/kr/fs v0.1.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/minio/sio v0.4.0 // indirect
github.com/muhlemmer/gu v0.3.1 // indirect
Expand All @@ -37,6 +41,7 @@ require (
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/teris-io/shortid v0.0.0-20220617161101-71ec9f2aa569 // indirect
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/zitadel/oidc/v2 v2.12.0 // indirect
Expand Down
15 changes: 13 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/cloudbase/garm-provider-common v0.1.4-0.20240906095211-57315d4ac8ae h1:GDcVb/ForsCtpGj2kFH+iPu6/IIj0b5pV/h27qL6+GI=
github.com/cloudbase/garm-provider-common v0.1.4-0.20240906095211-57315d4ac8ae/go.mod h1:sK26i2NpjjAjhanNKiWw8iPkqt+XeohTKpFnEP7JdZ4=
github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk=
github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg=
github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs=
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
github.com/cloudbase/garm-provider-common v0.1.4-0.20240912084949-899c120c80ce h1:spSF26dB6llkdyEeVPDdobdbv2z09zL10MUJG/YYkoE=
github.com/cloudbase/garm-provider-common v0.1.4-0.20240912084949-899c120c80ce/go.mod h1:sK26i2NpjjAjhanNKiWw8iPkqt+XeohTKpFnEP7JdZ4=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -29,8 +33,11 @@ github.com/gorilla/securecookie v1.1.2 h1:YCIWL56dvtr73r6715mJs5ZvhtnY73hBvEF8kX
github.com/gorilla/securecookie v1.1.2/go.mod h1:NfCASbcHqRSY+3a8tlWJwsQap2VX5pwzwo4h3eOamfo=
github.com/gorilla/websocket v1.5.4-0.20240702125206-a62d9d2a8413 h1:0Zn/h+BUQg6QHkybGvjFD7BnIbjjz3oWUObacn//1Go=
github.com/gorilla/websocket v1.5.4-0.20240702125206-a62d9d2a8413/go.mod h1:r4w70xmWCQKmi1ONH4KIaBptdivuRPyosB9RmPlGEwA=
github.com/invopop/jsonschema v0.12.0 h1:6ovsNSuvn9wEQVOyc72aycBMVQFKz7cPdMJn10CvzRI=
github.com/invopop/jsonschema v0.12.0/go.mod h1:ffZ5Km5SWWRAIN6wbDXItl95euhFz2uON45H2qjYt+0=
github.com/jeremija/gosubmit v0.2.7 h1:At0OhGCFGPXyjPYAsCchoBUhE099pcBXmsb4iZqROIc=
github.com/jeremija/gosubmit v0.2.7/go.mod h1:Ui+HS073lCFREXBbdfrJzMB57OI/bdxTiLtrDHHhFPI=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a/go.mod h1:UJSiEoRfvx3hP73CvoARgeLjaIOjybY9vj8PUPPFGeU=
github.com/juju/clock v1.0.3 h1:yJHIsWXeU8j3QcBdiess09SzfiXRRrsjKPn2whnMeds=
github.com/juju/clock v1.0.3/go.mod h1:HIBvJ8kiV/n7UHwKuCkdYL4l/MDECztHR2sAvWDxxf0=
Expand All @@ -53,6 +60,8 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/lunixbochs/vtclean v0.0.0-20160125035106-4fbf7632a2c6/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm/+2c2E2WMI=
github.com/lxc/incus v0.7.0 h1:8jmxeBgBWCViTmioVhThmsKD7z6CZxvObE/thvEyJUw=
github.com/lxc/incus v0.7.0/go.mod h1:8Qh8J+Y00qaSgEDx4h/c9Pvcm2Zho+t3p6g3idt8jKk=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/mattn/go-colorable v0.0.6/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.0-20160806122752-66b8e73f3f5c/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
Expand Down Expand Up @@ -90,6 +99,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/teris-io/shortid v0.0.0-20220617161101-71ec9f2aa569 h1:xzABM9let0HLLqFypcxvLmlvEciCHL7+Lv+4vwZqecI=
github.com/teris-io/shortid v0.0.0-20220617161101-71ec9f2aa569/go.mod h1:2Ly+NIftZN4de9zRmENdYbvPQeaVIYKWpLFStLFEBgI=
github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc=
github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb h1:zGWFAtiMcyryUHoUjUJX0/lt1H2+i2Ka2n+D3DImSNo=
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
Expand Down
52 changes: 7 additions & 45 deletions provider/specs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,60 +19,22 @@ import (
"encoding/json"
"fmt"

"github.com/cloudbase/garm-provider-common/cloudconfig"
commonParams "github.com/cloudbase/garm-provider-common/params"
"github.com/pkg/errors"
"github.com/xeipuuv/gojsonschema"
)

const jsonSchema string = `
{
"$schema": "http://cloudbase.it/garm-provider-incus/schemas/extra_specs#",
"type": "object",
"description": "Schema defining supported extra specs for the Garm Incus Provider",
"properties": {
"extra_packages": {
"type": "array",
"description": "A list of packages that cloud-init should install on the instance.",
"items": {
"type": "string"
}
},
"disable_updates": {
"type": "boolean",
"description": "Whether to disable updates when cloud-init comes online."
},
"enable_boot_debug": {
"type": "boolean",
"description": "Allows providers to set the -x flag in the runner install script."
},
"runner_install_template": {
"type": "string",
"description": "This option can be used to override the default runner install template. If used, the caller is responsible for the correctness of the template as well as the suitability of the template for the target OS. Use the extra_context extra spec if your template has variables in it that need to be expanded."
},
"extra_context": {
"type": "object",
"description": "Extra context that will be passed to the runner_install_template.",
"additionalProperties": {
"type": "string"
}
},
"pre_install_scripts": {
"type": "object",
"description": "A map of pre-install scripts that will be run before the runner install script. These will run as root and can be used to prep a generic image before we attempt to install the runner. The key of the map is the name of the script as it will be written to disk. The value is a byte array with the contents of the script."
}
},
"additionalProperties": false
}
`

type extraSpecs struct {
DisableUpdates bool `json:"disable_updates"`
ExtraPackages []string `json:"extra_packages"`
EnableBootDebug bool `json:"enable_boot_debug"`
ExtraPackages []string `json:"extra_packages,omitempty" jsonschema:"description=A list of packages that cloud-init should install on the instance."`
DisableUpdates bool `json:"disable_updates,omitempty" jsonschema:"description=Whether to disable updates when cloud-init comes online."`
EnableBootDebug bool `json:"enable_boot_debug,omitempty" jsonschema:"description=Allows providers to set the -x flag in the runner install script."`
cloudconfig.CloudConfigSpec
}

func jsonSchemaValidation(schema json.RawMessage) error {
schemaLoader := gojsonschema.NewStringLoader(jsonSchema)
jsonSchema := generateJSONSchema()
schemaLoader := gojsonschema.NewGoLoader(jsonSchema)
extraSpecsLoader := gojsonschema.NewBytesLoader(schema)
result, err := gojsonschema.Validate(schemaLoader, extraSpecsLoader)
if err != nil {
Expand Down
211 changes: 133 additions & 78 deletions provider/specs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,103 +19,158 @@ import (
"encoding/json"
"testing"

commonParams "github.com/cloudbase/garm-provider-common/params"
"github.com/cloudbase/garm-provider-common/cloudconfig"
"github.com/cloudbase/garm-provider-common/params"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestJsonSchemaValidation(t *testing.T) {
tests := []struct {
name string
input json.RawMessage
errString string
}{
{
name: "Valid input",
input: json.RawMessage(`{
"disable_updates": true,
"extra_packages": ["openssh-server", "jq"],
"enable_boot_debug": false
}`),
errString: "",
var testCases = []struct {
name string
input json.RawMessage
expectedOutput extraSpecs
errString string
}{
{
name: "full specs",
input: json.RawMessage(`{"disable_updates": true, "extra_packages": ["package1", "package2"], "enable_boot_debug": true, "runner_install_template": "IyEvYmluL2Jhc2gKZWNobyBJbnN0YWxsaW5nIHJ1bm5lci4uLg==", "pre_install_scripts": {"setup.sh": "IyEvYmluL2Jhc2gKZWNobyBTZXR1cCBzY3JpcHQuLi4="}, "extra_context": {"key": "value"}}`),
expectedOutput: extraSpecs{
DisableUpdates: true,
ExtraPackages: []string{"package1", "package2"},
EnableBootDebug: true,
CloudConfigSpec: cloudconfig.CloudConfigSpec{
RunnerInstallTemplate: []byte("#!/bin/bash\necho Installing runner..."),
PreInstallScripts: map[string][]byte{
"setup.sh": []byte("#!/bin/bash\necho Setup script..."),
},
ExtraContext: map[string]string{"key": "value"},
},
},
{
name: "Invalid input - wrong data type",
input: json.RawMessage(`{
"disable_updates": "true"
}`),
errString: "schema validation failed: [disable_updates: Invalid type. Expected: boolean, given: string]",
errString: "",
},
{
name: "specs just with disable_updates",
input: json.RawMessage(`{"disable_updates": true}`),
expectedOutput: extraSpecs{
DisableUpdates: true,
},
{
name: "Invalid input - additional property",
input: json.RawMessage(`{
"additional_property": true
}`),
errString: "Additional property additional_property is not allowed",
errString: "",
},
{
name: "specs just with extra_packages",
input: json.RawMessage(`{"extra_packages": ["package1", "package2"]}`),
expectedOutput: extraSpecs{
ExtraPackages: []string{"package1", "package2"},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := jsonSchemaValidation(tt.input)
if tt.errString == "" {
assert.NoError(t, err, "Expected no error, got %v", err)
} else {
assert.Error(t, err, "Expected an error")
if err != nil {
assert.Contains(t, err.Error(), tt.errString, "Error message does not match")
}
}
})
}
}

func TestExtraSpecs(t *testing.T) {
tests := []struct {
name string
specs commonParams.BootstrapInstance
expectedOutput extraSpecs
errString string
}{
{
name: "Empty specs",
specs: commonParams.BootstrapInstance{
ExtraSpecs: nil,
},
expectedOutput: extraSpecs{},
errString: "",
errString: "",
},
{
name: "specs just with enable_boot_debug",
input: json.RawMessage(`{"enable_boot_debug": true}`),
expectedOutput: extraSpecs{
EnableBootDebug: true,
},
{
name: "Valid specs",
specs: commonParams.BootstrapInstance{
ExtraSpecs: []byte(`{"disable_updates": true, "extra_packages": ["package1", "package2"], "enable_boot_debug": false}`),
errString: "",
},
{
name: "specs just with runner_install_template",
input: json.RawMessage(`{"runner_install_template": "IyEvYmluL2Jhc2gKZWNobyBJbnN0YWxsaW5nIHJ1bm5lci4uLg=="}`),
expectedOutput: extraSpecs{
CloudConfigSpec: cloudconfig.CloudConfigSpec{
RunnerInstallTemplate: []byte("#!/bin/bash\necho Installing runner..."),
},
expectedOutput: extraSpecs{
DisableUpdates: true,
ExtraPackages: []string{"package1", "package2"},
EnableBootDebug: false,
},
errString: "",
},
{
name: "specs just with pre_install_scripts",
input: json.RawMessage(`{"pre_install_scripts": {"setup.sh": "IyEvYmluL2Jhc2gKZWNobyBTZXR1cCBzY3JpcHQuLi4="}}`),
expectedOutput: extraSpecs{
CloudConfigSpec: cloudconfig.CloudConfigSpec{
PreInstallScripts: map[string][]byte{
"setup.sh": []byte("#!/bin/bash\necho Setup script..."),
},
},
errString: "",
},
{
name: "Invalid specs",
specs: commonParams.BootstrapInstance{
ExtraSpecs: []byte(`{"disable_updates": true, "extra_packages": ["package1", "package2", "package3"], "enable_boot_debug": "false"}`),
errString: "",
},
{
name: "specs just with extra_context",
input: json.RawMessage(`{"extra_context": {"key": "value"}}`),
expectedOutput: extraSpecs{
CloudConfigSpec: cloudconfig.CloudConfigSpec{
ExtraContext: map[string]string{"key": "value"},
},
expectedOutput: extraSpecs{},
errString: "Invalid type. Expected: boolean, given: string",
},
}
errString: "",
},
{
name: "empty specs",
input: json.RawMessage(`{}`),
expectedOutput: extraSpecs{},
errString: "",
},
{
name: "invalid json",
input: json.RawMessage(`{"disable_updates": true, "extra_packages": ["package1", "package2", "enable_boot_debug": true}`),
expectedOutput: extraSpecs{},
errString: "failed to validate extra specs",
},
{
name: "invalid input for disable_updates - wrong data type",
input: json.RawMessage(`{"disable_updates": "true"}`),
expectedOutput: extraSpecs{},
errString: "schema validation failed: [disable_updates: Invalid type. Expected: boolean, given: string]",
},
{
name: "invalid input for extra_packages - wrong data type",
input: json.RawMessage(`{"extra_packages": "package1"}`),
expectedOutput: extraSpecs{},
errString: "schema validation failed: [extra_packages: Invalid type. Expected: array, given: string]",
},
{
name: "invalid input for enable_boot_debug - wrong data type",
input: json.RawMessage(`{"enable_boot_debug": "true"}`),
expectedOutput: extraSpecs{},
errString: "schema validation failed: [enable_boot_debug: Invalid type. Expected: boolean, given: string]",
},
{
name: "invalid input for runner_install_template - wrong data type",
input: json.RawMessage(`{"runner_install_template": true}`),
expectedOutput: extraSpecs{},
errString: "schema validation failed: [runner_install_template: Invalid type. Expected: string, given: boolean]",
},
{
name: "invalid input for pre_install_scripts - wrong data type",
input: json.RawMessage(`{"pre_install_scripts": "setup.sh"}`),
expectedOutput: extraSpecs{},
errString: "schema validation failed: [pre_install_scripts: Invalid type. Expected: object, given: string]",
},
{
name: "invalid input for extra_context - wrong data type",
input: json.RawMessage(`{"extra_context": ["key", "value"]}`),
expectedOutput: extraSpecs{},
errString: "schema validation failed: [extra_context: Invalid type. Expected: object, given: array]",
},
{
name: "invalid input - additional property",
input: json.RawMessage(`{"additional_property": true}`),
expectedOutput: extraSpecs{},
errString: "Additional property additional_property is not allowed",
},
}

for _, tt := range tests {
func TestParseExtraSpecsFromBootstrapParams(t *testing.T) {
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
output, err := parseExtraSpecsFromBootstrapParams(tt.specs)
got, err := parseExtraSpecsFromBootstrapParams(params.BootstrapInstance{ExtraSpecs: tt.input})
assert.Equal(t, tt.expectedOutput, got)
if tt.errString != "" {
assert.ErrorContains(t, err, tt.errString)
require.Error(t, err)
assert.Contains(t, err.Error(), tt.errString)
} else {
require.NoError(t, err)
}
assert.Equal(t, tt.expectedOutput, output)
})
}
}
Loading

0 comments on commit 221162c

Please sign in to comment.