Skip to content

Commit

Permalink
test improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
SivaanandM committed Sep 12, 2024
1 parent 4d46d01 commit a18fd56
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
11 changes: 8 additions & 3 deletions spectrocloud/resource_cluster_custom_cloud_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,20 @@ func ParseResourceCustomCloudImportID(d *schema.ResourceData) (string, string, s
// Example: If the ID is a combination of ClusterId, then name of context/scope: `project` or `tenant` and then cloud type
// and if scope is then followed by projectID "cluster456:project:nutanix" or "cluster456:tenant:oracle"
parts := strings.Split(d.Id(), ":")

errMsg := "invalid cluster ID format specified for import custom cloud %s, Ex: it should cluster_id:context:custom_cloud_name, `cluster456:project:nutanix`"
scope := "invalid"
clusterID := ""
customCloudName := ""
if len(parts) == 3 && (parts[1] == "tenant" || parts[1] == "project") {
clusterID, scope, customCloudName = parts[0], parts[1], parts[2]
if strings.TrimSpace(parts[0]) == "" || strings.TrimSpace(parts[1]) == "" || strings.TrimSpace(parts[2]) == "" {
return "", "", "", fmt.Errorf(errMsg, d.Id())
} else {
clusterID, scope, customCloudName = parts[0], parts[1], parts[2]
}

}
if scope == "invalid" {
return "", "", "", fmt.Errorf("invalid cluster ID format specified for import custom cloud %s, Ex: it should cluster_id:context:custom_cloud_name, `cluster456:project:nutanix`", d.Id())
return "", "", "", fmt.Errorf(errMsg, d.Id())
}
return clusterID, scope, customCloudName, nil
}
69 changes: 69 additions & 0 deletions spectrocloud/resource_cluster_custom_cloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,72 @@ func TestFlattenMachinePoolConfigsCustomCloud(t *testing.T) {
})
}
}

func TestParseResourceCustomCloudImportID(t *testing.T) {
tests := []struct {
id string
expectedClusterID string
expectedScope string
expectedCloudName string
expectedError bool
expectedErrorString string
}{
{
id: "cluster456:project:nutanix",
expectedClusterID: "cluster456",
expectedScope: "project",
expectedCloudName: "nutanix",
expectedError: false,
},
{
id: "cluster789:tenant:oracle",
expectedClusterID: "cluster789",
expectedScope: "tenant",
expectedCloudName: "oracle",
expectedError: false,
},
{
id: "cluster123:invalid:gcp",
expectedClusterID: "",
expectedScope: "",
expectedCloudName: "",
expectedError: true,
expectedErrorString: "invalid cluster ID format specified for import custom cloud cluster123:invalid:gcp, Ex: it should cluster_id:context:custom_cloud_name, `cluster456:project:nutanix`",
},
{
id: "cluster456:project",
expectedClusterID: "",
expectedScope: "",
expectedCloudName: "",
expectedError: true,
expectedErrorString: "invalid cluster ID format specified for import custom cloud cluster456:project, Ex: it should cluster_id:context:custom_cloud_name, `cluster456:project:nutanix`",
},
{
id: "cluster456:tenant:",
expectedClusterID: "",
expectedScope: "",
expectedCloudName: "",
expectedError: true,
expectedErrorString: "invalid cluster ID format specified for import custom cloud cluster456:tenant:, Ex: it should cluster_id:context:custom_cloud_name, `cluster456:project:nutanix`",
},
}

for _, test := range tests {
t.Run(test.id, func(t *testing.T) {
resourceData := resourceClusterCustomCloud().TestResourceData()
resourceData.SetId(test.id)

clusterID, scope, customCloudName, err := ParseResourceCustomCloudImportID(resourceData)

if test.expectedError {
assert.Error(t, err)
assert.EqualError(t, err, test.expectedErrorString)
} else {
assert.NoError(t, err)
assert.Equal(t, test.expectedClusterID, clusterID)
assert.Equal(t, test.expectedScope, scope)
assert.Equal(t, test.expectedCloudName, customCloudName)
}
})
}
}

0 comments on commit a18fd56

Please sign in to comment.