forked from digitalocean/terraform-provider-digitalocean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasource_digitalocean_vpc_test.go
125 lines (112 loc) · 3.58 KB
/
datasource_digitalocean_vpc_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
package digitalocean
import (
"fmt"
"regexp"
"testing"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)
func TestAccDataSourceDigitalOceanVPC_ByName(t *testing.T) {
vpcName := randomTestName()
vpcDesc := "A description for the VPC"
resourceConfig := fmt.Sprintf(testAccCheckDataSourceDigitalOceanVPCConfig_Basic, vpcName, vpcDesc)
dataSourceConfig := `
data "digitalocean_vpc" "foobar" {
name = digitalocean_vpc.foobar.name
}`
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{
Config: resourceConfig,
},
{
Config: resourceConfig + dataSourceConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckDigitalOceanVPCExists("data.digitalocean_vpc.foobar"),
resource.TestCheckResourceAttr(
"data.digitalocean_vpc.foobar", "name", vpcName),
resource.TestCheckResourceAttr(
"data.digitalocean_vpc.foobar", "description", vpcDesc),
resource.TestCheckResourceAttrSet(
"data.digitalocean_vpc.foobar", "default"),
resource.TestCheckResourceAttrSet(
"data.digitalocean_vpc.foobar", "ip_range"),
resource.TestCheckResourceAttrSet(
"data.digitalocean_vpc.foobar", "created_at"),
resource.TestCheckResourceAttrSet(
"data.digitalocean_vpc.foobar", "urn"),
),
},
},
})
}
func TestAccDataSourceDigitalOceanVPC_RegionDefault(t *testing.T) {
vpcDropletName := randomTestName()
vpcConfigRegionDefault := fmt.Sprintf(testAccCheckDataSourceDigitalOceanVPCConfig_RegionDefault, vpcDropletName)
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{
Config: vpcConfigRegionDefault,
Check: resource.ComposeTestCheckFunc(
testAccCheckDigitalOceanVPCExists("data.digitalocean_vpc.foobar"),
resource.TestCheckResourceAttrSet(
"data.digitalocean_vpc.foobar", "name"),
resource.TestCheckResourceAttr(
"data.digitalocean_vpc.foobar", "default", "true"),
resource.TestCheckResourceAttrSet(
"data.digitalocean_vpc.foobar", "created_at"),
),
},
},
})
}
func TestAccDataSourceDigitalOceanVPC_ExpectErrors(t *testing.T) {
vpcName := randomTestName()
vpcNotExist := fmt.Sprintf(testAccCheckDataSourceDigitalOceanVPCConfig_DoesNotExist, vpcName)
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccCheckDataSourceDigitalOceanVPCConfig_MissingRegionDefault,
ExpectError: regexp.MustCompile(`unable to find default VPC in foo region`),
},
{
Config: vpcNotExist,
ExpectError: regexp.MustCompile(`no VPCs found with name`),
},
},
})
}
const testAccCheckDataSourceDigitalOceanVPCConfig_Basic = `
resource "digitalocean_vpc" "foobar" {
name = "%s"
description = "%s"
region = "nyc3"
}`
const testAccCheckDataSourceDigitalOceanVPCConfig_RegionDefault = `
// Create Droplet to ensure default VPC exists
resource "digitalocean_droplet" "foo" {
image = "ubuntu-18-04-x64"
name = "%s"
region = "nyc3"
size = "s-1vcpu-1gb"
private_networking = "true"
}
data "digitalocean_vpc" "foobar" {
region = "nyc3"
}
`
const testAccCheckDataSourceDigitalOceanVPCConfig_MissingRegionDefault = `
data "digitalocean_vpc" "foobar" {
region = "foo"
}
`
const testAccCheckDataSourceDigitalOceanVPCConfig_DoesNotExist = `
data "digitalocean_vpc" "foobar" {
name = "%s"
}
`