Skip to content

Commit

Permalink
Add unit test coverage for Type, Vlan and Tag related methods/functi…
Browse files Browse the repository at this point in the history
…ons (#647)

* types_unit_tests

* vlan_tests

* unit_test_tag

* address_PR_comments

* fix

* fix lint
  • Loading branch information
vshanthe authored Jan 10, 2025
1 parent c613763 commit 2520ae9
Show file tree
Hide file tree
Showing 16 changed files with 405 additions and 12 deletions.
2 changes: 1 addition & 1 deletion go.work
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
go 1.22
go 1.22.0

use (
.
Expand Down
4 changes: 2 additions & 2 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ug
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
golang.org/x/oauth2 v0.24.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
Expand All @@ -60,11 +62,9 @@ golang.org/x/sync v0.9.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2 h1:IRJeR9r1pYWsHKTRe/IInb7lYvbBVIqOgsX/u0mbOWY=
golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE=
golang.org/x/term v0.24.0/go.mod h1:lOBK/LVxemqiMij05LGJ0tzNr8xlmwBRJ81PX6wVLH8=
golang.org/x/term v0.28.0/go.mod h1:Sw/lC2IAUZ92udQNf3WodGtn4k/XoLyZoh8v/8uiwek=
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 h1:H2TDz8ibqkAF6YGhCdN3jS9O0/s90v0rJh3X/OLHEUk=
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8=
Expand Down
4 changes: 3 additions & 1 deletion k8s/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ require (
golang.org/x/time v0.6.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.66.6 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/klog/v2 v2.110.1 // indirect
Expand All @@ -51,3 +51,5 @@ require (
replace github.com/linode/linodego => ../

go 1.22

toolchain go1.22.1
4 changes: 2 additions & 2 deletions k8s/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntN
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
gopkg.in/ini.v1 v1.66.6 h1:LATuAqN/shcYAOkv3wl2L4rkaKqkcgTBQjOyYDvcPKI=
gopkg.in/ini.v1 v1.66.6/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
Expand Down
7 changes: 5 additions & 2 deletions test/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/linode/linodego v1.33.0
github.com/linode/linodego/k8s v0.0.0-00010101000000-000000000000
github.com/stretchr/testify v1.10.0
golang.org/x/exp v0.0.0-20241217172543-b2144cdd0a67
golang.org/x/net v0.34.0
golang.org/x/oauth2 v0.25.0
k8s.io/client-go v0.29.4
Expand Down Expand Up @@ -42,7 +43,7 @@ require (
golang.org/x/time v0.6.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.66.6 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.29.4 // indirect
Expand All @@ -55,7 +56,9 @@ require (
sigs.k8s.io/yaml v1.3.0 // indirect
)

go 1.22
go 1.22.0

toolchain go1.22.1

replace github.com/linode/linodego => ../

Expand Down
10 changes: 6 additions & 4 deletions test/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/exp v0.0.0-20241217172543-b2144cdd0a67 h1:1UoZQm6f0P/ZO0w1Ri+f+ifG/gXhegadRdwBIXEFWDo=
golang.org/x/exp v0.0.0-20241217172543-b2144cdd0a67/go.mod h1:qj5a5QZpwLU2NLQudwIN5koi3beDhSAlJwa67PuM98c=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
Expand Down Expand Up @@ -122,8 +124,8 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
golang.org/x/tools v0.28.0 h1:WuB6qZ4RPCQo5aP3WdKZS7i595EdWqWR8vqJTlwTVK8=
golang.org/x/tools v0.28.0/go.mod h1:dcIOrVd3mfQKTgrDVQHqCPMWy6lnhfhtX3hLXYVLfRw=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand All @@ -135,8 +137,8 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntN
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
gopkg.in/ini.v1 v1.66.6 h1:LATuAqN/shcYAOkv3wl2L4rkaKqkcgTBQjOyYDvcPKI=
gopkg.in/ini.v1 v1.66.6/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
Expand Down
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
}

3 changes: 3 additions & 0 deletions test/unit/fixtures/tag_create.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"label": "new-tag"
}
15 changes: 15 additions & 0 deletions test/unit/fixtures/tagged_objects_list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"data": [
{
"type": "linode",
"data": {
"id": 12345,
"label": "example-instance",
"region": "us-east"
}
}
],
"page": 1,
"pages": 1,
"results": 1
}
10 changes: 10 additions & 0 deletions test/unit/fixtures/tags_list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"data": [
{
"label": "example-tag"
}
],
"page": 1,
"pages": 1,
"results": 1
}
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
}
122 changes: 122 additions & 0 deletions test/unit/tag_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package unit

import (
"context"
"fmt"
"testing"

"github.com/linode/linodego"
"github.com/stretchr/testify/assert"
"golang.org/x/exp/slices"
)

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

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

base.MockGet("tags", fixtureData)

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

assert.NotEmpty(t, tags, "Expected non-empty tag list")

// Check if a specific tag exists using slices.ContainsFunc
exists := slices.ContainsFunc(tags, func(tag linodego.Tag) bool {
return tag.Label == "example-tag"
})

assert.True(t, exists, "Expected tag list to contain 'example-tag'")
}


func TestCreateTag(t *testing.T) {
// Load the fixture data for tag creation
fixtureData, err := fixtures.GetFixture("tag_create")
assert.NoError(t, err)

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

base.MockPost("tags", fixtureData)

opts := linodego.TagCreateOptions{
Label: "new-tag",
}

tag, err := base.Client.CreateTag(context.Background(), opts)
assert.NoError(t, err, "Expected no error when creating tag")

// Verify the created tag's label
assert.Equal(t, "new-tag", tag.Label, "Expected created tag label to match input")
}


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

tagLabel := "delete-tag"
base.MockDelete(fmt.Sprintf("tags/%s", tagLabel), nil)

err := base.Client.DeleteTag(context.Background(), tagLabel)
assert.NoError(t, err, "Expected no error when deleting tag")
}

func TestListTaggedObjects(t *testing.T) {
// Load the fixture data for tagged objects
fixtureData, err := fixtures.GetFixture("tagged_objects_list")
assert.NoError(t, err)

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

tagLabel := "example-tag"
base.MockGet(fmt.Sprintf("tags/%s", tagLabel), fixtureData)

taggedObjects, err := base.Client.ListTaggedObjects(context.Background(), tagLabel, &linodego.ListOptions{})
assert.NoError(t, err)

assert.NotEmpty(t, taggedObjects, "Expected non-empty tagged objects list")

// Find the specific tagged object using slices.IndexFunc
index := slices.IndexFunc(taggedObjects, func(obj linodego.TaggedObject) bool {
return obj.Type == "linode"
})

assert.NotEqual(t, -1, index, "Expected to find a tagged object of type 'linode'")
if index != -1 {
assert.Equal(t, "linode", taggedObjects[index].Type, "Expected tagged object type to be 'linode'")
}
}


func TestSortedObjects(t *testing.T) {
// Load the fixture data for tagged objects
fixtureData, err := fixtures.GetFixture("tagged_objects_list")
assert.NoError(t, err)

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

tagLabel := "example-tag"
base.MockGet(fmt.Sprintf("tags/%s", tagLabel), fixtureData)

taggedObjects, err := base.Client.ListTaggedObjects(context.Background(), tagLabel, &linodego.ListOptions{})
assert.NoError(t, err)

sortedObjects, err := taggedObjects.SortedObjects()
assert.NoError(t, err)

assert.NotEmpty(t, sortedObjects.Instances, "Expected non-empty instances list in sorted objects")
assert.Equal(t, "example-instance", sortedObjects.Instances[0].Label, "Expected instance label to be 'example-instance'")
}
Loading

0 comments on commit 2520ae9

Please sign in to comment.