Commit b06db824 authored by Tomasz Maczukin's avatar Tomasz Maczukin

Add tests for new jobs request and artifacts upload paths

parent 458c9fc7
......@@ -16,29 +16,45 @@ func startWorkhorseServerWithLongPolling(authBackend string, pollingDuration tim
return startWorkhorseServerWithConfig(uc)
}
func postBuildsRegister(url string, body io.Reader) (*http.Response, error) {
type requestJobFunction func(url string, body io.Reader) (*http.Response, error)
func requestJobV1(url string, body io.Reader) (*http.Response, error) {
resource := `/ci/api/v1/builds/register.json`
return http.Post(url+resource, `application/json`, body)
}
func TestBuildsLongPullingEndpointDisabled(t *testing.T) {
ws := startWorkhorseServerWithLongPolling("http://localhost/", 0)
func requestJobV4(url string, body io.Reader) (*http.Response, error) {
resource := `/api/v4/jobs/request`
return http.Post(url+resource, `application/json`, body)
}
func testJobsLongPolling(t *testing.T, pollingDuration time.Duration, requestJob requestJobFunction) *http.Response {
ws := startWorkhorseServerWithLongPolling("http://localhost/", pollingDuration)
defer ws.Close()
resp, err := postBuildsRegister(ws.URL, nil)
resp, err := requestJob(ws.URL, nil)
assert.NoError(t, err)
defer resp.Body.Close()
return resp
}
func testJobsLongPollingEndpointDisabled(t *testing.T, requestJob requestJobFunction) {
resp := testJobsLongPolling(t, 0, requestJob)
assert.NotEqual(t, "yes", resp.Header.Get("Gitlab-Ci-Builds-Polling"))
}
func TestBuildsLongPullingEndpoint(t *testing.T) {
ws := startWorkhorseServerWithLongPolling("http://localhost/", time.Minute)
defer ws.Close()
func testJobsLongPollingEndpoint(t *testing.T, requestJob requestJobFunction) {
resp := testJobsLongPolling(t, time.Minute, requestJob)
assert.Equal(t, "yes", resp.Header.Get("Gitlab-Ci-Builds-Polling"))
}
resp, err := postBuildsRegister(ws.URL, nil)
assert.NoError(t, err)
defer resp.Body.Close()
func TestJobsLongPollingEndpointDisabled(t *testing.T) {
testJobsLongPollingEndpointDisabled(t, requestJobV1)
testJobsLongPollingEndpointDisabled(t, requestJobV4)
}
assert.Equal(t, "yes", resp.Header.Get("Gitlab-Ci-Builds-Polling"))
func TestJobsLongPollingEndpoint(t *testing.T) {
testJobsLongPollingEndpoint(t, requestJobV1)
testJobsLongPollingEndpoint(t, requestJobV4)
}
......@@ -17,28 +17,44 @@ import (
"gitlab.com/gitlab-org/gitlab-workhorse/internal/upload"
"github.com/dgrijalva/jwt-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestArtifactsUpload(t *testing.T) {
type uploadArtifactsFunction func(url, contentType string, body io.Reader) (*http.Response, error, string)
func uploadArtifactsV1(url, contentType string, body io.Reader) (*http.Response, error, string) {
resource := `/ci/api/v1/builds/123/artifacts`
resp, err := http.Post(url+resource, contentType, body)
return resp, err, resource
}
func uploadArtifactsV4(url, contentType string, body io.Reader) (*http.Response, error, string) {
resource := `/api/v4/jobs/123/artifacts`
resp, err := http.Post(url+resource, contentType, body)
return resp, err, resource
}
func testArtifactsUpload(t *testing.T, uploadArtifacts uploadArtifactsFunction) {
reqBody, contentType, err := multipartBodyWithFile()
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
ts := uploadTestServer(t, nil)
defer ts.Close()
ws := startWorkhorseServer(ts.URL)
defer ws.Close()
resource := `/ci/api/v1/builds/123/artifacts`
resp, err := http.Post(ws.URL+resource, contentType, reqBody)
if err != nil {
t.Error(err)
}
resp, err, resource := uploadArtifacts(ws.URL, contentType, reqBody)
assert.NoError(t, err)
defer resp.Body.Close()
if resp.StatusCode != 200 {
t.Errorf("GET %q: expected 200, got %d", resource, resp.StatusCode)
}
assert.Equal(t, 200, resp.StatusCode, "GET %q: expected 200, got %d", resource, resp.StatusCode)
}
func TestArtifactsUpload(t *testing.T) {
testArtifactsUpload(t, uploadArtifactsV1)
testArtifactsUpload(t, uploadArtifactsV4)
}
func uploadTestServer(t *testing.T, extraTests func(r *http.Request)) *httptest.Server {
......
// Package require implements the same assertions as the `assert` package but
// stops test execution when a test fails.
//
// Example Usage
//
// The following is a complete example using require in a standard test function:
// import (
// "testing"
// "github.com/stretchr/testify/require"
// )
//
// func TestSomething(t *testing.T) {
//
// var a string = "Hello"
// var b string = "Hello"
//
// require.Equal(t, a, b, "The two words should be the same.")
//
// }
//
// Assertions
//
// The `require` package have same global functions as in the `assert` package,
// but instead of returning a boolean result they call `t.FailNow()`.
//
// Every assertion function also takes an optional string message as the final argument,
// allowing custom error messages to be appended to the message the assertion method outputs.
package require
package require
// Assertions provides assertion methods around the
// TestingT interface.
type Assertions struct {
t TestingT
}
// New makes a new Assertions object for the specified TestingT.
func New(t TestingT) *Assertions {
return &Assertions{
t: t,
}
}
//go:generate go run ../_codegen/main.go -output-package=require -template=require_forward.go.tmpl
This diff is collapsed.
{{.Comment}}
func {{.DocInfo.Name}}(t TestingT, {{.Params}}) {
if !assert.{{.DocInfo.Name}}(t, {{.ForwardedParams}}) {
t.FailNow()
}
}
This diff is collapsed.
{{.CommentWithoutT "a"}}
func (a *Assertions) {{.DocInfo.Name}}({{.Params}}) {
{{.DocInfo.Name}}(a.t, {{.ForwardedParams}})
}
package require
// TestingT is an interface wrapper around *testing.T
type TestingT interface {
Errorf(format string, args ...interface{})
FailNow()
}
//go:generate go run ../_codegen/main.go -output-package=require -template=require.go.tmpl
......@@ -115,6 +115,12 @@
"revision": "18a02ba4a312f95da08ff4cfc0055750ce50ae9e",
"revisionTime": "2016-11-17T07:43:51Z"
},
{
"checksumSHA1": "omdvCNu8sJIc9FbOfObC484M7Dg=",
"path": "github.com/stretchr/testify/require",
"revision": "18a02ba4a312f95da08ff4cfc0055750ce50ae9e",
"revisionTime": "2016-11-17T07:43:51Z"
},
{
"checksumSHA1": "HWuFvDMQ5zp554X4QpVjBUgW5wk=",
"path": "gitlab.com/gitlab-org/gitaly-proto/go",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment