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 Volume,Volume types and Longview related methods/functions #661

Merged
merged 14 commits into from
Feb 4, 2025
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
)

go 1.22
go 1.22.0

toolchain go1.22.1

retract v1.0.0 // Accidental branch push
5 changes: 5 additions & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:l
github.com/creack/pty v1.1.9 h1:uDmaGzcdjhF4i/plgjmEsriH11Y0o7RKapEf/LDaM3w=
github.com/evanphx/json-patch v4.12.0+incompatible h1:4onqiflcdA9EOZ4RxV643DvftH5pOlLGNtQ5lPWQu84=
github.com/evanphx/json-patch v4.12.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/go-resty/resty/v2 v2.16.2/go.mod h1:0fHAoK7JoBy/Ch36N8VFeMsK7xQOHhvWaC3iOktwmIU=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/google/btree v1.0.1 h1:gK4Kx5IaGY9CD5sPJ36FHiBJ6ZXl0kilRiiCj+jdYp4=
Expand Down Expand Up @@ -50,6 +51,7 @@ golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ss
golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
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/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
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=
Expand All @@ -62,10 +64,13 @@ 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.28.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.27.0/go.mod h1:iMsnZpn0cago0GOrHO2+Y7u7JPn5AylBrcoWkElMTSM=
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/tools v0.29.0/go.mod h1:KMQVMRsVxU6nHCFXrBPhDB8XncLNLM0lIy/F14RP588=
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=
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
Expand Down
2 changes: 1 addition & 1 deletion k8s/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ require (

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

go 1.22
go 1.22.0

toolchain go1.22.1
16 changes: 16 additions & 0 deletions test/unit/fixtures/volume_attach.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"id": 123,
"label": "Test Volume",
"status": "active",
"region": "us-east",
"size": 20,
"linode_id": 456,
"filesystem_path": "/dev/disk/by-id/volume-123",
"tags": ["test"],
"hardware_type": "",
"linode_label": "linode-test",
"encryption": "",
"created": "2025-01-01T12:00:00",
"updated": "2025-01-20T12:00:00"
}

16 changes: 16 additions & 0 deletions test/unit/fixtures/volume_create.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"id": 124,
"label": "new-volume",
"status": "creating",
"region": "us-east",
"size": 20,
"linode_id": null,
"filesystem_path": "",
"tags": ["test"],
"hardware_type": "",
"linode_label": "",
"encryption": "",
"created": "2025-01-15T12:00:00",
"updated": "2025-01-15T12:00:00"
}

16 changes: 16 additions & 0 deletions test/unit/fixtures/volume_get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"id": 123,
"label": "Test Volume",
"status": "active",
"region": "us-east",
"size": 20,
"linode_id": null,
"filesystem_path": "",
"tags": ["test"],
"hardware_type": "",
"linode_label": "",
"encryption": "",
"created": "2025-01-01T12:00:00",
"updated": "2025-01-10T12:00:00"
}

36 changes: 36 additions & 0 deletions test/unit/fixtures/volume_types_list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"data": [
{
"id": "standard",
"label": "Standard Volume",
"price": {
"hourly": 0.10,
"monthly": 10.00
},
"region_prices": [
{
"region": "us-east",
"hourly": 0.08,
"monthly": 8.00
}
]
},
{
"id": "high-performance",
"label": "High Performance Volume",
"price": {
"hourly": 0.20,
"monthly": 20.00
},
"region_prices": [
{
"region": "us-east",
"hourly": 0.18,
"monthly": 18.00
}
]
}
],
"pages": 1,
"results": 2
}
16 changes: 16 additions & 0 deletions test/unit/fixtures/volume_update.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"id": 123,
"label": "updated-volume",
"status": "active",
"region": "us-east",
"size": 20,
"linode_id": null,
"filesystem_path": "",
"tags": ["updated"],
"hardware_type": "",
"linode_label": "",
"encryption": "",
"created": "2025-01-01T12:00:00",
"updated": "2025-01-18T12:00:00"
}

20 changes: 20 additions & 0 deletions test/unit/fixtures/volumes_list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"data": [
{
"id": 123,
"label": "Test Volume",
"status": "active",
"region": "us-east",
"size": 20,
"linode_id": null,
"filesystem_path": "",
"tags": ["test"],
"hardware_type": "",
"linode_label": "",
"encryption": "",
"created": "2025-01-01T12:00:00",
"updated": "2025-01-10T12:00:00"
}
]
}

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

import (
"context"
"fmt"
"testing"

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

func TestListVolumes(t *testing.T) {
fixtureData, err := fixtures.GetFixture("volumes_list")
assert.NoError(t, err)

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

base.MockGet("volumes", fixtureData)

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

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

// Assert specific volume details
assert.Equal(t, 123, volumes[0].ID, "Expected volume ID to match")
assert.Equal(t, "Test Volume", volumes[0].Label, "Expected volume label to match")
assert.Equal(t, "active", string(volumes[0].Status), "Expected volume status to match")
assert.Equal(t, "us-east", volumes[0].Region, "Expected volume region to match")
assert.Equal(t, 20, volumes[0].Size, "Expected volume size to match")
assert.Equal(t, "test", volumes[0].Tags[0], "Expected volume tag to match")
}

func TestGetVolume(t *testing.T) {
fixtureData, err := fixtures.GetFixture("volume_get")
assert.NoError(t, err)

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

volumeID := 123
base.MockGet(fmt.Sprintf("volumes/%d", volumeID), fixtureData)

volume, err := base.Client.GetVolume(context.Background(), volumeID)
assert.NoError(t, err)

// Assert all fields
assert.Equal(t, 123, volume.ID, "Expected volume ID to match")
assert.Equal(t, "Test Volume", volume.Label, "Expected volume label to match")
assert.Equal(t, "active", string(volume.Status), "Expected volume status to match")
assert.Equal(t, "us-east", volume.Region, "Expected volume region to match")
assert.Equal(t, 20, volume.Size, "Expected volume size to match")
assert.Nil(t, volume.LinodeID, "Expected LinodeID to be nil")
assert.Empty(t, volume.FilesystemPath, "Expected filesystem path to be empty")
assert.Contains(t, volume.Tags, "test", "Expected tags to include 'test'")
assert.Empty(t, volume.HardwareType, "Expected hardware type to be empty")
assert.Empty(t, volume.LinodeLabel, "Expected Linode label to be empty")
}

func TestCreateVolume(t *testing.T) {
fixtureData, err := fixtures.GetFixture("volume_create")
assert.NoError(t, err)

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

base.MockPost("volumes", fixtureData)

opts := linodego.VolumeCreateOptions{
Label: "new-volume",
Size: 20,
Tags: []string{"test"},
}

volume, err := base.Client.CreateVolume(context.Background(), opts)
assert.NoError(t, err)

// Assert all fields
assert.Equal(t, 124, volume.ID, "Expected created volume ID to match")
assert.Equal(t, "new-volume", volume.Label, "Expected created volume label to match")
assert.Equal(t, "creating", string(volume.Status), "Expected created volume status to be 'creating'")
assert.Equal(t, "us-east", volume.Region, "Expected created volume region to match")
assert.Equal(t, 20, volume.Size, "Expected created volume size to match")
assert.Nil(t, volume.LinodeID, "Expected LinodeID to be nil for newly created volume")
assert.Contains(t, volume.Tags, "test", "Expected created volume tags to include 'test'")
}

func TestUpdateVolume(t *testing.T) {
fixtureData, err := fixtures.GetFixture("volume_update")
assert.NoError(t, err)

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

volumeID := 123
base.MockPut(fmt.Sprintf("volumes/%d", volumeID), fixtureData)

opts := linodego.VolumeUpdateOptions{
Label: "updated-volume",
Tags: &[]string{"updated"},
}

updatedVolume, err := base.Client.UpdateVolume(context.Background(), volumeID, opts)
assert.NoError(t, err)

// Assert all fields
assert.Equal(t, 123, updatedVolume.ID, "Expected updated volume ID to match")
assert.Equal(t, "updated-volume", updatedVolume.Label, "Expected updated volume label to match")
assert.Equal(t, "active", string(updatedVolume.Status), "Expected updated volume status to match")
assert.Contains(t, updatedVolume.Tags, "updated", "Expected updated volume tags to include 'updated'")
}

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

volumeID := 123
base.MockDelete(fmt.Sprintf("volumes/%d", volumeID), nil)

err := base.Client.DeleteVolume(context.Background(), volumeID)
assert.NoError(t, err, "Expected no error when deleting volume")
}

func TestAttachVolume(t *testing.T) {
// Mock the API response for attaching a volume
fixtureData, err := fixtures.GetFixture("volume_attach")
assert.NoError(t, err)

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

volumeID := 123
base.MockPost(fmt.Sprintf("volumes/%d/attach", volumeID), fixtureData)

// Use direct pointer assignment for PersistAcrossBoots
persistAcrossBoots := true
opts := &linodego.VolumeAttachOptions{
LinodeID: 456,
PersistAcrossBoots: &persistAcrossBoots,
}

attachedVolume, err := base.Client.AttachVolume(context.Background(), volumeID, opts)
assert.NoError(t, err, "Expected no error when attaching volume")

// Verify the attached volume's LinodeID and filesystem path
assert.Equal(t, 456, *attachedVolume.LinodeID, "Expected LinodeID to match input")
assert.Equal(t, "/dev/disk/by-id/volume-123", attachedVolume.FilesystemPath, "Expected filesystem path to match fixture")
}

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

volumeID := 123
base.MockPost(fmt.Sprintf("volumes/%d/detach", volumeID), nil)

err := base.Client.DetachVolume(context.Background(), volumeID)
assert.NoError(t, err, "Expected no error when detaching volume")
}

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

volumeID := 123
base.MockPost(fmt.Sprintf("volumes/%d/resize", volumeID), nil)

err := base.Client.ResizeVolume(context.Background(), volumeID, 50)
assert.NoError(t, err, "Expected no error when resizing volume")
}
38 changes: 38 additions & 0 deletions test/unit/volume_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package unit

import (
"context"
"testing"

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

func TestListVolumeTypes(t *testing.T) {
// Load the mock fixture for volume types
fixtureData, err := fixtures.GetFixture("volume_types_list")
assert.NoError(t, err, "Expected no error when getting fixture")

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

// Mock the GET request for the volume types endpoint
base.MockGet("volumes/types", fixtureData)

// Call the ListVolumeTypes method
volumeTypes, err := base.Client.ListVolumeTypes(context.Background(), &linodego.ListOptions{})
assert.NoError(t, err, "Expected no error when listing volume types")
assert.NotEmpty(t, volumeTypes, "Expected non-empty volume types list")

// Validate the first volume type's details
assert.Equal(t, "standard", volumeTypes[0].ID, "Expected volume type ID to match")
assert.Equal(t, "Standard Volume", volumeTypes[0].Label, "Expected volume type label to match")
assert.Equal(t, 0.10, volumeTypes[0].Price.Hourly, "Expected hourly price to match")
assert.Equal(t, 10.00, volumeTypes[0].Price.Monthly, "Expected monthly price to match")

// Validate regional pricing for the first volume type
assert.NotEmpty(t, volumeTypes[0].RegionPrices, "Expected region prices to be non-empty")
assert.Equal(t, 0.08, volumeTypes[0].RegionPrices[0].Hourly, "Expected regional hourly price to match")
assert.Equal(t, 8.00, volumeTypes[0].RegionPrices[0].Monthly, "Expected regional monthly price to match")
}
Loading