forked from digitalocean/terraform-provider-digitalocean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasource_digitalocean_droplet_snapshot_test.go
159 lines (140 loc) · 5.22 KB
/
datasource_digitalocean_droplet_snapshot_test.go
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
153
154
155
156
157
158
159
package digitalocean
import (
"context"
"fmt"
"testing"
"github.com/digitalocean/godo"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)
func TestAccDataSourceDigitalOceanDropletSnapshot_basic(t *testing.T) {
var snapshot godo.Snapshot
testName := randomTestName()
resourceConfig := fmt.Sprintf(testAccCheckDataSourceDigitalOceanDropletSnapshot_basic, testName, testName)
dataSourceConfig := `
data "digitalocean_droplet_snapshot" "foobar" {
most_recent = true
name = digitalocean_droplet_snapshot.foo.name
}`
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{
Config: resourceConfig,
},
{
Config: resourceConfig + dataSourceConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckDataSourceDigitalOceanDropletSnapshotExists("data.digitalocean_droplet_snapshot.foobar", &snapshot),
resource.TestCheckResourceAttr("data.digitalocean_droplet_snapshot.foobar", "name", fmt.Sprintf("%s-snapshot", testName)),
resource.TestCheckResourceAttr("data.digitalocean_droplet_snapshot.foobar", "min_disk_size", "25"),
resource.TestCheckResourceAttr("data.digitalocean_droplet_snapshot.foobar", "regions.#", "1"),
resource.TestCheckResourceAttrSet("data.digitalocean_droplet_snapshot.foobar", "droplet_id"),
),
},
},
})
}
func TestAccDataSourceDigitalOceanDropletSnapshot_regex(t *testing.T) {
var snapshot godo.Snapshot
testName := randomTestName()
resourceConfig := fmt.Sprintf(testAccCheckDataSourceDigitalOceanDropletSnapshot_basic, testName, testName)
dataSourceConfig := fmt.Sprintf(`
data "digitalocean_droplet_snapshot" "foobar" {
most_recent = true
name_regex = "^%s"
}`, testName)
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{
Config: resourceConfig,
},
{
Config: resourceConfig + dataSourceConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckDataSourceDigitalOceanDropletSnapshotExists("data.digitalocean_droplet_snapshot.foobar", &snapshot),
resource.TestCheckResourceAttr("data.digitalocean_droplet_snapshot.foobar", "name", fmt.Sprintf("%s-snapshot", testName)),
resource.TestCheckResourceAttr("data.digitalocean_droplet_snapshot.foobar", "min_disk_size", "25"),
resource.TestCheckResourceAttr("data.digitalocean_droplet_snapshot.foobar", "regions.#", "1"),
resource.TestCheckResourceAttrSet("data.digitalocean_droplet_snapshot.foobar", "droplet_id"),
),
},
},
})
}
func TestAccDataSourceDigitalOceanDropletSnapshot_region(t *testing.T) {
var snapshot godo.Snapshot
testName := randomTestName()
nycResourceConfig := fmt.Sprintf(testAccCheckDataSourceDigitalOceanDropletSnapshot_basic, testName, testName)
lonResourceConfig := fmt.Sprintf(`
resource "digitalocean_droplet" "bar" {
region = "lon1"
name = "%s"
size = "s-1vcpu-1gb"
image = "ubuntu-22-04-x64"
}
resource "digitalocean_droplet_snapshot" "bar" {
name = "%s-snapshot"
droplet_id = "${digitalocean_droplet.bar.id}"
}`, testName, testName)
dataSourceConfig := `
data "digitalocean_droplet_snapshot" "foobar" {
name = digitalocean_droplet_snapshot.bar.name
region = "lon1"
}`
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{
Config: nycResourceConfig + lonResourceConfig,
},
{
Config: nycResourceConfig + lonResourceConfig + dataSourceConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckDataSourceDigitalOceanDropletSnapshotExists("data.digitalocean_droplet_snapshot.foobar", &snapshot),
resource.TestCheckResourceAttr("data.digitalocean_droplet_snapshot.foobar", "name", fmt.Sprintf("%s-snapshot", testName)),
resource.TestCheckResourceAttr("data.digitalocean_droplet_snapshot.foobar", "min_disk_size", "25"),
resource.TestCheckResourceAttr("data.digitalocean_droplet_snapshot.foobar", "regions.#", "1"),
resource.TestCheckResourceAttrSet("data.digitalocean_droplet_snapshot.foobar", "droplet_id"),
),
},
},
})
}
func testAccCheckDataSourceDigitalOceanDropletSnapshotExists(n string, snapshot *godo.Snapshot) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := testAccProvider.Meta().(*CombinedConfig).godoClient()
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No snapshot ID is set")
}
foundSnapshot, _, err := client.Snapshots.Get(context.Background(), rs.Primary.ID)
if err != nil {
return err
}
if foundSnapshot.ID != rs.Primary.ID {
return fmt.Errorf("Snapshot not found")
}
*snapshot = *foundSnapshot
return nil
}
}
const testAccCheckDataSourceDigitalOceanDropletSnapshot_basic = `
resource "digitalocean_droplet" "foo" {
name = "%s"
size = "s-1vcpu-1gb"
image = "ubuntu-22-04-x64"
region = "nyc3"
}
resource "digitalocean_droplet_snapshot" "foo" {
name = "%s-snapshot"
droplet_id = digitalocean_droplet.foo.id
}
`