From 958d18311f73fa763b7d223c4c76b4b524ad6f68 Mon Sep 17 00:00:00 2001 From: vshanthe Date: Fri, 7 Feb 2025 10:40:24 +0530 Subject: [PATCH] vpc_tests --- test/unit/fixtures/vpc_create.json | 21 +++ test/unit/fixtures/vpc_get.json | 9 ++ test/unit/fixtures/vpc_ips_list.json | 15 ++ test/unit/fixtures/vpc_list.json | 21 +++ test/unit/fixtures/vpc_specific_ips_list.json | 15 ++ test/unit/fixtures/vpc_subnet_create.json | 8 ++ test/unit/fixtures/vpc_subnet_get.json | 22 +++ test/unit/fixtures/vpc_subnet_update.json | 8 ++ test/unit/fixtures/vpc_subnets_list.json | 17 +++ test/unit/fixtures/vpc_update.json | 9 ++ test/unit/vpc_ips_test.go | 52 +++++++ test/unit/vpc_subnets_test.go | 106 +++++++++++++++ test/unit/vpc_test.go | 128 ++++++++++++++++++ 13 files changed, 431 insertions(+) create mode 100644 test/unit/fixtures/vpc_create.json create mode 100644 test/unit/fixtures/vpc_get.json create mode 100644 test/unit/fixtures/vpc_ips_list.json create mode 100644 test/unit/fixtures/vpc_list.json create mode 100644 test/unit/fixtures/vpc_specific_ips_list.json create mode 100644 test/unit/fixtures/vpc_subnet_create.json create mode 100644 test/unit/fixtures/vpc_subnet_get.json create mode 100644 test/unit/fixtures/vpc_subnet_update.json create mode 100644 test/unit/fixtures/vpc_subnets_list.json create mode 100644 test/unit/fixtures/vpc_update.json create mode 100644 test/unit/vpc_ips_test.go create mode 100644 test/unit/vpc_subnets_test.go create mode 100644 test/unit/vpc_test.go diff --git a/test/unit/fixtures/vpc_create.json b/test/unit/fixtures/vpc_create.json new file mode 100644 index 000000000..f66c19df5 --- /dev/null +++ b/test/unit/fixtures/vpc_create.json @@ -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" +} + \ No newline at end of file diff --git a/test/unit/fixtures/vpc_get.json b/test/unit/fixtures/vpc_get.json new file mode 100644 index 000000000..49ec79cd5 --- /dev/null +++ b/test/unit/fixtures/vpc_get.json @@ -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" +} \ No newline at end of file diff --git a/test/unit/fixtures/vpc_ips_list.json b/test/unit/fixtures/vpc_ips_list.json new file mode 100644 index 000000000..6eabb80fa --- /dev/null +++ b/test/unit/fixtures/vpc_ips_list.json @@ -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 +} diff --git a/test/unit/fixtures/vpc_list.json b/test/unit/fixtures/vpc_list.json new file mode 100644 index 000000000..549e47f96 --- /dev/null +++ b/test/unit/fixtures/vpc_list.json @@ -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 +} diff --git a/test/unit/fixtures/vpc_specific_ips_list.json b/test/unit/fixtures/vpc_specific_ips_list.json new file mode 100644 index 000000000..74e33d664 --- /dev/null +++ b/test/unit/fixtures/vpc_specific_ips_list.json @@ -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 +} diff --git a/test/unit/fixtures/vpc_subnet_create.json b/test/unit/fixtures/vpc_subnet_create.json new file mode 100644 index 000000000..ded5c83b1 --- /dev/null +++ b/test/unit/fixtures/vpc_subnet_create.json @@ -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" +} \ No newline at end of file diff --git a/test/unit/fixtures/vpc_subnet_get.json b/test/unit/fixtures/vpc_subnet_get.json new file mode 100644 index 000000000..e1dfdf0c9 --- /dev/null +++ b/test/unit/fixtures/vpc_subnet_get.json @@ -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" +} \ No newline at end of file diff --git a/test/unit/fixtures/vpc_subnet_update.json b/test/unit/fixtures/vpc_subnet_update.json new file mode 100644 index 000000000..9c766e345 --- /dev/null +++ b/test/unit/fixtures/vpc_subnet_update.json @@ -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" +} \ No newline at end of file diff --git a/test/unit/fixtures/vpc_subnets_list.json b/test/unit/fixtures/vpc_subnets_list.json new file mode 100644 index 000000000..b13895a53 --- /dev/null +++ b/test/unit/fixtures/vpc_subnets_list.json @@ -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 +} + \ No newline at end of file diff --git a/test/unit/fixtures/vpc_update.json b/test/unit/fixtures/vpc_update.json new file mode 100644 index 000000000..035e2f3f1 --- /dev/null +++ b/test/unit/fixtures/vpc_update.json @@ -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" +} \ No newline at end of file diff --git a/test/unit/vpc_ips_test.go b/test/unit/vpc_ips_test.go new file mode 100644 index 000000000..f4f7858e0 --- /dev/null +++ b/test/unit/vpc_ips_test.go @@ -0,0 +1,52 @@ +package unit + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/linode/linodego" +) + +func TestVPCListAllIPAddresses(t *testing.T) { + fixtureData, err := fixtures.GetFixture("vpc_ips_list") + assert.NoError(t, err) + + var base ClientBaseCase + base.SetUp(t) + defer base.TearDown(t) + + // Mock the GET response with fixture data + 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 TestVPCListSpecificIPAddresses(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) + + // Mock the GET response for a specific VPC + 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") +} diff --git a/test/unit/vpc_subnets_test.go b/test/unit/vpc_subnets_test.go new file mode 100644 index 000000000..091fde7f8 --- /dev/null +++ b/test/unit/vpc_subnets_test.go @@ -0,0 +1,106 @@ +package unit + +import ( + "context" + "testing" + + "github.com/linode/linodego" + "github.com/stretchr/testify/assert" +) + +func TestVPCCreateSubnet(t *testing.T) { + fixtureData, err := fixtures.GetFixture("vpc_subnet_create") + assert.NoError(t, err) + + var base ClientBaseCase + base.SetUp(t) + defer base.TearDown(t) + + // Mock the POST request with the fixture response + 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 TestVPCGetSubnet(t *testing.T) { + fixtureData, err := fixtures.GetFixture("vpc_subnet_get") + assert.NoError(t, err) + + var base ClientBaseCase + base.SetUp(t) + defer base.TearDown(t) + + // Mock the GET request with the fixture response + 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 TestVPCListSubnets(t *testing.T) { + fixtureData, err := fixtures.GetFixture("vpc_subnets_list") + assert.NoError(t, err) + + var base ClientBaseCase + base.SetUp(t) + defer base.TearDown(t) + + // Mock the GET request with the fixture response + 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 TestVPCUpdateSubnet(t *testing.T) { + fixtureData, err := fixtures.GetFixture("vpc_subnet_update") + assert.NoError(t, err) + + var base ClientBaseCase + base.SetUp(t) + defer base.TearDown(t) + + // Mock the PUT request with the fixture response + 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 TestVPCDeleteSubnet(t *testing.T) { + var base ClientBaseCase + base.SetUp(t) + defer base.TearDown(t) + + // Mock the DELETE request + 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") +} diff --git a/test/unit/vpc_test.go b/test/unit/vpc_test.go new file mode 100644 index 000000000..bbbc17c90 --- /dev/null +++ b/test/unit/vpc_test.go @@ -0,0 +1,128 @@ +package unit + +import ( + "context" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/linode/linodego" +) + +func TestVPCCreate(t *testing.T) { + var base ClientBaseCase + base.SetUp(t) + defer base.TearDown(t) + + // Mock API response + base.MockPost("vpcs", linodego.VPC{ + ID: 123, + Label: "test-vpc", + Description: "Test VPC description", + Region: "us-east", + Subnets: []linodego.VPCSubnet{ + {ID: 1, Label: "subnet-1"}, + {ID: 2, Label: "subnet-2"}, + }, + Created: linodego.Pointer(time.Now()), + Updated: linodego.Pointer(time.Now()), + }) + + vpc, err := base.Client.CreateVPC(context.Background(), linodego.VPCCreateOptions{ + Label: "test-vpc", + Description: "Test VPC description", + Region: "us-east", + Subnets: []linodego.VPCSubnetCreateOptions{ + {Label: "subnet-1"}, + {Label: "subnet-2"}, + }, + }) + assert.NoError(t, err, "Expected no error when creating VPC") + assert.NotNil(t, vpc, "Expected VPC to be created") + assert.Equal(t, "test-vpc", vpc.Label, "Expected VPC label to match") + assert.Equal(t, "us-east", vpc.Region, "Expected VPC region to match") + assert.Len(t, vpc.Subnets, 2, "Expected VPC to have 2 subnets") +} + +func TestVPCGet(t *testing.T) { + var base ClientBaseCase + base.SetUp(t) + defer base.TearDown(t) + + // Mock API response + mockVPC := linodego.VPC{ + ID: 123, + Label: "test-vpc", + } + base.MockGet("vpcs/123", mockVPC) + + vpc, err := base.Client.GetVPC(context.Background(), 123) + assert.NoError(t, err, "Expected no error when getting VPC") + assert.NotNil(t, vpc, "Expected non-nil VPC") + assert.Equal(t, mockVPC.ID, vpc.ID, "Expected VPC ID to match") + assert.Equal(t, mockVPC.Label, vpc.Label, "Expected VPC label to match") +} + +func TestVPCList(t *testing.T) { + fixtureData, err := fixtures.GetFixture("vpc_list") + assert.NoError(t, err) + + var base ClientBaseCase + base.SetUp(t) + defer base.TearDown(t) + + // Mock the GET response with fixture data + base.MockGet("vpcs", fixtureData) + + vpcs, err := base.Client.ListVPCs(context.Background(), &linodego.ListOptions{}) + assert.NoError(t, err) + + assert.NotEmpty(t, vpcs, "Expected non-empty VPC list") + + assert.Equal(t, 123, vpcs[0].ID, "Expected VPC ID to match") + assert.Equal(t, "test-vpc", vpcs[0].Label, "Expected VPC label to match") + assert.Equal(t, "Test VPC description", vpcs[0].Description, "Expected VPC description to match") + assert.Equal(t, "us-east", vpcs[0].Region, "Expected VPC region to match") + assert.NotEmpty(t, vpcs[0].Subnets, "Expected VPC to have subnets") + assert.Equal(t, 456, vpcs[0].Subnets[0].ID, "Expected subnet ID to match") + assert.Equal(t, "subnet-1", vpcs[0].Subnets[0].Label, "Expected subnet label to match") + assert.Equal(t, "192.168.1.0/24", vpcs[0].Subnets[0].IPv4, "Expected subnet IPv4 to match") +} + + +func TestVPCUpdate(t *testing.T) { + var base ClientBaseCase + base.SetUp(t) + defer base.TearDown(t) + + // Mock API response + updatedMockVPC := linodego.VPC{ + ID: 123, + Label: "updated-vpc", + Description: "Updated description", + } + base.MockPut("vpcs/123", updatedMockVPC) + + opts := linodego.VPCUpdateOptions{ + Label: "updated-vpc", + Description: "Updated description", + } + + vpc, err := base.Client.UpdateVPC(context.Background(), 123, opts) + assert.NoError(t, err, "Expected no error when updating VPC") + assert.NotNil(t, vpc, "Expected non-nil updated VPC") + assert.Equal(t, "updated-vpc", vpc.Label, "Expected VPC label to match") + assert.Equal(t, "Updated description", vpc.Description, "Expected VPC description to match") +} + +func TestVPCDelete(t *testing.T) { + var base ClientBaseCase + base.SetUp(t) + defer base.TearDown(t) + + // Mock API response + base.MockDelete("vpcs/123", nil) + + err := base.Client.DeleteVPC(context.Background(), 123) + assert.NoError(t, err, "Expected no error when deleting VPC") +} \ No newline at end of file