func (a artifactTestCase) Test() { taskID := slugid.Nice() ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "Hello, client.") })) defer ts.Close() s3resp, _ := json.Marshal(queue.S3ArtifactResponse{ PutURL: ts.URL, }) resp := queue.PostArtifactResponse(s3resp) mockedQueue := &client.MockQueue{} for _, path := range a.Artifacts { mockedQueue.On( "CreateArtifact", taskID, "0", path, client.PostS3ArtifactRequest, ).Return(&resp, nil) } a.Case.QueueMock = mockedQueue a.Case.TaskID = taskID a.Case.Test() mockedQueue.AssertExpectations(a.Case.TestStruct) }
// ExpectS3Artifact will setup queue to expect an S3 artifact with given // name to be created for taskID and runID using m and returns // a channel which will receive the artifact. func (m *MockQueue) ExpectS3Artifact(taskID string, runID int, name string) <-chan []byte { // make channel size 100 so we don't have to handle synchronously c := make(chan []byte, 100) var s *httptest.Server s = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { d, err := ioutil.ReadAll(r.Body) if err != nil { close(c) w.WriteHeader(500) return } if r.Header.Get("Content-Encoding") == "gzip" { reader, err := gzip.NewReader(bytes.NewReader(d)) if err != nil { close(c) w.WriteHeader(500) return } d, err = ioutil.ReadAll(reader) if err != nil { close(c) w.WriteHeader(500) return } } w.WriteHeader(200) c <- d go s.Close() // Close when all requests are done (don't block the request) })) data, _ := json.Marshal(queue.S3ArtifactResponse{ StorageType: "s3", PutURL: s.URL, ContentType: "application/octet", Expires: tcclient.Time(time.Now().Add(30 * time.Minute)), }) result := queue.PostArtifactResponse(data) m.On( "CreateArtifact", taskID, fmt.Sprintf("%d", runID), name, PostS3ArtifactRequest, ).Return(&result, nil) return c }
func setupArtifactTest(name string, artifactResp queue.PostArtifactRequest) (*TaskContext, *client.MockQueue) { resp := queue.PostArtifactResponse(artifactResp) taskID := slugid.Nice() context := &TaskContext{ TaskInfo: TaskInfo{ TaskID: taskID, RunID: 0, }, } controller := &TaskContextController{context} mockedQueue := &client.MockQueue{} mockedQueue.On( "CreateArtifact", taskID, "0", name, client.PostAnyArtifactRequest, ).Return(&resp, nil) controller.SetQueueClient(mockedQueue) return context, mockedQueue }
// ExpectRedirectArtifact will setup m to expect a redirect artifact with given // name for taskID and runID to be created. This function returns a channel for // the url of the redirect artifact. func (m *MockQueue) ExpectRedirectArtifact(taskID string, runID int, name string) <-chan string { // make channel size 100 so we don't have to handle synchronously c := make(chan string, 100) data, _ := json.Marshal(queue.RedirectArtifactResponse{ StorageType: "reference", }) result := queue.PostArtifactResponse(data) m.On( "CreateArtifact", taskID, fmt.Sprintf("%d", runID), name, PostAnyArtifactRequest, ).Run(func(args mock.Arguments) { d := args.Get(3).(*queue.PostArtifactRequest) var r queue.RedirectArtifactRequest if json.Unmarshal(*d, &r) != nil { close(c) return } c <- r.URL }).Return(&result, nil) return c }