Commit c17736b6 authored by Alessio Caiazza's avatar Alessio Caiazza

Merge branch 'jv-assert-require-new' into 'master'

Stop using require.New and assert.New

See merge request gitlab-org/gitlab-workhorse!567
parents 652d2c9b ec75b632
......@@ -54,33 +54,28 @@ func assertObjectStoreDeletedAsync(t *testing.T, expectedDeletes int, osStub *te
}
func TestSaveFileWrongSize(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
tmpFolder, err := ioutil.TempDir("", "workhorse-test-tmp")
require.NoError(err)
require.NoError(t, err)
defer os.RemoveAll(tmpFolder)
opts := &filestore.SaveFileOpts{LocalTempPath: tmpFolder, TempFilePrefix: "test-file"}
fh, err := filestore.SaveFileFromReader(ctx, strings.NewReader(test.ObjectContent), test.ObjectSize+1, opts)
assert.Error(err)
assert.Error(t, err)
_, isSizeError := err.(filestore.SizeError)
assert.True(isSizeError, "Should fail with SizeError")
assert.Nil(fh)
assert.True(t, isSizeError, "Should fail with SizeError")
assert.Nil(t, fh)
}
func TestSaveFromDiskNotExistingFile(t *testing.T) {
assert := assert.New(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
fh, err := filestore.SaveFileFromDisk(ctx, "/I/do/not/exist", &filestore.SaveFileOpts{})
assert.Error(err, "SaveFileFromDisk should fail")
assert.True(os.IsNotExist(err), "Provided file should not exists")
assert.Nil(fh, "On error FileHandler should be nil")
assert.Error(t, err, "SaveFileFromDisk should fail")
assert.True(t, os.IsNotExist(err), "Provided file should not exists")
assert.Nil(t, fh, "On error FileHandler should be nil")
}
func TestSaveFileWrongETag(t *testing.T) {
......@@ -94,8 +89,6 @@ func TestSaveFileWrongETag(t *testing.T) {
for _, spec := range tests {
t.Run(spec.name, func(t *testing.T) {
assert := assert.New(t)
osStub, ts := test.StartObjectStoreWithCustomMD5(map[string]string{test.ObjectPath: "brokenMD5"})
defer ts.Close()
......@@ -118,30 +111,27 @@ func TestSaveFileWrongETag(t *testing.T) {
}
ctx, cancel := context.WithCancel(context.Background())
fh, err := filestore.SaveFileFromReader(ctx, strings.NewReader(test.ObjectContent), test.ObjectSize, opts)
assert.Nil(fh)
assert.Error(err)
assert.Equal(1, osStub.PutsCnt(), "File not uploaded")
assert.Nil(t, fh)
assert.Error(t, err)
assert.Equal(t, 1, osStub.PutsCnt(), "File not uploaded")
cancel() // this will trigger an async cleanup
assertObjectStoreDeletedAsync(t, 1, osStub)
assert.False(spec.multipart && osStub.IsMultipartUpload(test.ObjectPath), "there must be no multipart upload in progress now")
assert.False(t, spec.multipart && osStub.IsMultipartUpload(test.ObjectPath), "there must be no multipart upload in progress now")
})
}
}
func TestSaveFileFromDiskToLocalPath(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
f, err := ioutil.TempFile("", "workhorse-test")
require.NoError(err)
require.NoError(t, err)
defer os.Remove(f.Name())
_, err = fmt.Fprint(f, test.ObjectContent)
require.NoError(err)
require.NoError(t, err)
tmpFolder, err := ioutil.TempDir("", "workhorse-test-tmp")
require.NoError(err)
require.NoError(t, err)
defer os.RemoveAll(tmpFolder)
ctx, cancel := context.WithCancel(context.Background())
......@@ -149,12 +139,12 @@ func TestSaveFileFromDiskToLocalPath(t *testing.T) {
opts := &filestore.SaveFileOpts{LocalTempPath: tmpFolder}
fh, err := filestore.SaveFileFromDisk(ctx, f.Name(), opts)
assert.NoError(err)
require.NotNil(fh)
assert.NoError(t, err)
require.NotNil(t, fh)
assert.NotEmpty(fh.LocalPath, "File not persisted on disk")
assert.NotEmpty(t, fh.LocalPath, "File not persisted on disk")
_, err = os.Stat(fh.LocalPath)
assert.NoError(err)
assert.NoError(t, err)
}
func TestSaveFile(t *testing.T) {
......@@ -185,7 +175,6 @@ func TestSaveFile(t *testing.T) {
for _, spec := range tests {
t.Run(spec.name, func(t *testing.T) {
assert := assert.New(t)
logHook := testhelper.SetupLogger()
var opts filestore.SaveFileOpts
......@@ -240,24 +229,24 @@ func TestSaveFile(t *testing.T) {
defer cancel()
fh, err := filestore.SaveFileFromReader(ctx, strings.NewReader(test.ObjectContent), test.ObjectSize, &opts)
assert.NoError(err)
assert.NoError(t, err)
require.NotNil(t, fh)
require.Equal(t, opts.RemoteID, fh.RemoteID)
require.Equal(t, opts.RemoteURL, fh.RemoteURL)
if spec.local {
assert.NotEmpty(fh.LocalPath, "File not persisted on disk")
assert.NotEmpty(t, fh.LocalPath, "File not persisted on disk")
_, err := os.Stat(fh.LocalPath)
assert.NoError(err)
assert.NoError(t, err)
dir := path.Dir(fh.LocalPath)
require.Equal(t, opts.LocalTempPath, dir)
filename := path.Base(fh.LocalPath)
beginsWithPrefix := strings.HasPrefix(filename, opts.TempFilePrefix)
assert.True(beginsWithPrefix, fmt.Sprintf("LocalPath filename %q do not begin with TempFilePrefix %q", filename, opts.TempFilePrefix))
assert.True(t, beginsWithPrefix, fmt.Sprintf("LocalPath filename %q do not begin with TempFilePrefix %q", filename, opts.TempFilePrefix))
} else {
assert.Empty(fh.LocalPath, "LocalPath must be empty for non local uploads")
assert.Empty(t, fh.LocalPath, "LocalPath must be empty for non local uploads")
}
require.Equal(t, test.ObjectSize, fh.Size)
......@@ -388,8 +377,6 @@ func TestSaveFileWithUnknownGoCloudScheme(t *testing.T) {
}
func TestSaveMultipartInBodyFailure(t *testing.T) {
assert := assert.New(t)
osStub, ts := test.StartObjectStore()
defer ts.Close()
......@@ -412,9 +399,9 @@ func TestSaveMultipartInBodyFailure(t *testing.T) {
defer cancel()
fh, err := filestore.SaveFileFromReader(ctx, strings.NewReader(test.ObjectContent), test.ObjectSize, &opts)
assert.Nil(fh)
assert.Nil(t, fh)
require.Error(t, err)
assert.EqualError(err, test.MultipartUploadInternalError().Error())
assert.EqualError(t, err, test.MultipartUploadInternalError().Error())
}
func checkFileHandlerWithFields(t *testing.T, fh *filestore.FileHandler, fields map[string]string, prefix string, remote bool) {
......
......@@ -61,18 +61,15 @@ func TestSaveFileOptsLocalAndRemote(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert := assert.New(t)
opts := filestore.SaveFileOpts{
LocalTempPath: test.localTempPath,
PresignedPut: test.presignedPut,
PartSize: test.partSize,
}
assert.Equal(test.isLocal, opts.IsLocal(), "IsLocal() mismatch")
assert.Equal(test.isRemote, opts.IsRemote(), "IsRemote() mismatch")
assert.Equal(test.isMultipart, opts.IsMultipart(), "IsMultipart() mismatch")
assert.Equal(t, test.isLocal, opts.IsLocal(), "IsLocal() mismatch")
assert.Equal(t, test.isRemote, opts.IsRemote(), "IsRemote() mismatch")
assert.Equal(t, test.isMultipart, opts.IsMultipart(), "IsMultipart() mismatch")
})
}
}
......@@ -114,8 +111,6 @@ func TestGetOpts(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert := assert.New(t)
apiResponse := &api.Response{
TempPath: "/tmp",
RemoteObject: api.RemoteObject{
......@@ -132,42 +127,40 @@ func TestGetOpts(t *testing.T) {
deadline := time.Now().Add(time.Duration(apiResponse.RemoteObject.Timeout) * time.Second)
opts := filestore.GetOpts(apiResponse)
assert.Equal(apiResponse.TempPath, opts.LocalTempPath)
assert.WithinDuration(deadline, opts.Deadline, time.Second)
assert.Equal(apiResponse.RemoteObject.ID, opts.RemoteID)
assert.Equal(apiResponse.RemoteObject.GetURL, opts.RemoteURL)
assert.Equal(apiResponse.RemoteObject.StoreURL, opts.PresignedPut)
assert.Equal(apiResponse.RemoteObject.DeleteURL, opts.PresignedDelete)
assert.Equal(t, apiResponse.TempPath, opts.LocalTempPath)
assert.WithinDuration(t, deadline, opts.Deadline, time.Second)
assert.Equal(t, apiResponse.RemoteObject.ID, opts.RemoteID)
assert.Equal(t, apiResponse.RemoteObject.GetURL, opts.RemoteURL)
assert.Equal(t, apiResponse.RemoteObject.StoreURL, opts.PresignedPut)
assert.Equal(t, apiResponse.RemoteObject.DeleteURL, opts.PresignedDelete)
if test.customPutHeaders {
assert.Equal(opts.PutHeaders, apiResponse.RemoteObject.PutHeaders)
assert.Equal(t, opts.PutHeaders, apiResponse.RemoteObject.PutHeaders)
} else {
assert.Equal(opts.PutHeaders, map[string]string{"Content-Type": "application/octet-stream"})
assert.Equal(t, opts.PutHeaders, map[string]string{"Content-Type": "application/octet-stream"})
}
if test.multipart == nil {
assert.False(opts.IsMultipart())
assert.Empty(opts.PresignedCompleteMultipart)
assert.Empty(opts.PresignedAbortMultipart)
assert.Zero(opts.PartSize)
assert.Empty(opts.PresignedParts)
assert.False(t, opts.IsMultipart())
assert.Empty(t, opts.PresignedCompleteMultipart)
assert.Empty(t, opts.PresignedAbortMultipart)
assert.Zero(t, opts.PartSize)
assert.Empty(t, opts.PresignedParts)
} else {
assert.True(opts.IsMultipart())
assert.Equal(test.multipart.CompleteURL, opts.PresignedCompleteMultipart)
assert.Equal(test.multipart.AbortURL, opts.PresignedAbortMultipart)
assert.Equal(test.multipart.PartSize, opts.PartSize)
assert.Equal(test.multipart.PartURLs, opts.PresignedParts)
assert.True(t, opts.IsMultipart())
assert.Equal(t, test.multipart.CompleteURL, opts.PresignedCompleteMultipart)
assert.Equal(t, test.multipart.AbortURL, opts.PresignedAbortMultipart)
assert.Equal(t, test.multipart.PartSize, opts.PartSize)
assert.Equal(t, test.multipart.PartURLs, opts.PresignedParts)
}
})
}
}
func TestGetOptsDefaultTimeout(t *testing.T) {
assert := assert.New(t)
deadline := time.Now().Add(filestore.DefaultObjectStoreTimeout)
opts := filestore.GetOpts(&api.Response{})
assert.WithinDuration(deadline, opts.Deadline, time.Minute)
assert.WithinDuration(t, deadline, opts.Deadline, time.Minute)
}
func TestUseWorkhorseClientEnabled(t *testing.T) {
......@@ -251,7 +244,6 @@ func TestUseWorkhorseClientEnabled(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
apiResponse := &api.Response{
TempPath: "/tmp",
RemoteObject: api.RemoteObject{
......
......@@ -21,8 +21,6 @@ const testTimeout = 10 * time.Second
type osFactory func() (*test.ObjectstoreStub, *httptest.Server)
func testObjectUploadNoErrors(t *testing.T, startObjectStore osFactory, useDeleteURL bool, contentType string) {
assert := assert.New(t)
osStub, ts := startObjectStore()
defer ts.Close()
......@@ -43,21 +41,21 @@ func testObjectUploadNoErrors(t *testing.T, startObjectStore osFactory, useDelet
// copy data
n, err := io.Copy(object, strings.NewReader(test.ObjectContent))
assert.NoError(err)
assert.Equal(test.ObjectSize, n, "Uploaded file mismatch")
assert.NoError(t, err)
assert.Equal(t, test.ObjectSize, n, "Uploaded file mismatch")
// close HTTP stream
err = object.Close()
assert.NoError(err)
assert.NoError(t, err)
assert.Equal(contentType, osStub.GetHeader(test.ObjectPath, "Content-Type"))
assert.Equal(t, contentType, osStub.GetHeader(test.ObjectPath, "Content-Type"))
// Checking MD5 extraction
assert.Equal(osStub.GetObjectMD5(test.ObjectPath), object.ETag())
assert.Equal(t, osStub.GetObjectMD5(test.ObjectPath), object.ETag())
// Checking cleanup
cancel()
assert.Equal(1, osStub.PutsCnt(), "Object hasn't been uploaded")
assert.Equal(t, 1, osStub.PutsCnt(), "Object hasn't been uploaded")
var expectedDeleteCnt int
if useDeleteURL {
......@@ -72,9 +70,9 @@ func testObjectUploadNoErrors(t *testing.T, startObjectStore osFactory, useDelet
}
if useDeleteURL {
assert.Equal(1, osStub.DeletesCnt(), "Object hasn't been deleted")
assert.Equal(t, 1, osStub.DeletesCnt(), "Object hasn't been deleted")
} else {
assert.Equal(0, osStub.DeletesCnt(), "Object has been deleted")
assert.Equal(t, 0, osStub.DeletesCnt(), "Object has been deleted")
}
}
......@@ -102,9 +100,6 @@ func TestObjectUpload(t *testing.T) {
}
func TestObjectUpload404(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
ts := httptest.NewServer(http.NotFoundHandler())
defer ts.Close()
......@@ -114,15 +109,15 @@ func TestObjectUpload404(t *testing.T) {
deadline := time.Now().Add(testTimeout)
objectURL := ts.URL + test.ObjectPath
object, err := objectstore.NewObject(ctx, objectURL, "", map[string]string{}, deadline, test.ObjectSize)
require.NoError(err)
require.NoError(t, err)
_, err = io.Copy(object, strings.NewReader(test.ObjectContent))
assert.NoError(err)
assert.NoError(t, err)
err = object.Close()
assert.Error(err)
assert.Error(t, err)
_, isStatusCodeError := err.(objectstore.StatusCodeError)
require.True(isStatusCodeError, "Should fail with StatusCodeError")
require.Contains(err.Error(), "404")
require.True(t, isStatusCodeError, "Should fail with StatusCodeError")
require.Contains(t, err.Error(), "404")
}
type endlessReader struct{}
......
......@@ -26,67 +26,56 @@ func doRequest(method, url string, body io.Reader) error {
}
func TestObjectStoreStub(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
stub, ts := StartObjectStore()
defer ts.Close()
assert.Equal(0, stub.PutsCnt())
assert.Equal(0, stub.DeletesCnt())
assert.Equal(t, 0, stub.PutsCnt())
assert.Equal(t, 0, stub.DeletesCnt())
objectURL := ts.URL + ObjectPath
require.NoError(doRequest(http.MethodPut, objectURL, strings.NewReader(ObjectContent)))
require.NoError(t, doRequest(http.MethodPut, objectURL, strings.NewReader(ObjectContent)))
assert.Equal(1, stub.PutsCnt())
assert.Equal(0, stub.DeletesCnt())
assert.Equal(ObjectMD5, stub.GetObjectMD5(ObjectPath))
assert.Equal(t, 1, stub.PutsCnt())
assert.Equal(t, 0, stub.DeletesCnt())
assert.Equal(t, ObjectMD5, stub.GetObjectMD5(ObjectPath))
require.NoError(doRequest(http.MethodDelete, objectURL, nil))
require.NoError(t, doRequest(http.MethodDelete, objectURL, nil))
assert.Equal(1, stub.PutsCnt())
assert.Equal(1, stub.DeletesCnt())
assert.Equal(t, 1, stub.PutsCnt())
assert.Equal(t, 1, stub.DeletesCnt())
}
func TestObjectStoreStubDelete404(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
stub, ts := StartObjectStore()
defer ts.Close()
objectURL := ts.URL + ObjectPath
req, err := http.NewRequest(http.MethodDelete, objectURL, nil)
require.NoError(err)
require.NoError(t, err)
resp, err := http.DefaultClient.Do(req)
require.NoError(err)
require.NoError(t, err)
defer resp.Body.Close()
assert.Equal(404, resp.StatusCode)
assert.Equal(t, 404, resp.StatusCode)
assert.Equal(0, stub.DeletesCnt())
assert.Equal(t, 0, stub.DeletesCnt())
}
func TestObjectStoreInitiateMultipartUpload(t *testing.T) {
require := require.New(t)
stub, ts := StartObjectStore()
defer ts.Close()
path := "/my-multipart"
err := stub.InitiateMultipartUpload(path)
require.NoError(err)
require.NoError(t, err)
err = stub.InitiateMultipartUpload(path)
require.Error(err, "second attempt to open the same MultipartUpload")
require.Error(t, err, "second attempt to open the same MultipartUpload")
}
func TestObjectStoreCompleteMultipartUpload(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
stub, ts := StartObjectStore()
defer ts.Close()
......@@ -109,9 +98,9 @@ func TestObjectStoreCompleteMultipartUpload(t *testing.T) {
stub.InitiateMultipartUpload(ObjectPath)
require.True(stub.IsMultipartUpload(ObjectPath))
assert.Equal(0, stub.PutsCnt())
assert.Equal(0, stub.DeletesCnt())
require.True(t, stub.IsMultipartUpload(ObjectPath))
assert.Equal(t, 0, stub.PutsCnt())
assert.Equal(t, 0, stub.DeletesCnt())
// Workhorse knows nothing about S3 MultipartUpload, it receives some URLs
// from GitLab-rails and PUTs chunk of data to each of them.
......@@ -125,13 +114,13 @@ func TestObjectStoreCompleteMultipartUpload(t *testing.T) {
for i, partPutURL := range partPutURLs {
part := parts[i]
require.NoError(doRequest(http.MethodPut, partPutURL, strings.NewReader(part.content)))
require.NoError(t, doRequest(http.MethodPut, partPutURL, strings.NewReader(part.content)))
assert.Equal(i+1, stub.PutsCnt())
assert.Equal(0, stub.DeletesCnt())
assert.Equal(part.contentMD5, stub.multipart[ObjectPath][part.number], "Part %d was not uploaded into ObjectStorage", part.number)
assert.Empty(stub.GetObjectMD5(ObjectPath), "Part %d was mistakenly uploaded as a single object", part.number)
assert.True(stub.IsMultipartUpload(ObjectPath), "MultipartUpload completed or aborted")
assert.Equal(t, i+1, stub.PutsCnt())
assert.Equal(t, 0, stub.DeletesCnt())
assert.Equal(t, part.contentMD5, stub.multipart[ObjectPath][part.number], "Part %d was not uploaded into ObjectStorage", part.number)
assert.Empty(t, stub.GetObjectMD5(ObjectPath), "Part %d was mistakenly uploaded as a single object", part.number)
assert.True(t, stub.IsMultipartUpload(ObjectPath), "MultipartUpload completed or aborted")
}
completeBody := fmt.Sprintf(`<CompleteMultipartUpload>
......@@ -144,39 +133,36 @@ func TestObjectStoreCompleteMultipartUpload(t *testing.T) {
<ETag>%s</ETag>
</Part>
</CompleteMultipartUpload>`, parts[0].contentMD5, parts[1].contentMD5)
require.NoError(doRequest(http.MethodPost, completePostURL, strings.NewReader(completeBody)))
require.NoError(t, doRequest(http.MethodPost, completePostURL, strings.NewReader(completeBody)))
assert.Equal(len(parts), stub.PutsCnt())
assert.Equal(0, stub.DeletesCnt())
assert.False(stub.IsMultipartUpload(ObjectPath), "MultipartUpload is still in progress")
assert.Equal(t, len(parts), stub.PutsCnt())
assert.Equal(t, 0, stub.DeletesCnt())
assert.False(t, stub.IsMultipartUpload(ObjectPath), "MultipartUpload is still in progress")
}
func TestObjectStoreAbortMultipartUpload(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
stub, ts := StartObjectStore()
defer ts.Close()
stub.InitiateMultipartUpload(ObjectPath)
require.True(stub.IsMultipartUpload(ObjectPath))
assert.Equal(0, stub.PutsCnt())
assert.Equal(0, stub.DeletesCnt())
require.True(t, stub.IsMultipartUpload(ObjectPath))
assert.Equal(t, 0, stub.PutsCnt())
assert.Equal(t, 0, stub.DeletesCnt())
objectURL := ts.URL + ObjectPath
require.NoError(doRequest(http.MethodPut, fmt.Sprintf("%s?partNumber=%d", objectURL, 1), strings.NewReader(ObjectContent)))
require.NoError(t, doRequest(http.MethodPut, fmt.Sprintf("%s?partNumber=%d", objectURL, 1), strings.NewReader(ObjectContent)))
assert.Equal(1, stub.PutsCnt())
assert.Equal(0, stub.DeletesCnt())
assert.Equal(ObjectMD5, stub.multipart[ObjectPath][1], "Part was not uploaded into ObjectStorage")
assert.Empty(stub.GetObjectMD5(ObjectPath), "Part was mistakenly uploaded as a single object")
assert.True(stub.IsMultipartUpload(ObjectPath), "MultipartUpload completed or aborted")
assert.Equal(t, 1, stub.PutsCnt())
assert.Equal(t, 0, stub.DeletesCnt())
assert.Equal(t, ObjectMD5, stub.multipart[ObjectPath][1], "Part was not uploaded into ObjectStorage")
assert.Empty(t, stub.GetObjectMD5(ObjectPath), "Part was mistakenly uploaded as a single object")
assert.True(t, stub.IsMultipartUpload(ObjectPath), "MultipartUpload completed or aborted")
require.NoError(doRequest(http.MethodDelete, objectURL, nil))
require.NoError(t, doRequest(http.MethodDelete, objectURL, nil))
assert.Equal(1, stub.PutsCnt())
assert.Equal(1, stub.DeletesCnt())
assert.Empty(stub.GetObjectMD5(ObjectPath), "MultiUpload has been completed")
assert.False(stub.IsMultipartUpload(ObjectPath), "MultiUpload is still in progress")
assert.Equal(t, 1, stub.PutsCnt())
assert.Equal(t, 1, stub.DeletesCnt())
assert.Empty(t, stub.GetObjectMD5(ObjectPath), "MultiUpload has been completed")
assert.False(t, stub.IsMultipartUpload(ObjectPath), "MultiUpload is still in progress")
}
......@@ -101,26 +101,24 @@ func testUpload(auth PreAuthorizer, preparer Preparer, proxy http.Handler, body
}
func echoProxy(t *testing.T, expectedBodyLength int) http.Handler {
require := require.New(t)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
require.NoError(err)
require.NoError(t, err)
require.Equal("application/x-www-form-urlencoded", r.Header.Get("Content-Type"), "Wrong Content-Type header")
require.Equal(t, "application/x-www-form-urlencoded", r.Header.Get("Content-Type"), "Wrong Content-Type header")
require.Contains(r.PostForm, "file.md5")
require.Contains(r.PostForm, "file.sha1")
require.Contains(r.PostForm, "file.sha256")
require.Contains(r.PostForm, "file.sha512")
require.Contains(t, r.PostForm, "file.md5")
require.Contains(t, r.PostForm, "file.sha1")
require.Contains(t, r.PostForm, "file.sha256")
require.Contains(t, r.PostForm, "file.sha512")
require.Contains(r.PostForm, "file.path")
require.Contains(r.PostForm, "file.size")
require.Contains(r.PostForm, "file.gitlab-workhorse-upload")
require.Equal(strconv.Itoa(expectedBodyLength), r.PostFormValue("file.size"))
require.Contains(t, r.PostForm, "file.path")
require.Contains(t, r.PostForm, "file.size")
require.Contains(t, r.PostForm, "file.gitlab-workhorse-upload")
require.Equal(t, strconv.Itoa(expectedBodyLength), r.PostFormValue("file.size"))
token, err := jwt.ParseWithClaims(r.Header.Get(RewrittenFieldsHeader), &MultipartClaims{}, testhelper.ParseJWT)
require.NoError(err, "Wrong JWT header")
require.NoError(t, err, "Wrong JWT header")
rewrittenFields := token.Claims.(*MultipartClaims).RewrittenFields
if len(rewrittenFields) != 1 || len(rewrittenFields["file"]) == 0 {
......@@ -128,22 +126,22 @@ func echoProxy(t *testing.T, expectedBodyLength int) http.Handler {
}
token, jwtErr := jwt.ParseWithClaims(r.PostFormValue("file.gitlab-workhorse-upload"), &testhelper.UploadClaims{}, testhelper.ParseJWT)
require.NoError(jwtErr, "Wrong signed upload fields")
require.NoError(t, jwtErr, "Wrong signed upload fields")
uploadFields := token.Claims.(*testhelper.UploadClaims).Upload
require.Contains(uploadFields, "name")
require.Contains(uploadFields, "path")
require.Contains(uploadFields, "remote_url")
require.Contains(uploadFields, "remote_id")
require.Contains(uploadFields, "size")
require.Contains(uploadFields, "md5")
require.Contains(uploadFields, "sha1")
require.Contains(uploadFields, "sha256")
require.Contains(uploadFields, "sha512")
require.Contains(t, uploadFields, "name")
require.Contains(t, uploadFields, "path")
require.Contains(t, uploadFields, "remote_url")
require.Contains(t, uploadFields, "remote_id")
require.Contains(t, uploadFields, "size")
require.Contains(t, uploadFields, "md5")
require.Contains(t, uploadFields, "sha1")
require.Contains(t, uploadFields, "sha256")
require.Contains(t, uploadFields, "sha512")
path := r.PostFormValue("file.path")
uploaded, err := os.Open(path)
require.NoError(err, "File not uploaded")
require.NoError(t, err, "File not uploaded")
//sending back the file for testing purpose
io.Copy(w, uploaded)
......
......@@ -59,30 +59,29 @@ func validateMetadata(r io.Reader) error {
func TestGenerateZipMetadataFromFile(t *testing.T) {
var metaBuffer bytes.Buffer
require := require.New(t)
f, err := ioutil.TempFile("", "workhorse-metadata.zip-")
if f != nil {
defer os.Remove(f.Name())
}
require.NoError(err)
require.NoError(t, err)
defer f.Close()
err = generateTestArchive(f)
require.NoError(err)
require.NoError(t, err)
f.Close()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
archive, err := zipartifacts.OpenArchive(ctx, f.Name())
require.NoError(err, "zipartifacts: OpenArchive failed")
require.NoError(t, err, "zipartifacts: OpenArchive failed")
err = zipartifacts.GenerateZipMetadata(&metaBuffer, archive)
require.NoError(err, "zipartifacts: GenerateZipMetadata failed")
require.NoError(t, err, "zipartifacts: GenerateZipMetadata failed")
err = validateMetadata(&metaBuffer)
require.NoError(err)
require.NoError(t, err)
}
func TestErrNotAZip(t *testing.T) {
......
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