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 Type, Vlan and Tag related methods/functions #647

Merged
merged 9 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
26 changes: 26 additions & 0 deletions test/unit/fixtures/linode_type_get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"id": "g6-standard-2",
"disk": 4000,
"class": "standard",
"price": {
"hourly": 0.015,
"monthly": 10
},
"label": "Linode 2GB",
"addons": {
"backups": {
"price": {
"hourly": 0.003,
"monthly": 2
},
"region_prices": []
}
},
"region_prices": [],
"network_out": 2000,
"memory": 4000,
"transfer": 500,
"vcpus": 2,
"gpus": 0,
"successor": null
}
44 changes: 44 additions & 0 deletions test/unit/fixtures/linode_types_list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"data": [
{
"id": "g6-nanode-1",
"disk": 25600,
"class": "nanode",
"price": {
"hourly": 0.0075,
"monthly": 5
},
"label": "Nanode 1GB",
"addons": null,
"region_prices": [],
"network_out": 1000,
"memory": 1024,
"transfer": 250,
"vcpus": 1,
"gpus": 0,
"successor": null
},
{
"id": "g6-standard-2",
"disk": 51200,
"class": "standard",
"price": {
"hourly": 0.015,
"monthly": 10
},
"label": "Linode 2GB",
"addons": null,
"region_prices": [],
"network_out": 2000,
"memory": 2048,
"transfer": 500,
"vcpus": 2,
"gpus": 0,
"successor": null
}
],
"page": 1,
"pages": 1,
"results": 2
}

15 changes: 15 additions & 0 deletions test/unit/fixtures/vlan_get_ipam_address.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"data": [
{
"interfaces": [
{
"label": "test-vlan",
"ipam_address": "10.0.0.1/24"
}
]
}
],
"page": 1,
"pages": 1,
"results": 1
}
13 changes: 13 additions & 0 deletions test/unit/fixtures/vlans_list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"data": [
{
"label": "test-vlan",
"linodes": [12345],
"region": "us-east",
"created": "2024-12-01T12:00:00"
}
],
"page": 1,
"pages": 1,
"results": 1
}
76 changes: 76 additions & 0 deletions test/unit/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package unit

import (
"context"
"fmt"
"github.com/linode/linodego"
"github.com/stretchr/testify/assert"
"testing"
)

func TestLinodeTypes_List(t *testing.T) {
// Load the fixture data for types
fixtureData, err := fixtures.GetFixture("linode_types_list")
assert.NoError(t, err)

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

base.MockGet("linode/types", fixtureData)

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

// Verify a specific type exists in the list
var nanodeType *linodego.LinodeType
for _, t := range types {
if t.ID == "g6-nanode-1" {
nanodeType = &t
break
}
}
vshanthe marked this conversation as resolved.
Show resolved Hide resolved

if nanodeType == nil {
t.Errorf("Expected type 'g6-nanode-1' to be in the response, but it was not found")
} else {
assert.Equal(t, "nanode", string(nanodeType.Class), "Expected class to be 'nanode'")
assert.Equal(t, 1, nanodeType.VCPUs, "Expected VCPUs for 'g6-nanode-1' to be 1")
assert.Equal(t, 250, nanodeType.Transfer, "Expected transfer for 'g6-nanode-1' to be 250GB")
assert.NotNil(t, nanodeType.Price, "Expected 'g6-nanode-1' to have a price object")
if nanodeType.Price != nil {
assert.Equal(t, float32(5), nanodeType.Price.Monthly, "Expected monthly price for 'g6-nanode-1' to be $5")
}
}
}

func TestLinodeType_Get(t *testing.T) {
// Load the fixture data for a specific type
fixtureData, err := fixtures.GetFixture("linode_type_get")
assert.NoError(t, err)

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

typeID := "g6-standard-2"
base.MockGet(fmt.Sprintf("linode/types/%s", typeID), fixtureData)

typeObj, err := base.Client.GetType(context.Background(), typeID)
assert.NoError(t, err)

assert.Equal(t, typeID, typeObj.ID, "Expected type ID to match")
assert.Equal(t, "standard", string(typeObj.Class), "Expected class to be 'standard'")
assert.Equal(t, 2, typeObj.VCPUs, "Expected VCPUs to be 2")
assert.Equal(t, 4000, typeObj.Disk, "Expected disk to be 4000MB")
assert.Equal(t, 4000, typeObj.Memory, "Expected memory to be 4000MB")
assert.NotNil(t, typeObj.Price, "Expected type to have a price object")
if typeObj.Price != nil {
assert.Equal(t, float32(10), typeObj.Price.Monthly, "Expected monthly price to be $10")
}

assert.NotNil(t, typeObj.Addons, "Expected type to have addons")
if typeObj.Addons != nil && typeObj.Addons.Backups != nil {
assert.NotNil(t, typeObj.Addons.Backups.Price, "Expected backups to have a price object")
}
}
65 changes: 65 additions & 0 deletions test/unit/vlan_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package unit

import (
"context"
"fmt"
"github.com/linode/linodego"
"github.com/stretchr/testify/assert"
"testing"
)

func TestVLAN_List(t *testing.T) {
// Load the fixture data for VLANs
fixtureData, err := fixtures.GetFixture("vlans_list")
assert.NoError(t, err)

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

// Mock the GET request
base.MockGet("networking/vlans", fixtureData)

vlans, err := base.Client.ListVLANs(context.Background(), &linodego.ListOptions{})
assert.NoError(t, err)
assert.NotEmpty(t, vlans, "Expected non-empty VLAN list")

// Verify a specific VLAN exists in the list
var testVLAN *linodego.VLAN
for _, v := range vlans {
if v.Label == "test-vlan" {
testVLAN = &v
break
}
}

if testVLAN == nil {
t.Errorf("Expected VLAN 'test-vlan' to be in the response, but it was not found")
} else {
assert.Equal(t, "us-east", testVLAN.Region, "Expected region to be 'us-east'")
assert.Contains(t, testVLAN.Linodes, 12345, "Expected Linodes to include 12345")
assert.NotNil(t, testVLAN.Created, "Expected 'test-vlan' to have a created timestamp")
}
}

func TestVLAN_GetIPAMAddress(t *testing.T) {
// Load the fixture data for VLAN IPAM address
fixtureData, err := fixtures.GetFixture("vlan_get_ipam_address")
assert.NoError(t, err)

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

linodeID := 12345
vlanLabel := "test-vlan"
// Mock the GET request
base.MockGet(fmt.Sprintf("linode/instances/%d/configs", linodeID), fixtureData)

ipamAddress, err := base.Client.GetVLANIPAMAddress(context.Background(), linodeID, vlanLabel)
assert.NoError(t, err)
assert.NotEmpty(t, ipamAddress, "Expected non-empty IPAM address")

// Verify the returned IPAM address
assert.Equal(t, "10.0.0.1/24", ipamAddress, "Expected IPAM address to be '10.0.0.1/24'")
}
Loading