Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test coverage for Network related methods/functions #670

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions test/unit/fixtures/network_ip_address_get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"address": "192.168.1.1",
"linode_id": 12345,
"reserved": false
}
16 changes: 16 additions & 0 deletions test/unit/fixtures/network_ip_addresses_list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"data": [
{
"address": "192.168.1.1",
"linode_id": 12345,
"reserved": false
},
{
"address": "192.168.1.2",
"linode_id": 67890,
"reserved": true
}
],
"pages": 1,
"results": 2
}
8 changes: 8 additions & 0 deletions test/unit/fixtures/network_ipv6_pools_get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"range": "2600:3c00::/64",
"region": "us-east",
"prefix": 64,
"route_target": "2600:3c00::1",
"is_bgp": false,
"linodes": [54321]
}
15 changes: 15 additions & 0 deletions test/unit/fixtures/network_ipv6_pools_list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"data": [
{
"range": "2600:3c00::/64",
"region": "us-east",
"prefix": 64,
"route_target": "2600:3c00::1",
"is_bgp": true,
"linodes": [12345, 67890]
}
],
"pages": 1,
"page": 1,
"results": 1
}
8 changes: 8 additions & 0 deletions test/unit/fixtures/network_ipv6_ranges_create.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"range": "2600:3c00::/64",
"region": "us-east",
"prefix": 64,
"route_target": "2600:3c00::1",
"is_bgp": false,
"linodes": [12345]
}
8 changes: 8 additions & 0 deletions test/unit/fixtures/network_ipv6_ranges_get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"range": "2600:3c00::/64",
"region": "us-east",
"prefix": 64,
"route_target": "2600:3c00::1",
"is_bgp": false,
"linodes": [54321]
}
15 changes: 15 additions & 0 deletions test/unit/fixtures/network_ipv6_ranges_list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"data": [
{
"range": "2600:3c00::/64",
"region": "us-east",
"prefix": 64,
"route_target": "2600:3c00::1",
"is_bgp": true,
"linodes": [12345, 67890]
}
],
"pages": 1,
"results": 1
}

9 changes: 9 additions & 0 deletions test/unit/fixtures/network_reserved_ips.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"address": "192.168.1.30",
"region": "us-west",
"linode_id": 13579,
"label": "test-ip-3",
"created": "2025-02-03T12:00:00",
"status": "reserved"
}

9 changes: 9 additions & 0 deletions test/unit/fixtures/network_reserved_ips_get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"address": "192.168.1.10",
"region": "us-east",
"linode_id": 12345,
"label": "test-ip-1",
"created": "2025-02-03T12:00:00",
"status": "reserved"
}

23 changes: 23 additions & 0 deletions test/unit/fixtures/network_reserved_ips_list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"data": [
{
"address": "192.168.1.10",
"region": "us-east",
"linode_id": 12345,
"label": "test-ip-1",
"created": "2025-02-03T12:00:00",
"status": "reserved"
},
{
"address": "192.168.1.20",
"region": "us-west",
"linode_id": 67890,
"label": "test-ip-2",
"created": "2025-02-03T12:00:00",
"status": "reserved"
}
],
"pages": 1,
"results": 2
}

88 changes: 88 additions & 0 deletions test/unit/network_ips_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package unit

import (
"context"
"testing"

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

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

ip := "192.168.1.1"

// Mock API response
base.MockPut("networking/ips/"+ip, linodego.InstanceIP{
Address: ip,
Reserved: true,
})

updatedIP, err := base.Client.UpdateIPAddressV2(context.Background(), ip, linodego.IPAddressUpdateOptionsV2{
Reserved: linodego.Pointer(true),
})
assert.NoError(t, err, "Expected no error when updating IP address")
assert.NotNil(t, updatedIP, "Expected non-nil updated IP address")
assert.Equal(t, ip, updatedIP.Address, "Expected updated IP address to match")
assert.True(t, updatedIP.Reserved, "Expected Reserved to be true")
}

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

// Mock API response
base.MockPost("networking/ips", linodego.InstanceIP{
Address: "192.168.1.3",
Region: "us-east",
Public: true,
})

ip, err := base.Client.AllocateReserveIP(context.Background(), linodego.AllocateReserveIPOptions{
Type: "ipv4",
Public: true,
Region: "us-east",
LinodeID: 12345,
})
assert.NoError(t, err, "Expected no error when allocating reserve IP")
assert.NotNil(t, ip, "Expected non-nil allocated IP")
assert.Equal(t, "192.168.1.3", ip.Address, "Expected allocated IP address to match")
assert.Equal(t, "us-east", ip.Region, "Expected Region to match")
assert.True(t, ip.Public, "Expected Public to be true")
}

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

// Mock API response
base.MockPost("networking/ips/assign", nil)

err := base.Client.InstancesAssignIPs(context.Background(), linodego.LinodesAssignIPsOptions{
Region: "us-east",
Assignments: []linodego.LinodeIPAssignment{
{Address: "192.168.1.10", LinodeID: 123},
},
})
assert.NoError(t, err, "Expected no error when assigning IPs to instances")
}

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

// Mock API response
base.MockPost("networking/ips/share", nil)

err := base.Client.ShareIPAddresses(context.Background(), linodego.IPAddressesShareOptions{
IPs: []string{"192.168.1.20"},
LinodeID: 456,
})
assert.NoError(t, err, "Expected no error when sharing IP addresses")
}
71 changes: 71 additions & 0 deletions test/unit/network_ipv6_pools_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package unit

import (
"context"
"testing"

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

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

mockResponse := struct {
Data []linodego.IPv6Range `json:"data"`
}{
Data: []linodego.IPv6Range{
{
Range: "2600:3c00::/64",
Region: "us-east",
Prefix: 64,
RouteTarget: "2600:3c00::1",
IsBGP: true,
Linodes: []int{12345, 67890},
},
},
}

base.MockGet("networking/ipv6/pools", mockResponse)

pools, err := base.Client.ListIPv6Pools(context.Background(), nil)

assert.NoError(t, err, "Expected no error when listing IPv6 pools")
assert.NotNil(t, pools, "Expected non-nil IPv6 pools response")
assert.Len(t, pools, 1, "Expected one IPv6 pool in response")
assert.Equal(t, "2600:3c00::/64", pools[0].Range, "Expected matching IPv6 range")
assert.Equal(t, "us-east", pools[0].Region, "Expected matching region")
assert.Equal(t, 64, pools[0].Prefix, "Expected matching prefix length")
assert.Equal(t, "2600:3c00::1", pools[0].RouteTarget, "Expected matching route target")
assert.True(t, pools[0].IsBGP, "Expected IsBGP to be true")
assert.ElementsMatch(t, []int{12345, 67890}, pools[0].Linodes, "Expected matching Linodes list")
}

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

id := "1"
mockResponse := linodego.IPv6Range{
Range: "2600:3c00::/64",
Region: "us-east",
Prefix: 64,
RouteTarget: "2600:3c00::1",
IsBGP: false,
Linodes: []int{54321},
}
base.MockGet("networking/ipv6/pools/"+id, mockResponse)

pool, err := base.Client.GetIPv6Pool(context.Background(), id)
assert.NoError(t, err, "Expected no error when getting IPv6 pool")
assert.NotNil(t, pool, "Expected non-nil IPv6 pool response")
assert.Equal(t, "2600:3c00::/64", pool.Range, "Expected matching IPv6 range")
assert.Equal(t, "us-east", pool.Region, "Expected matching region")
assert.Equal(t, 64, pool.Prefix, "Expected matching prefix length")
assert.Equal(t, "2600:3c00::1", pool.RouteTarget, "Expected matching route target")
assert.False(t, pool.IsBGP, "Expected IsBGP to be false")
assert.ElementsMatch(t, []int{54321}, pool.Linodes, "Expected matching Linodes list")
}
Loading