Add version to stack file

A default version is added to the stack file. Tested on
a Linux client by creating a new function to confirm that
a default version is added.

Unit tests were created to validate the version when parsing an
existing stack YAML file.

Signed-off-by: Kevin <kevin_turcios@outlook.com>
This commit is contained in:
Kevin
2019-04-20 23:18:46 -04:00
committed by Alex Ellis
parent 2487d1d344
commit 220db62f95
7 changed files with 104 additions and 11 deletions

2
.gitignore vendored
View File

@@ -24,4 +24,4 @@ obj
template
*.pem
secrets.yml
secrets.yml

View File

@@ -14,9 +14,10 @@ import (
)
const (
defaultGateway = "http://127.0.0.1:8080"
defaultNetwork = ""
defaultYAML = "stack.yml"
defaultGateway = "http://127.0.0.1:8080"
defaultNetwork = ""
defaultYAML = "stack.yml"
defaultSchemaVersion = "1.0"
)
// Flags that are to be added to all commands.

View File

@@ -238,7 +238,8 @@ func prepareYAMLContent(appendMode bool, gateway string, function *stack.Functio
`
if !appendMode {
yamlContent = `provider:
yamlContent = `version: ` + defaultSchemaVersion + `
provider:
name: openfaas
gateway: ` + gateway + `
functions:

View File

@@ -145,7 +145,6 @@ func runNewFunctionTest(t *testing.T, nft NewFunctionTest) {
faasCmd.SetArgs(cmdParameters)
execErr := faasCmd.Execute()
if nft.expectedMsg == SuccessMsg {
// Make sure that the folder and file was created:
@@ -172,6 +171,12 @@ func runNewFunctionTest(t *testing.T, nft NewFunctionTest) {
services := *parsedServices
var testServices stack.Services
testServices.Version = defaultSchemaVersion
if services.Version != testServices.Version {
t.Fatalf("YAML `version` section was not created correctly for file %s: got %v", funcYAML, services.Version)
}
testServices.Provider = stack.Provider{Name: "openfaas", GatewayURL: defaultGateway}
if !reflect.DeepEqual(services.Provider, testServices.Provider) {
t.Fatalf("YAML `provider` section was not created correctly for file %s: got %v", funcYAML, services.Provider)

View File

@@ -71,6 +71,7 @@ type EnvironmentFile struct {
// Services root level YAML file to define FaaS function-set
type Services struct {
Version string `yaml:"version,omitempty"`
Functions map[string]Function `yaml:"functions,omitempty"`
Provider Provider `yaml:"provider,omitempty"`
}

View File

@@ -20,6 +20,12 @@ import (
const providerName = "faas"
const providerNameLong = "openfaas"
const defaultSchemaVersion = "1.0"
// ValidSchemaVersions available schema versions
var ValidSchemaVersions = []string{
"1.0",
}
// ParseYAMLFile parse YAML file into a stack of "services".
func ParseYAMLFile(yamlFile, regex, filter string, envsubst bool) (*Services, error) {
@@ -92,6 +98,10 @@ func ParseYAMLData(fileData []byte, regex string, filter string, envsubst bool)
return nil, fmt.Errorf("['%s', '%s'] is the only valid provider for this tool - found: %s", providerName, providerNameLong, services.Provider.Name)
}
if len(services.Version) > 0 && !IsValidSchemaVersion(services.Version) {
return nil, fmt.Errorf("%s are the only valid versions for the stack file - found: %s", ValidSchemaVersions, services.Version)
}
if regexExists && filterExists {
return nil, fmt.Errorf("pass in a regex or a filter, not both")
}
@@ -167,3 +177,13 @@ func fetchYAML(address *url.URL) ([]byte, error) {
return resBytes, err
}
// IsValidSchemaVersion validates schema version
func IsValidSchemaVersion(schemaVersion string) bool {
for _, validVersion := range ValidSchemaVersions {
if schemaVersion == validVersion {
return true
}
}
return false
}

View File

@@ -6,12 +6,14 @@ package stack
import (
"os"
"reflect"
"regexp"
"sort"
"strings"
"testing"
)
const TestData_1 string = `provider:
const TestData_1 string = `version: 1.0
provider:
name: openfaas
gateway: http://127.0.0.1:8080
network: "func_functions"
@@ -44,7 +46,8 @@ functions:
image: stuff2/stuff23423
`
const TestData_2 string = `provider:
const TestData_2 string = `version: 1.0
provider:
name: openfaas
gateway: http://127.0.0.1:8080
network: "func_functions"
@@ -335,7 +338,8 @@ func Test_ParseYAMLData_ProviderValues(t *testing.T) {
title: "Provider is faas and gives no error",
provider: "faas",
expectedError: "",
file: `provider:
file: `version: 1.0
provider:
name: openfaas
gateway: http://127.0.0.1:8080
network: "func_functions"
@@ -345,7 +349,8 @@ func Test_ParseYAMLData_ProviderValues(t *testing.T) {
title: "Provider is openfaas and gives no error",
provider: "faas",
expectedError: "",
file: `provider:
file: `version: 1.0
provider:
name: openfaas
gateway: http://127.0.0.1:8080
network: "func_functions"
@@ -355,7 +360,8 @@ func Test_ParseYAMLData_ProviderValues(t *testing.T) {
title: "Provider is serverless-openfaas and gives error",
provider: "faas",
expectedError: "['faas', 'openfaas'] is the only valid provider for this tool - found: serverless-openfaas",
file: `provider:
file: `version: 1.0
provider:
name: serverless-openfaas
gateway: http://127.0.0.1:8080
network: "func_functions"
@@ -377,6 +383,65 @@ func Test_ParseYAMLData_ProviderValues(t *testing.T) {
}
}
func Test_ParseYAMLData_SchemaVersionValues(t *testing.T) {
testCases := []struct {
title string
provider string
version string
expectedError string
file string
}{
{
title: "Missing schema version assumes default with no error",
provider: "",
version: "",
expectedError: "",
file: `
provider:
name: openfaas
gateway: http://127.0.0.1:8080
network: "func_functions"
`,
},
{
title: "Insupported schema version and gives error",
provider: "faas",
version: "1.35",
expectedError: `(?m: are the only valid versions for the stack file - found)`,
file: `version: 1.35
provider:
name: openfaas
gateway: http://127.0.0.1:8080
network: "func_functions"
`,
},
{
title: "Schema version is valid",
provider: "faas",
version: "1.0",
expectedError: "",
file: `version: 1.0
provider:
name: serverless-openfaas
gateway: http://127.0.0.1:8080
network: "func_functions"
`,
},
}
for _, test := range testCases {
t.Run(test.title, func(t *testing.T) {
_, err := ParseYAMLData([]byte(test.file), ".*", "*", true)
if len(test.expectedError) > 0 {
if found, err2 := regexp.MatchString(test.expectedError, err.Error()); err2 != nil || !found {
t.Fatalf("Output is not as expected: %s\n", err)
}
}
})
}
}
func Test_substituteEnvironment_DefaultOverridden(t *testing.T) {
os.Setenv("USER", "alexellis2")