From 4cb6892d7a730cd045f31277b2732271108eca73 Mon Sep 17 00:00:00 2001 From: Adam Charrett Date: Thu, 30 Nov 2023 19:26:34 +0000 Subject: [PATCH 1/5] Add test and build on PR --- .github/workflows/pr.yml | 32 +++++++++++++++++++ .../release/services/release/release_test.go | 1 + 2 files changed, 33 insertions(+) create mode 100644 .github/workflows/pr.yml diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml new file mode 100644 index 0000000..3f7b788 --- /dev/null +++ b/.github/workflows/pr.yml @@ -0,0 +1,32 @@ +name: Build and run tests +on: + push: + branches: + - main + + pull_request: + + workflow_dispatch: + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Checkout Code + uses: actions/checkout@v2 + + - name: Set up Go + uses: actions/setup-go@v3 + with: + stable: 'true' + go-version: 1.21 + + - name: Test Code + run: | + export CI=true + go test './...' + echo "Tests passed successfully." + + - name: Build + run: | + go build \ No newline at end of file diff --git a/internal/release/services/release/release_test.go b/internal/release/services/release/release_test.go index 0e915f6..3993491 100644 --- a/internal/release/services/release/release_test.go +++ b/internal/release/services/release/release_test.go @@ -56,6 +56,7 @@ func Test_RegisterReleaseWithServer(t *testing.T) { db := &mocks.DynamoDB{ DescribeTableErrors: []error{errors.New("some error")}, + CreateTableError: errors.New("some error"), } rs := &ReleaseService{ From 4a886fcb313cce334c13fdd36cb762bfa737e52e Mon Sep 17 00:00:00 2001 From: Adam Charrett Date: Thu, 30 Nov 2023 19:30:45 +0000 Subject: [PATCH 2/5] Update release_test.go --- internal/release/services/release/release_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/release/services/release/release_test.go b/internal/release/services/release/release_test.go index 3993491..3fd1a19 100644 --- a/internal/release/services/release/release_test.go +++ b/internal/release/services/release/release_test.go @@ -75,8 +75,8 @@ func Test_RegisterReleaseWithServer(t *testing.T) { t.Errorf("Expected 1 call to DescribeTable, got %v.", db.DescribeTableInvocations) } - if db.CreateTableInvocations != 0 { - t.Errorf("Expected 0 calls to CreateTable, got %v.", db.CreateTableInvocations) + if db.CreateTableInvocations != 1 { + t.Errorf("Expected 1 calls to CreateTable, got %v.", db.CreateTableInvocations) } }) } From 8e1b3192905d91fe9ef6a0fbbaf0941ac34485b5 Mon Sep 17 00:00:00 2001 From: Adam Charrett Date: Thu, 30 Nov 2023 19:32:00 +0000 Subject: [PATCH 3/5] Update dependency_manager_test.go --- .../services/dependency_manager/dependency_manager_test.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/internal/module/services/dependency_manager/dependency_manager_test.go b/internal/module/services/dependency_manager/dependency_manager_test.go index 59f5351..02a00a7 100644 --- a/internal/module/services/dependency_manager/dependency_manager_test.go +++ b/internal/module/services/dependency_manager/dependency_manager_test.go @@ -123,10 +123,13 @@ func Test_RegisterDependencyManagerWithServer(t *testing.T) { // - Module dependencies will fail... // - Container dependencies expectedDescribeTableInvocations := 1 - expectedCreateTableInvocations := 0 + expectedCreateTableInvocations := 1 expectedError := ModuleDependenciesTableInitializationError - db := &mocks.DynamoDB{DescribeTableErrors: []error{errors.New("some error")}} + db := &mocks.DynamoDB{ + DescribeTableErrors: []error{errors.New("some error")}, + CreateTableError: errors.New("some error"), + } dms := &DependencyManagerService{Db: db} From 815c9c4c3445ba42c02f36ebb676bc348939a8db Mon Sep 17 00:00:00 2001 From: Adam Charrett Date: Thu, 30 Nov 2023 19:32:45 +0000 Subject: [PATCH 4/5] Update registrar_test.go --- internal/module/services/registrar/registrar_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/internal/module/services/registrar/registrar_test.go b/internal/module/services/registrar/registrar_test.go index 5ad8bce..67fd3b0 100644 --- a/internal/module/services/registrar/registrar_test.go +++ b/internal/module/services/registrar/registrar_test.go @@ -217,6 +217,7 @@ func Test_RegisterRegistrarWithServer(t *testing.T) { db := &mocks.DynamoDB{ DescribeTableErrors: []error{errors.New("some error")}, + CreateTableError: errors.New("some error"), } rs := &RegistrarService{ @@ -235,8 +236,8 @@ func Test_RegisterRegistrarWithServer(t *testing.T) { t.Errorf("Expected 1 call to DescribeTable, got %v.", db.DescribeTableInvocations) } - if db.CreateTableInvocations != 0 { - t.Errorf("Expected 0 calls to CreateTable, got %v.", db.CreateTableInvocations) + if db.CreateTableInvocations != 1 { + t.Errorf("Expected 1 calls to CreateTable, got %v.", db.CreateTableInvocations) } }) } From 100d8e14bd9bd678a94bcf32bbd95c95a00e98b5 Mon Sep 17 00:00:00 2001 From: Adam Charrett Date: Thu, 30 Nov 2023 19:49:49 +0000 Subject: [PATCH 5/5] Fix more unit tests --- .../dependency_manager/dependency_manager_test.go | 15 +++++++++++++-- .../services/tag_manager/tag_manager_test.go | 5 +++-- .../version_manager/version_manager_test.go | 9 ++++++--- internal/storage/dynamodb_test.go | 9 ++------- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/internal/module/services/dependency_manager/dependency_manager_test.go b/internal/module/services/dependency_manager/dependency_manager_test.go index 02a00a7..5645cff 100644 --- a/internal/module/services/dependency_manager/dependency_manager_test.go +++ b/internal/module/services/dependency_manager/dependency_manager_test.go @@ -59,6 +59,10 @@ func (srv *MockRetrieveContainerDependenciesServer) Send(res *terrarium.Containe return srv.Err } +func (src *MockRetrieveContainerDependenciesServer) Context() context.Context { + return context.TODO() +} + type MockRetrieveModuleDependenciesServer struct { grpc.ServerStream SendInvocations int @@ -72,6 +76,10 @@ func (srv *MockRetrieveModuleDependenciesServer) Send(res *terrarium.ModuleDepen return srv.Err } +func (src *MockRetrieveModuleDependenciesServer) Context() context.Context { + return context.TODO() +} + type MockGetDependenciesResponse struct { Dependencies []*terrarium.Module Err error @@ -156,9 +164,12 @@ func Test_RegisterDependencyManagerWithServer(t *testing.T) { // - Container dependencies will fail... expectedError := ContainerDependenciesTableInitializationError expectedDescribeTableInvocations := 2 - expectedCreateTableInvocations := 0 + expectedCreateTableInvocations := 1 - db := &mocks.DynamoDB{DescribeTableErrors: []error{nil, errors.New("some error")}} + db := &mocks.DynamoDB{ + DescribeTableErrors: []error{nil, errors.New("some error")}, + CreateTableError: errors.New("some error"), + } dms := &DependencyManagerService{Db: db} diff --git a/internal/module/services/tag_manager/tag_manager_test.go b/internal/module/services/tag_manager/tag_manager_test.go index 8e4a4f9..fd6b6cc 100644 --- a/internal/module/services/tag_manager/tag_manager_test.go +++ b/internal/module/services/tag_manager/tag_manager_test.go @@ -45,6 +45,7 @@ func Test_RegisterTagManagerWithServer(t *testing.T) { db := &mocks.DynamoDB{ DescribeTableErrors: []error{errors.New("some error")}, + CreateTableError: errors.New("some error"), } tm := &TagManagerService{ @@ -63,8 +64,8 @@ func Test_RegisterTagManagerWithServer(t *testing.T) { t.Errorf("Expected 1 call to DescribeTable, got %v.", db.DescribeTableInvocations) } - if db.CreateTableInvocations != 0 { - t.Errorf("Expected 0 calls to CreateTable, got %v.", db.CreateTableInvocations) + if db.CreateTableInvocations != 1 { + t.Errorf("Expected 1 calls to CreateTable, got %v.", db.CreateTableInvocations) } }) } diff --git a/internal/module/services/version_manager/version_manager_test.go b/internal/module/services/version_manager/version_manager_test.go index 9d57bde..276bd7d 100644 --- a/internal/module/services/version_manager/version_manager_test.go +++ b/internal/module/services/version_manager/version_manager_test.go @@ -44,7 +44,10 @@ func Test_RegisterVersionManagerWithServer(t *testing.T) { }) t.Run("when Table initialization fails", func(t *testing.T) { - db := &mocks.DynamoDB{DescribeTableErrors: []error{errors.New("some error")}} + db := &mocks.DynamoDB{ + DescribeTableErrors: []error{errors.New("some error")}, + CreateTableError: errors.New("some error"), + } vms := &VersionManagerService{Db: db} @@ -60,8 +63,8 @@ func Test_RegisterVersionManagerWithServer(t *testing.T) { t.Errorf("Expected 1 call to DescribeTable, got %v.", db.DescribeTableInvocations) } - if db.CreateTableInvocations != 0 { - t.Errorf("Expected 0 calls to CreateTable, got %v.", db.CreateTableInvocations) + if db.CreateTableInvocations != 1 { + t.Errorf("Expected 1 calls to CreateTable, got %v.", db.CreateTableInvocations) } }) } diff --git a/internal/storage/dynamodb_test.go b/internal/storage/dynamodb_test.go index 87faa17..f0c7042 100644 --- a/internal/storage/dynamodb_test.go +++ b/internal/storage/dynamodb_test.go @@ -70,7 +70,6 @@ func Test_InitializeDynamoDb(t *testing.T) { t.Run("when checking for table existence fails", func(t *testing.T) { table := "Test" schema := &dynamodb.CreateTableInput{} - someError := errors.New("some error") db := &mocks.DynamoDB{ DescribeTableErrors: []error{errors.New("some error")}, } @@ -85,12 +84,8 @@ func Test_InitializeDynamoDb(t *testing.T) { t.Errorf("Expected %v, got %v.", table, db.TableName) } - if err == nil { - t.Error("Expected error, got nil.") - } - - if err.Error() != someError.Error() { - t.Errorf("Expected %v, got %v.", someError.Error(), err.Error()) + if err != nil { + t.Errorf("Expected no error, got %v.", err) } })