forked from oracle/terraform-provider-oci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore_vnic_attachment_resource_test.go
205 lines (196 loc) · 8.43 KB
/
core_vnic_attachment_resource_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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
package provider
import (
"fmt"
"regexp"
"testing"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/oracle/oci-go-sdk/core"
"github.com/stretchr/testify/suite"
)
type ResourceCoreVnicAttachmentTestSuite struct {
suite.Suite
Providers map[string]terraform.ResourceProvider
Config string
ResourceName string
VnicResourceName string
}
func (s *ResourceCoreVnicAttachmentTestSuite) SetupTest() {
s.Providers = testAccProviders
s.Config = legacyTestProviderConfig() + instanceDnsConfig
s.ResourceName = "oci_core_vnic_attachment.va"
s.VnicResourceName = "data.oci_core_vnic.v"
}
func (s *ResourceCoreVnicAttachmentTestSuite) TestAccResourceCoreVnicAttachment_basic() {
var vaId string
resource.Test(s.T(), resource.TestCase{
Providers: s.Providers,
Steps: []resource.TestStep{
{
ImportState: true,
ImportStateVerify: true,
Config: s.Config + `
resource "oci_core_vnic_attachment" "va" {
instance_id = "${oci_core_instance.t.id}"
display_name = "-tf-va1"
create_vnic_details {
subnet_id = "${oci_core_subnet.t.id}"
assign_public_ip = false
}
}
data "oci_core_vnic" "v" {
vnic_id = "${oci_core_vnic_attachment.va.vnic_id}"
}`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(s.ResourceName, "availability_domain"),
resource.TestCheckResourceAttrSet(s.ResourceName, "compartment_id"),
resource.TestCheckResourceAttr(s.ResourceName, "display_name", "-tf-va1"),
resource.TestCheckResourceAttrSet(s.ResourceName, "id"),
resource.TestCheckResourceAttrSet(s.ResourceName, "instance_id"),
resource.TestCheckResourceAttr(s.ResourceName, "state", string(core.VnicAttachmentLifecycleStateAttached)),
resource.TestCheckResourceAttrSet(s.ResourceName, "instance_id"),
resource.TestCheckResourceAttrSet(s.ResourceName, "subnet_id"),
resource.TestCheckResourceAttrSet(s.ResourceName, "time_created"),
resource.TestCheckResourceAttrSet(s.ResourceName, "vlan_tag"),
resource.TestCheckResourceAttrSet(s.ResourceName, "vnic_id"),
resource.TestCheckResourceAttrSet(s.VnicResourceName, "id"),
resource.TestCheckResourceAttrSet(s.VnicResourceName, "display_name"),
resource.TestCheckResourceAttrSet(s.VnicResourceName, "private_ip_address"),
// @SDK 1/2018: Since we don't assign a public IP to this vnic, we will get a response from server
// without a public_ip_address. Old SDK would have set it to empty, but new SDK will set it to nil.
// Commenting out until we have a better way of handling this.
//resource.TestCheckResourceAttr(s.VnicResourceName, "public_ip_address", ""),
resource.TestCheckNoResourceAttr(s.VnicResourceName, "public_ip_address"),
resource.TestCheckResourceAttr(s.VnicResourceName, "skip_source_dest_check", "false"),
func(ts *terraform.State) (err error) {
vaId, err = fromInstanceState(ts, s.ResourceName, "id")
return err
},
),
},
{
// Update the VNIC
Config: s.Config + `
resource "oci_core_vnic_attachment" "va" {
instance_id = "${oci_core_instance.t.id}"
display_name = "-tf-va1"
create_vnic_details {
subnet_id = "${oci_core_subnet.t.id}"
display_name = "-tf-vnic-2"
assign_public_ip = false
hostname_label = "myvnichostname"
skip_source_dest_check = true
}
}
data "oci_core_vnic" "v" {
vnic_id = "${oci_core_vnic_attachment.va.vnic_id}"
}`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(s.ResourceName, "availability_domain"),
resource.TestCheckResourceAttrSet(s.ResourceName, "compartment_id"),
resource.TestCheckResourceAttr(s.ResourceName, "display_name", "-tf-va1"),
resource.TestCheckResourceAttrSet(s.ResourceName, "id"),
resource.TestCheckResourceAttrSet(s.ResourceName, "instance_id"),
resource.TestCheckResourceAttr(s.ResourceName, "state", string(core.VnicAttachmentLifecycleStateAttached)),
resource.TestCheckResourceAttrSet(s.ResourceName, "instance_id"),
resource.TestCheckResourceAttrSet(s.ResourceName, "subnet_id"),
resource.TestCheckResourceAttrSet(s.ResourceName, "time_created"),
resource.TestCheckResourceAttrSet(s.ResourceName, "vlan_tag"),
resource.TestCheckResourceAttrSet(s.ResourceName, "vnic_id"),
resource.TestCheckResourceAttrSet(s.VnicResourceName, "id"),
resource.TestCheckResourceAttr(s.ResourceName, "create_vnic_details.0.display_name", "-tf-vnic-2"),
resource.TestCheckResourceAttrSet(s.VnicResourceName, "private_ip_address"),
// @SDK 1/2018: Since we don't assign a public IP to this vnic, we will get a response from server
// without a public_ip_address. Old SDK would have set it to empty, but new SDK will set it to nil.
// Commenting out until we have a better way of handling this.
//resource.TestCheckResourceAttr(s.VnicResourceName, "public_ip_address", ""),
resource.TestCheckNoResourceAttr(s.VnicResourceName, "public_ip_address"),
resource.TestCheckResourceAttr(s.ResourceName, "create_vnic_details.0.skip_source_dest_check", "true"),
func(ts *terraform.State) (err error) {
newId, err := fromInstanceState(ts, s.ResourceName, "id")
if newId != vaId {
return fmt.Errorf("Expected same ocid, got different.")
}
return err
},
),
},
{
// Create a new VNIC and VNIC Attachment with different options.
Config: s.Config + `
resource "oci_core_vnic_attachment" "va" {
instance_id = "${oci_core_instance.t.id}"
display_name = "-tf-va1"
create_vnic_details {
subnet_id = "${oci_core_subnet.t.id}"
display_name = "-tf-vnic"
assign_public_ip = true
private_ip = "10.0.1.20"
hostname_label = "myvnichostname"
skip_source_dest_check = true
}
}
data "oci_core_vnic" "v" {
vnic_id = "${oci_core_vnic_attachment.va.vnic_id}"
}
`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(s.ResourceName, "state", string(core.VnicAttachmentLifecycleStateAttached)),
resource.TestCheckResourceAttrSet(s.VnicResourceName, "id"),
resource.TestCheckResourceAttr(s.VnicResourceName, "private_ip_address", "10.0.1.20"),
resource.TestCheckResourceAttrSet(s.VnicResourceName, "public_ip_address"),
resource.TestMatchResourceAttr(s.VnicResourceName, "public_ip_address", regexp.MustCompile(`[0-9]+\.[0-9]+\.[0-9]+\.[0-9]`)),
resource.TestCheckResourceAttr(s.VnicResourceName, "hostname_label", "myvnichostname"),
resource.TestCheckResourceAttr(s.VnicResourceName, "skip_source_dest_check", "true"),
func(ts *terraform.State) (err error) {
newId, err := fromInstanceState(ts, s.ResourceName, "id")
if newId == vaId {
return fmt.Errorf("Expected new ocid, got the same.")
}
vaId = newId
return err
},
),
},
{
// Switching skip_source_dest_check and assign_public_ip from true to "true" will destroy and recreate, but should result in a
// VNIC with the same value.
ImportState: true,
ImportStateVerify: true,
Config: s.Config + `
resource "oci_core_vnic_attachment" "va" {
instance_id = "${oci_core_instance.t.id}"
display_name = "-tf-va1"
create_vnic_details {
subnet_id = "${oci_core_subnet.t.id}"
display_name = "-tf-vnic"
assign_public_ip = "true"
private_ip = "10.0.1.20"
hostname_label = "myvnichostname"
skip_source_dest_check = "true"
}
}
data "oci_core_vnic" "v" {
vnic_id = "${oci_core_vnic_attachment.va.vnic_id}"
}
`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(s.ResourceName, "state", string(core.VnicAttachmentLifecycleStateAttached)),
resource.TestCheckResourceAttr(s.VnicResourceName, "private_ip_address", "10.0.1.20"),
resource.TestCheckResourceAttr(s.VnicResourceName, "skip_source_dest_check", "true"),
func(ts *terraform.State) (err error) {
newId, err := fromInstanceState(ts, s.ResourceName, "id")
if newId != vaId {
return fmt.Errorf("Expected same ocid, got different.")
}
return err
},
),
},
},
})
}
func TestResourceCoreVnicAttachmentTestSuite(t *testing.T) {
suite.Run(t, new(ResourceCoreVnicAttachmentTestSuite))
}