Skip to content

Commit

Permalink
add assertions for mocks expectations
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenruizdegauna committed Feb 28, 2025
1 parent 5dc1f53 commit 1b5c84e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 34 deletions.
45 changes: 11 additions & 34 deletions publisher/release/marker_aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func Test_Start(t *testing.T) {
mark, err := markerS3.Start(releaseInfo)
require.NoError(t, err)
require.Equal(t, releaseInfo, mark.ReleaseInfo)
mock.AssertExpectationsForObjects(t, s3ClientMock, timeProviderMock)
}

func Test_StartErrorReadingMarkers(t *testing.T) {
Expand Down Expand Up @@ -105,6 +106,7 @@ func Test_StartErrorReadingMarkers(t *testing.T) {
markerS3 := &markerAWS{timeProvider: timeProviderMock, conf: s3Config, client: s3ClientMock, logfn: nolog}
_, err := markerS3.Start(releaseInfo)
assert.ErrorIs(t, err, someError)
mock.AssertExpectationsForObjects(t, s3ClientMock, timeProviderMock)
}

func Test_StartErrorWritingMarkers(t *testing.T) {
Expand Down Expand Up @@ -158,6 +160,7 @@ func Test_StartErrorWritingMarkers(t *testing.T) {
markerS3 := &markerAWS{timeProvider: timeProviderMock, conf: s3Config, client: s3ClientMock, logfn: nolog}
_, err := markerS3.Start(releaseInfo)
assert.ErrorIs(t, err, someError)
mock.AssertExpectationsForObjects(t, s3ClientMock, timeProviderMock)
}
func Test_End(t *testing.T) {
s3ClientMock := &S3ClientMock{}
Expand Down Expand Up @@ -221,6 +224,7 @@ func Test_End(t *testing.T) {
err := markerS3.End(mark)
require.NoError(t, err)
require.Equal(t, releaseInfo, mark.ReleaseInfo)
mock.AssertExpectationsForObjects(t, s3ClientMock, timeProviderMock)
}

func Test_End_ErrorOnWriting(t *testing.T) {
Expand Down Expand Up @@ -285,6 +289,7 @@ func Test_End_ErrorOnWriting(t *testing.T) {
}
err := markerS3.End(mark)
assert.ErrorIs(t, err, someError)
mock.AssertExpectationsForObjects(t, s3ClientMock, timeProviderMock)
}

func Test_End_ErrorIfNoMarkerFound(t *testing.T) {
Expand All @@ -308,10 +313,6 @@ func Test_End_ErrorIfNoMarkerFound(t *testing.T) {
&s3.GetObjectInput{Bucket: &s3Config.Bucket, Key: aws.String(fmt.Sprintf("%s/%s", s3Config.Directory, markerName))},
&s3.GetObjectOutput{Body: readCloser})

// It should get current time for the end of the started marker
endTime := time.Date(2025, 3, 4, 11, 12, 13, 0, time.UTC)
timeProviderMock.ShouldProvideNow(endTime)

startTime := time.Date(2023, 1, 2, 00, 00, 00, 0, time.UTC)
mark := Mark{
ReleaseInfo: ReleaseInfo{
Expand All @@ -333,6 +334,7 @@ func Test_End_ErrorIfNoMarkerFound(t *testing.T) {
}
err := markerS3.End(mark)
assert.ErrorIs(t, err, ErrNoStartedMarkersFound)
mock.AssertExpectationsForObjects(t, s3ClientMock, timeProviderMock)
}
func Test_End_ErrorOnReadingMarkers(t *testing.T) {
s3ClientMock := &S3ClientMock{}
Expand Down Expand Up @@ -371,6 +373,7 @@ func Test_End_ErrorOnReadingMarkers(t *testing.T) {
}
err := markerS3.End(mark)
assert.ErrorIs(t, err, someError)
mock.AssertExpectationsForObjects(t, s3ClientMock, timeProviderMock)
}

func Test_EndFailsIfLatestMarkerIsEnded(t *testing.T) {
Expand Down Expand Up @@ -398,10 +401,6 @@ func Test_EndFailsIfLatestMarkerIsEnded(t *testing.T) {
&s3.GetObjectInput{Bucket: &s3Config.Bucket, Key: aws.String(fmt.Sprintf("%s/%s", s3Config.Directory, markerName))},
&s3.GetObjectOutput{Body: readCloser})

// It should get current time for the end of the started marker
endTime := time.Date(2025, 3, 4, 11, 12, 13, 0, time.UTC)
timeProviderMock.ShouldProvideNow(endTime)

startTime := time.Date(2023, 1, 2, 00, 00, 00, 0, time.UTC)
mark := Mark{
ReleaseInfo: ReleaseInfo{
Expand All @@ -423,6 +422,7 @@ func Test_EndFailsIfLatestMarkerIsEnded(t *testing.T) {
}
err := markerS3.End(mark)
assert.ErrorIs(t, err, ErrLastMarkerEnded)
mock.AssertExpectationsForObjects(t, s3ClientMock, timeProviderMock)
}

func Test_EndFailsIfMarkerForAppNotFound(t *testing.T) {
Expand Down Expand Up @@ -450,10 +450,6 @@ func Test_EndFailsIfMarkerForAppNotFound(t *testing.T) {
&s3.GetObjectInput{Bucket: &s3Config.Bucket, Key: aws.String(fmt.Sprintf("%s/%s", s3Config.Directory, markerName))},
&s3.GetObjectOutput{Body: readCloser})

// It should get current time for the end of the started marker
endTime := time.Date(2025, 3, 4, 11, 12, 13, 0, time.UTC)
timeProviderMock.ShouldProvideNow(endTime)

startTime := time.Date(2023, 1, 2, 00, 00, 00, 0, time.UTC)
mark := Mark{
ReleaseInfo: ReleaseInfo{
Expand All @@ -475,6 +471,7 @@ func Test_EndFailsIfMarkerForAppNotFound(t *testing.T) {
}
err := markerS3.End(mark)
assert.ErrorIs(t, err, ErrNoStartedMarkerFoundForApp)
mock.AssertExpectationsForObjects(t, s3ClientMock, timeProviderMock)
}

func Test_EndFailsIfMarkerStartTimeIsDifferent(t *testing.T) {
Expand Down Expand Up @@ -502,10 +499,6 @@ func Test_EndFailsIfMarkerStartTimeIsDifferent(t *testing.T) {
&s3.GetObjectInput{Bucket: &s3Config.Bucket, Key: aws.String(fmt.Sprintf("%s/%s", s3Config.Directory, markerName))},
&s3.GetObjectOutput{Body: readCloser})

// It should get current time for the end of the started marker
endTime := time.Date(2025, 3, 4, 11, 12, 13, 0, time.UTC)
timeProviderMock.ShouldProvideNow(endTime)

startTime := time.Date(2023, 1, 2, 00, 00, 00, 0, time.UTC)
mark := Mark{
ReleaseInfo: ReleaseInfo{
Expand All @@ -527,6 +520,7 @@ func Test_EndFailsIfMarkerStartTimeIsDifferent(t *testing.T) {
}
err := markerS3.End(mark)
assert.ErrorIs(t, err, ErrNoStartedMarkerFoundForApp)
mock.AssertExpectationsForObjects(t, s3ClientMock, timeProviderMock)
}

func Test_EndFailsIfMarkerStartIsZero(t *testing.T) {
Expand All @@ -540,24 +534,6 @@ func Test_EndFailsIfMarkerStartIsZero(t *testing.T) {
Directory: "directory",
}

// It should read existing markers
existingMarkers := `
[
{"app_name": "app1", "tag": "v1.0", "run_id": "run1", "start": "2023-01-01T00:00:00Z", "end": "2023-01-01T01:00:00Z", "repo_name": "repo1", "schema": "schema1", "schema_url": "url1"},
{"app_name": "my-app", "tag": "v1.2", "run_id": "run3", "start": "2023-01-02T11:00:00Z", "end": "0001-01-01T00:00:00Z", "repo_name": "repo3", "schema": "schema3", "schema_url": "url3"}
]`

reader := bytes.NewReader([]byte(existingMarkers))
readCloser := io.NopCloser(reader)

s3ClientMock.ShouldGetObject(
&s3.GetObjectInput{Bucket: &s3Config.Bucket, Key: aws.String(fmt.Sprintf("%s/%s", s3Config.Directory, markerName))},
&s3.GetObjectOutput{Body: readCloser})

// It should get current time for the end of the started marker
endTime := time.Date(2025, 3, 4, 11, 12, 13, 0, time.UTC)
timeProviderMock.ShouldProvideNow(endTime)

startTime := time.Time{}
mark := Mark{
ReleaseInfo: ReleaseInfo{
Expand All @@ -579,6 +555,7 @@ func Test_EndFailsIfMarkerStartIsZero(t *testing.T) {
}
err := markerS3.End(mark)
assert.ErrorIs(t, err, ErrNotStartedMark)
mock.AssertExpectationsForObjects(t, s3ClientMock, timeProviderMock)
}

///////////////////////////////////////////////////////////////
Expand Down
6 changes: 6 additions & 0 deletions publisher/upload/upload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ func TestUploadArtifacts(t *testing.T) {
_, err = os.Stat(path.Join(dest, expectedFile))
assert.NoError(t, err)
}
mock.AssertExpectationsForObjects(t, marker)
})
}

Expand Down Expand Up @@ -242,6 +243,7 @@ func TestUploadArtifactsShouldFailIfMarkerCannotBeStarted(t *testing.T) {

err := UploadArtifacts(cfg, artifact.schema, lock.NewInMemory(), marker)
assert.ErrorIs(t, err, markerErr)
mock.AssertExpectationsForObjects(t, marker)
})
}
}
Expand Down Expand Up @@ -329,6 +331,7 @@ func TestUploadArtifactsShouldNotFailIfMarkerCannotBeEnded(t *testing.T) {
_, err = os.Stat(path.Join(dest, expectedFile))
assert.NoError(t, err)
}
mock.AssertExpectationsForObjects(t, marker)
})
}
}
Expand Down Expand Up @@ -382,13 +385,15 @@ func TestUploadArtifacts_cantBeRunInParallel(t *testing.T) {
marker.ShouldStart(releaseInfo, mark)
marker.ShouldEnd(mark)
err1 = UploadArtifacts(cfg, schema, l, marker)
mock.AssertExpectationsForObjects(t, marker)
wg.Done()
}()
go func() {
<-ready
time.Sleep(1 * time.Millisecond)
marker := &MarkerMock{}
err2 = UploadArtifacts(cfg, schema, l, marker)
mock.AssertExpectationsForObjects(t, marker)
wg.Done()
}()

Expand Down Expand Up @@ -467,6 +472,7 @@ func TestUploadArtifacts_errorsIfAnyArchFails(t *testing.T) {
} else {
assert.NoError(t, err)
}
mock.AssertExpectationsForObjects(t, marker)
})
}
}
Expand Down

0 comments on commit 1b5c84e

Please sign in to comment.