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

test: Refactors resource tests to use GetClusterInfo federated_database_instance #2412

Merged
Merged
Changes from 1 commit
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
Next Next commit
test: Support getting cluster info with project
EspenAlbert committed Jul 15, 2024

Verified

This commit was signed with the committer’s verified signature.
EspenAlbert Espen Albert
commit e11cf69640251a624ee19afe3255e6c634eba1bd
10 changes: 8 additions & 2 deletions internal/testutil/acc/cluster.go
Original file line number Diff line number Diff line change
@@ -28,6 +28,8 @@ type ClusterInfo struct {
ClusterTerraformStr string
}

const DefaultClusterResourceSuffix = "cluster_info"

// GetClusterInfo is used to obtain a project and cluster configuration resource.
// When `MONGODB_ATLAS_CLUSTER_NAME` and `MONGODB_ATLAS_PROJECT_ID` are defined, creation of resources is avoided. This is useful for local execution but not intended for CI executions.
// Clusters will be created in project ProjectIDExecution.
@@ -48,11 +50,15 @@ func GetClusterInfo(tb testing.TB, req *ClusterRequest) ClusterInfo {
}
}
projectID = ProjectIDExecution(tb)
clusterTerraformStr, clusterName, err := ClusterResourceHcl(projectID, req)
return GetClusterInfoWithProject(tb, req, projectID, DefaultClusterResourceSuffix)
}

func GetClusterInfoWithProject(tb testing.TB, req *ClusterRequest, projectID, resourceSuffix string) ClusterInfo {
EspenAlbert marked this conversation as resolved.
Show resolved Hide resolved
tb.Helper()
clusterTerraformStr, clusterName, clusterResourceName, err := ClusterResourceHcl(projectID, req, resourceSuffix)
if err != nil {
tb.Error(err)
}
clusterResourceName := "mongodbatlas_advanced_cluster.cluster_info"
return ClusterInfo{
ProjectIDStr: fmt.Sprintf("%q", projectID),
ProjectID: projectID,
14 changes: 8 additions & 6 deletions internal/testutil/acc/config_formatter.go
Original file line number Diff line number Diff line change
@@ -75,7 +75,7 @@ func ToSnakeCase(str string) string {
return strings.ToLower(snake)
}

func ClusterResourceHcl(projectID string, req *ClusterRequest) (configStr, clusterName string, err error) {
func ClusterResourceHcl(projectID string, req *ClusterRequest, resourceSuffix string) (configStr, clusterName, resourceName string, err error) {
if req == nil {
req = new(ClusterRequest)
}
@@ -98,7 +98,8 @@ func ClusterResourceHcl(projectID string, req *ClusterRequest) (configStr, clust

f := hclwrite.NewEmptyFile()
root := f.Body()
cluster := root.AppendNewBlock("resource", []string{"mongodbatlas_advanced_cluster", "cluster_info"}).Body()
resourceType := "mongodbatlas_advanced_cluster"
cluster := root.AppendNewBlock("resource", []string{resourceType, resourceSuffix}).Body()
clusterRootAttributes := map[string]any{
"project_id": projectID,
"cluster_type": clusterTypeStr,
@@ -113,7 +114,7 @@ func ClusterResourceHcl(projectID string, req *ClusterRequest) (configStr, clust
for i, spec := range specs {
err = writeReplicationSpec(cluster, spec)
if err != nil {
return "", "", fmt.Errorf("error writing hcl for replication spec %d: %w", i, err)
return "", "", "", fmt.Errorf("error writing hcl for replication spec %d: %w", i, err)
}
}
if len(req.Tags) > 0 {
@@ -127,14 +128,15 @@ func ClusterResourceHcl(projectID string, req *ClusterRequest) (configStr, clust
cluster.AppendNewline()
if req.ResourceDependencyName != "" {
if !strings.Contains(req.ResourceDependencyName, ".") {
return "", "", fmt.Errorf("req.ResourceDependencyName must have a '.'")
return "", "", "", fmt.Errorf("req.ResourceDependencyName must have a '.'")
}
err = setAttributeHcl(cluster, fmt.Sprintf("depends_on = [%s]", req.ResourceDependencyName))
if err != nil {
return "", "", err
return "", "", "", err
}
}
return "\n" + string(f.Bytes()), clusterName, err
clusterResourceName := fmt.Sprintf("%s.%s", resourceType, resourceSuffix)
return "\n" + string(f.Bytes()), clusterName, clusterResourceName, err
}

func writeReplicationSpec(cluster *hclwrite.Body, spec admin.ReplicationSpec) error {
3 changes: 2 additions & 1 deletion internal/testutil/acc/config_formatter_test.go
Original file line number Diff line number Diff line change
@@ -395,8 +395,9 @@ func Test_ClusterResourceHcl(t *testing.T) {
)
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
config, actualClusterName, err := acc.ClusterResourceHcl("project", &tc.req)
config, actualClusterName, actualResourceName, err := acc.ClusterResourceHcl("project", &tc.req, acc.DefaultClusterResourceSuffix)
require.NoError(t, err)
assert.Equal(t, "mongodbatlas_advanced_cluster.cluster_info", actualResourceName)
assert.Equal(t, clusterName, actualClusterName)
assert.Equal(t, tc.expected, config)
})