Skip to content

Commit

Permalink
Update Default for UTs
Browse files Browse the repository at this point in the history
- Set some default values and clean up some of the tests to work with
  the unit test mock server.
  • Loading branch information
doriac11 committed Nov 22, 2023
1 parent 61fb459 commit 223fa24
Show file tree
Hide file tree
Showing 20 changed files with 184 additions and 425 deletions.
3 changes: 3 additions & 0 deletions powerstore/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ const (
//CreateVolumeGroupDetailErrorMsg specifies error caused when invalid attribute values are provided
CreateVolumeGroupDetailErrorMsg = "Error creating volume group"

//CreateVolumeErrorMsg specifies error caused when invalid attribute values are provided
CreateVolumeErrorMsg = "Error creating volume"

//CreateResourceMissingErrorMsg specifies error caused when required attribute value is not provided
CreateResourceMissingErrorMsg = "Missing required argument"

Expand Down
2 changes: 1 addition & 1 deletion powerstore/datasource_host_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

// Test to Fetch Host Groups
func TestAccHostGroup_FetchHostGroup(t *testing.T) {
func TestAccHostGroupDs_FetchHostGroup(t *testing.T) {
if os.Getenv("TF_ACC") == "" {
t.Skip("Dont run with units tests because it will try to create the context")
}
Expand Down
4 changes: 2 additions & 2 deletions powerstore/datasource_host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

// Test to Fetch Host details
func TestAccHost_FetchHost(t *testing.T) {
func TestAccHostDs_FetchHost(t *testing.T) {
if os.Getenv("TF_ACC") == "" {
t.Skip("Dont run with units tests because it will try to create the context")
}
Expand All @@ -49,7 +49,7 @@ func TestAccHost_FetchHost(t *testing.T) {
}

// Test to fetch Host - Negative
func TestAccHost_FetchHostNegative(t *testing.T) {
func TestAccHostDs_FetchHostNegative(t *testing.T) {
if os.Getenv("TF_ACC") == "" {
t.Skip("Dont run with units tests because it will try to create the context")
}
Expand Down
5 changes: 2 additions & 3 deletions powerstore/datasource_protection_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ import (
)

// Test to Fetch Protection Policy
func TestAccProtectionPolicy_FetchPolicy(t *testing.T) {
func TestAccProtectionPolicyDs_FetchPolicy(t *testing.T) {
if os.Getenv("TF_ACC") == "" {
t.Skip("Dont run with units tests because it will try to create the context")
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testProviderFactory,
Expand All @@ -49,7 +48,7 @@ func TestAccProtectionPolicy_FetchPolicy(t *testing.T) {
}

// Test to Fetch Protection Policy- Negative
func TestAccProtectionPolicy_FetchPolicyNegative(t *testing.T) {
func TestAccProtectionPolicyDs_FetchPolicyNegative(t *testing.T) {
if os.Getenv("TF_ACC") == "" {
t.Skip("Dont run with units tests because it will try to create the context")
}
Expand Down
2 changes: 1 addition & 1 deletion powerstore/datasource_snapshotrule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

// Test to Fetch SnapshotRule
func TestAccSnapshotRule_FetchSnapshotRule(t *testing.T) {
func TestAccSnapshotRuleDs_FetchSnapshotRule(t *testing.T) {
if os.Getenv("TF_ACC") == "" {
t.Skip("Dont run with units tests because it will try to create the context")
}
Expand Down
2 changes: 1 addition & 1 deletion powerstore/datasource_volume_group_snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

// Test to Fetch Volume Group snapshots
func TestAccVolumeGroupSnapshot_FetchVolumeGroupSnapshot(t *testing.T) {
func TestAccVolumeGroupSnapshotDs_FetchVolumeGroupSnapshot(t *testing.T) {
if os.Getenv("TF_ACC") == "" {
t.Skip("Dont run with units tests because it will try to create the context")
}
Expand Down
2 changes: 1 addition & 1 deletion powerstore/datasource_volume_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

// Test to Fetch Volume Groups
func TestAccVolumeGroup_FetchVolumeGroup(t *testing.T) {
func TestAccVolumeGroupDs_FetchVolumeGroup(t *testing.T) {
if os.Getenv("TF_ACC") == "" {
t.Skip("Dont run with units tests because it will try to create the context")
}
Expand Down
2 changes: 1 addition & 1 deletion powerstore/datasource_volume_snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

// Test to Fetch Volume Snapshot
func TestAccVolume_FetchVolumeSnapshot(t *testing.T) {
func TestAccVolumeDs_FetchVolumeSnapshot(t *testing.T) {
if os.Getenv("TF_ACC") == "" {
t.Skip("Dont run with units tests because it will try to create the context")
}
Expand Down
2 changes: 1 addition & 1 deletion powerstore/datasource_volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

// Test to Fetch Volume
func TestAccVolume_FetchVolume(t *testing.T) {
func TestAccVolumeDs_FetchVolume(t *testing.T) {
if os.Getenv("TF_ACC") == "" {
t.Skip("Dont run with units tests because it will try to create the context")
}
Expand Down
67 changes: 38 additions & 29 deletions powerstore/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,36 +30,38 @@ import (
// var testAccProviders map[string]func() tfsdk.Provider
var testProvider provider.Provider
var testProviderFactory map[string]func() (tfprotov6.ProviderServer, error)
var endpoint = os.Getenv("POWERSTORE_ENDPOINT")
var username = os.Getenv("POWERSTORE_USERNAME")
var password = os.Getenv("POWERSTORE_PASSWORD")
var hostID = os.Getenv("HOST_ID")
var hostIDRead = os.Getenv("HOST_ID_READ")
var hostGroupID = os.Getenv("HOST_GROUP_ID")
var volumeGroupID = os.Getenv("VOLUME_GROUP_ID")
var hostName = os.Getenv("HOST_NAME")
var hostNameRead = os.Getenv("HOST_NAME_READ")
var hostGroupName = os.Getenv("HOST_GROUP_NAME")
var volumeID = os.Getenv("VOLUME_ID")
var volumeName = os.Getenv("VOLUME_NAME")
var snapshotRuleID = os.Getenv("SNAPSHOT_RULE_ID")
var replicationRuleID = os.Getenv("REPLICATION_RULE_ID")
var snapshotRuleName = os.Getenv("SNAPSHOT_RULE_NAME")
var replicationRuleName = os.Getenv("REPLICATION_RULE_NAME")
var policyName = os.Getenv("PROTECTION_POLICY_NAME")
var policyID = os.Getenv("PROTECTION_POLICY_ID")
var volumeGroupName = os.Getenv("VOLUME_GROUP_NAME")
var volumeGroupSnapshotName = os.Getenv("VOLUME_GROUP_SNAPSHOT_NAME")
var volumeGroupSnapshotID = os.Getenv("VOLUME_GROUP_SNAPSHOT_ID")
var volumeSnapshotID = os.Getenv("VOLUME_SNAPSHOT_ID")
var volumeSnapshotName = os.Getenv("VOLUME_SNAPSHOT_NAME")

var endpoint = setDefault(os.Getenv("POWERSTORE_ENDPOINT"), "http://localhost:3003/api/rest")
var username = setDefault(os.Getenv("POWERSTORE_USERNAME"), "test")
var password = setDefault(os.Getenv("POWERSTORE_PASSWORD"), "test")
var hostID = setDefault(os.Getenv("HOST_ID"), "tfacc_host_id")
var hostIDRead = setDefault(os.Getenv("HOST_ID_READ"), "tfacc_host_id")
var hostGroupID = setDefault(os.Getenv("HOST_GROUP_ID"), "tfacc_host_group_id")
var volumeGroupID = setDefault(os.Getenv("VOLUME_GROUP_ID"), "tfacc_volume_group_id")
var hostName = setDefault(os.Getenv("HOST_NAME"), "tfacc_host_name")
var hostNameRead = setDefault(os.Getenv("HOST_NAME_READ"), "tfacc_host_name")
var hostGroupName = setDefault(os.Getenv("HOST_GROUP_NAME"), "tfacc_host_group_name")
var volumeID = setDefault(os.Getenv("VOLUME_ID"), "tfacc_volume_id")
var volumeName = setDefault(os.Getenv("VOLUME_NAME"), "tfacc_volume_name")
var snapshotRuleID = setDefault(os.Getenv("SNAPSHOT_RULE_ID"), "tfacc_snapshot_rule_id")
var replicationRuleID = setDefault(os.Getenv("REPLICATION_RULE_ID"), "tfacc_replication_rule_id")
var snapshotRuleName = setDefault(os.Getenv("SNAPSHOT_RULE_NAME"), "tfacc_snapshot_rule_name")
var replicationRuleName = setDefault(os.Getenv("REPLICATION_RULE_NAME"), "tfacc_replication_rule_name")
var policyName = setDefault(os.Getenv("PROTECTION_POLICY_NAME"), "tfacc_policy_name")
var policyID = setDefault(os.Getenv("PROTECTION_POLICY_ID"), "tfacc_policy_id")
var volumeGroupName = setDefault(os.Getenv("VOLUME_GROUP_NAME"), "tfacc_volume_group_name")
var volumeGroupSnapshotName = setDefault(os.Getenv("VOLUME_GROUP_SNAPSHOT_NAME"), "tfacc_volume_group_snapshot_name")
var volumeGroupSnapshotID = setDefault(os.Getenv("VOLUME_GROUP_SNAPSHOT_ID"), "tfacc_volume_group_snapshot_id")
var volumeSnapshotID = setDefault(os.Getenv("VOLUME_SNAPSHOT_ID"), "tfacc_volume_snapshot_id")
var volumeSnapshotName = setDefault(os.Getenv("VOLUME_SNAPSHOT_NAME"), "tfacc_volume_snapshot_name")

var ProviderConfigForTesting = ``

func init() {
username := os.Getenv("POWERSTORE_USERNAME")
password := os.Getenv("POWERSTORE_PASSWORD")
endpoint := os.Getenv("POWERSTORE_ENDPOINT")

username := username
password := password
endpoint := endpoint
insecure := "true"

ProviderConfigForTesting = fmt.Sprintf(`
Expand All @@ -78,16 +80,23 @@ func init() {
}

func testAccPreCheck(t *testing.T) {
if v := os.Getenv("POWERSTORE_USERNAME"); v == "" {
if v := username; v == "" {
t.Fatal("POWERSTORE_USERNAME must be set for acceptance tests")
}

if v := os.Getenv("POWERSTORE_PASSWORD"); v == "" {
if v := password; v == "" {
t.Fatal("POWERSTORE_PASSWORD must be set for acceptance tests")
}

if v := os.Getenv("POWERSTORE_ENDPOINT"); v == "" {
if v := endpoint; v == "" {
t.Fatal("POWERSTORE_ENDPOINT must be set for acceptance tests")
}

}

func setDefault(osInput string, defaultStr string) string {
if osInput == "" {
return defaultStr
}
return osInput
}
117 changes: 23 additions & 94 deletions powerstore/resource_host_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,29 @@ func TestAccHostGroup_Create(t *testing.T) {
resource.TestCheckResourceAttr("powerstore_hostgroup.test", "host_ids.0", hostID),
),
},
// Import Testing
{
Config: ProviderConfigForTesting + HostGroupParamsCreate,
ResourceName: "powerstore_hostgroup.test",
ImportState: true,
ExpectError: nil,
ImportStateCheck: func(s []*terraform.InstanceState) error {
assert.Equal(t, "test_hostgroup", s[0].Attributes["name"])
assert.Equal(t, "Test Create Host Group", s[0].Attributes["description"])
assert.Equal(t, hostID, s[0].Attributes["host_ids.0"])
return nil
},
},
// Update Testing
{
Config: ProviderConfigForTesting + HostGroupParamsUpdate,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("powerstore_hostgroup.test", "name", "test_hostgroup"),
resource.TestCheckResourceAttr("powerstore_hostgroup.test", "description", "Test Update Host Group"),
resource.TestCheckResourceAttr("powerstore_hostgroup.test", "host_ids.0", hostID),
),
},
// Remove host before cleanup
{
Config: ProviderConfigForTesting + HostGroupParamsUpdateRemoveHost,
},
Expand Down Expand Up @@ -167,100 +190,6 @@ func TestAccHostGroup_CreateWithHostIDAndName(t *testing.T) {
})
}

// Test to Update existing HostGroup Params
func TestAccHostGroup_Update(t *testing.T) {
if os.Getenv("TF_ACC") == "" {
t.Skip("Dont run with units tests because it will try to create the context")
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testProviderFactory,
Steps: []resource.TestStep{
{
Config: ProviderConfigForTesting + HostGroupParamsCreate,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("powerstore_hostgroup.test", "name", "test_hostgroup"),
resource.TestCheckResourceAttr("powerstore_hostgroup.test", "description", "Test Create Host Group"),
resource.TestCheckResourceAttr("powerstore_hostgroup.test", "host_ids.0", hostID),
),
},
{
Config: ProviderConfigForTesting + HostGroupParamsUpdate,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("powerstore_hostgroup.test", "name", "test_hostgroup"),
resource.TestCheckResourceAttr("powerstore_hostgroup.test", "description", "Test Update Host Group"),
resource.TestCheckResourceAttr("powerstore_hostgroup.test", "host_ids.0", hostID),
),
},
{
Config: ProviderConfigForTesting + HostGroupParamsUpdateRemoveHost,
},
},
})
}

// Test to Update existing HostGroup Params
func TestAccHostGroup_UpdateRemoveHost(t *testing.T) {
if os.Getenv("TF_ACC") == "" {
t.Skip("Dont run with units tests because it will try to create the context")
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testProviderFactory,
Steps: []resource.TestStep{
{
Config: ProviderConfigForTesting + HostGroupParamsCreate,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("powerstore_hostgroup.test", "name", "test_hostgroup"),
resource.TestCheckResourceAttr("powerstore_hostgroup.test", "description", "Test Create Host Group"),
resource.TestCheckResourceAttr("powerstore_hostgroup.test", "host_ids.0", hostID),
),
},
{
Config: ProviderConfigForTesting + HostGroupParamsUpdateRemoveHost,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("powerstore_hostgroup.test", "name", "test_hostgroup"),
resource.TestCheckResourceAttr("powerstore_hostgroup.test", "description", "Test Create Host Group"),
),
},
},
})
}

// Test to import host group successfully
func TestAccHostGroup_ImportSuccess(t *testing.T) {
if os.Getenv("TF_ACC") == "" {
t.Skip("Dont run with units tests because it will try to create the context")
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testProviderFactory,
Steps: []resource.TestStep{
{
Config: ProviderConfigForTesting + HostGroupParamsCreate,
},
{
Config: ProviderConfigForTesting + HostGroupParamsCreate,
ResourceName: "powerstore_hostgroup.test",
ImportState: true,
ExpectError: nil,
ImportStateCheck: func(s []*terraform.InstanceState) error {
assert.Equal(t, "test_hostgroup", s[0].Attributes["name"])
assert.Equal(t, "Test Create Host Group", s[0].Attributes["description"])
assert.Equal(t, hostID, s[0].Attributes["host_ids.0"])
return nil
},
},
{
Config: ProviderConfigForTesting + HostGroupParamsUpdateRemoveHost,
},
},
})
}

// Negative - Test to import host group
func TestAccHostGroup_ImportFailure(t *testing.T) {
if os.Getenv("TF_ACC") == "" {
Expand Down
43 changes: 12 additions & 31 deletions powerstore/resource_host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ func TestAccHost_Create(t *testing.T) {
resource.TestCheckResourceAttr("powerstore_host.test", "description", "Test Host Resource"),
),
},
{
Config: ProviderConfigForTesting + HostParamsCreate,
ResourceName: "powerstore_host.test",
ImportState: true,
ExpectError: nil,
ImportStateVerify: true,
ImportStateCheck: func(s []*terraform.InstanceState) error {
assert.Equal(t, "tf_host_acc_new", s[0].Attributes["name"])
assert.Equal(t, "Linux", s[0].Attributes["os_type"])
return nil
},
},
},
})
}
Expand Down Expand Up @@ -210,37 +222,6 @@ func TestAccHost_ImportFailure(t *testing.T) {
})
}

// Test to import successfully
func TestAccHost_ImportSuccess(t *testing.T) {

if os.Getenv("TF_ACC") == "" {
t.Skip("Dont run with units tests because it will try to create the context")
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testProviderFactory,
Steps: []resource.TestStep{
{
Config: ProviderConfigForTesting + HostParamsCreate,
},
{
Config: ProviderConfigForTesting + HostParamsCreate,
ResourceName: "powerstore_host.test",
ImportState: true,
ExpectError: nil,
ImportStateVerify: true,
ImportStateCheck: func(s []*terraform.InstanceState) error {
assert.Equal(t, "tf_host_acc_new", s[0].Attributes["name"])
assert.Equal(t, "Linux", s[0].Attributes["os_type"])
return nil
},
},
},
})

}

var HostParamsCreate = `
resource "powerstore_host" "test" {
name = "tf_host_acc_new"
Expand Down
Loading

0 comments on commit 223fa24

Please sign in to comment.