Skip to content

Commit

Permalink
--mock-http working, replaces http client with fake client, returns d…
Browse files Browse the repository at this point in the history
…ummy zip file

Signed-off-by: Eric Stoekl <ems5311@gmail.com>
  • Loading branch information
ericstoekl committed Sep 4, 2017
1 parent 003bd75 commit 16585bc
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
20 changes: 16 additions & 4 deletions commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"log"
"os"
"net/http"

"github.com/alexellis/faas-cli/builder"
"github.com/alexellis/faas-cli/stack"
Expand All @@ -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() {
Expand All @@ -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{})
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions commands/fetch_templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -62,22 +62,22 @@ 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 {
log.Println(err.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
Expand Down
25 changes: 25 additions & 0 deletions commands/httpClient.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 16585bc

Please sign in to comment.