func TestCreateBucket(t *testing.T) { mockdb := &database.MockDatabase{} // Used to verify creation timestamp. mockClock := common.NewFrozenClock() // var bucket *model.Bucket var err error // Bad request _, err = CreateBucket(mockdb, mockClock, "", "owner") assert.Error(t, err) _, err = CreateBucket(mockdb, mockClock, "id", "") assert.Error(t, err) // DB error mockdb.On("GetBucket", "id").Return(nil, database.WrapInternalDatabaseError(fmt.Errorf("Internal Error"))).Once() _, err = CreateBucket(mockdb, mockClock, "id", "owner") assert.Error(t, err) // Entity exists mockdb.On("GetBucket", "id").Return(&model.Bucket{}, nil).Once() _, err = CreateBucket(mockdb, mockClock, "id", "owner") assert.Error(t, err) // DB error while creating bucket mockdb.On("GetBucket", "id").Return(nil, database.NewEntityNotFoundError("ENF")).Once() mockdb.On("InsertBucket", mock.AnythingOfType("*model.Bucket")).Return(database.WrapInternalDatabaseError(fmt.Errorf("INT"))).Once() _, err = CreateBucket(mockdb, mockClock, "id", "owner") assert.Error(t, err) // Successfully created bucket mockdb.On("GetBucket", "id").Return(nil, database.NewEntityNotFoundError("ENF")).Once() mockdb.On("InsertBucket", mock.AnythingOfType("*model.Bucket")).Return(nil).Once() bucket, err := CreateBucket(mockdb, mockClock, "id", "owner") assert.NoError(t, err) assert.NotNil(t, bucket) mockdb.AssertExpectations(t) }
func TestCreateArtifact(t *testing.T) { mockdb := &database.MockDatabase{} var artifact *model.Artifact var err error // Create artifact with no name artifact, err = CreateArtifact(CreateArtifactReq{}, &model.Bucket{ State: model.CLOSED, }, mockdb) assert.Error(t, err) assert.Nil(t, artifact) // Create artifact in closed bucket artifact, err = CreateArtifact(CreateArtifactReq{ Name: "aName", }, &model.Bucket{ State: model.CLOSED, }, mockdb) assert.Error(t, err) assert.Nil(t, artifact) // Duplicate artifact name mockdb.On("GetArtifactByName", "bName", "aName").Return(&model.Artifact{}, nil).Once() artifact, err = CreateArtifact(CreateArtifactReq{ Name: "aName", }, &model.Bucket{ State: model.OPEN, Id: "bName", }, mockdb) assert.Error(t, err) assert.Nil(t, artifact) // ---------- BEGIN Streamed artifact creation -------------- mockdb.On("GetArtifactByName", "bName", "aName").Return(nil, database.NewEntityNotFoundError("Notfound")).Once() artifact, err = CreateArtifact(CreateArtifactReq{ Name: "aName", Size: 0, // Invalid size Chunked: false, }, &model.Bucket{ State: model.OPEN, Id: "bName", }, mockdb) assert.Error(t, err) assert.Nil(t, artifact) // Fail on inserting into DB mockdb.On("GetArtifactByName", "bName", "aName").Return(nil, database.NewEntityNotFoundError("Notfound")).Once() mockdb.On("InsertArtifact", mock.AnythingOfType("*model.Artifact")).Return(database.WrapInternalDatabaseError(fmt.Errorf("Err"))).Once() artifact, err = CreateArtifact(CreateArtifactReq{ Name: "aName", Size: 10, Chunked: false, }, &model.Bucket{ State: model.OPEN, Id: "bName", }, mockdb) assert.Error(t, err) assert.Nil(t, artifact) // Successfully create artifact mockdb.On("GetArtifactByName", "bName", "aName").Return(nil, database.NewEntityNotFoundError("Notfound")).Once() mockdb.On("InsertArtifact", mock.AnythingOfType("*model.Artifact")).Return(nil).Once() artifact, err = CreateArtifact(CreateArtifactReq{ Name: "aName", Size: 10, Chunked: false, }, &model.Bucket{ State: model.OPEN, Id: "bName", }, mockdb) assert.NoError(t, err) assert.NotNil(t, artifact) assert.Equal(t, "aName", artifact.Name) assert.Equal(t, "bName", artifact.BucketId) assert.Equal(t, model.WAITING_FOR_UPLOAD, artifact.State) assert.Equal(t, int64(10), artifact.Size) // ---------- END Streamed artifact creation -------------- // ---------- BEGIN Chunked artifact creation -------------- // Fail on inserting into DB mockdb.On("GetArtifactByName", "bName", "aName").Return(nil, database.NewEntityNotFoundError("Notfound")).Once() mockdb.On("InsertArtifact", mock.AnythingOfType("*model.Artifact")).Return(database.WrapInternalDatabaseError(fmt.Errorf("Err"))).Once() artifact, err = CreateArtifact(CreateArtifactReq{ Name: "aName", Chunked: true, }, &model.Bucket{ State: model.OPEN, Id: "bName", }, mockdb) assert.Error(t, err) assert.Nil(t, artifact) // Successfully create artifact mockdb.On("GetArtifactByName", "bName", "aName").Return(nil, database.NewEntityNotFoundError("Notfound")).Once() mockdb.On("InsertArtifact", mock.AnythingOfType("*model.Artifact")).Return(nil).Once() artifact, err = CreateArtifact(CreateArtifactReq{ Name: "aName", Chunked: true, DeadlineMins: 20, }, &model.Bucket{ State: model.OPEN, Id: "bName", }, mockdb) assert.NoError(t, err) assert.NotNil(t, artifact) assert.Equal(t, "aName", artifact.Name) assert.Equal(t, "bName", artifact.BucketId) assert.Equal(t, model.APPENDING, artifact.State) assert.Equal(t, int64(0), artifact.Size) // ---------- END Streamed artifact creation -------------- mockdb.AssertExpectations(t) }