From be11d104fd9f632b9dad9619d8059fea390ce5ff Mon Sep 17 00:00:00 2001 From: nikchern Date: Thu, 26 Jan 2023 21:28:47 -0800 Subject: [PATCH] PLT-257-1: fixing compile errors and adding unit tests. (#206) Co-authored-by: nikolay-spectro --- .../resource_cloud_account_vsphere.go | 3 + ...rce_cloud_account_vsphere_negative_test.go | 104 ++++++++++++++++++ .../resource_cloud_account_vsphere_test.go | 72 ++++++++++++ spectrocloud/resource_macros.go | 2 +- 4 files changed, 180 insertions(+), 1 deletion(-) create mode 100644 spectrocloud/resource_cloud_account_vsphere_negative_test.go create mode 100644 spectrocloud/resource_cloud_account_vsphere_test.go diff --git a/spectrocloud/resource_cloud_account_vsphere.go b/spectrocloud/resource_cloud_account_vsphere.go index f1da0375..dba422e6 100644 --- a/spectrocloud/resource_cloud_account_vsphere.go +++ b/spectrocloud/resource_cloud_account_vsphere.go @@ -110,6 +110,9 @@ func flattenVsphereCloudAccount(d *schema.ResourceData, account *models.V1Vspher if err := d.Set("name", account.Metadata.Name); err != nil { return diag.FromErr(err), true } + if err := d.Set("context", account.Metadata.Annotations["scope"]); err != nil { + return diag.FromErr(err), true + } if err := d.Set("private_cloud_gateway_id", account.Metadata.Annotations[OverlordUID]); err != nil { return diag.FromErr(err), true } diff --git a/spectrocloud/resource_cloud_account_vsphere_negative_test.go b/spectrocloud/resource_cloud_account_vsphere_negative_test.go new file mode 100644 index 00000000..51503667 --- /dev/null +++ b/spectrocloud/resource_cloud_account_vsphere_negative_test.go @@ -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)) + } + } + } +} diff --git a/spectrocloud/resource_cloud_account_vsphere_test.go b/spectrocloud/resource_cloud_account_vsphere_test.go new file mode 100644 index 00000000..eba798f9 --- /dev/null +++ b/spectrocloud/resource_cloud_account_vsphere_test.go @@ -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")) +} diff --git a/spectrocloud/resource_macros.go b/spectrocloud/resource_macros.go index eab58a16..88951fd9 100644 --- a/spectrocloud/resource_macros.go +++ b/spectrocloud/resource_macros.go @@ -18,7 +18,7 @@ func resourceMacro() *schema.Resource { ReadContext: resourceMacrosRead, UpdateContext: resourceMacrosUpdate, DeleteContext: resourceMacrosDelete, - Description: "A resource for creating and managing service output variables and macros.", + Description: "A resource for creating and managing service output variables and macros.", Timeouts: &schema.ResourceTimeout{ Create: schema.DefaultTimeout(10 * time.Minute),