diff --git a/commands/generate.go b/commands/generate.go index 7e9b4bdc5..53164d535 100644 --- a/commands/generate.go +++ b/commands/generate.go @@ -94,6 +94,20 @@ func runGenerate(cmd *cobra.Command, args []string) error { format = schema.BranchAndSHAFormat } + objectsString, err := generateCRDYAML(services, format, api, functionNamespace, branch, version) + if err != nil { + return err + } + + if len(objectsString) > 0 { + fmt.Println(objectsString) + } + return nil +} + +//generateCRDYAML generates CRD YAML for functions +func generateCRDYAML(services stack.Services, format schema.BuildFormat, apiVersion, namespace, branch, version string) (string, error) { + var objectsString string if len(services.Functions) > 0 { @@ -101,16 +115,16 @@ func runGenerate(cmd *cobra.Command, args []string) error { //read environment variables from the file fileEnvironment, err := readFiles(function.EnvironmentFile) if err != nil { - return err + return "", err } //combine all environment variables allEnvironment, envErr := compileEnvironment([]string{}, function.Environment, fileEnvironment) if envErr != nil { - return envErr + return "", envErr } - metadata := schema.Metadata{Name: name, Namespace: functionNamespace} + metadata := schema.Metadata{Name: name, Namespace: namespace} imageName := schema.BuildImageName(format, function.Image, version, branch) spec := schema.Spec{ @@ -125,7 +139,7 @@ func runGenerate(cmd *cobra.Command, args []string) error { } crd := schema.CRD{ - APIVersion: api, + APIVersion: apiVersion, Kind: resourceKind, Metadata: metadata, Spec: spec, @@ -134,14 +148,11 @@ func runGenerate(cmd *cobra.Command, args []string) error { //Marshal the object definition to yaml objectString, err := yaml.Marshal(crd) if err != nil { - return err + return "", err } objectsString += "---\n" + string(objectString) } } - if len(objectsString) > 0 { - fmt.Println(objectsString) - } - return nil + return objectsString, nil } diff --git a/commands/generate_test.go b/commands/generate_test.go new file mode 100644 index 000000000..437fe4f67 --- /dev/null +++ b/commands/generate_test.go @@ -0,0 +1,178 @@ +// Copyright (c) OpenFaaS Author(s) 2018. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. +package commands + +import ( + "testing" + + "github.com/openfaas/faas-cli/schema" + + "github.com/openfaas/faas-cli/stack" +) + +var generateTestcases = []struct { + Name string + Input string + Output string + Format schema.BuildFormat + APIVersion string + Namespace string + Branch string + Version string +}{ + { + Name: "Default Namespace and API Version", + Input: ` +provider: + name: faas + gateway: http://127.0.0.1:8080 + network: "func_functions" +functions: + url-ping: + lang: python + handler: ./sample/url-ping + image: alexellis/faas-url-ping:0.2`, + Output: `--- +apiVersion: openfaas.com/v1alpha2 +kind: Function +metadata: + name: url-ping + namespace: openfaas-fn +spec: + name: url-ping + image: alexellis/faas-url-ping:0.2 +`, + Format: schema.DefaultFormat, + APIVersion: "openfaas.com/v1alpha2", + Namespace: "openfaas-fn", + Branch: "", + Version: "", + }, + + { + Name: "Blank namespace", + Input: ` +provider: + name: faas + gateway: http://127.0.0.1:8080 + network: "func_functions" +functions: + url-ping: + lang: python + handler: ./sample/url-ping + image: alexellis/faas-url-ping:0.2`, + Output: `--- +apiVersion: openfaas.com/v1alpha2 +kind: Function +metadata: + name: url-ping +spec: + name: url-ping + image: alexellis/faas-url-ping:0.2 +`, + Format: schema.DefaultFormat, + APIVersion: "openfaas.com/v1alpha2", + Namespace: "", + Branch: "", + Version: "", + }, + { + Name: "BranchAndSHA Image format", + Input: ` +provider: + name: faas + gateway: http://127.0.0.1:8080 + network: "func_functions" +functions: + url-ping: + lang: python + handler: ./sample/url-ping + image: alexellis/faas-url-ping:0.2`, + Output: `--- +apiVersion: openfaas.com/v1alpha2 +kind: Function +metadata: + name: url-ping + namespace: openfaas-function +spec: + name: url-ping + image: alexellis/faas-url-ping:0.2-master-6bgf36qd +`, + Format: schema.BranchAndSHAFormat, + APIVersion: "openfaas.com/v1alpha2", + Namespace: "openfaas-function", + Branch: "master", + Version: "6bgf36qd", + }, + { + Name: "Multiple functions", + Input: ` +provider: + name: faas + gateway: http://127.0.0.1:8080 + network: "func_functions" +functions: + url-ping: + lang: python + handler: ./sample/url-ping + image: alexellis/faas-url-ping:0.2 + astronaut-finder: + lang: python3 + handler: ./astronaut-finder + image: astronaut-finder + environment: + write_debug: true`, + Output: `--- +apiVersion: openfaas.com/v2alpha2 +kind: Function +metadata: + name: url-ping + namespace: openfaas-fn +spec: + name: url-ping + image: alexellis/faas-url-ping:0.2 +--- +apiVersion: openfaas.com/v2alpha2 +kind: Function +metadata: + name: astronaut-finder + namespace: openfaas-fn +spec: + name: astronaut-finder + image: astronaut-finder:latest + environment: + write_debug: "true" +`, + Format: schema.DefaultFormat, + APIVersion: "openfaas.com/v2alpha2", + Namespace: "openfaas-fn", + Branch: "", + Version: "", + }, +} + +func Test_generateCRDYAML(t *testing.T) { + + for _, testcase := range generateTestcases { + parsedServices, err := stack.ParseYAMLData([]byte(testcase.Input), "", "") + + if err != nil { + t.Fatalf("%s failed: error while parsing the input data.", testcase.Name) + } + + if parsedServices == nil { + t.Fatalf("%s failed: empty input file", testcase.Name) + } + services := *parsedServices + + generatedYAML, err := generateCRDYAML(services, testcase.Format, testcase.APIVersion, testcase.Namespace, testcase.Branch, testcase.Version) + if err != nil { + t.Fatalf("%s failed: error while generating CRD YAML.", testcase.Name) + } + + if generatedYAML != testcase.Output { + t.Fatalf("%s failed: ouput is not as expected: %s", testcase.Name, generatedYAML) + } + } + +}