From b4b04f89f92c117b2e6ffdb22d64b85b8d6be33d Mon Sep 17 00:00:00 2001 From: sundowndev Date: Mon, 18 Oct 2021 10:45:28 +0200 Subject: [PATCH] test: azurerm_postgresql_database --- .../azurerm_postgresql_database_enumerator.go | 13 +++++- .../repository/mock_PostgresqlRespository.go | 14 +++--- pkg/remote/azurerm/repository/postgresql.go | 8 ++-- .../azurerm/repository/postgresql_test.go | 13 ++++-- pkg/remote/azurerm/util.go | 11 ----- pkg/remote/azurerm/util_test.go | 43 ------------------- pkg/remote/azurerm_postgresql_scanner_test.go | 12 +++--- .../azurerm_postgresql_database_test.go | 2 +- .../azurerm_postgresql_database/.driftignore | 2 + .../azurerm_postgresql_database/terraform.tf | 2 +- 10 files changed, 43 insertions(+), 77 deletions(-) delete mode 100644 pkg/remote/azurerm/util.go delete mode 100644 pkg/remote/azurerm/util_test.go create mode 100644 pkg/resource/azurerm/testdata/acc/azurerm_postgresql_database/.driftignore diff --git a/pkg/remote/azurerm/azurerm_postgresql_database_enumerator.go b/pkg/remote/azurerm/azurerm_postgresql_database_enumerator.go index ea2e412e..0fc7ba6a 100644 --- a/pkg/remote/azurerm/azurerm_postgresql_database_enumerator.go +++ b/pkg/remote/azurerm/azurerm_postgresql_database_enumerator.go @@ -1,10 +1,12 @@ package azurerm import ( + "github.com/Azure/go-autorest/autorest/azure" "github.com/cloudskiff/driftctl/pkg/remote/azurerm/repository" remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error" "github.com/cloudskiff/driftctl/pkg/resource" "github.com/cloudskiff/driftctl/pkg/resource/azurerm" + "github.com/sirupsen/logrus" ) type AzurermPostgresqlDatabaseEnumerator struct { @@ -31,7 +33,16 @@ func (e *AzurermPostgresqlDatabaseEnumerator) Enumerate() ([]*resource.Resource, results := make([]*resource.Resource, 0) for _, server := range servers { - databases, err := e.repository.ListAllDatabasesByServer(trimResourceGroupName(*server.ID), *server.Name) + res, err := azure.ParseResourceID(*server.ID) + if err != nil { + logrus.WithFields(map[string]interface{}{ + "type": azurerm.AzurePostgresqlServerResourceType, + "id": *server.ID, + }).Errorf("Error listing %s: failed to parse resource ID", string(e.SupportedType())) + continue + } + + databases, err := e.repository.ListAllDatabasesByServer(res.ResourceGroup, server) if err != nil { return nil, remoteerror.NewResourceListingError(err, string(e.SupportedType())) } diff --git a/pkg/remote/azurerm/repository/mock_PostgresqlRespository.go b/pkg/remote/azurerm/repository/mock_PostgresqlRespository.go index 1b7f1503..93d49f6d 100644 --- a/pkg/remote/azurerm/repository/mock_PostgresqlRespository.go +++ b/pkg/remote/azurerm/repository/mock_PostgresqlRespository.go @@ -12,13 +12,13 @@ type MockPostgresqlRespository struct { mock.Mock } -// ListAllDatabasesByServer provides a mock function with given fields: resGroup, serverName -func (_m *MockPostgresqlRespository) ListAllDatabasesByServer(resGroup string, serverName string) ([]*armpostgresql.Database, error) { - ret := _m.Called(resGroup, serverName) +// ListAllDatabasesByServer provides a mock function with given fields: resGroup, server +func (_m *MockPostgresqlRespository) ListAllDatabasesByServer(resGroup string, server *armpostgresql.Server) ([]*armpostgresql.Database, error) { + ret := _m.Called(resGroup, server) var r0 []*armpostgresql.Database - if rf, ok := ret.Get(0).(func(string, string) []*armpostgresql.Database); ok { - r0 = rf(resGroup, serverName) + if rf, ok := ret.Get(0).(func(string, *armpostgresql.Server) []*armpostgresql.Database); ok { + r0 = rf(resGroup, server) } else { if ret.Get(0) != nil { r0 = ret.Get(0).([]*armpostgresql.Database) @@ -26,8 +26,8 @@ func (_m *MockPostgresqlRespository) ListAllDatabasesByServer(resGroup string, s } var r1 error - if rf, ok := ret.Get(1).(func(string, string) error); ok { - r1 = rf(resGroup, serverName) + if rf, ok := ret.Get(1).(func(string, *armpostgresql.Server) error); ok { + r1 = rf(resGroup, server) } else { r1 = ret.Error(1) } diff --git a/pkg/remote/azurerm/repository/postgresql.go b/pkg/remote/azurerm/repository/postgresql.go index c7e6888d..e70d34c4 100644 --- a/pkg/remote/azurerm/repository/postgresql.go +++ b/pkg/remote/azurerm/repository/postgresql.go @@ -12,7 +12,7 @@ import ( type PostgresqlRespository interface { ListAllServers() ([]*armpostgresql.Server, error) - ListAllDatabasesByServer(resGroup string, serverName string) ([]*armpostgresql.Database, error) + ListAllDatabasesByServer(resGroup string, server *armpostgresql.Server) ([]*armpostgresql.Database, error) } type postgresqlServersClientImpl struct { @@ -70,13 +70,13 @@ func (s *postgresqlRepository) ListAllServers() ([]*armpostgresql.Server, error) return res.Value, nil } -func (s *postgresqlRepository) ListAllDatabasesByServer(resGroup string, serverName string) ([]*armpostgresql.Database, error) { - cacheKey := fmt.Sprintf("postgresqlListAllDatabases_%s_%s", resGroup, serverName) +func (s *postgresqlRepository) ListAllDatabasesByServer(resGroup string, server *armpostgresql.Server) ([]*armpostgresql.Database, error) { + cacheKey := fmt.Sprintf("postgresqlListAllDatabases_%s_%s", resGroup, *server.Name) if v := s.cache.Get(cacheKey); v != nil { return v.([]*armpostgresql.Database), nil } - res, err := s.databaseClient.ListByServer(context.Background(), resGroup, serverName, &armpostgresql.DatabasesListByServerOptions{}) + res, err := s.databaseClient.ListByServer(context.Background(), resGroup, *server.Name, nil) if err != nil { return nil, err } diff --git a/pkg/remote/azurerm/repository/postgresql_test.go b/pkg/remote/azurerm/repository/postgresql_test.go index 74a2e199..73203193 100644 --- a/pkg/remote/azurerm/repository/postgresql_test.go +++ b/pkg/remote/azurerm/repository/postgresql_test.go @@ -129,7 +129,7 @@ func Test_Postgresql_ListAllDatabases(t *testing.T) { { name: "should return postgres servers", mocks: func(client *mockPostgresqlDatabaseClient, mockCache *cache.MockCache) { - client.On("ListByServer", context.Background(), "res-group", "server", &armpostgresql.DatabasesListByServerOptions{}).Return(armpostgresql.DatabasesListByServerResponse{ + client.On("ListByServer", context.Background(), "res-group", "server", (*armpostgresql.DatabasesListByServerOptions)(nil)).Return(armpostgresql.DatabasesListByServerResponse{ DatabasesListByServerResult: armpostgresql.DatabasesListByServerResult{ DatabaseListResult: armpostgresql.DatabaseListResult{ Value: expectedResults, @@ -154,7 +154,7 @@ func Test_Postgresql_ListAllDatabases(t *testing.T) { mocks: func(client *mockPostgresqlDatabaseClient, mockCache *cache.MockCache) { mockCache.On("Get", "postgresqlListAllDatabases_res-group_server").Return(nil).Times(1) - client.On("ListByServer", context.Background(), "res-group", "server", &armpostgresql.DatabasesListByServerOptions{}).Return(armpostgresql.DatabasesListByServerResponse{}, errors.New("remote error")).Times(1) + client.On("ListByServer", context.Background(), "res-group", "server", (*armpostgresql.DatabasesListByServerOptions)(nil)).Return(armpostgresql.DatabasesListByServerResponse{}, errors.New("remote error")).Times(1) }, wantErr: "remote error", }, @@ -171,7 +171,14 @@ func Test_Postgresql_ListAllDatabases(t *testing.T) { databaseClient: fakeClient, cache: mockCache, } - got, err := s.ListAllDatabasesByServer("res-group", "server") + got, err := s.ListAllDatabasesByServer("res-group", &armpostgresql.Server{ + TrackedResource: armpostgresql.TrackedResource{ + Resource: armpostgresql.Resource{ + ID: to.StringPtr("server"), + Name: to.StringPtr("server"), + }, + }, + }) if tt.wantErr != "" { assert.EqualError(t, err, tt.wantErr) } else { diff --git a/pkg/remote/azurerm/util.go b/pkg/remote/azurerm/util.go deleted file mode 100644 index 70d9aab1..00000000 --- a/pkg/remote/azurerm/util.go +++ /dev/null @@ -1,11 +0,0 @@ -package azurerm - -import "regexp" - -func trimResourceGroupName(name string) string { - re, _ := regexp.Compile(`\/resourceGroups\/(?P[\w-_]+)\/?`) - if m := re.FindStringSubmatch(name); len(m) > 0 { - return m[len(m)-1] - } - return "" -} diff --git a/pkg/remote/azurerm/util_test.go b/pkg/remote/azurerm/util_test.go deleted file mode 100644 index d1d957dc..00000000 --- a/pkg/remote/azurerm/util_test.go +++ /dev/null @@ -1,43 +0,0 @@ -package azurerm - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestAzurermUtil_trimResourceGroupName(t *testing.T) { - testcases := []struct { - name string - resourceId string - expected string - }{ - { - name: "should return resource's group name", - resourceId: "/subscriptions/7bfb2c5c-7308-46ed-8ae4-fffa356eb406/resourceGroups/api-rg-pro/providers/Microsoft.DBforPostgreSQL/servers/postgresql-server-8791542", - expected: "api-rg-pro", - }, - { - name: "should return resource's group name", - resourceId: "/subscriptions/7bfb2c5c-7308-46ed-8ae4-fffa356eb406/resourceGroups/api-rg-pro", - expected: "api-rg-pro", - }, - { - name: "should return resource's group name", - resourceId: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foobar/providers/Microsoft.Storage/storageAccounts/testeliedriftctl", - expected: "foobar", - }, - { - name: "should return resource's group name", - resourceId: "/subscriptions/00000000-0000-0000-0000-000000000000/test/foobar/providers/Microsoft.Storage/storageAccounts/testeliedriftctl", - expected: "", - }, - } - - for _, tt := range testcases { - t.Run(tt.name, func(t *testing.T) { - got := trimResourceGroupName(tt.resourceId) - assert.Equal(t, tt.expected, got) - }) - } -} diff --git a/pkg/remote/azurerm_postgresql_scanner_test.go b/pkg/remote/azurerm_postgresql_scanner_test.go index 45a47d7d..882f2101 100644 --- a/pkg/remote/azurerm_postgresql_scanner_test.go +++ b/pkg/remote/azurerm_postgresql_scanner_test.go @@ -10,7 +10,7 @@ import ( "github.com/cloudskiff/driftctl/pkg/remote/azurerm" "github.com/cloudskiff/driftctl/pkg/remote/azurerm/repository" "github.com/cloudskiff/driftctl/pkg/remote/common" - error2 "github.com/cloudskiff/driftctl/pkg/remote/error" + remoteerr "github.com/cloudskiff/driftctl/pkg/remote/error" "github.com/cloudskiff/driftctl/pkg/resource" resourceazure "github.com/cloudskiff/driftctl/pkg/resource/azurerm" "github.com/cloudskiff/driftctl/pkg/terraform" @@ -44,7 +44,7 @@ func TestAzurermPostgresqlServer(t *testing.T) { mocks: func(repository *repository.MockPostgresqlRespository, alerter *mocks.AlerterInterface) { repository.On("ListAllServers").Return(nil, dummyError) }, - wantErr: error2.NewResourceListingError(dummyError, resourceazure.AzurePostgresqlServerResourceType), + wantErr: remoteerr.NewResourceListingError(dummyError, resourceazure.AzurePostgresqlServerResourceType), }, { test: "multiple postgres servers", @@ -141,7 +141,7 @@ func TestAzurermPostgresqlDatabase(t *testing.T) { mocks: func(repository *repository.MockPostgresqlRespository, alerter *mocks.AlerterInterface) { repository.On("ListAllServers").Return(nil, dummyError) }, - wantErr: error2.NewResourceListingErrorWithType(dummyError, resourceazure.AzurePostgresqlDatabaseResourceType, resourceazure.AzurePostgresqlServerResourceType), + wantErr: remoteerr.NewResourceListingErrorWithType(dummyError, resourceazure.AzurePostgresqlDatabaseResourceType, resourceazure.AzurePostgresqlServerResourceType), }, { test: "error listing postgres databases", @@ -157,9 +157,9 @@ func TestAzurermPostgresqlDatabase(t *testing.T) { }, }, nil).Once() - repository.On("ListAllDatabasesByServer", "api-rg-pro", "postgresql-server-8791542").Return(nil, dummyError).Once() + repository.On("ListAllDatabasesByServer", "api-rg-pro", mock.IsType(&armpostgresql.Server{})).Return(nil, dummyError).Once() }, - wantErr: error2.NewResourceListingError(dummyError, resourceazure.AzurePostgresqlDatabaseResourceType), + wantErr: remoteerr.NewResourceListingError(dummyError, resourceazure.AzurePostgresqlDatabaseResourceType), }, { test: "multiple postgres databases", @@ -175,7 +175,7 @@ func TestAzurermPostgresqlDatabase(t *testing.T) { }, }, nil).Once() - repository.On("ListAllDatabasesByServer", "api-rg-pro", "postgresql-server-8791542").Return([]*armpostgresql.Database{ + repository.On("ListAllDatabasesByServer", "api-rg-pro", mock.IsType(&armpostgresql.Server{})).Return([]*armpostgresql.Database{ { ProxyResource: armpostgresql.ProxyResource{ Resource: armpostgresql.Resource{ diff --git a/pkg/resource/azurerm/azurerm_postgresql_database_test.go b/pkg/resource/azurerm/azurerm_postgresql_database_test.go index 2b98d90d..da6ce1e0 100644 --- a/pkg/resource/azurerm/azurerm_postgresql_database_test.go +++ b/pkg/resource/azurerm/azurerm_postgresql_database_test.go @@ -14,7 +14,7 @@ func TestAcc_Azure_PostgresqlDatabase(t *testing.T) { Args: []string{ "scan", "--to", "azure+tf", - "--filter", "Type=='azurerm_postgresql_database' && contains(Id, 'acc-test-db')", + "--filter", "contains(Id, 'acc-test-db')", }, Checks: []acceptance.AccCheck{ { diff --git a/pkg/resource/azurerm/testdata/acc/azurerm_postgresql_database/.driftignore b/pkg/resource/azurerm/testdata/acc/azurerm_postgresql_database/.driftignore new file mode 100644 index 00000000..d26f7166 --- /dev/null +++ b/pkg/resource/azurerm/testdata/acc/azurerm_postgresql_database/.driftignore @@ -0,0 +1,2 @@ +* +!azurerm_postgresql_database diff --git a/pkg/resource/azurerm/testdata/acc/azurerm_postgresql_database/terraform.tf b/pkg/resource/azurerm/testdata/acc/azurerm_postgresql_database/terraform.tf index 4669d7ec..2d2a87a9 100644 --- a/pkg/resource/azurerm/testdata/acc/azurerm_postgresql_database/terraform.tf +++ b/pkg/resource/azurerm/testdata/acc/azurerm_postgresql_database/terraform.tf @@ -12,7 +12,7 @@ provider "azurerm" { } data "azurerm_resource_group" "qa1" { - name = "raphael-dev" // driftctl-qa-1 + name = "driftctl-qa-1" } resource "random_string" "suffix" {