Skip to content

Commit

Permalink
ref(tests): remove generator from functional tests (#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamreese committed Mar 16, 2018
1 parent 5636c6e commit 4ac40fa
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 126 deletions.
12 changes: 4 additions & 8 deletions Makefile
Expand Up @@ -91,17 +91,13 @@ test-unit: vendor
#
# This will clone the github.com/deis/empty-testbed repo and run the brigade.js
# file found there.
.PHONY: test-functional
test-functional: vendor test-functional-prepare
test-functional:
go test --tags integration ./tests

# Test Repo is https://github.com/deis/empty-testbed
TEST_REPO_COMMIT = 589e15029e1e44dee48de4800daf1f78e64287c0
KUBECONFIG ?= ${HOME}/.kube/config
.PHONY: test-functional-prepare
test-functional-prepare:
go run ./tests/cmd/generate.go -kubeconfig $(KUBECONFIG) $(TEST_REPO_COMMIT)
.PHONY: test-functional
test-functional: vendor
test-functional:
go test --tags integration ./tests -kubeconfig $(KUBECONFIG) $(TEST_REPO_COMMIT)

# JS test is local only
.PHONY: test-js
Expand Down
101 changes: 0 additions & 101 deletions tests/cmd/generate.go

This file was deleted.

97 changes: 80 additions & 17 deletions tests/tests_test.go
Expand Up @@ -3,35 +3,98 @@
package tests

import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"testing"

"github.com/google/go-github/github"
"k8s.io/api/core/v1"

"github.com/Azure/brigade/pkg/storage/kube"
"github.com/Azure/brigade/pkg/webhook"
)

func TestFunctional(t *testing.T) {
githubPushFile, err := os.Open("testdata/test-repo-generated.json")
var (
kubeconfig string
namespace string
)

func init() {
flag.StringVar(&kubeconfig, "kubeconfig", "", "absolute path to the kubeconfig file")
flag.StringVar(&namespace, "namespace", os.Getenv("BRIGADE_NAMESPACE"), "kubernetes namespace")
}

func generate() (payload []byte, hmac string) {
if flag.NArg() < 1 {
fmt.Fprintln(os.Stderr, "required arg: Git SHA")
os.Exit(1)
}
commit := flag.Arg(0)

eventType := "push"

data, err := ioutil.ReadFile("./testdata/test-repo-push.json")
if err != nil {
t.Fatal(err)
panic(err)
}
defer githubPushFile.Close()
hubSignature, err := ioutil.ReadFile("testdata/test-repo-generated.hash")

event, err := github.ParseWebHook(eventType, data)
if err != nil {
t.Fatal(err)
}
requests := []*http.Request{
{
Method: "POST",
URL: &url.URL{Scheme: "http", Host: "localhost:7744", Path: "/events/github"},
Body: githubPushFile,
Header: http.Header{
"X-Github-Event": []string{"push"},
"X-Hub-Signature": []string{string(hubSignature)},
},
},
panic(err)
}

var repo string

switch event := event.(type) {
case *github.PushEvent:
event.HeadCommit.ID = github.String(commit)
repo = event.Repo.GetFullName()
case *github.PullRequestEvent:
event.PullRequest.Head.SHA = github.String(commit)
repo = event.Repo.GetFullName()
}

out, err := json.MarshalIndent(event, "", " ")
if err != nil {
panic(err)
}

clientset, err := kube.GetClient("", kubeconfig)
if err != nil {
panic(err)
}

if namespace == "" {
namespace = v1.NamespaceDefault
}

proj, err := kube.New(clientset, namespace).GetProject(repo)
if err != nil {
panic(err)
}
hmac = webhook.SHA1HMAC([]byte(proj.SharedSecret), out)
return out, hmac
}

func TestFunctional(t *testing.T) {
payload, hmac := generate()

requests := []*http.Request{{
Method: "POST",
URL: &url.URL{Scheme: "http", Host: "localhost:7744", Path: "/events/github"},
Body: ioutil.NopCloser(bytes.NewReader(payload)),
Header: http.Header{
"X-Github-Event": []string{"push"},
"X-Hub-Signature": []string{hmac},
},
}}

for _, request := range requests {
resp, err := http.DefaultClient.Do(request)
if err != nil {
Expand Down

0 comments on commit 4ac40fa

Please sign in to comment.