Skip to content

Commit

Permalink
Restructuring of add-template by Minh-Quan
Browse files Browse the repository at this point in the history
1. add-template updates
2. overwrite functionality
3. cacheLanguages for faster parsing, cache directory for zip file
4. tests for add-template, with overwrite option
5. test for PullTemplates function

Signed-off-by: Eric Stoekl <ems5311@gmail.com>
Signed-off-by: Minh-Quan TRAN <account@itscaro.me>
  • Loading branch information
ericstoekl committed Sep 18, 2017
1 parent a2c488e commit cd9bb4c
Show file tree
Hide file tree
Showing 17 changed files with 483 additions and 48 deletions.
62 changes: 62 additions & 0 deletions commands/add_template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) Alex Ellis, Eric Stoekl 2017. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package commands

import (
"errors"

"fmt"

"os"

"regexp"

"github.com/spf13/cobra"
)

// Args and Flags that are to be added to commands

var (
repository string
overwrite bool
)

func init() {
addTemplateCmd.Flags().BoolVar(&overwrite, "overwrite", false, "Overwrite existing templates?")

faasCmd.AddCommand(addTemplateCmd)
}

// addTemplateCmd allows the user to fetch a template from a repository
var addTemplateCmd = &cobra.Command{
Use: "add-template <repository URL>",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("A repository URL must be specified")
} else {
var validURL = regexp.MustCompile(`^https?://.+`)

if !validURL.MatchString(args[0]) {
return errors.New("The given URL does not begin with http")
}
}
return nil
},
Short: "Downloads templates from the specified github repo",
Long: `Downloads the compressed github repo specified by [URL], and extracts the 'template'
directory from the root of the repo, if it exists.`,
Example: "faas-cli add-template https://github.com/alexellis/faas-cli",
Run: runAddTemplate,
}

func runAddTemplate(cmd *cobra.Command, args []string) {
repository = args[0]

fmt.Println("Get " + repository)

if err := fetchTemplates(repository, overwrite); err != nil {
fmt.Println(err)

os.Exit(1)
}
}
122 changes: 122 additions & 0 deletions commands/add_template_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// Copyright (c) Alex Ellis, Eric Stoekl 2017. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package commands

import (
"bytes"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"os"
"regexp"
"strings"
"testing"
)

func Test_addTemplate(t *testing.T) {
defer tearDown_fetch_templates(t)

ts := httpTestServer(t)
defer ts.Close()

repository = ts.URL + "/owner/repo"
faasCmd.SetArgs([]string{"add-template", repository})
faasCmd.Execute()

// Verify created directories
for _, dir := range []string{".cache", "template"} {
if _, err := os.Stat(dir); err != nil {
t.Fatalf("The directory %s was not created", dir)
}
}
}

func Test_addTemplate_with_overwriting(t *testing.T) {
defer tearDown_fetch_templates(t)

ts := httpTestServer(t)
defer ts.Close()

repository = ts.URL + "/owner/repo"
faasCmd.SetArgs([]string{"add-template", repository})
faasCmd.Execute()

// reset cacheCanWriteLanguage
cacheCanWriteLanguage = make(map[string]bool)

var buf bytes.Buffer
log.SetOutput(&buf)

r := regexp.MustCompile(`(?m:overwriting is not allowed)`)

faasCmd.SetArgs([]string{"add-template", repository})
faasCmd.Execute()

// reset cacheCanWriteLanguage
cacheCanWriteLanguage = make(map[string]bool)

if !r.MatchString(buf.String()) {
t.Fatal(buf.String())
}

buf.Reset()

faasCmd.SetArgs([]string{"add-template", repository, "--overwrite"})
faasCmd.Execute()

if r.MatchString(buf.String()) {
t.Fatal()
}

// Verify created directories
for _, dir := range []string{".cache", "template"} {
if _, err := os.Stat(dir); err != nil {
t.Fatalf("The directory %s was not created", dir)
}
}
}

func Test_addTemplate_error_no_arg(t *testing.T) {
var buf bytes.Buffer

faasCmd.SetArgs([]string{"add-template"})
faasCmd.SetOutput(&buf)
faasCmd.Execute()

if !strings.Contains(buf.String(), "Error: A repository URL must be specified") {
t.Fatal("Output does not contain the required string")
}
}

func Test_addTemplate_error_not_valid_url(t *testing.T) {
var buf bytes.Buffer

faasCmd.SetArgs([]string{"add-template", "git@github.com:alexellis/faas-cli.git"})
faasCmd.SetOutput(&buf)
faasCmd.Execute()

if !strings.Contains(buf.String(), "Error: The given URL does not begin with http") {
t.Fatal("Output does not contain the required string")
}
}

// httpTestServer returns a testing http server
func httpTestServer(t *testing.T) *httptest.Server {
const sampleMasterZipPath string = "testdata/master_test.zip"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

if _, err := os.Stat(sampleMasterZipPath); os.IsNotExist(err) {
t.Error(err)
}

fileData, err := ioutil.ReadFile(sampleMasterZipPath)
if err != nil {
t.Error(err)
}

w.Write(fileData)
}))

return ts
}
2 changes: 1 addition & 1 deletion commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func PullTemplates() error {
if err != nil || exists == nil {
log.Println("No templates found in current directory.")

err = fetchTemplates()
err = fetchTemplates("", false)
if err != nil {
log.Println("Unable to download templates from Github.")
return err
Expand Down
23 changes: 22 additions & 1 deletion commands/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,32 @@ func runDeploy(cmd *cobra.Command, args []string) {

for k, function := range services.Functions {
function.Name = k
fmt.Printf("Deploying: %s.\n", function.Name)
if function.Constraints != nil {
constraints = *function.Constraints
}

// Get FProcess to use from the ./template/template.yml, if a template is being used
if function.Language != "" && function.Language != "Dockerfile" && function.Language != "dockerfile" {
pathToTemplateYAML := "./template/" + function.Language + "/template.yml"
if _, err := os.Stat(pathToTemplateYAML); os.IsNotExist(err) {
log.Fatalln(err.Error())
return
}

var langTemplate stack.LanguageTemplate
parsedLangTemplate, err := stack.ParseYAMLForLanguageTemplate(pathToTemplateYAML)

if err != nil {
log.Fatalln(err.Error())
return
}

if parsedLangTemplate != nil {
langTemplate = *parsedLangTemplate
function.FProcess = langTemplate.FProcess
}
}

proxy.DeployFunction(function.FProcess, services.Provider.GatewayURL, function.Name, function.Image, function.Language, replace, function.Environment, services.Provider.Network, constraints)
}
} else {
Expand Down
Loading

0 comments on commit cd9bb4c

Please sign in to comment.