forked from DavidKrau/terraform-provider-simplemdm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevice_resource_test.go
More file actions
96 lines (90 loc) · 4.52 KB
/
device_resource_test.go
File metadata and controls
96 lines (90 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package provider
import (
"context"
"fmt"
"testing"
simplemdm "github.com/DavidKrau/simplemdm-go-client"
"github.com/DavidKrau/terraform-provider-simplemdm/internal/simplemdmext"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
)
func testAccCheckDeviceDestroy(s *terraform.State) error {
return testAccCheckResourceDestroyed("simplemdm_device", func(client *simplemdm.Client, id string) error {
_, err := simplemdmext.GetDevice(context.Background(), client, id, false)
return err
})(s)
}
func TestAccDeviceResource(t *testing.T) {
testAccPreCheck(t)
deviceGroupID := testAccRequireEnv(t, "SIMPLEMDM_DEVICE_GROUP_ID")
profileID := testAccRequireEnv(t, "SIMPLEMDM_DEVICE_GROUP_PROFILE_ID")
profileUpdatedID := testAccRequireEnv(t, "SIMPLEMDM_DEVICE_GROUP_PROFILE_UPDATED_ID")
customProfileID := testAccRequireEnv(t, "SIMPLEMDM_DEVICE_GROUP_CUSTOM_PROFILE_ID")
customProfileUpdatedID := testAccRequireEnv(t, "SIMPLEMDM_DEVICE_GROUP_CUSTOM_PROFILE_UPDATED_ID")
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
CheckDestroy: testAccCheckDeviceDestroy,
Steps: []resource.TestStep{
// Create and Read testing
{
Config: fmt.Sprintf(providerConfig+`
resource "simplemdm_device" "test" {
name = "Created test device"
devicename = "Created test device"
devicegroup = %s
profiles = [%s]
customprofiles = [%s]
}
`, deviceGroupID, profileID, customProfileID),
Check: resource.ComposeAggregateTestCheckFunc(
// Verify attributes
resource.TestCheckResourceAttr("simplemdm_device.test", "name", "Created test device"),
resource.TestCheckResourceAttr("simplemdm_device.test", "devicename", "Created test device"),
resource.TestCheckResourceAttr("simplemdm_device.test", "devicegroup", deviceGroupID),
resource.TestCheckResourceAttr("simplemdm_device.test", "profiles.#", "1"),
resource.TestCheckResourceAttr("simplemdm_device.test", "profiles.0", profileID),
resource.TestCheckResourceAttr("simplemdm_device.test", "customprofiles.#", "1"),
resource.TestCheckResourceAttr("simplemdm_device.test", "customprofiles.0", customProfileID),
// Verify dynamic values have any value set in the state.
resource.TestCheckResourceAttrSet("simplemdm_device.test", "id"),
resource.TestCheckResourceAttrSet("simplemdm_device.test", "enrollmenturl"),
),
},
// ImportState testing
{
ResourceName: "simplemdm_device.test",
ImportState: true,
ImportStateVerify: true,
// The profiles and customprofiles attributes does not exist in SimpleMDM
// API, therefore there is no value for it during import.
ImportStateVerifyIgnore: []string{"profiles", "customprofiles"},
},
// Update and Read testing
{
Config: fmt.Sprintf(providerConfig+`
resource "simplemdm_device" "test" {
name = "Created test device changed"
devicename = "Created test device changed"
devicegroup = %s
profiles = [%s]
customprofiles = [%s]
}
`, deviceGroupID, profileUpdatedID, customProfileUpdatedID),
Check: resource.ComposeAggregateTestCheckFunc(
// Verify attributes
resource.TestCheckResourceAttr("simplemdm_device.test", "name", "Created test device changed"),
resource.TestCheckResourceAttr("simplemdm_device.test", "devicename", "Created test device changed"),
resource.TestCheckResourceAttr("simplemdm_device.test", "devicegroup", deviceGroupID),
resource.TestCheckResourceAttr("simplemdm_device.test", "profiles.#", "1"),
resource.TestCheckResourceAttr("simplemdm_device.test", "profiles.0", profileUpdatedID),
resource.TestCheckResourceAttr("simplemdm_device.test", "customprofiles.#", "1"),
resource.TestCheckResourceAttr("simplemdm_device.test", "customprofiles.0", customProfileUpdatedID),
// Verify dynamic values have any value set in the state.
resource.TestCheckResourceAttrSet("simplemdm_device.test", "id"),
resource.TestCheckResourceAttrSet("simplemdm_device.test", "enrollmenturl"),
),
},
// Delete testing automatically occurs in TestCase
},
})
}