Skip to content
This repository has been archived by the owner on Mar 10, 2023. It is now read-only.

Add tests for cloning a git repo #616

Merged
merged 7 commits into from
Mar 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions garbage-collect/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"net/http"
"os"
"strings"
"time"

"github.com/alexellis/hmac"
"github.com/openfaas/openfaas-cloud/sdk"
Expand Down Expand Up @@ -45,7 +44,12 @@ func Handle(req []byte) string {
log.Fatal(err)
}

fmt.Printf("Functions owned by %s:\n %s", owner, deployedFunctions)
deployedList := ""
for _, fn := range deployedFunctions {
deployedList += fn.GetOwner() + "/" + fn.GetRepo() + ", "
}

log.Printf("Functions owned by %s:\n %s", owner, strings.Trim(deployedList, ", "))

deleted := 0
for _, fn := range deployedFunctions {
Expand Down Expand Up @@ -110,11 +114,8 @@ func included(fn *openFaaSFunction, owner string, functionStack []string) bool {

func deleteFunction(target, gatewayURL string) error {
var err error
fmt.Println("Delete ", target)
log.Printf("Deleting: %s", target)

c := http.Client{
Timeout: time.Second * 3,
}
delReq := struct {
FunctionName string
}{
Expand All @@ -127,19 +128,21 @@ func deleteFunction(target, gatewayURL string) error {

addAuthErr := sdk.AddBasicAuth(httpReq)
if addAuthErr != nil {
log.Printf("Basic auth error %s", addAuthErr)
log.Printf("Basic auth error: %s", addAuthErr)
return addAuthErr
}

response, err := c.Do(httpReq)
response, err := http.DefaultClient.Do(httpReq)

if err == nil {
defer response.Body.Close()
if response.Body != nil {
defer response.Body.Close()

bodyBytes, bErr := ioutil.ReadAll(response.Body)
if bErr != nil {
log.Fatal(bErr)
}
log.Println(string(bodyBytes))
log.Println("Delete response:", string(bodyBytes))
}
}

Expand All @@ -150,18 +153,14 @@ func listFunctions(owner, gatewayURL string) ([]openFaaSFunction, error) {

var err error

c := http.Client{
Timeout: time.Second * 3,
}

request, _ := http.NewRequest(http.MethodGet, gatewayURL+"/function/list-functions?user="+owner, nil)

response, err := c.Do(request)
response, err := http.DefaultClient.Do(request)

if err == nil {
defer response.Body.Close()

if response.Body != nil {
defer response.Body.Close()

bodyBytes, bErr := ioutil.ReadAll(response.Body)
if bErr != nil {
log.Fatal(bErr)
Expand Down
18 changes: 8 additions & 10 deletions git-tar/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
FROM golang:1.11-alpine3.10 as build
FROM openfaas/faas-cli:0.11.8 as faas-cli
FROM openfaas/classic-watchdog:0.18.1 as watchdog
FROM golang:1.13-alpine3.11 as build

ENV CGO_ENABLED=0
ENV GO111MODULE=off

RUN apk --no-cache add curl \
&& echo "Pulling binaries from GitHub." \
&& curl -sSLf https://github.com/openfaas/faas/releases/download/0.18.1/fwatchdog > /usr/bin/fwatchdog \
&& curl -sSLf https://github.com/openfaas/faas-cli/releases/download/0.9.3/faas-cli > /usr/local/bin/faas-cli \
&& chmod +x /usr/bin/fwatchdog \
&& chmod +x /usr/local/bin/faas-cli \
&& apk del curl --no-cache
COPY --from=watchdog /fwatchdog /usr/bin/
COPY --from=faas-cli /usr/bin/faas-cli /usr/bin/

WORKDIR /go/src/handler
COPY . .
Expand All @@ -21,7 +19,7 @@ RUN CGO_ENABLED=0 GOOS=linux \
go build --ldflags "-s -w" -a -installsuffix cgo -o handler . && \
go test $(go list ./... | grep -v /vendor/) -cover

FROM alpine:3.10
FROM alpine:3.11 as ship

RUN apk --no-cache add \
ca-certificates \
Expand All @@ -40,7 +38,7 @@ WORKDIR /home/app

COPY --from=build /go/src/handler/handler .
COPY --from=build /usr/bin/fwatchdog /usr/bin/fwatchdog
COPY --from=build /usr/local/bin/faas-cli /usr/local/bin/faas-cli
COPY --from=build /usr/bin/faas-cli /usr/local/bin/faas-cli

RUN chmod 777 /tmp

Expand Down
7 changes: 4 additions & 3 deletions git-tar/function/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ func Handle(req []byte) []byte {
os.Exit(-1)
}

clonePath, err := clone(pushEvent)
fetcher := GitRepoFetcher{}
clonePath, err := clone(fetcher, pushEvent)
if err != nil {
log.Println("Clone ", err.Error())
status.AddStatus(sdk.StatusFailure, "clone error : "+err.Error(), sdk.StackContext)
Expand Down Expand Up @@ -218,7 +219,7 @@ func Handle(req []byte) []byte {
tarMsg += fmt.Sprintf("%s @ %s, ", tar.functionName, tar.imageName)
}

deploymentMessage := fmt.Sprintf("Deployed: %s. Took %s", strings.TrimRight(tarMsg, ", "), completed.String())
deploymentMessage := fmt.Sprintf("Deployed: %s, time taken: %.2fs", strings.TrimRight(tarMsg, ", "), completed.Seconds())

auditEvent := sdk.AuditEvent{
Message: deploymentMessage,
Expand All @@ -228,7 +229,7 @@ func Handle(req []byte) []byte {
}
sdk.PostAudit(auditEvent)

return []byte(deploymentMessage)
return []byte(deploymentMessage + "\n")
}

func collect(pushEvent sdk.PushEvent, stack *stack.Services) error {
Expand Down
53 changes: 40 additions & 13 deletions git-tar/function/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,17 @@ func getRepositoryURL(e sdk.PushEvent, authToken tokener) (string, error) {
return cu, nil
}

func clone(pushEvent sdk.PushEvent) (string, error) {
func clone(fetcher RepoFetcher, pushEvent sdk.PushEvent) (string, error) {
workDir := os.TempDir()
destPath := path.Join(workDir, path.Join(pushEvent.Repository.Owner.Login, pushEvent.Repository.Name))

if len(pushEvent.Repository.Owner.Login) == 0 {
return "", fmt.Errorf("login must be specified")
}
if len(pushEvent.Repository.Name) == 0 {
return "", fmt.Errorf("repo name must be specified")
}

if _, err := os.Stat(destPath); err == nil {
truncateErr := os.RemoveAll(destPath)
if truncateErr != nil {
Expand Down Expand Up @@ -314,25 +321,45 @@ func clone(pushEvent sdk.PushEvent) (string, error) {
}
}

git := exec.Command("git", "clone", cloneURL)
git.Dir = path.Join(workDir, pushEvent.Repository.Owner.Login)
log.Println(git.Dir)
err = git.Start()
fetcher.Clone(cloneURL, path.Join(workDir, pushEvent.Repository.Owner.Login))
fetcher.Checkout(pushEvent.AfterCommitID, destPath)

return destPath, err
}

type RepoFetcher interface {
Clone(url, path string) error
Checkout(commitID, path string) error
}

type GitRepoFetcher struct {
}

func (c GitRepoFetcher) Clone(url, path string) error {
git := exec.Command("git", "clone", url)
git.Dir = path
log.Printf("Cloning %s to %s", url, path)

err := git.Start()
if err != nil {
return "", fmt.Errorf("Cannot start git: %t", err)
return fmt.Errorf("Cannot start git: %t", err)
}

err = git.Wait()
return git.Wait()
}

func (c GitRepoFetcher) Checkout(commitID, path string) error {
git := exec.Command("git", "checkout", commitID)
git.Dir = path
log.Printf("Checking out SHA: %s to %s", commitID, path)

git = exec.Command("git", "checkout", pushEvent.AfterCommitID)
git.Dir = destPath
err = git.Start()
err := git.Start()
if err != nil {
return "", fmt.Errorf("Cannot start git checkout: %t", err)
return fmt.Errorf("Cannot start git checkout: %t", err)
}
err = git.Wait()

return destPath, err
err = git.Wait()
return err
}

func deploy(tars []tarEntry, pushEvent sdk.PushEvent, stack *stack.Services, status *sdk.Status, payloadSecret string) error {
Expand Down
90 changes: 90 additions & 0 deletions git-tar/function/ops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,3 +356,93 @@ func mockTempTemplatesDir(files []string, directory string) (string, error) {
}
return templatesDir, nil
}

func Test_Clone(t *testing.T) {
fetcher := FakeFetcher{}
ev := sdk.PushEvent{
Repository: sdk.PushEventRepository{
Owner: sdk.Owner{
Login: "alexellis",
},
Name: "test-repo",
},
}

path, err := clone(fetcher, ev)
if err != nil {
t.Error(err)
t.Fail()
}

wantPathSuffix := ev.Repository.Owner.Login + "-" + ev.Repository.Name
if strings.HasSuffix(path, wantPathSuffix) {
t.Errorf("want suffix: %s, path was: %s", wantPathSuffix, path)
}

if _, err := os.Stat(path); err != nil {
t.Errorf("repo not cloned to: %s", path)
}

defer os.RemoveAll(path)
}

func Test_CloneErrorsWithEmptyOwner(t *testing.T) {
fetcher := FakeFetcher{}
ev := sdk.PushEvent{
Repository: sdk.PushEventRepository{
Owner: sdk.Owner{
Login: "",
},
Name: "test-repo",
},
}

_, err := clone(fetcher, ev)
if err == nil {
t.Error("no login should throw an error")
t.Fail()
return
}

wantErr := "login must be specified"
if err.Error() != wantErr {
t.Errorf("want err: %q, got %q", wantErr, err.Error())
t.Fail()
}
}

func Test_CloneErrorsWithEmptyRepoName(t *testing.T) {
fetcher := FakeFetcher{}
ev := sdk.PushEvent{
Repository: sdk.PushEventRepository{
Owner: sdk.Owner{
Login: "alexellis",
},
Name: "",
},
}

_, err := clone(fetcher, ev)
if err == nil {
t.Error("no repo name should throw an error")
t.Fail()
return
}

wantErr := "repo name must be specified"
if err.Error() != wantErr {
t.Errorf("want err: %q, got %q", wantErr, err.Error())
t.Fail()
}
}

type FakeFetcher struct {
}

func (c FakeFetcher) Clone(url, path string) error {
return os.MkdirAll(path, 0700)
}

func (c FakeFetcher) Checkout(commitID, path string) error {
return os.MkdirAll(path, 0700)
}
9 changes: 6 additions & 3 deletions github-event/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,11 @@ func Handle(req []byte) string {
"Content-Type": "application/json",
}

body, statusCode, err := forward(req, "github-push", headers)
forwardTo := "github-push"
body, statusCode, err := forward(req, forwardTo, headers)

if statusCode == http.StatusOK {
return fmt.Sprintf("Forwarded to function: %d, %s", statusCode, body)
return fmt.Sprintf("[%s]: %d, %s", forwardTo, statusCode, body)
}

if err != nil {
Expand Down Expand Up @@ -318,9 +319,11 @@ func forward(req []byte, function string, headers map[string]string) (string, in
if res.Body != nil {
defer res.Body.Close()
}

body, _ := ioutil.ReadAll(res.Body)

if res.StatusCode != http.StatusOK {
if res.StatusCode != http.StatusOK &&
res.StatusCode != http.StatusAccepted {
err = fmt.Errorf(string(body))
}

Expand Down
6 changes: 5 additions & 1 deletion github-push/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ func Handle(req []byte) string {

sdk.PostAudit(auditEvent)

return fmt.Sprintf("Push - %v, git-tar status: %d\n", pushEvent, statusCode)
return fmt.Sprintf("Push: %s\n, git-tar: %d\n", formatPushEvent(pushEvent), statusCode)
}

func formatPushEvent(pushEvent sdk.PushEvent) string {
return pushEvent.Repository.Owner.Login + "/" + pushEvent.Repository.Name + "@" + pushEvent.Ref + "#" + pushEvent.Ref + " [" + pushEvent.Repository.CloneURL + "]"
}

func postEvent(pushEvent sdk.PushEvent) (int, error) {
Expand Down
8 changes: 4 additions & 4 deletions stack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ functions:
system-github-event:
lang: go
handler: ./github-event
image: functions/github-event:0.9.1
image: functions/github-event:0.9.2
labels:
openfaas-cloud: "1"
role: openfaas-system
Expand All @@ -27,7 +27,7 @@ functions:
github-push:
lang: go
handler: ./github-push
image: functions/github-push:0.7.4
image: functions/github-push:0.7.5
labels:
openfaas-cloud: "1"
role: openfaas-system
Expand All @@ -49,7 +49,7 @@ functions:
git-tar:
lang: dockerfile
handler: ./git-tar
image: functions/of-git-tar:0.17.1
image: functions/of-git-tar:0.17.2
labels:
openfaas-cloud: "1"
role: openfaas-system
Expand Down Expand Up @@ -95,7 +95,7 @@ functions:
garbage-collect:
lang: go
handler: ./garbage-collect
image: functions/garbage-collect:0.4.6
image: functions/garbage-collect:0.4.7
labels:
openfaas-cloud: "1"
role: openfaas-system
Expand Down