From 7e645fef8c1fd84bb25a0507c3859bfe931b213a Mon Sep 17 00:00:00 2001 From: Murad Biashimov Date: Thu, 11 Jan 2024 13:23:54 +0100 Subject: [PATCH] chore(vpc): fix tests conflict --- .../service/vpc/project_vpc_test.go | 18 ++--- tools/selproj/client.go | 66 +++++-------------- tools/selproj/mock_client.go | 40 ++++------- tools/selproj/selectproject.go | 19 ++++-- tools/selproj/selectproject_test.go | 25 +++---- 5 files changed, 57 insertions(+), 111 deletions(-) diff --git a/internal/sdkprovider/service/vpc/project_vpc_test.go b/internal/sdkprovider/service/vpc/project_vpc_test.go index 6ec7ff27e..22b5dc43b 100644 --- a/internal/sdkprovider/service/vpc/project_vpc_test.go +++ b/internal/sdkprovider/service/vpc/project_vpc_test.go @@ -27,7 +27,7 @@ func TestAccAivenProjectVPC_basic(t *testing.T) { Steps: []resource.TestStep{ { PlanOnly: true, - Config: testAccProjectVPCResourceFail(rName), + Config: testAccProjectVPCResourceFail(), ExpectError: regexp.MustCompile("invalid resource id"), }, { @@ -154,21 +154,11 @@ resource "aiven_pg" "bar" { `, os.Getenv("AIVEN_PROJECT_NAME"), name) } -func testAccProjectVPCResourceFail(name string) string { - return fmt.Sprintf(` -resource "aiven_project" "foo" { - project = "test-acc-pr-%s" -} - -resource "aiven_project_vpc" "bar" { - project = aiven_project.foo.project - cloud_name = "google-europe-west1" - network_cidr = "192.168.0.0/24" -} - +func testAccProjectVPCResourceFail() string { + return ` data "aiven_project_vpc" "vpc" { vpc_id = "some_wrong_id" -}`, name) +}` } func testAccProjectVPCResourceGetByID() string { diff --git a/tools/selproj/client.go b/tools/selproj/client.go index aa09109ad..bcea2ccef 100644 --- a/tools/selproj/client.go +++ b/tools/selproj/client.go @@ -9,69 +9,37 @@ import ( ) // AivenClient defines the interface for Aiven client operations. -// It is implemented by AivenClientAdapter. +// It is implemented by aivenClientAdapter. type AivenClient interface { - Projects() ProjectsHandler - Services() ServicesHandler + Projects(ctx context.Context) ([]*aiven.Project, error) + Services(ctx context.Context, projectName string) ([]*aiven.Service, error) + VPCs(ctx context.Context, projectName string) ([]*aiven.VPC, error) } -// ProjectsHandler defines the interface for project handling operations. -// It is implemented by ProjectsHandlerAdapter and MockProjectsHandler. -type ProjectsHandler interface { - List(ctx context.Context) ([]*aiven.Project, error) -} - -// ServicesHandler defines the interface for service handling operations. -// It is implemented by ServicesHandlerAdapter and MockServicesHandler. -type ServicesHandler interface { - List(ctx context.Context, projectName string) ([]*aiven.Service, error) -} +var _ AivenClient = (*aivenClientAdapter)(nil) // NewAivenClientAdapter creates a new adapter for the given aiven.Client. // This is needed because the aiven.Client does not implement an interface that is mockable. func NewAivenClientAdapter(client *aiven.Client) AivenClient { - return &AivenClientAdapter{client: client} + return &aivenClientAdapter{client: client} } -// AivenClientAdapter adapts aiven.Client to AivenClient. -type AivenClientAdapter struct { +// aivenClientAdapter adapts aiven.Client to AivenClient. +type aivenClientAdapter struct { client *aiven.Client } -// Projects returns the projects handler. -func (a *AivenClientAdapter) Projects() ProjectsHandler { - return &ProjectsHandlerAdapter{handler: a.client.Projects} -} - -// Services returns the services handler. -func (a *AivenClientAdapter) Services() ServicesHandler { - return &ServicesHandlerAdapter{handler: a.client.Services} -} - -// ProjectsHandlerAdapter adapts aiven.ProjectsHandler to ProjectsHandler. -// This is needed because the aiven.ProjectsHandler does not implement an interface that is mockable. -type ProjectsHandlerAdapter struct { - handler *aiven.ProjectsHandler -} - -// List returns a list of projects. -func (a *ProjectsHandlerAdapter) List(ctx context.Context) ([]*aiven.Project, error) { - return a.handler.List(ctx) +// Projects returns project list +func (a *aivenClientAdapter) Projects(ctx context.Context) ([]*aiven.Project, error) { + return a.client.Projects.List(ctx) } -// ServicesHandlerAdapter adapts aiven.ServicesHandler to ServicesHandler. -// This is needed because the aiven.ServicesHandler does not implement an interface that is mockable. -type ServicesHandlerAdapter struct { - handler *aiven.ServicesHandler +// Services returns service list +func (a *aivenClientAdapter) Services(ctx context.Context, projectName string) ([]*aiven.Service, error) { + return a.client.Services.List(ctx, projectName) } -// List returns a list of services. -func (a *ServicesHandlerAdapter) List(ctx context.Context, projectName string) ([]*aiven.Service, error) { - return a.handler.List(ctx, projectName) +// VPCs returns vpc list +func (a *aivenClientAdapter) VPCs(ctx context.Context, projectName string) ([]*aiven.VPC, error) { + return a.client.VPCs.List(ctx, projectName) } - -var _ AivenClient = (*AivenClientAdapter)(nil) - -var _ ProjectsHandler = (*ProjectsHandlerAdapter)(nil) - -var _ ServicesHandler = (*ServicesHandlerAdapter)(nil) diff --git a/tools/selproj/mock_client.go b/tools/selproj/mock_client.go index 4c6480ac7..5146d25e4 100644 --- a/tools/selproj/mock_client.go +++ b/tools/selproj/mock_client.go @@ -9,45 +9,27 @@ import ( "github.com/stretchr/testify/mock" ) +var _ AivenClient = (*MockAivenClient)(nil) + // MockAivenClient mocks AivenClient. type MockAivenClient struct { mock.Mock } -// Projects returns the projects handler. -func (m *MockAivenClient) Projects() ProjectsHandler { - return m.Called().Get(0).(*MockProjectsHandler) -} - -// Services returns the services handler. -func (m *MockAivenClient) Services() ServicesHandler { - return m.Called().Get(0).(*MockServicesHandler) -} - -// MockProjectsHandler mocks ProjectsHandler. -type MockProjectsHandler struct { - mock.Mock -} - -// List returns a list of projects. -func (m *MockProjectsHandler) List(ctx context.Context) ([]*aiven.Project, error) { +// Projects returns project list +func (m *MockAivenClient) Projects(ctx context.Context) ([]*aiven.Project, error) { args := m.Called(ctx) return args.Get(0).([]*aiven.Project), args.Error(1) } -// MockServicesHandler mocks ServicesHandler. -type MockServicesHandler struct { - mock.Mock -} - -// List returns a list of services. -func (m *MockServicesHandler) List(ctx context.Context, projectName string) ([]*aiven.Service, error) { +// Services returns service list +func (m *MockAivenClient) Services(ctx context.Context, projectName string) ([]*aiven.Service, error) { args := m.Called(ctx, projectName) return args.Get(0).([]*aiven.Service), args.Error(1) } -var _ AivenClient = (*MockAivenClient)(nil) - -var _ ProjectsHandler = (*MockProjectsHandler)(nil) - -var _ ServicesHandler = (*MockServicesHandler)(nil) +// VPCs returns VPC list +func (m *MockAivenClient) VPCs(ctx context.Context, projectName string) ([]*aiven.VPC, error) { + args := m.Called(ctx, projectName) + return args.Get(0).([]*aiven.VPC), args.Error(1) +} diff --git a/tools/selproj/selectproject.go b/tools/selproj/selectproject.go index 3a46e0c9f..3b742e16c 100644 --- a/tools/selproj/selectproject.go +++ b/tools/selproj/selectproject.go @@ -13,7 +13,7 @@ var errNoSuitableProjectFound = errors.New("no suitable project found") // selectProject selects a project with the given prefix. func selectProject(ctx context.Context, client AivenClient, prefix string) (string, error) { - projects, err := client.Projects().List(ctx) + projects, err := client.Projects(ctx) if err != nil { return "", err } @@ -23,14 +23,25 @@ func selectProject(ctx context.Context, client AivenClient, prefix string) (stri continue } - services, err := client.Services().List(ctx, project.Name) + services, err := client.Services(ctx, project.Name) if err != nil { return "", err } - if len(services) == 0 { - return strings.TrimPrefix(project.Name, prefix), nil + if len(services) != 0 { + continue + } + + vpcs, err := client.VPCs(ctx, project.Name) + if err != nil { + return "", err } + + if len(vpcs) != 0 { + continue + } + + return strings.TrimPrefix(project.Name, prefix), nil } return "", errNoSuitableProjectFound diff --git a/tools/selproj/selectproject_test.go b/tools/selproj/selectproject_test.go index 8783a9ab4..1eb1698e6 100644 --- a/tools/selproj/selectproject_test.go +++ b/tools/selproj/selectproject_test.go @@ -8,6 +8,7 @@ import ( "github.com/aiven/aiven-go-client/v2" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" ) // testProjectNamePrefix is the prefix used for testing. @@ -22,31 +23,25 @@ var testServicesList = []*aiven.Service{ var emptyServicesList []*aiven.Service // setup sets up the test environment. -func setup() (context.Context, *MockAivenClient, *MockProjectsHandler, *MockServicesHandler) { +func setup() (context.Context, *MockAivenClient) { ctx := context.Background() - client := new(MockAivenClient) - projectsHandler := new(MockProjectsHandler) - servicesHandler := new(MockServicesHandler) - - client.On("Projects").Return(projectsHandler) - client.On("Services").Return(servicesHandler) - - return ctx, client, projectsHandler, servicesHandler + client.On("VPCs", ctx, mock.Anything).Return([]*aiven.VPC{}, nil) + return ctx, client } // TestSelectProject_Basic tests selectProject. func TestSelectProject_Basic(t *testing.T) { - ctx, client, projectsHandler, servicesHandler := setup() + ctx, client := setup() - projectsHandler.On("List", ctx).Return([]*aiven.Project{ + client.On("Projects", ctx).Return([]*aiven.Project{ {Name: "test-project-1"}, {Name: "other-project"}, {Name: "test-project-2"}, }, nil) - servicesHandler.On("List", ctx, "test-project-1").Return(testServicesList, nil) - servicesHandler.On("List", ctx, "test-project-2").Return(emptyServicesList, nil) + client.On("Services", ctx, "test-project-1").Return(testServicesList, nil) + client.On("Services", ctx, "test-project-2").Return(emptyServicesList, nil) projectName, err := selectProject(ctx, client, testProjectNamePrefix) @@ -58,9 +53,9 @@ func TestSelectProject_Basic(t *testing.T) { // TestSelectProject_WrongPrefix tests selectProject with a wrong prefix. func TestSelectProject_WrongPrefix(t *testing.T) { - ctx, client, projectsHandler, _ := setup() + ctx, client := setup() - projectsHandler.On("List", ctx).Return([]*aiven.Project{ + client.On("Projects", ctx).Return([]*aiven.Project{ {Name: "mismatched-project-1"}, {Name: "another-mismatched-project"}, {Name: "mismatched-project-2"},