From 16585bce593754e9cfca0b9543b0d6af21cc1923 Mon Sep 17 00:00:00 2001 From: Eric Stoekl Date: Sun, 3 Sep 2017 15:01:17 -0700 Subject: [PATCH] --mock-http working, replaces http client with fake client, returns dummy zip file Signed-off-by: Eric Stoekl --- commands/build.go | 20 ++++++++++++++++---- commands/fetch_templates.go | 10 +++++----- commands/httpClient.go | 25 +++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 9 deletions(-) create mode 100644 commands/httpClient.go diff --git a/commands/build.go b/commands/build.go index 72fdbe9a6..50c6de44c 100644 --- a/commands/build.go +++ b/commands/build.go @@ -7,6 +7,7 @@ import ( "fmt" "log" "os" + "net/http" "github.com/alexellis/faas-cli/builder" "github.com/alexellis/faas-cli/stack" @@ -15,8 +16,9 @@ import ( // Flags that are to be added to commands. var ( - nocache bool - squash bool + nocache bool + squash bool + mockHttp bool ) func init() { @@ -30,6 +32,7 @@ func init() { buildCmd.Flags().BoolVar(&nocache, "no-cache", false, "Do not use Docker's build cache") buildCmd.Flags().BoolVar(&squash, "squash", false, `Use Docker's squash flag for smaller images [experimental] `) + buildCmd.Flags().BoolVar(&mockHttp, "mock-http", false, "Use mock http client for testing") // Set bash-completion. _ = buildCmd.Flags().SetAnnotation("handler", cobra.BashCompSubdirsInDir, []string{}) @@ -88,7 +91,9 @@ func runBuild(cmd *cobra.Command, args []string) { return } - builder.BuildImage(function.Image, function.Handler, function.Name, function.Language, nocache, squash) + if !mockHttp { + builder.BuildImage(function.Image, function.Handler, function.Name, function.Language, nocache, squash) + } } } } else { @@ -115,7 +120,14 @@ func pullTemplates() error { if err != nil || exists == nil { log.Println("No templates found in current directory.") - err = fetchTemplates() + var client HttpClient + if mockHttp { + client = &ClientMock{} + } else { + client = &http.Client{} + } + + err = fetchTemplates(client) if err != nil { log.Println("Unable to download templates from Github.") return err diff --git a/commands/fetch_templates.go b/commands/fetch_templates.go index 4390ec24a..c3c5e53ae 100644 --- a/commands/fetch_templates.go +++ b/commands/fetch_templates.go @@ -16,10 +16,10 @@ import ( ) // fetchTemplates fetch code templates from GitHub master zip file. -func fetchTemplates() error { +func fetchTemplates(client HttpClient) error { const zipFileName = "./master.zip" - err := fetchMasterZip() + err := fetchMasterZip(client) zipFile, err := zip.OpenReader(zipFileName) if err != nil { @@ -62,14 +62,14 @@ func fetchTemplates() error { return err } -func fetchMasterZip() error { +func fetchMasterZip(client HttpClient) error { var err error if _, err = os.Stat("master.zip"); err != nil { templateURL := os.Getenv("templateUrl") if len(templateURL) == 0 { templateURL = "https://github.com/alexellis/faas-cli/archive/master.zip" } - c := http.Client{} + //c := MockableClient{} req, err := http.NewRequest("GET", templateURL, nil) if err != nil { @@ -77,7 +77,7 @@ func fetchMasterZip() error { return err } log.Printf("HTTP GET %s\n", templateURL) - res, err := c.Do(req) + res, err := client.Do(req) if err != nil { log.Println(err.Error()) return err diff --git a/commands/httpClient.go b/commands/httpClient.go new file mode 100644 index 000000000..d5a198ab7 --- /dev/null +++ b/commands/httpClient.go @@ -0,0 +1,25 @@ +package commands + +import ( + "net/http" + "io/ioutil" + "bytes" + "log" +) + +type ClientMock struct { +} + +func (c *ClientMock) Do(req *http.Request) (*http.Response, error) { + log.Printf("hi from ClientMock Do()") + zip := []byte{80, 75, 05, 06, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00} + + resp := http.Response{ + Body: ioutil.NopCloser(bytes.NewBuffer(zip)), + } + return &resp, nil +} + +type HttpClient interface { + Do(req *http.Request) (*http.Response, error) +}