File: volume_test.go

package info (click to toggle)
golang-github-linode-linodego 1.55.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,112 kB
  • sloc: makefile: 96; sh: 52; python: 24
file content (179 lines) | stat: -rw-r--r-- 6,005 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package unit

import (
	"context"
	"fmt"
	"testing"

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

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")
}