-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PLT-257-1: fixing compile errors and adding unit tests. (#206)
Co-authored-by: nikolay-spectro <[email protected]>
- Loading branch information
Showing
4 changed files
with
180 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
spectrocloud/resource_cloud_account_vsphere_negative_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package spectrocloud | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/spectrocloud/gomi/pkg/ptr" | ||
"github.com/spectrocloud/hapi/models" | ||
"testing" | ||
) | ||
|
||
func skipSchemaAttributes(originalSchema map[string]*schema.Schema, keysToRemove []string) map[string]*schema.Schema { | ||
newSchema := make(map[string]*schema.Schema) | ||
for key, value := range originalSchema { | ||
if !contains(keysToRemove, key) { | ||
newSchema[key] = value | ||
} | ||
} | ||
return newSchema | ||
} | ||
|
||
func contains(s []string, e string) bool { | ||
for _, a := range s { | ||
if a == e { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func TestFlattenVsphereCloudAccountAttributes(t *testing.T) { | ||
// Create a dummy vSphere account | ||
account := &models.V1VsphereAccount{ | ||
Metadata: &models.V1ObjectMeta{ | ||
Name: "test-account", | ||
Annotations: map[string]string{ | ||
OverlordUID: "gateway-id", | ||
}, | ||
}, | ||
Spec: &models.V1VsphereCloudAccount{ | ||
VcenterServer: ptr.StringPtr("vcenter.example.com"), | ||
Username: ptr.StringPtr("user"), | ||
Insecure: false, | ||
}, | ||
} | ||
|
||
// Create a table of test cases | ||
testCases := []struct { | ||
AttrName string | ||
ExpectedErr bool | ||
}{ | ||
{"name", true}, | ||
{"context", true}, | ||
{"private_cloud_gateway_id", true}, | ||
{"vsphere_vcenter", true}, | ||
{"vsphere_username", true}, | ||
{"vsphere_ignore_insecure_error", true}, | ||
} | ||
|
||
// Get a copy of the original schema | ||
originalSchema := resourceCloudAccountVsphere().Schema | ||
|
||
// Iterate through each test case | ||
for _, test := range testCases { | ||
attrName := test.AttrName | ||
expectedErr := test.ExpectedErr | ||
|
||
// Get the attribute from the original schema | ||
_, ok := originalSchema[attrName] | ||
if !ok { | ||
t.Errorf("Attribute %s: Not found in original schema", attrName) | ||
continue | ||
} | ||
|
||
// Create a new schema with only the current attribute | ||
newSchema := skipSchemaAttributes(originalSchema, []string{attrName}) | ||
|
||
resourceCloudAccountVsphereWithSkippedAttrs := &schema.Resource{ | ||
CreateContext: resourceCloudAccountVsphereCreate, | ||
ReadContext: resourceCloudAccountVsphereRead, | ||
UpdateContext: resourceCloudAccountVsphereUpdate, | ||
DeleteContext: resourceCloudAccountVsphereDelete, | ||
Schema: newSchema, | ||
} | ||
|
||
d := resourceCloudAccountVsphereWithSkippedAttrs.TestResourceData() | ||
|
||
// Test case where d.Set returns an error | ||
diags, _ := flattenVsphereCloudAccount(d, account) | ||
|
||
if expectedErr { | ||
if len(diags) != 1 { | ||
t.Errorf("Attribute %s: Expected one diagnostic, got %d", attrName, len(diags)) | ||
} | ||
|
||
// Check if diags has error for specific attribute | ||
if !diags.HasError() { | ||
t.Errorf("attribute %s: Expected error, got no error", attrName) | ||
} | ||
} else { | ||
if len(diags) != 0 { | ||
t.Errorf("attribute %s: Expected no diagnostics, got %d", attrName, len(diags)) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package spectrocloud | ||
|
||
import ( | ||
"github.com/spectrocloud/hapi/models" | ||
"github.com/spectrocloud/terraform-provider-spectrocloud/types" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestToVsphereAccount(t *testing.T) { | ||
rd := resourceCloudAccountVsphere().TestResourceData() | ||
rd.Set("name", "vsphere_unit_test_acc") | ||
rd.Set("vsphere_vcenter", "vcenter.example.com") | ||
rd.Set("vsphere_username", "testuser") | ||
rd.Set("vsphere_password", "testpass") | ||
rd.Set("vsphere_ignore_insecure_error", false) | ||
rd.Set("private_cloud_gateway_id", "12345") | ||
acc := toVsphereAccount(rd) | ||
|
||
assert.Equal(t, rd.Get("name"), acc.Metadata.Name) | ||
assert.Equal(t, rd.Get("vsphere_vcenter"), *acc.Spec.VcenterServer) | ||
assert.Equal(t, rd.Get("vsphere_username"), *acc.Spec.Username) | ||
assert.Equal(t, rd.Get("vsphere_password"), *acc.Spec.Password) | ||
assert.Equal(t, rd.Get("vsphere_ignore_insecure_error"), acc.Spec.Insecure) | ||
assert.Equal(t, rd.Get("private_cloud_gateway_id"), acc.Metadata.Annotations[OverlordUID]) | ||
assert.Equal(t, rd.Id(), acc.Metadata.UID) | ||
} | ||
|
||
func TestToVsphereAccountIgnoreInsecureError(t *testing.T) { | ||
rd := resourceCloudAccountVsphere().TestResourceData() | ||
rd.Set("name", "vsphere_unit_test_acc") | ||
rd.Set("vsphere_vcenter", "vcenter.example.com") | ||
rd.Set("vsphere_username", "testuser") | ||
rd.Set("vsphere_password", "testpass") | ||
rd.Set("vsphere_ignore_insecure_error", true) | ||
rd.Set("private_cloud_gateway_id", "67890") | ||
acc := toVsphereAccount(rd) | ||
|
||
assert.Equal(t, rd.Get("name"), acc.Metadata.Name) | ||
assert.Equal(t, rd.Get("vsphere_vcenter"), *acc.Spec.VcenterServer) | ||
assert.Equal(t, rd.Get("vsphere_username"), *acc.Spec.Username) | ||
assert.Equal(t, rd.Get("vsphere_password"), *acc.Spec.Password) | ||
assert.Equal(t, rd.Get("vsphere_ignore_insecure_error"), acc.Spec.Insecure) | ||
assert.Equal(t, rd.Get("private_cloud_gateway_id"), acc.Metadata.Annotations[OverlordUID]) | ||
assert.Equal(t, rd.Id(), acc.Metadata.UID) | ||
} | ||
|
||
func TestFlattenVsphereCloudAccount(t *testing.T) { | ||
rd := resourceCloudAccountVsphere().TestResourceData() | ||
account := &models.V1VsphereAccount{ | ||
Metadata: &models.V1ObjectMeta{ | ||
Name: "test_account", | ||
Annotations: map[string]string{OverlordUID: "12345"}, | ||
UID: "abcdef", | ||
}, | ||
Spec: &models.V1VsphereCloudAccount{ | ||
VcenterServer: types.Ptr("vcenter.example.com"), | ||
Username: types.Ptr("testuser"), | ||
Insecure: true, | ||
}, | ||
} | ||
|
||
diags, hasError := flattenVsphereCloudAccount(rd, account) | ||
|
||
assert.Nil(t, diags) | ||
assert.False(t, hasError) | ||
assert.Equal(t, "test_account", rd.Get("name")) | ||
assert.Equal(t, "12345", rd.Get("private_cloud_gateway_id")) | ||
assert.Equal(t, "vcenter.example.com", rd.Get("vsphere_vcenter")) | ||
assert.Equal(t, "testuser", rd.Get("vsphere_username")) | ||
assert.Equal(t, true, rd.Get("vsphere_ignore_insecure_error")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters