Skip to content

Commit

Permalink
Make dockerfile a language with a file in the repo
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Ellis <[email protected]>

mocked local template repository for test
update TEMPLATE.md to reflect that docker is a language
rename stack variable to stackYaml to avoid conflict with imported package

Signed-off-by: Minh-Quan TRAN <[email protected]>
  • Loading branch information
itscaro authored and alexellis committed Jan 24, 2018
1 parent 2555d8d commit 28019a2
Show file tree
Hide file tree
Showing 31 changed files with 83 additions and 101 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "commands/testdata/templates"]
path = commands/testdata/templates
url = https://github.com/openfaas/templates.git
2 changes: 1 addition & 1 deletion commands/fetch_templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func fetchTemplates(templateURL string, overwrite bool) error {
log.Printf("Attempting to expand templates from %s\n", templateURL)
pullDebugPrint(fmt.Sprintf("Temp files in %s", dir))
args := map[string]string{"dir": dir, "repo": templateURL}
if err := versioncontrol.GitClone.Invoke(args); err != nil {
if err := versioncontrol.GitClone.Invoke(".", args); err != nil {
return err
}

Expand Down
15 changes: 9 additions & 6 deletions commands/fetch_templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ package commands
import (
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/openfaas/faas-cli/builder"
"github.com/openfaas/faas-cli/versioncontrol"
)

Expand Down Expand Up @@ -41,12 +43,13 @@ func setupLocalTemplateRepo(t *testing.T) string {
t.Error(err)
}

args := map[string]string{
"dir": dir,
"repo": defaultTemplateRepository,
}
if err := versioncontrol.GitClone.Invoke(args); err != nil {
t.Error(err)
// Copy the submodule to temp directory to avoid altering it during tests
testRepoGit := filepath.Join("testdata", "templates")
builder.CopyFiles(testRepoGit, dir)
// Remove submodule .git file
os.Remove(filepath.Join(dir, ".git"))
if err := versioncontrol.GitInitRepo.Invoke(dir, map[string]string{"dir": "."}); err != nil {
t.Fatal(err)
}

return dir
Expand Down
39 changes: 8 additions & 31 deletions commands/new_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"

"github.com/openfaas/faas-cli/builder"
"github.com/openfaas/faas-cli/stack"
Expand All @@ -20,7 +20,6 @@ var (
list bool
)

// Implement interface for sorting array of strings
type StrSort []string

func (a StrSort) Len() int { return len(a) }
Expand Down Expand Up @@ -62,12 +61,8 @@ func runNewFunction(cmd *cobra.Command, args []string) error {
}
}

fmt.Printf(`Languages available as templates:
` + printAvailableTemplates(availableTemplates) + `
fmt.Printf("Languages available as templates:\n%s\n", printAvailableTemplates(availableTemplates))

Or alternatively create a folder containing a Dockerfile, then pick
the "Dockerfile" lang type in your YAML file.
`)
return nil
}

Expand All @@ -90,36 +85,19 @@ the "Dockerfile" lang type in your YAML file.
return fmt.Errorf("folder: %s already exists", functionName)
}

if err := os.Mkdir("./"+functionName, 0700); err == nil {
if err := os.Mkdir(functionName, 0700); err == nil {
fmt.Printf("Folder: %s created.\n", functionName)
} else {
return fmt.Errorf("folder: could not create %s : %s", functionName, err)
}

if err := updateGitignore(); err != nil {
return fmt.Errorf("got unexpected error while updating .gitignore file: %s", err)
}

// Only "template" language templates - Dockerfile must be custom, so start with empty directory.
if strings.ToLower(lang) != "dockerfile" {
builder.CopyFiles("./template/"+lang+"/function/", "./"+functionName+"/")
} else {
ioutil.WriteFile("./"+functionName+"/Dockerfile", []byte(`FROM alpine:3.6
# Use any image as your base image, or "scratch"
# Add fwatchdog binary via https://github.com/openfaas/faas/releases/
# Then set fprocess to the process you want to invoke per request - i.e. "cat" or "my_binary"
ADD https://github.com/openfaas/faas/releases/download/0.6.9/fwatchdog /usr/bin
# COPY ./fwatchdog /usr/bin/
RUN chmod +x /usr/bin/fwatchdog
# Populate example here - i.e. "cat", "sha512sum" or "node index.js"
ENV fprocess="wc -l"
builder.CopyFiles(filepath.Join("template", lang, "function"), functionName)

HEALTHCHECK --interval=5s CMD [ -e /tmp/.lock ] || exit 1
CMD ["fwatchdog"]
`), 0600)
}

stack := `provider:
stackYaml := `provider:
name: faas
gateway: ` + gateway + `
Expand All @@ -134,7 +112,7 @@ functions:
fmt.Println()
fmt.Printf("Function created in folder: %s\n", functionName)

stackWriteErr := ioutil.WriteFile("./"+functionName+".yml", []byte(stack), 0600)
stackWriteErr := ioutil.WriteFile("./"+functionName+".yml", []byte(stackYaml), 0600)
if stackWriteErr != nil {
return fmt.Errorf("error writing stack file %s", stackWriteErr)
}
Expand All @@ -148,7 +126,6 @@ func printAvailableTemplates(availableTemplates []string) string {
sort.Sort(StrSort(availableTemplates))
for _, template := range availableTemplates {
result += fmt.Sprintf("- %s\n", template)

}
return result
}
55 changes: 25 additions & 30 deletions commands/new_function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package commands
import (
"fmt"
"os"
"path/filepath"
"reflect"
"regexp"
"strings"
Expand All @@ -21,6 +20,7 @@ const InvalidYAMLMsg = `is not valid YAML`
const InvalidYAMLMap = `map is empty`
const ListOptionOutput = `Languages available as templates:
- csharp
- dockerfile
- go
- go-armhf
- node
Expand Down Expand Up @@ -123,29 +123,23 @@ func runNewFunctionTest(t *testing.T, nft NewFunctionTest) {
}

func Test_newFunctionTests(t *testing.T) {

homeDir, _ := filepath.Abs(".")
if err := os.Chdir("testdata/new_function"); err != nil {
t.Fatalf("Error on cd to testdata dir: %v", err)
}

for _, test := range NewFunctionTests {
t.Run(test.title, func(t *testing.T) {
runNewFunctionTest(t, test)
// Download templates
templatePullLocalTemplateRepo(t)
defer tearDownFetchTemplates(t)
defer tearDownNewFunction(t)

for _, testcase := range NewFunctionTests {
t.Run(testcase.title, func(t *testing.T) {
runNewFunctionTest(t, testcase)
})
}

if err := os.Chdir(homeDir); err != nil {
t.Fatalf("Error on cd back to commands/ directory: %v", err)
}
}

func Test_newFunctionListCmds(t *testing.T) {

homeDir, _ := filepath.Abs(".")
if err := os.Chdir("testdata/new_function"); err != nil {
t.Fatalf("Error on cd to testdata dir: %v", err)
}
// Download templates
templatePullLocalTemplateRepo(t)
defer tearDownFetchTemplates(t)
defer tearDownNewFunction(t)

cmdParameters := []string{
"new",
Expand All @@ -161,18 +155,13 @@ func Test_newFunctionListCmds(t *testing.T) {
if !strings.HasPrefix(stdOut, ListOptionOutput) {
t.Fatalf("Output is not as expected: %s\n", stdOut)
}

if err := os.Chdir(homeDir); err != nil {
t.Fatalf("Error on cd back to commands/ directory: %v", err)
}
}

func Test_languageNotExists(t *testing.T) {

homeDir, _ := filepath.Abs(".")
if err := os.Chdir("testdata/new_function"); err != nil {
t.Fatalf("Error on cd to testdata dir: %v", err)
}
// Download templates
templatePullLocalTemplateRepo(t)
defer tearDownFetchTemplates(t)
defer tearDownNewFunction(t)

// Attempt to create a function with a non-existing language
cmdParameters := []string{
Expand All @@ -190,8 +179,14 @@ func Test_languageNotExists(t *testing.T) {
if found, err := regexp.MatchString(LangNotExistsOutput, stdOut); err != nil || !found {
t.Fatalf("Output is not as expected: %s\n", stdOut)
}
}

if err := os.Chdir(homeDir); err != nil {
t.Fatalf("Error on cd back to commands/ directory: %v", err)
func tearDownNewFunction(t *testing.T) {
// Remove existing archive file if it exists
if _, err := os.Stat(".gitignore"); err == nil {
err := os.Remove(".gitignore")
if err != nil {
t.Log(err)
}
}
}
9 changes: 9 additions & 0 deletions commands/template_pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,12 @@ func Test_repositoryUrlRemoteRegExp(t *testing.T) {
})
}
}

// templatePullLocalTemplateRepo executes `template pull` on a local repository to get templates
func templatePullLocalTemplateRepo(t *testing.T) {
localTemplateRepository := setupLocalTemplateRepo(t)
defer os.RemoveAll(localTemplateRepository)

faasCmd.SetArgs([]string{"template", "pull", localTemplateRepository})
faasCmd.Execute()
}
Binary file removed commands/testdata/master_test.zip
Binary file not shown.
2 changes: 0 additions & 2 deletions commands/testdata/new_function/.gitignore

This file was deleted.

Empty file.
2 changes: 0 additions & 2 deletions commands/testdata/new_function/template/csharp/template.yml

This file was deleted.

2 changes: 0 additions & 2 deletions commands/testdata/new_function/template/go-armhf/template.yml

This file was deleted.

Empty file.
2 changes: 0 additions & 2 deletions commands/testdata/new_function/template/go/template.yml

This file was deleted.

Empty file.

This file was deleted.

This file was deleted.

Empty file.
2 changes: 0 additions & 2 deletions commands/testdata/new_function/template/node/template.yml

This file was deleted.

This file was deleted.

Empty file.
2 changes: 0 additions & 2 deletions commands/testdata/new_function/template/python/template.yml

This file was deleted.

Empty file.
2 changes: 0 additions & 2 deletions commands/testdata/new_function/template/python3/template.yml

This file was deleted.

Empty file.
2 changes: 0 additions & 2 deletions commands/testdata/new_function/template/ruby/template.yml

This file was deleted.

1 change: 1 addition & 0 deletions commands/testdata/templates
Submodule templates added at 9c4570
3 changes: 3 additions & 0 deletions guide/TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ template
├── csharp
│   ├── Dockerfile
│   └── template.yml
├── dockerfile
│   ├── Dockerfile
│   └── template.yml
├── node
│   ├── Dockerfile
│   └── template.yml
Expand Down
12 changes: 8 additions & 4 deletions stack/language_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,17 @@ func ParseYAMLDataForLanguageTemplate(fileData []byte) (*LanguageTemplate, error

func IsValidTemplate(lang string) bool {
var found bool

// TODO harmonise to lowercase when fetching template & parsing yaml
// Ensure that `lang` is lowercase in case of Dockerfile
if strings.ToLower(lang) == "dockerfile" {
found = true
} else if _, err := os.Stat("./template/" + lang); err == nil {
lang = strings.ToLower(lang)
}

if _, err := os.Stat("./template/" + lang); err == nil {
templateYAMLPath := "./template/" + lang + "/template.yml"

_, err := ParseYAMLForLanguageTemplate(templateYAMLPath)
if err == nil {
if _, err := ParseYAMLForLanguageTemplate(templateYAMLPath); err == nil {
found = true
}
}
Expand Down
4 changes: 0 additions & 4 deletions stack/language_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ fprocess: python index.py
}

func Test_IsValidTemplate(t *testing.T) {
if !IsValidTemplate("Dockerfile") || !IsValidTemplate("dockerfile") {
t.Fatalf("Dockerfile and dockerfile must be valid")
}

if IsValidTemplate("unknown-language") {
t.Fatalf("unknown-language must be invalid")
}
Expand Down
5 changes: 2 additions & 3 deletions versioncontrol/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ type vcsCmd struct {

// Invoke executes the vcsCmd replacing varibables in the cmds with the keyval
// variables passed.
func (v *vcsCmd) Invoke(args map[string]string) error {

func (v *vcsCmd) Invoke(dir string, args map[string]string) error {
for _, cmd := range v.cmds {
if _, err := v.run(".", cmd, args, true); err != nil {
if _, err := v.run(dir, cmd, args, true); err != nil {
return err
}
}
Expand Down
14 changes: 14 additions & 0 deletions versioncontrol/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,17 @@ var GitClone = &vcsCmd{
cmds: []string{"clone {repo} {dir} --depth=1"},
scheme: []string{"git", "https", "http", "git+ssh", "ssh"},
}

// GitInitRepo initializes the working directory add commit all files & directories
var GitInitRepo = &vcsCmd{
name: "Git",
cmd: "git",
cmds: []string{
"init {dir}",
"config user.email \"[email protected]\"",
"config user.name \"OpenFaaS\"",
"add {dir}",
"commit -m \"Test-commit\"",
},
scheme: []string{"git", "https", "http", "git+ssh", "ssh"},
}

0 comments on commit 28019a2

Please sign in to comment.