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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
|
package integration
import (
"context"
"fmt"
"strings"
"testing"
"github.com/linode/linodego"
. "github.com/linode/linodego"
)
const (
TestSubnet = "192.168.0.0/25"
)
func formatVPCSubnetError(err error, action string, vpcID, vpcSubnetID *int) error {
if err == nil {
return nil
}
vpcMsg := ""
if vpcID != nil {
vpcMsg = fmt.Sprintf(" in VPC %v", *vpcID)
}
if vpcSubnetID == nil {
return fmt.Errorf(
"an error occurs when %v the subnet(s)%v: %v",
action,
vpcMsg,
err,
)
}
return fmt.Errorf(
"an error occurs when %v the subnet %v%v: %v",
action,
*vpcSubnetID,
vpcMsg,
err,
)
}
func vpcSubnetCheck(vpcSubnet *linodego.VPCSubnet, t *testing.T) {
if vpcSubnet.ID == 0 {
t.Error("expected a VPC subnet ID, but got 0")
}
assertDateSet(t, vpcSubnet.Created)
assertDateSet(t, vpcSubnet.Updated)
}
func vpcSubnetCreateOptionsCheck(
opts *linodego.VPCSubnetCreateOptions,
vpcSubnet *linodego.VPCSubnet,
t *testing.T,
) {
if !(opts.IPv4 == vpcSubnet.IPv4 && opts.Label == vpcSubnet.Label) {
t.Error(
"the VPC subnet instance and the VPC subnet " +
"create options instance are mismatched",
)
}
}
func vpcSubnetUpdateOptionsCheck(
opts *linodego.VPCSubnetUpdateOptions,
vpcSubnet *linodego.VPCSubnet,
t *testing.T,
) {
if !(opts.Label == vpcSubnet.Label) {
t.Error(
"the VPC subnet instance and the VPC subnet " +
"update options instance are mismatched",
)
}
}
func createVPCWithSubnet(t *testing.T, client *linodego.Client, vpcModifier ...vpcModifier) (
*linodego.VPC,
*linodego.VPCSubnet,
func(),
error,
) {
t.Helper()
createOpts := linodego.VPCCreateOptions{
Label: "go-test-vpc-" + getUniqueText(),
Region: getRegionsWithCaps(t, client, []string{"Linodes", "VPCs"})[0],
Subnets: []VPCSubnetCreateOptions{
{
Label: "linodego-vpc-test-" + getUniqueText(),
IPv4: TestSubnet,
},
},
}
for _, mod := range vpcModifier {
mod(client, &createOpts)
}
vpc, err := client.CreateVPC(context.Background(), createOpts)
if err != nil {
t.Fatal(formatVPCError(err, "creating", nil))
}
teardown := func() {
if err := client.DeleteVPC(context.Background(), vpc.ID); err != nil {
t.Error(formatVPCError(err, "deleting", &vpc.ID))
}
}
return vpc, &vpc.Subnets[0], teardown, err
}
func createSubnetInVPC(
t *testing.T,
client *linodego.Client,
vpc linodego.VPC,
vpcModifier ...vpcModifier,
) (
*linodego.VPCSubnet,
func(),
error,
) {
t.Helper()
createOpts := linodego.VPCSubnetCreateOptions{
Label: "linodego-vpc-test-" + getUniqueText(),
IPv4: TestSubnet,
}
vpcSubnet, err := client.CreateVPCSubnet(context.Background(), createOpts, vpc.ID)
if err != nil {
t.Fatal(formatVPCSubnetError(err, "creating", &vpc.ID, nil))
}
teardown := func() {
err = client.DeleteVPCSubnet(context.Background(), vpc.ID, vpcSubnet.ID)
if err != nil {
t.Error(formatVPCSubnetError(err, "deleting", &vpc.ID, &vpcSubnet.ID))
}
}
return vpcSubnet, teardown, err
}
func setupVPCWithSubnet(
t *testing.T,
fixturesYaml string,
) (
*linodego.Client,
*linodego.VPC,
*linodego.VPCSubnet,
func(),
error,
) {
t.Helper()
client, fixtureTeardown := createTestClient(t, fixturesYaml)
vpc, vpcSubnet, vpcSubnetTeardown, err := createVPCWithSubnet(t, client)
if err != nil {
if vpcSubnetTeardown != nil {
vpcSubnetTeardown()
}
fixtureTeardown()
t.Fatal(err)
}
teardown := func() {
vpcSubnetTeardown()
fixtureTeardown()
}
return client, vpc, vpcSubnet, teardown, err
}
func TestVPC_Subnet_Create(t *testing.T) {
_, _, vpcSubnet, teardown, err := setupVPCWithSubnet(t, "fixtures/TestVPC_Subnet_Create")
defer teardown()
if err != nil {
t.Error(formatVPCSubnetError(err, "setting up", nil, nil))
}
vpcSubnetCheck(vpcSubnet, t)
opts := vpcSubnet.GetCreateOptions()
vpcSubnetCreateOptionsCheck(&opts, vpcSubnet, t)
}
func TestVPC_Subnet_Update(t *testing.T) {
client, vpc, vpcSubnet, teardown, err := setupVPCWithSubnet(t, "fixtures/TestVPC_Subnet_Update")
defer teardown()
if err != nil {
t.Error(formatVPCSubnetError(err, "setting up", nil, nil))
}
vpcSubnetCheck(vpcSubnet, t)
opts := vpcSubnet.GetUpdateOptions()
vpcSubnetUpdateOptionsCheck(&opts, vpcSubnet, t)
updatedVPCSubnet, err := client.UpdateVPCSubnet(
context.Background(),
vpc.ID,
vpcSubnet.ID,
opts,
)
if err != nil {
t.Error(formatVPCSubnetError(err, "updating", &vpc.ID, &vpcSubnet.ID))
}
vpcSubnetUpdateOptionsCheck(&opts, updatedVPCSubnet, t)
}
func TestVPC_Subnet_List(t *testing.T) {
client, vpc, vpcSubnet, teardown, err := setupVPCWithSubnet(t, "fixtures/TestVPC_Subnet_List")
defer teardown()
if err != nil {
t.Error(formatVPCSubnetError(err, "setting up", nil, nil))
}
vpcSubnetCheck(vpcSubnet, t)
opts := vpcSubnet.GetCreateOptions()
vpcSubnetCreateOptionsCheck(&opts, vpcSubnet, t)
vpcSubnets, err := client.ListVPCSubnets(context.Background(), vpc.ID, nil)
found := false
for _, v := range vpcSubnets {
if v.ID == vpcSubnet.ID {
found = true
}
}
if !found {
t.Errorf("the VPC %v subnet %v not found in list", vpc.ID, vpcSubnet.ID)
}
}
func TestVPC_Subnet_Create_Invalid_data(t *testing.T) {
client, vpc, teardown, err := setupVPC(t, "fixtures/TestVPC_Subnet_Create_Invalid_data")
defer teardown()
if err != nil {
t.Error(formatVPCSubnetError(err, "setting up", nil, nil))
}
createOpts := linodego.VPCSubnetCreateOptions{
Label: "linodego-vpc-test_invalid_label" + getUniqueText(),
IPv4: TestSubnet,
}
_, err = client.CreateVPCSubnet(context.Background(), createOpts, vpc.ID)
e, _ := err.(*Error)
if e.Code != 400 {
t.Errorf("should have received a 400 Code with invalid label, got %v", e.Code)
}
expectedErrorMessage := "Label must include only ASCII letters, numbers, and dashes"
if !strings.Contains(e.Message, expectedErrorMessage) {
t.Errorf("Wrong error message displayed should have contained, %s", expectedErrorMessage)
}
}
func TestVPC_Subnet_Update_Invalid_data(t *testing.T) {
client, vpc, vpcSubnet, teardown, err := setupVPCWithSubnet(t, "fixtures/TestVPC_Subnet_Update_Invalid_Label")
defer teardown()
if err != nil {
t.Error(formatVPCSubnetError(err, "setting up", nil, nil))
}
vpcSubnetCheck(vpcSubnet, t)
opts := vpcSubnet.GetUpdateOptions()
vpcSubnetUpdateOptionsCheck(&opts, vpcSubnet, t)
opts.Label = "invalid_label"
_, err = client.UpdateVPCSubnet(
context.Background(),
vpc.ID,
vpcSubnet.ID,
opts,
)
e, _ := err.(*Error)
if e.Code != 400 {
t.Errorf("should have received a 400 Code with invalid label, got %v", e.Code)
}
expectedErrorMessage := "Label must include only ASCII letters, numbers, and dashes"
if !strings.Contains(e.Message, expectedErrorMessage) {
t.Errorf("Wrong error message displayed should have contained, %s", expectedErrorMessage)
}
}
func TestVPC_Subnet_WithInstance(t *testing.T) {
client, vpc, vpcSubnet, inst, config, teardown := setupInstanceWith3Interfaces(t, "fixtures/TestVPC_Subnet_WithInstance")
defer teardown()
// Refresh the subnet to show the assigned instance/interface
refreshedSubnet, err := client.GetVPCSubnet(context.Background(), vpc.ID, vpcSubnet.ID)
if err != nil {
t.Fatal(err)
}
if len(refreshedSubnet.Linodes) != 1 {
t.Fatalf("expected 1 assigned linode, got %d", len(refreshedSubnet.Linodes))
}
targetLinode := refreshedSubnet.Linodes[0]
if targetLinode.ID != inst.ID {
t.Fatalf("expected assigned instance to have id %d, got %d", inst.ID, targetLinode.ID)
}
if len(targetLinode.Interfaces) != 1 {
t.Fatalf("expected 1 assigned interface, got %d", len(targetLinode.Interfaces))
}
targetInterface := targetLinode.Interfaces[0]
if targetInterface.ID != config.Interfaces[2].ID {
t.Fatalf("interface ID mismatch, expected %d for %d", config.Interfaces[2].ID, targetInterface.ID)
}
// Ensure the NAT 1:1 information is reflected in the IP configuration of this instance
networking, err := client.GetInstanceIPAddresses(context.Background(), inst.ID)
if err != nil {
t.Fatal(err)
}
nat1To1 := networking.IPv4.Public[0].VPCNAT1To1
if nat1To1 == nil {
t.Fatalf("expected VPCNAT1To1 to contain data, got nil")
}
if nat1To1.SubnetID != refreshedSubnet.ID {
t.Fatal("IP/subnet id mismatch")
}
if nat1To1.VPCID != vpc.ID {
t.Fatal("IP/VPC id mismatch")
}
if nat1To1.Address != config.Interfaces[2].IPv4.VPC {
t.Fatalf("nat_1_1 subnet IP mismatch")
}
}
|