Skip to content

Commit

Permalink
Add unit test coverage for VPC related methods/functions (#673)
Browse files Browse the repository at this point in the history
* vpc_tests

* update_testname

* fix

* fix_format
  • Loading branch information
vshanthe authored Feb 19, 2025
1 parent 33c0c1d commit 71bdfa1
Show file tree
Hide file tree
Showing 13 changed files with 418 additions and 0 deletions.
21 changes: 21 additions & 0 deletions test/unit/fixtures/vpc_create.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"id": 123,
"label": "test-vpc",
"description": "Test VPC description",
"region": "us-east",
"subnets": [
{
"id": 1,
"label": "subnet-1",
"region": "us-east"
},
{
"id": 2,
"label": "subnet-2",
"region": "us-east"
}
],
"created": "2023-01-01T12:00:00",
"updated": "2023-01-02T12:00:00"
}

9 changes: 9 additions & 0 deletions test/unit/fixtures/vpc_get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"id": 123,
"label": "test-vpc",
"description": "Test VPC description",
"region": "us-east",
"subnets": [],
"created": "2023-01-01T12:00:00",
"updated": "2023-01-02T12:00:00"
}
15 changes: 15 additions & 0 deletions test/unit/fixtures/vpc_ips_list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"data": [
{
"address": "192.168.1.10",
"vpc_id": 123
},
{
"address": "192.168.1.11",
"vpc_id": 124
}
],
"page": 1,
"pages": 1,
"results": 2
}
21 changes: 21 additions & 0 deletions test/unit/fixtures/vpc_list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"data": [
{
"id": 123,
"label": "test-vpc",
"description": "Test VPC description",
"region": "us-east",
"subnets": [
{
"id": 456,
"label": "subnet-1",
"ipv4": "192.168.1.0/24",
"linodes": []
}
]
}
],
"page": 1,
"pages": 1,
"results": 1
}
15 changes: 15 additions & 0 deletions test/unit/fixtures/vpc_specific_ips_list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"data": [
{
"address": "192.168.1.20",
"vpc_id": 123
},
{
"address": "192.168.1.21",
"vpc_id": 123
}
],
"page": 1,
"pages": 1,
"results": 2
}
8 changes: 8 additions & 0 deletions test/unit/fixtures/vpc_subnet_create.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"id": 789,
"label": "Test Subnet",
"ipv4": "192.168.1.0/24",
"linodes": [],
"created": "2025-01-01T12:00:00",
"updated": "2025-01-01T12:00:00"
}
22 changes: 22 additions & 0 deletions test/unit/fixtures/vpc_subnet_get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"id": 456,
"label": "Existing Subnet",
"ipv4": "192.168.2.0/24",
"linodes": [
{
"id": 101,
"interfaces": [
{
"id": 1,
"active": true
},
{
"id": 2,
"active": false
}
]
}
],
"created": "2025-01-01T10:00:00",
"updated": "2025-01-02T10:00:00"
}
8 changes: 8 additions & 0 deletions test/unit/fixtures/vpc_subnet_update.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"id": 456,
"label": "Updated Subnet",
"ipv4": "192.168.2.0/24",
"linodes": [],
"created": "2025-01-01T10:00:00",
"updated": "2025-02-01T10:00:00"
}
17 changes: 17 additions & 0 deletions test/unit/fixtures/vpc_subnets_list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"data": [
{
"id": 123,
"label": "Subnet A",
"ipv4": "192.168.3.0/24"
},
{
"id": 124,
"label": "Subnet B",
"ipv4": "192.168.4.0/24"
}
],
"page": 1,
"pages": 1
}

9 changes: 9 additions & 0 deletions test/unit/fixtures/vpc_update.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"id": 123,
"label": "updated-vpc",
"description": "Updated description",
"region": "us-east",
"subnets": [],
"created": "2023-01-01T12:00:00",
"updated": "2023-01-02T12:00:00"
}
50 changes: 50 additions & 0 deletions test/unit/vpc_ips_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package unit

import (
"context"
"testing"

"github.com/linode/linodego"
"github.com/stretchr/testify/assert"
)

func TestVPCAllIPAddresses_List(t *testing.T) {
fixtureData, err := fixtures.GetFixture("vpc_ips_list")
assert.NoError(t, err)

var base ClientBaseCase
base.SetUp(t)
defer base.TearDown(t)

base.MockGet("vpcs/ips", fixtureData)

vpcIPs, err := base.Client.ListAllVPCIPAddresses(context.Background(), &linodego.ListOptions{})
assert.NoError(t, err)

assert.NotEmpty(t, vpcIPs, "Expected non-empty VPC IP addresses list")

assert.NotNil(t, vpcIPs[0].Address, "Expected IP address to be non-nil")
assert.Equal(t, "192.168.1.10", *vpcIPs[0].Address, "Expected IP address to match")
assert.Equal(t, 123, vpcIPs[0].VPCID, "Expected VPC ID to match")
}

func TestVPCSpecificIPAddresses_List(t *testing.T) {
fixtureData, err := fixtures.GetFixture("vpc_specific_ips_list")
assert.NoError(t, err)

var base ClientBaseCase
base.SetUp(t)
defer base.TearDown(t)

vpcID := 123
base.MockGet("vpcs/123/ips", fixtureData)

vpcIPs, err := base.Client.ListVPCIPAddresses(context.Background(), vpcID, &linodego.ListOptions{})
assert.NoError(t, err)

assert.NotEmpty(t, vpcIPs, "Expected non-empty VPC IP addresses list for the specified VPC")

assert.NotNil(t, vpcIPs[0].Address, "Expected IP address to be non-nil")
assert.Equal(t, "192.168.1.20", *vpcIPs[0].Address, "Expected IP address to match")
assert.Equal(t, vpcID, vpcIPs[0].VPCID, "Expected VPC ID to match")
}
101 changes: 101 additions & 0 deletions test/unit/vpc_subnets_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package unit

import (
"context"
"testing"

"github.com/linode/linodego"
"github.com/stretchr/testify/assert"
)

func TestVPCSubnet_Create(t *testing.T) {
fixtureData, err := fixtures.GetFixture("vpc_subnet_create")
assert.NoError(t, err)

var base ClientBaseCase
base.SetUp(t)
defer base.TearDown(t)

base.MockPost("vpcs/123/subnets", fixtureData)

subnetCreateOpts := linodego.VPCSubnetCreateOptions{
Label: "Test Subnet",
IPv4: "192.168.1.0/24",
}
subnet, err := base.Client.CreateVPCSubnet(context.Background(), subnetCreateOpts, 123)
assert.NoError(t, err)

assert.Equal(t, 789, subnet.ID, "Expected subnet ID to match")
assert.Equal(t, "Test Subnet", subnet.Label, "Expected subnet label to match")
assert.Equal(t, "192.168.1.0/24", subnet.IPv4, "Expected subnet IPv4 to match")
}

func TestVPCSubnet_Get(t *testing.T) {
fixtureData, err := fixtures.GetFixture("vpc_subnet_get")
assert.NoError(t, err)

var base ClientBaseCase
base.SetUp(t)
defer base.TearDown(t)

base.MockGet("vpcs/123/subnets/456", fixtureData)

subnet, err := base.Client.GetVPCSubnet(context.Background(), 123, 456)
assert.NoError(t, err)

assert.Equal(t, 456, subnet.ID, "Expected subnet ID to match")
assert.Equal(t, "Existing Subnet", subnet.Label, "Expected subnet label to match")
assert.Equal(t, "192.168.2.0/24", subnet.IPv4, "Expected subnet IPv4 to match")
assert.Equal(t, 101, subnet.Linodes[0].ID, "Expected Linode ID to match")
assert.True(t, subnet.Linodes[0].Interfaces[0].Active, "Expected interface to be active")
}

func TestVPCSubnets_List(t *testing.T) {
fixtureData, err := fixtures.GetFixture("vpc_subnets_list")
assert.NoError(t, err)

var base ClientBaseCase
base.SetUp(t)
defer base.TearDown(t)

base.MockGet("vpcs/123/subnets", fixtureData)

subnets, err := base.Client.ListVPCSubnets(context.Background(), 123, &linodego.ListOptions{})
assert.NoError(t, err, "Expected no error when listing subnets")
assert.Len(t, subnets, 2, "Expected two subnets in the list")

assert.Equal(t, 123, subnets[0].ID, "Expected first subnet ID to match")
assert.Equal(t, "Subnet A", subnets[0].Label, "Expected first subnet label to match")
assert.Equal(t, "192.168.3.0/24", subnets[0].IPv4, "Expected first subnet IPv4 to match")
}

func TestVPCSubnet_Update(t *testing.T) {
fixtureData, err := fixtures.GetFixture("vpc_subnet_update")
assert.NoError(t, err)

var base ClientBaseCase
base.SetUp(t)
defer base.TearDown(t)

base.MockPut("vpcs/123/subnets/456", fixtureData)

subnetUpdateOpts := linodego.VPCSubnetUpdateOptions{
Label: "Updated Subnet",
}
subnet, err := base.Client.UpdateVPCSubnet(context.Background(), 123, 456, subnetUpdateOpts)
assert.NoError(t, err)

assert.Equal(t, 456, subnet.ID, "Expected subnet ID to match")
assert.Equal(t, "Updated Subnet", subnet.Label, "Expected subnet label to match")
}

func TestVPCSubnet_Delete(t *testing.T) {
var base ClientBaseCase
base.SetUp(t)
defer base.TearDown(t)

base.MockDelete("vpcs/123/subnets/456", nil)

err := base.Client.DeleteVPCSubnet(context.Background(), 123, 456)
assert.NoError(t, err, "Expected no error when deleting VPCSubnet")
}
Loading

0 comments on commit 71bdfa1

Please sign in to comment.