Skip to content

Commit

Permalink
PLT-257-1: fixing compile errors and adding unit tests. (#206)
Browse files Browse the repository at this point in the history
Co-authored-by: nikolay-spectro <[email protected]>
  • Loading branch information
nikchern and nikchern authored Jan 27, 2023
1 parent 647ffd4 commit be11d10
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 1 deletion.
3 changes: 3 additions & 0 deletions spectrocloud/resource_cloud_account_vsphere.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
104 changes: 104 additions & 0 deletions spectrocloud/resource_cloud_account_vsphere_negative_test.go
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))
}
}
}
}
72 changes: 72 additions & 0 deletions spectrocloud/resource_cloud_account_vsphere_test.go
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"))
}
2 changes: 1 addition & 1 deletion spectrocloud/resource_macros.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit be11d10

Please sign in to comment.