Skip to content

Commit

Permalink
Fix remaining broken tests
Browse files Browse the repository at this point in the history
  • Loading branch information
roothorp committed Jan 13, 2025
1 parent 8f942a1 commit 76afdcd
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,15 @@ func TestRegularClusterReconciliation(t *testing.T) {
orgID := "0987654321"
atlasProvider := &atlasmock.TestProvider{
SdkSetClientFunc: func(secretRef *client.ObjectKey, log *zap.SugaredLogger) (*atlas.ClientSet, string, error) {
return &atlas.ClientSet{SdkClient20241113001: &adminv20241113001.APIClient{}}, "", nil
err := &adminv20241113001.GenericOpenAPIError{}
err.SetModel(adminv20241113001.ApiError{ErrorCode: atlas.NonFlexInFlexAPI})

flexAPI := mockadminv20241113001.NewFlexClustersApi(t)
flexAPI.EXPECT().GetFlexCluster(mock.Anything, project.ID(), d.GetDeploymentName()).
Return(adminv20241113001.GetFlexClusterApiRequest{ApiService: flexAPI})
flexAPI.EXPECT().GetFlexClusterExecute(mock.AnythingOfType("admin.GetFlexClusterApiRequest")).
Return(nil, &http.Response{}, err)
return &atlas.ClientSet{SdkClient20241113001: &adminv20241113001.APIClient{FlexClustersApi: flexAPI}}, "", nil
},
SdkClientFunc: func(secretRef *client.ObjectKey, log *zap.SugaredLogger) (*admin.APIClient, string, error) {
clusterAPI := mockadmin.NewClustersApi(t)
Expand Down Expand Up @@ -621,7 +629,15 @@ func TestServerlessInstanceReconciliation(t *testing.T) {
orgID := "0987654321"
atlasProvider := &atlasmock.TestProvider{
SdkSetClientFunc: func(secretRef *client.ObjectKey, log *zap.SugaredLogger) (*atlas.ClientSet, string, error) {
return &atlas.ClientSet{SdkClient20241113001: &adminv20241113001.APIClient{}}, "", nil
err := &adminv20241113001.GenericOpenAPIError{}
err.SetModel(adminv20241113001.ApiError{ErrorCode: atlas.NonFlexInFlexAPI})

flexAPI := mockadminv20241113001.NewFlexClustersApi(t)
flexAPI.EXPECT().GetFlexCluster(mock.Anything, project.ID(), d.GetDeploymentName()).
Return(adminv20241113001.GetFlexClusterApiRequest{ApiService: flexAPI})
flexAPI.EXPECT().GetFlexClusterExecute(mock.AnythingOfType("admin.GetFlexClusterApiRequest")).
Return(nil, &http.Response{}, err)
return &atlas.ClientSet{SdkClient20241113001: &adminv20241113001.APIClient{FlexClustersApi: flexAPI}}, "", nil
},
SdkClientFunc: func(secretRef *client.ObjectKey, log *zap.SugaredLogger) (*admin.APIClient, string, error) {
err := &admin.GenericOpenAPIError{}
Expand Down Expand Up @@ -795,7 +811,15 @@ func TestDeletionReconciliation(t *testing.T) {
logger := zaptest.NewLogger(t).Sugar()
atlasProvider := &atlasmock.TestProvider{
SdkSetClientFunc: func(secretRef *client.ObjectKey, log *zap.SugaredLogger) (*atlas.ClientSet, string, error) {
return &atlas.ClientSet{SdkClient20241113001: &adminv20241113001.APIClient{}}, "", nil
err := &adminv20241113001.GenericOpenAPIError{}
err.SetModel(adminv20241113001.ApiError{ErrorCode: atlas.NonFlexInFlexAPI})

flexAPI := mockadminv20241113001.NewFlexClustersApi(t)
flexAPI.EXPECT().GetFlexCluster(mock.Anything, project.ID(), d.GetDeploymentName()).
Return(adminv20241113001.GetFlexClusterApiRequest{ApiService: flexAPI})
flexAPI.EXPECT().GetFlexClusterExecute(mock.AnythingOfType("admin.GetFlexClusterApiRequest")).
Return(nil, &http.Response{}, err)
return &atlas.ClientSet{SdkClient20241113001: &adminv20241113001.APIClient{FlexClustersApi: flexAPI}}, "", nil
},
SdkClientFunc: func(secretRef *client.ObjectKey, log *zap.SugaredLogger) (*admin.APIClient, string, error) {
clusterAPI := mockadmin.NewClustersApi(t)
Expand Down Expand Up @@ -1238,12 +1262,15 @@ func TestChangeDeploymentType(t *testing.T) {
return true
},
SdkSetClientFunc: func(secretRef *client.ObjectKey, log *zap.SugaredLogger) (*atlas.ClientSet, string, error) {
err := &adminv20241113001.GenericOpenAPIError{}
err.SetModel(adminv20241113001.ApiError{ErrorCode: atlas.NonFlexInFlexAPI})

flexAPI := mockadminv20241113001.NewFlexClustersApi(t)
return &atlas.ClientSet{
SdkClient20241113001: &adminv20241113001.APIClient{
FlexClustersApi: flexAPI,
},
}, "", nil
flexAPI.EXPECT().GetFlexCluster(mock.Anything, "abc123", "cluster0").
Return(adminv20241113001.GetFlexClusterApiRequest{ApiService: flexAPI})
flexAPI.EXPECT().GetFlexClusterExecute(mock.AnythingOfType("admin.GetFlexClusterApiRequest")).
Return(nil, &http.Response{}, err)
return &atlas.ClientSet{SdkClient20241113001: &adminv20241113001.APIClient{FlexClustersApi: flexAPI}}, "", nil
},
ClientFunc: func(secretRef *client.ObjectKey, log *zap.SugaredLogger) (*mongodbatlas.Client, string, error) {
return &mongodbatlas.Client{}, "org-id", nil
Expand Down
11 changes: 10 additions & 1 deletion internal/controller/validate/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,15 @@ func serverlessPrivateEndpoints(privateEndpoints []akov2.ServerlessPrivateEndpoi
}

func flexDeployment(spec *akov2.FlexSpec) error {
// TODO
supportedProviders := provider.SupportedProviders()
switch {
case spec.ProviderSettings == nil:
return errors.New("provider settings cannot be empty")
case !supportedProviders.IsSupported(provider.ProviderName(spec.ProviderSettings.BackingProviderName)):
return errors.New("backing provider name is not supported")
case spec.ProviderSettings.RegionName == "":
return errors.New("regionName cannot be empty")
}

return nil
}
57 changes: 53 additions & 4 deletions internal/controller/validate/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,58 @@ func TestAtlasDeployment(t *testing.T) {
regionUsageRestrictions string
expectedError string
}{
"Both specs present": {
"All specs present": {
atlasDeployment: &akov2.AtlasDeployment{
Spec: akov2.AtlasDeploymentSpec{
DeploymentSpec: &akov2.AdvancedDeploymentSpec{},
ServerlessSpec: &akov2.ServerlessSpec{},
FlexSpec: &akov2.FlexSpec{},
},
},
isGov: false,
regionUsageRestrictions: "",
expectedError: "expected exactly one of spec.deploymentSpec or spec.serverlessSpec to be present, but none were",
expectedError: "expected exactly one of spec.deploymentSpec or spec.serverlessSpec or spec.flexSpec to be present, but none were",
},
"Neither spec present": {
"Advanced & Serverless specs present": {
atlasDeployment: &akov2.AtlasDeployment{
Spec: akov2.AtlasDeploymentSpec{
DeploymentSpec: &akov2.AdvancedDeploymentSpec{},
ServerlessSpec: &akov2.ServerlessSpec{},
},
},
isGov: false,
regionUsageRestrictions: "",
expectedError: "expected exactly one of spec.deploymentSpec or spec.serverlessSpec or spec.flexSpec to be present, but none were",
},
"Advanced & Flex specs present": {
atlasDeployment: &akov2.AtlasDeployment{
Spec: akov2.AtlasDeploymentSpec{
DeploymentSpec: &akov2.AdvancedDeploymentSpec{},
FlexSpec: &akov2.FlexSpec{},
},
},
isGov: false,
regionUsageRestrictions: "",
expectedError: "expected exactly one of spec.deploymentSpec or spec.serverlessSpec or spec.flexSpec to be present, but none were",
},
"Serverless & Flex specs present": {
atlasDeployment: &akov2.AtlasDeployment{
Spec: akov2.AtlasDeploymentSpec{
ServerlessSpec: &akov2.ServerlessSpec{},
FlexSpec: &akov2.FlexSpec{},
},
},
isGov: false,
regionUsageRestrictions: "",
expectedError: "expected exactly one of spec.deploymentSpec or spec.serverlessSpec or spec.flexSpec to be present, but none were",
},
"No spec present": {
atlasDeployment: &akov2.AtlasDeployment{
Spec: akov2.AtlasDeploymentSpec{},
},
isGov: false,
regionUsageRestrictions: "",
expectedError: "expected exactly one of spec.deploymentSpec or spec.serverlessSpec to be present, but none were",
expectedError: "expected exactly one of spec.deploymentSpec or spec.serverlessSpec or spec.flexSpec to be present, but none were",
},
"Only DeploymentSpec present": {
atlasDeployment: &akov2.AtlasDeployment{
Expand Down Expand Up @@ -72,6 +106,21 @@ func TestAtlasDeployment(t *testing.T) {
regionUsageRestrictions: "",
expectedError: "",
},
"Only FlexSpec present": {
atlasDeployment: &akov2.AtlasDeployment{
Spec: akov2.AtlasDeploymentSpec{
FlexSpec: &akov2.FlexSpec{
ProviderSettings: &akov2.FlexProviderSettings{
BackingProviderName: "AWS",
RegionName: "US_EAST_1",
},
},
},
},
isGov: false,
regionUsageRestrictions: "",
expectedError: "",
},
"ServerlessSpec with provider config error": {
atlasDeployment: &akov2.AtlasDeployment{
Spec: akov2.AtlasDeploymentSpec{
Expand Down
77 changes: 68 additions & 9 deletions internal/translation/deployment/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,15 @@ func TestClusterExists(t *testing.T) {
Return(nil, nil, errors.New("failed to get cluster from atlas"))

serverlessInstanceAPI := mockadmin.NewServerlessInstancesApi(t)

err := &adminv20241113001.GenericOpenAPIError{}
err.SetModel(adminv20241113001.ApiError{ErrorCode: atlas.ClusterNotFound})

flexAPI := mockadminv20241113001.NewFlexClustersApi(t)
flexAPI.EXPECT().GetFlexCluster(context.Background(), "project-id", "cluster0").
Return(adminv20241113001.GetFlexClusterApiRequest{ApiService: flexAPI})
flexAPI.EXPECT().GetFlexClusterExecute(mock.AnythingOfType("admin.GetFlexClusterApiRequest")).
Return(nil, nil, err)

return clusterAPI, serverlessInstanceAPI, flexAPI
},
Expand All @@ -105,11 +113,14 @@ func TestClusterExists(t *testing.T) {
serverlessInstanceAPI.EXPECT().GetServerlessInstanceExecute(mock.AnythingOfType("admin.GetServerlessInstanceApiRequest")).
Return(nil, nil, errors.New("failed to get serverless instance from atlas"))

err := &adminv20241113001.GenericOpenAPIError{}
err.SetModel(adminv20241113001.ApiError{ErrorCode: atlas.ClusterNotFound})

flexAPI := mockadminv20241113001.NewFlexClustersApi(t)
flexAPI.EXPECT().GetFlexCluster(context.Background(), "project-id", "instance0").
Return(adminv20241113001.GetFlexClusterApiRequest{ApiService: flexAPI})
flexAPI.EXPECT().GetFlexClusterExecute(mock.AnythingOfType("admin.GetFlexClusterApiRequest")).
Return(nil, nil, atlasAPIError(atlas.ClusterNotFound))
Return(nil, nil, err)

return clusterAPI, serverlessInstanceAPI, flexAPI
},
Expand All @@ -130,11 +141,14 @@ func TestClusterExists(t *testing.T) {
serverlessInstanceAPI.EXPECT().GetServerlessInstanceExecute(mock.AnythingOfType("admin.GetServerlessInstanceApiRequest")).
Return(nil, nil, atlasAPIError(atlas.ProviderUnsupported))

err := &adminv20241113001.GenericOpenAPIError{}
err.SetModel(adminv20241113001.ApiError{ErrorCode: atlas.NonFlexInFlexAPI})

flexAPI := mockadminv20241113001.NewFlexClustersApi(t)
flexAPI.EXPECT().GetFlexCluster(context.Background(), "project-id", "cluster0").
Return(adminv20241113001.GetFlexClusterApiRequest{ApiService: flexAPI})
flexAPI.EXPECT().GetFlexClusterExecute(mock.AnythingOfType("admin.GetFlexClusterApiRequest")).
Return(nil, nil, atlasAPIError(atlas.NonFlexInFlexAPI))
Return(nil, nil, err)

return clusterAPI, serverlessInstanceAPI, flexAPI
},
Expand All @@ -154,11 +168,14 @@ func TestClusterExists(t *testing.T) {
serverlessInstanceAPI.EXPECT().GetServerlessInstanceExecute(mock.AnythingOfType("admin.GetServerlessInstanceApiRequest")).
Return(nil, nil, atlasAPIError(atlas.ServerlessInstanceNotFound))

err := &adminv20241113001.GenericOpenAPIError{}
err.SetModel(adminv20241113001.ApiError{ErrorCode: atlas.NonFlexInFlexAPI})

flexAPI := mockadminv20241113001.NewFlexClustersApi(t)
flexAPI.EXPECT().GetFlexCluster(context.Background(), "project-id", "instance0").
Return(adminv20241113001.GetFlexClusterApiRequest{ApiService: flexAPI})
flexAPI.EXPECT().GetFlexClusterExecute(mock.AnythingOfType("admin.GetFlexClusterApiRequest")).
Return(nil, nil, atlasAPIError(atlas.NonFlexInFlexAPI))
Return(nil, nil, err)

return clusterAPI, serverlessInstanceAPI, flexAPI
},
Expand All @@ -177,7 +194,15 @@ func TestClusterExists(t *testing.T) {
)

serverlessInstanceAPI := mockadmin.NewServerlessInstancesApi(t)

err := &adminv20241113001.GenericOpenAPIError{}
err.SetModel(adminv20241113001.ApiError{ErrorCode: atlas.NonFlexInFlexAPI})

flexAPI := mockadminv20241113001.NewFlexClustersApi(t)
flexAPI.EXPECT().GetFlexCluster(mock.Anything, "project-id", "cluster0").
Return(adminv20241113001.GetFlexClusterApiRequest{ApiService: flexAPI})
flexAPI.EXPECT().GetFlexClusterExecute(mock.AnythingOfType("admin.GetFlexClusterApiRequest")).
Return(nil, &http.Response{}, err)

return clusterAPI, serverlessInstanceAPI, flexAPI
},
Expand All @@ -202,11 +227,14 @@ func TestClusterExists(t *testing.T) {
nil,
)

err := &adminv20241113001.GenericOpenAPIError{}
err.SetModel(adminv20241113001.ApiError{ErrorCode: atlas.NonFlexInFlexAPI})

flexAPI := mockadminv20241113001.NewFlexClustersApi(t)
flexAPI.EXPECT().GetFlexCluster(context.Background(), "project-id", "instance0").
Return(adminv20241113001.GetFlexClusterApiRequest{ApiService: flexAPI})
flexAPI.EXPECT().GetFlexClusterExecute(mock.AnythingOfType("admin.GetFlexClusterApiRequest")).
Return(nil, nil, atlasAPIError(atlas.NonFlexInFlexAPI))
Return(nil, nil, err)

return clusterAPI, serverlessInstanceAPI, flexAPI
},
Expand All @@ -223,11 +251,14 @@ func TestClusterExists(t *testing.T) {

serverlessInstanceAPI := mockadmin.NewServerlessInstancesApi(t)

err := &adminv20241113001.GenericOpenAPIError{}
err.SetModel(adminv20241113001.ApiError{ErrorCode: atlas.NonFlexInFlexAPI})

flexAPI := mockadminv20241113001.NewFlexClustersApi(t)
flexAPI.EXPECT().GetFlexCluster(context.Background(), "project-id", "instance0").
Return(adminv20241113001.GetFlexClusterApiRequest{ApiService: flexAPI})
flexAPI.EXPECT().GetFlexClusterExecute(mock.AnythingOfType("admin.GetFlexClusterApiRequest")).
Return(nil, nil, atlasAPIError(atlas.NonFlexInFlexAPI))
Return(nil, nil, err)

return clusterAPI, serverlessInstanceAPI, flexAPI
},
Expand Down Expand Up @@ -265,7 +296,15 @@ func TestGetDeployment(t *testing.T) {
Return(nil, nil, errors.New("failed to get cluster from atlas"))

serverlessInstanceAPI := mockadmin.NewServerlessInstancesApi(t)

err := &adminv20241113001.GenericOpenAPIError{}
err.SetModel(adminv20241113001.ApiError{ErrorCode: atlas.ClusterNotFound})

flexAPI := mockadminv20241113001.NewFlexClustersApi(t)
flexAPI.EXPECT().GetFlexCluster(context.Background(), "project-id", "cluster0").
Return(adminv20241113001.GetFlexClusterApiRequest{ApiService: flexAPI})
flexAPI.EXPECT().GetFlexClusterExecute(mock.AnythingOfType("admin.GetFlexClusterApiRequest")).
Return(nil, nil, err)

return clusterAPI, serverlessInstanceAPI, flexAPI
},
Expand All @@ -286,11 +325,14 @@ func TestGetDeployment(t *testing.T) {
serverlessInstanceAPI.EXPECT().GetServerlessInstanceExecute(mock.AnythingOfType("admin.GetServerlessInstanceApiRequest")).
Return(nil, nil, errors.New("failed to get serverless instance from atlas"))

err := &adminv20241113001.GenericOpenAPIError{}
err.SetModel(adminv20241113001.ApiError{ErrorCode: atlas.ClusterNotFound})

flexAPI := mockadminv20241113001.NewFlexClustersApi(t)
flexAPI.EXPECT().GetFlexCluster(context.Background(), "project-id", "instance0").
Return(adminv20241113001.GetFlexClusterApiRequest{ApiService: flexAPI})
flexAPI.EXPECT().GetFlexClusterExecute(mock.AnythingOfType("admin.GetFlexClusterApiRequest")).
Return(nil, nil, atlasAPIError(atlas.ClusterNotFound))
Return(nil, nil, err)

return clusterAPI, serverlessInstanceAPI, flexAPI
},
Expand All @@ -311,11 +353,14 @@ func TestGetDeployment(t *testing.T) {
serverlessInstanceAPI.EXPECT().GetServerlessInstanceExecute(mock.AnythingOfType("admin.GetServerlessInstanceApiRequest")).
Return(nil, nil, atlasAPIError(atlas.ProviderUnsupported))

err := &adminv20241113001.GenericOpenAPIError{}
err.SetModel(adminv20241113001.ApiError{ErrorCode: atlas.ClusterNotFound})

flexAPI := mockadminv20241113001.NewFlexClustersApi(t)
flexAPI.EXPECT().GetFlexCluster(context.Background(), "project-id", "cluster0").
Return(adminv20241113001.GetFlexClusterApiRequest{ApiService: flexAPI})
flexAPI.EXPECT().GetFlexClusterExecute(mock.AnythingOfType("admin.GetFlexClusterApiRequest")).
Return(nil, nil, atlasAPIError(atlas.NonFlexInFlexAPI))
Return(nil, nil, err)

return clusterAPI, serverlessInstanceAPI, flexAPI
},
Expand All @@ -335,11 +380,14 @@ func TestGetDeployment(t *testing.T) {
serverlessInstanceAPI.EXPECT().GetServerlessInstanceExecute(mock.AnythingOfType("admin.GetServerlessInstanceApiRequest")).
Return(nil, nil, atlasAPIError(atlas.ServerlessInstanceNotFound))

err := &adminv20241113001.GenericOpenAPIError{}
err.SetModel(adminv20241113001.ApiError{ErrorCode: atlas.NonFlexInFlexAPI})

flexAPI := mockadminv20241113001.NewFlexClustersApi(t)
flexAPI.EXPECT().GetFlexCluster(context.Background(), "project-id", "instance0").
Return(adminv20241113001.GetFlexClusterApiRequest{ApiService: flexAPI})
flexAPI.EXPECT().GetFlexClusterExecute(mock.AnythingOfType("admin.GetFlexClusterApiRequest")).
Return(nil, nil, atlasAPIError(atlas.NonFlexInFlexAPI))
Return(nil, nil, err)

return clusterAPI, serverlessInstanceAPI, flexAPI
},
Expand All @@ -358,7 +406,15 @@ func TestGetDeployment(t *testing.T) {
)

serverlessInstanceAPI := mockadmin.NewServerlessInstancesApi(t)

err := &adminv20241113001.GenericOpenAPIError{}
err.SetModel(adminv20241113001.ApiError{ErrorCode: atlas.NonFlexInFlexAPI})

flexAPI := mockadminv20241113001.NewFlexClustersApi(t)
flexAPI.EXPECT().GetFlexCluster(context.Background(), "project-id", "cluster0").
Return(adminv20241113001.GetFlexClusterApiRequest{ApiService: flexAPI})
flexAPI.EXPECT().GetFlexClusterExecute(mock.AnythingOfType("admin.GetFlexClusterApiRequest")).
Return(nil, nil, err)

return clusterAPI, serverlessInstanceAPI, flexAPI
},
Expand All @@ -383,11 +439,14 @@ func TestGetDeployment(t *testing.T) {
nil,
)

err := &adminv20241113001.GenericOpenAPIError{}
err.SetModel(adminv20241113001.ApiError{ErrorCode: atlas.NonFlexInFlexAPI})

flexAPI := mockadminv20241113001.NewFlexClustersApi(t)
flexAPI.EXPECT().GetFlexCluster(context.Background(), "project-id", "instance0").
Return(adminv20241113001.GetFlexClusterApiRequest{ApiService: flexAPI})
flexAPI.EXPECT().GetFlexClusterExecute(mock.AnythingOfType("admin.GetFlexClusterApiRequest")).
Return(nil, nil, atlasAPIError(atlas.NonFlexInFlexAPI))
Return(nil, nil, err)

return clusterAPI, serverlessInstanceAPI, flexAPI
},
Expand Down

0 comments on commit 76afdcd

Please sign in to comment.