Skip to content
Permalink
Browse files

chore(gateway): add tests for default script support

  • Loading branch information
mumoshu committed Feb 8, 2018
1 parent 13b9e5d commit dea1ad2381290b61c2cb006407a6b4817a830021
Showing with 74 additions and 3 deletions.
  1. +28 −3 pkg/webhook/dockerhub_test.go
  2. +46 −0 pkg/webhook/github_test.go
@@ -6,8 +6,8 @@ import (
"github.com/Azure/brigade/pkg/brigade"
)

func TestDoDockerImagePush(t *testing.T) {
proj := &brigade.Project{
func newProject() *brigade.Project {
return &brigade.Project{
ID: "brigade-1234",
Name: "org/proj",
Repo: brigade.Repo{
@@ -23,14 +23,39 @@ func TestDoDockerImagePush(t *testing.T) {
"mysecret": "value",
},
}
}

func TestDoDockerImagePush(t *testing.T) {
proj := newProject()

commit := "e1e10"
store := &testStore{}
hook := NewDockerPushHook(store)

hook := NewDockerPushHook(&testStore{})
if err := hook.doDockerImagePush(proj, commit, []byte(exampleWebhook)); err != nil {
t.Errorf("failed docker image push: %s", err)
}
script := string(store.builds[0].Script)
if script != "" {
t.Errorf("unexpected build script: %s", script)
}
}

func TestDoDockerImagePush_WithDefaultScript(t *testing.T) {
proj := newProject()
proj.DefaultScript = `console.log("hello default script")`

commit := "e1e10"
store := &testStore{}
hook := NewDockerPushHook(store)

if err := hook.doDockerImagePush(proj, commit, []byte(exampleWebhook)); err != nil {
t.Errorf("failed docker image push: %s", err)
}
script := string(store.builds[0].Script)
if script != proj.DefaultScript {
t.Errorf("unexpected build script: %s", script)
}
}

const exampleWebhook = `
@@ -2,6 +2,7 @@ package webhook

import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
@@ -231,6 +232,46 @@ func TestGithubHandler_badevent(t *testing.T) {
}
}

func TestGithubHandler_WithDefaultScript(t *testing.T) {
store := newTestStore()
store.proj.DefaultScript = `console.log("hello default script")'`
s := newTestGithubHandler(store, t)
// Treat the repo to have no file, to eventually trigger the fall-back to the default script
s.getFile = failingFileGet

payloadFile := "testdata/github-push-payload.json"
event := "push"

payload, err := ioutil.ReadFile(payloadFile)
if err != nil {
t.Fatalf("failed to read testdata: %s", err)
}

w := httptest.NewRecorder()
r, err := http.NewRequest("POST", "", bytes.NewReader(payload))
if err != nil {
t.Fatalf("failed to create request: %s", err)
}
r.Header.Add("X-GitHub-Event", event)
r.Header.Add("X-Hub-Signature", SHA1HMAC([]byte("asdf"), payload))

ctx, _ := gin.CreateTestContext(w)
ctx.Request = r

s.Handle(ctx)

if w.Code != http.StatusOK {
t.Fatalf("unexpected error: %d\n%s", w.Code, w.Body.String())
}
if len(store.builds) != 1 {
t.Fatalf("expected exactly one build to be created, but there are %d", len(store.builds))
}
script := string(store.builds[0].Script)
if script != store.proj.DefaultScript {
t.Errorf("unexpected build script: %s", script)
}
}

func TestTruncAt(t *testing.T) {
if "foo" != truncAt("foo", 100) {
t.Fatal("modified string that was fine.")
@@ -244,3 +285,8 @@ func TestTruncAt(t *testing.T) {
t.Errorf("Unexpected truncation of foobar1: %s", got)
}
}

// failingFileGet is a `fileGetter` which is useful for simulating a situation that the project repository to contain no file
func failingFileGet(commit, path string, proj *brigade.Project) ([]byte, error) {
return []byte{}, fmt.Errorf("simulated \"missing file\" error for commit=%s, path=%s, proj.name=%s", commit, path, proj.Name)
}

0 comments on commit dea1ad2

Please sign in to comment.
You can’t perform that action at this time.