-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_source_openstack_blockstorage_snapshot_v3_test.go
More file actions
152 lines (127 loc) · 3.73 KB
/
Copy pathdata_source_openstack_blockstorage_snapshot_v3_test.go
File metadata and controls
152 lines (127 loc) · 3.73 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package openstack
import (
"fmt"
"os"
"testing"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/blockstorage/v3/snapshots"
"github.com/gophercloud/gophercloud/openstack/blockstorage/v3/volumes"
)
func TestAccBlockStorageV3SnapshotDataSource_basic(t *testing.T) {
resourceName := "data.openstack_blockstorage_snapshot_v3.snapshot_1"
volumeName := acctest.RandomWithPrefix("tf-acc-volume")
snapshotName := acctest.RandomWithPrefix("tf-acc-snapshot")
var volumeID, snapshotID string
if os.Getenv("TF_ACC") != "" {
var err error
volumeID, snapshotID, err = testAccBlockStorageV3CreateVolumeAndSnapshot(volumeName, snapshotName)
if err != nil {
t.Fatal(err)
}
defer testAccBlockStorageV3DeleteVolumeAndSnapshot(t, volumeID, snapshotID)
}
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckNonAdminOnly(t)
},
ProviderFactories: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccBlockStorageV3SnapshotDataSourceBasic(snapshotName),
Check: resource.ComposeTestCheckFunc(
testAccCheckBlockStorageV3SnapshotDataSourceID(resourceName),
resource.TestCheckResourceAttr(resourceName, "name", snapshotName),
resource.TestCheckResourceAttr(resourceName, "volume_id", volumeID),
),
},
},
})
}
func testAccBlockStorageV3CreateVolumeAndSnapshot(volumeName, snapshotName string) (string, string, error) {
config, err := testAccAuthFromEnv()
if err != nil {
return "", "", err
}
bsClient, err := config.BlockStorageV3Client(osRegionName)
if err != nil {
return "", "", err
}
volCreateOpts := volumes.CreateOpts{
Size: 1,
Name: volumeName,
}
volume, err := volumes.Create(bsClient, volCreateOpts).Extract()
if err != nil {
return "", "", err
}
err = volumes.WaitForStatus(bsClient, volume.ID, "available", 60)
if err != nil {
return "", "", err
}
snapCreateOpts := snapshots.CreateOpts{
VolumeID: volume.ID,
Name: snapshotName,
}
snapshot, err := snapshots.Create(bsClient, snapCreateOpts).Extract()
if err != nil {
return volume.ID, "", err
}
err = snapshots.WaitForStatus(bsClient, snapshot.ID, "available", 60)
if err != nil {
return volume.ID, "", err
}
return volume.ID, snapshot.ID, nil
}
func testAccBlockStorageV3DeleteVolumeAndSnapshot(t *testing.T, volumeID, snapshotID string) {
config, err := testAccAuthFromEnv()
if err != nil {
t.Fatal(err)
}
bsClient, err := config.BlockStorageV3Client(osRegionName)
if err != nil {
t.Fatal(err)
}
err = snapshots.Delete(bsClient, snapshotID).ExtractErr()
if err != nil {
t.Fatal(err)
}
err = snapshots.WaitForStatus(bsClient, snapshotID, "DELETED", 60)
if err != nil {
if _, ok := err.(gophercloud.ErrDefault404); !ok {
t.Fatal(err)
}
}
err = volumes.Delete(bsClient, volumeID, nil).ExtractErr()
if err != nil {
t.Fatal(err)
}
err = volumes.WaitForStatus(bsClient, volumeID, "DELETED", 60)
if err != nil {
if _, ok := err.(gophercloud.ErrDefault404); !ok {
t.Fatal(err)
}
}
}
func testAccCheckBlockStorageV3SnapshotDataSourceID(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Can't find snapshot data source: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("Snapshot data source ID not set")
}
return nil
}
}
func testAccBlockStorageV3SnapshotDataSourceBasic(snapshotName string) string {
return fmt.Sprintf(`
data "openstack_blockstorage_snapshot_v3" "snapshot_1" {
name = "%s"
}
`, snapshotName)
}