Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(vpc): fix tests conflict #1526

Merged
merged 1 commit into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 4 additions & 14 deletions internal/sdkprovider/service/vpc/project_vpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
},
{
Expand Down Expand Up @@ -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 {
Expand Down
63 changes: 14 additions & 49 deletions tools/selproj/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,69 +9,34 @@ 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
ProjectList(ctx context.Context) ([]*aiven.Project, error)
ServiceList(ctx context.Context, projectName string) ([]*aiven.Service, error)
VPCList(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)
func (a *aivenClientAdapter) ProjectList(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
func (a *aivenClientAdapter) ServiceList(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)
func (a *aivenClientAdapter) VPCList(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)
38 changes: 8 additions & 30 deletions tools/selproj/mock_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,23 @@ import (
"github.com/stretchr/testify/mock"
)

// MockAivenClient mocks AivenClient.
type MockAivenClient struct {
mock.Mock
}

// Projects returns the projects handler.
func (m *MockAivenClient) Projects() ProjectsHandler {
return m.Called().Get(0).(*MockProjectsHandler)
}
var _ AivenClient = (*mockAivenClient)(nil)

// Services returns the services handler.
func (m *MockAivenClient) Services() ServicesHandler {
return m.Called().Get(0).(*MockServicesHandler)
}

// MockProjectsHandler mocks ProjectsHandler.
type MockProjectsHandler struct {
type mockAivenClient struct {
mock.Mock
}

// List returns a list of projects.
func (m *MockProjectsHandler) List(ctx context.Context) ([]*aiven.Project, error) {
func (m *mockAivenClient) ProjectList(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) {
func (m *mockAivenClient) ServiceList(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)
func (m *mockAivenClient) VPCList(ctx context.Context, projectName string) ([]*aiven.VPC, error) {
args := m.Called(ctx, projectName)
return args.Get(0).([]*aiven.VPC), args.Error(1)
}
19 changes: 15 additions & 4 deletions tools/selproj/selectproject.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.ProjectList(ctx)
if err != nil {
return "", err
}
Expand All @@ -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.ServiceList(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.VPCList(ctx, project.Name)
if err != nil {
return "", err
}

if len(vpcs) != 0 {
continue
}

return strings.TrimPrefix(project.Name, prefix), nil
}

return "", errNoSuitableProjectFound
Expand Down
27 changes: 11 additions & 16 deletions tools/selproj/selectproject_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 := new(mockAivenClient)
client.On("VPCList", 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("ProjectList", 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("ServiceList", ctx, "test-project-1").Return(testServicesList, nil)
client.On("ServiceList", ctx, "test-project-2").Return(emptyServicesList, nil)

projectName, err := selectProject(ctx, client, testProjectNamePrefix)

Expand All @@ -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("ProjectList", ctx).Return([]*aiven.Project{
{Name: "mismatched-project-1"},
{Name: "another-mismatched-project"},
{Name: "mismatched-project-2"},
Expand Down
Loading