Skip to content

Commit

Permalink
Add unit tests for generate command
Browse files Browse the repository at this point in the history
This commit add unit tests for `generate` verb of faas-cli.

This also refactors some of the code for `generate` verb.

Fixes: #492

Signed-off-by: Vivek Singh <vivekkmr45@yahoo.in>
  • Loading branch information
viveksyngh authored and alexellis committed Aug 14, 2018
1 parent 418db17 commit 517378c
Show file tree
Hide file tree
Showing 2 changed files with 198 additions and 9 deletions.
29 changes: 20 additions & 9 deletions commands/generate.go
Expand Up @@ -94,23 +94,37 @@ 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 {
for name, function := range services.Functions {
//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{
Expand All @@ -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,
Expand All @@ -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
}
178 changes: 178 additions & 0 deletions 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)
}
}

}

0 comments on commit 517378c

Please sign in to comment.