custom-script-extension-linux/schema_test.go

122 строки
5.1 KiB
Go

package main
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestValidatePublicSettings_empty(t *testing.T) {
require.NotNil(t, validatePublicSettings(""))
require.NotNil(t, validatePublicSettings("{}"))
}
func TestValidatePublicSettings_missingArgument(t *testing.T) {
err := validatePublicSettings(`{"fileUris": ["http://a.b/c.txt"]}`)
require.NotNil(t, err)
require.Contains(t, err.Error(), "commandToExecute is required")
}
func TestValidatePublicSettings_hasRequiredArgument_butWrongType(t *testing.T) {
err := validatePublicSettings(`{"commandToExecute": ["foo"]}`)
require.NotNil(t, err)
require.Contains(t, err.Error(), "Invalid type. Expected: string, given: array")
}
func TestValidatePublicSettings_unrecognizedField(t *testing.T) {
err := validatePublicSettings(`{"commandToExecute": "date", "alien":0}`)
require.NotNil(t, err)
require.Contains(t, err.Error(), "Additional property alien is not allowed")
}
func TestValidatePublicSettings_fileUris(t *testing.T) {
// empty
err := validatePublicSettings(`{"commandToExecute": "date", "fileUris":[]}`)
require.Nil(t, err)
// not a URL
err = validatePublicSettings(`{"commandToExecute": "date", "fileUris":["a"]}`)
require.NotNil(t, err)
require.Contains(t, err.Error(), "Does not match format 'uri'")
// mixed types
err = validatePublicSettings(`{"commandToExecute": "date", "fileUris":["https://a.b/c.txt?d=e&f=g", 0]}`)
require.NotNil(t, err)
require.Contains(t, err.Error(), "Expected: string, given: integer")
}
func TestValidatePublicSettings_timestampSupported(t *testing.T) {
require.Nil(t, validatePublicSettings(`{"commandToExecute": "date", "timestamp": 1}`))
}
func TestValidateProtectedSettings_empty(t *testing.T) {
require.Nil(t, validateProtectedSettings(""), "empty string")
require.Nil(t, validateProtectedSettings("{}"), "empty string")
}
func TestValidateProtectedSettings_unrecognizedField(t *testing.T) {
err := validateProtectedSettings(`{"alien":0}`)
require.NotNil(t, err)
require.Contains(t, err.Error(), "Additional property alien is not allowed")
}
func TestValidateProtectedSettings_commandToExecute(t *testing.T) {
// Invalid type
err := validateProtectedSettings(`{"commandToExecute": false}`)
require.NotNil(t, err)
require.Contains(t, err.Error(), "Expected: string, given: boolean")
// Valid
require.Nil(t, validateProtectedSettings(`{"commandToExecute":"date"}`))
}
func TestValidateProtectedSettings_storageAccountName(t *testing.T) {
chkPatternMismatch := func(e error, reason string) {
require.NotNil(t, e, reason)
require.Contains(t, e.Error(), "storageAccountName: Does not match pattern", reason)
}
// Specified but empty
chkPatternMismatch(validateProtectedSettings(`{"storageAccountName": ""}`), "empty")
// Too short
chkPatternMismatch(validateProtectedSettings(`{"storageAccountName": "aa"}`), "too short")
// Too long
chkPatternMismatch(validateProtectedSettings(`{"storageAccountName": "1234567890123456789012345"}`), "too long")
// invalid chars
chkPatternMismatch(validateProtectedSettings(`{"storageAccountName": "foo-bar"}`), "invalid char")
chkPatternMismatch(validateProtectedSettings(`{"storageAccountName": " foobar"}`), "invalid char")
chkPatternMismatch(validateProtectedSettings(`{"storageAccountName": "foobar "}`), "invalid char")
chkPatternMismatch(validateProtectedSettings(`{"storageAccountName": "foo.bar"}`), "invalid char")
// ok storage account name
require.Nil(t, validateProtectedSettings(`{"storageAccountName": "0foobarquuxyz12345"}`))
}
func TestValidateProtectedSettings_storageAccountKey(t *testing.T) {
chkPatternMismatch := func(e error, reason string) {
require.NotNil(t, e, reason)
require.Contains(t, e.Error(), "storageAccountKey: Does not match pattern", reason)
}
// empty
chkPatternMismatch(validateProtectedSettings(`{"storageAccountKey": ""}`), "empty")
// bad string
chkPatternMismatch(validateProtectedSettings(`{"storageAccountKey": "NotABase64Really!"}`), "not b64")
// for a base64 string ending with '==', removing one of the '=' is not valid b64, the schema validation should catch that
chkPatternMismatch(validateProtectedSettings(`{"storageAccountKey": "OllwYfXmC0mSMhWg4x+lUdLg6Eoa/d44+PxPTXBaadO5l87L4JzgkyyVvQr8r60WIzG2X8r6LLxkhNBQaHa3XQ="}`), "bad b64")
// spacing
chkPatternMismatch(validateProtectedSettings(`{"storageAccountKey": "OllwYfXmC0mSMhWg4x+lUdLg6Eoa/d44+PxPTXBaadO5l87L4JzgkyyVvQr8r60WIzG2X8r6LLxkhNBQaHa3XQ== "}`), "whitespace")
chkPatternMismatch(validateProtectedSettings(`{"storageAccountKey": " OllwYfXmC0mSMhWg4x+lUdLg6Eoa/d44+PxPTXBaadO5l87L4JzgkyyVvQr8r60WIzG2X8r6LLxkhNBQaHa3XQ=="}`), "whitespace")
// ok
require.Nil(t, validateProtectedSettings(`{"storageAccountKey": "OllwYfXmC0mSMhWg4x+lUdLg6Eoa/d44+PxPTXBaadO5l87L4JzgkyyVvQr8r60WIzG2X8r6LLxkhNBQaHa3XQ=="}`), "ok")
require.Nil(t, validateProtectedSettings(`{"storageAccountKey": "A+hMRrsZQ6COPXTYX/EiKiF2HVtfhCfLDo3Dkc3ekKoX3jA58zXVG2QRe/C1+zdEFSrVX6FZsKyivsSlnwmWOw=="}`), "ok")
require.Nil(t, validateProtectedSettings(`{"storageAccountKey": "/yGnx6KyxQ8Pjzk0QXeY+66Du0BeTWaCt83la59w72hu/81e6TzskXXvL/IlO3q6g0k0kJrR9MYQNi+cNR3SXA=="}`), "ok")
}