-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit test coverage for Type, Vlan and Tag related methods/functi…
…ons (#647) * types_unit_tests * vlan_tests * unit_test_tag * address_PR_comments * fix * fix lint
- Loading branch information
Showing
16 changed files
with
405 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
go 1.22 | ||
go 1.22.0 | ||
|
||
use ( | ||
. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"label": "new-tag" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"data": [ | ||
{ | ||
"label": "example-tag" | ||
} | ||
], | ||
"page": 1, | ||
"pages": 1, | ||
"results": 1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'") | ||
} |
Oops, something went wrong.