func newConveyor(t testing.TB) *Conveyor { db := sqlx.MustConnect("postgres", databaseURL) if err := core.Reset(db); err != nil { t.Fatal(err) } c := core.New(db) c.BuildQueue = core.NewBuildQueue(100) c.Logger = logs.Discard ch := make(chan core.BuildContext) c.BuildQueue.Subscribe(ch) w := worker.New(c, worker.Options{ Builder: builder.BuilderFunc(func(ctx context.Context, w io.Writer, options builder.BuildOptions) (string, error) { io.WriteString(w, "Pulling base image\n") return "remind101/acme-inc:1234", nil }), BuildRequests: ch, }) go w.Start() return &Conveyor{ Conveyor: c, worker: w, } }
func TestServer_Push_Fork(t *testing.T) { var called bool b := func(ctx context.Context, w io.Writer, opts builder.BuildOptions) (string, error) { called = true return "", nil } s := NewServer(New(builder.BuilderFunc(b))) resp := httptest.NewRecorder() req, _ := http.NewRequest("POST", "/", strings.NewReader(`{ "ref": "refs/heads/master", "head_commit": { "id": "abcd" }, "repository": { "full_name": "remind101/acme-inc", "fork": true } }`)) req.Header.Set("X-GitHub-Event", "push") s.ServeHTTP(resp, req) if resp.Code != http.StatusOK { t.Fatal("Expected 200 OK") } if called { t.Fatal("Expected builder to have not been called") } }
func TestBuilder_Build(t *testing.T) { c := new(mockStatsdClient) b := &Builder{ Builder: builder.BuilderFunc(func(ctx context.Context, w io.Writer, options builder.BuildOptions) (string, error) { return "remind101/acme-inc:1234", nil }), statsd: c, } c.On("TimeInMilliseconds", "conveyor.build.time", float64(1000), []string{"repo:remind101/acme-inc"}, float64(1), ).Return(nil) c.On("Event", &statsd.Event{ Title: "Conveyor built remind101/acme-inc:1234", Text: "Built remind101/acme-inc:1234 from remind101/acme-inc@master", Tags: []string{ "repo:remind101/acme-inc", "branch:master", "sha:1234", "image:remind101/acme-inc:1234", }, }).Return(nil) _, err := b.Build(context.Background(), ioutil.Discard, builder.BuildOptions{ Repository: "remind101/acme-inc", Branch: "master", Sha: "1234", }) assert.NoError(t, err) }
func TestServer_Ping(t *testing.T) { b := func(ctx context.Context, w io.Writer, opts builder.BuildOptions) (string, error) { return "", nil } s := NewServer(New(builder.BuilderFunc(b))) resp := httptest.NewRecorder() req, _ := http.NewRequest("POST", "/", nil) req.Header.Set("X-GitHub-Event", "ping") s.ServeHTTP(resp, req) if resp.Code != http.StatusOK { t.Fatal("Expected 200 OK") } }
func TestBuilder(t *testing.T) { c, err := statsd.New("localhost:8125") assert.NoError(t, err) b := datadog.WithStats( builder.BuilderFunc(func(ctx context.Context, w io.Writer, options builder.BuildOptions) (string, error) { return "remind101/acme-inc:1234", nil }), c, ) _, err = b.Build(context.Background(), ioutil.Discard, builder.BuildOptions{ Repository: "remind101/acme-inc", Branch: "master", Sha: "1234", }) assert.NoError(t, err) }
func TestServer_Push(t *testing.T) { called := make(chan struct{}) b := func(ctx context.Context, w io.Writer, opts builder.BuildOptions) (string, error) { close(called) expected := builder.BuildOptions{ Repository: "remind101/acme-inc", Branch: "master", Sha: "abcd", } if got, want := opts, expected; got != want { t.Fatalf("BuildOptions => %v; want %v", got, want) } return "", nil } s := NewServer(New(builder.BuilderFunc(b))) resp := httptest.NewRecorder() req, _ := http.NewRequest("POST", "/", strings.NewReader(`{ "ref": "refs/heads/master", "head_commit": { "id": "abcd" }, "repository": { "full_name": "remind101/acme-inc" } }`)) req.Header.Set("X-GitHub-Event", "push") s.ServeHTTP(resp, req) if resp.Code != http.StatusOK { t.Fatal("Expected 200 OK") } select { case <-called: case <-time.After(time.Second): t.Fatal("Expected builder to have been called") } }
func TestBuilder_Build_Err(t *testing.T) { errBoom := errors.New("container returned non-zero exit") c := new(mockStatsdClient) b := &Builder{ Builder: builder.BuilderFunc(func(ctx context.Context, w io.Writer, options builder.BuildOptions) (string, error) { return "", errBoom }), statsd: c, } c.On("Count", "conveyor.build.error", int64(1), []string{"repo:remind101/acme-inc"}, float64(1), ).Return(nil) _, err := b.Build(context.Background(), ioutil.Discard, builder.BuildOptions{ Repository: "remind101/acme-inc", }) assert.Equal(t, err, errBoom) }