Skip to content

Commit a8748fc

Browse files
authored
Add unit test coverage for Stackscripts, Database and Region related methods/functions (#652)
* unit_tests * add_test * database_tests
1 parent fcf4b18 commit a8748fc

16 files changed

+592
-0
lines changed

test/unit/.DS_Store

8 KB
Binary file not shown.

test/unit/database_test.go

+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package unit
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"slices"
8+
"testing"
9+
10+
"github.com/linode/linodego"
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
func TestListDatabases(t *testing.T) {
15+
fixtureData, err := fixtures.GetFixture("databases_list")
16+
assert.NoError(t, err)
17+
18+
var base ClientBaseCase
19+
base.SetUp(t)
20+
defer base.TearDown(t)
21+
22+
base.MockGet("databases/instances", fixtureData)
23+
databases, err := base.Client.ListDatabases(context.Background(), &linodego.ListOptions{})
24+
assert.NoError(t, err)
25+
assert.NotEmpty(t, databases, "Expected non-empty database list")
26+
}
27+
28+
func TestGetDatabaseEngine(t *testing.T) {
29+
fixtureData, err := fixtures.GetFixture("database_engine_get")
30+
assert.NoError(t, err)
31+
32+
var base ClientBaseCase
33+
base.SetUp(t)
34+
defer base.TearDown(t)
35+
36+
engineID := "mysql-8"
37+
base.MockGet(fmt.Sprintf("databases/engines/%s", engineID), fixtureData)
38+
databaseEngine, err := base.Client.GetDatabaseEngine(context.Background(), &linodego.ListOptions{}, engineID)
39+
assert.NoError(t, err)
40+
assert.NotNil(t, databaseEngine, "Expected database engine object to be returned")
41+
assert.Equal(t, engineID, databaseEngine.ID, "Expected correct database engine ID")
42+
assert.Equal(t, "mysql", databaseEngine.Engine, "Expected MySQL engine")
43+
assert.Equal(t, "8.0", databaseEngine.Version, "Expected MySQL 8.0 version")
44+
}
45+
46+
func TestListDatabaseTypes(t *testing.T) {
47+
fixtureData, err := fixtures.GetFixture("database_types_list")
48+
assert.NoError(t, err)
49+
50+
var base ClientBaseCase
51+
base.SetUp(t)
52+
defer base.TearDown(t)
53+
54+
base.MockGet("databases/types", fixtureData)
55+
databaseTypes, err := base.Client.ListDatabaseTypes(context.Background(), &linodego.ListOptions{})
56+
assert.NoError(t, err)
57+
assert.NotEmpty(t, databaseTypes, "Expected non-empty database types list")
58+
}
59+
60+
func TestUnmarshalDatabase(t *testing.T) {
61+
fixtureData, err := fixtures.GetFixture("database_unmarshal")
62+
assert.NoError(t, err)
63+
64+
var data []byte
65+
switch v := fixtureData.(type) {
66+
case []byte:
67+
data = v
68+
case string:
69+
data = []byte(v)
70+
case map[string]interface{}:
71+
data, err = json.Marshal(v) // Convert map to JSON string
72+
assert.NoError(t, err, "Failed to marshal fixtureData")
73+
default:
74+
assert.Fail(t, "Unexpected fixtureData type")
75+
}
76+
77+
var db linodego.Database
78+
err = json.Unmarshal(data, &db)
79+
assert.NoError(t, err)
80+
assert.Equal(t, 123, db.ID, "Expected correct database ID")
81+
assert.Equal(t, "active", string(db.Status), "Expected active status")
82+
assert.Equal(t, "mysql", db.Engine, "Expected MySQL engine")
83+
assert.Equal(t, 3, db.ClusterSize, "Expected cluster size 3")
84+
assert.NotNil(t, db.Created, "Expected Created timestamp to be set")
85+
}
86+
87+
func TestDatabaseMaintenanceWindowUnmarshal(t *testing.T) {
88+
fixtureData, err := fixtures.GetFixture("database_maintenance_window")
89+
assert.NoError(t, err)
90+
91+
var data []byte
92+
switch v := fixtureData.(type) {
93+
case []byte:
94+
data = v
95+
case string:
96+
data = []byte(v)
97+
case map[string]interface{}:
98+
data, err = json.Marshal(v)
99+
assert.NoError(t, err, "Failed to marshal fixtureData")
100+
default:
101+
assert.Fail(t, "Unexpected fixtureData type")
102+
}
103+
104+
var window linodego.DatabaseMaintenanceWindow
105+
err = json.Unmarshal(data, &window)
106+
assert.NoError(t, err)
107+
assert.Equal(t, linodego.DatabaseMaintenanceDayMonday, window.DayOfWeek, "Expected Monday as maintenance day")
108+
assert.Equal(t, 2, window.Duration, "Expected 2-hour maintenance window")
109+
assert.Equal(t, linodego.DatabaseMaintenanceFrequencyWeekly, window.Frequency, "Expected weekly frequency")
110+
assert.Equal(t, 3, window.HourOfDay, "Expected maintenance at 3 AM")
111+
}
112+
113+
func TestDatabaseStatusAssertions(t *testing.T) {
114+
expectedStatuses := []string{
115+
"provisioning", "active", "deleting", "deleted",
116+
"suspending", "suspended", "resuming", "restoring",
117+
"failed", "degraded", "updating", "backing_up",
118+
}
119+
120+
statuses := []linodego.DatabaseStatus{
121+
linodego.DatabaseStatusProvisioning, linodego.DatabaseStatusActive, linodego.DatabaseStatusDeleting,
122+
linodego.DatabaseStatusDeleted, linodego.DatabaseStatusSuspending, linodego.DatabaseStatusSuspended,
123+
linodego.DatabaseStatusResuming, linodego.DatabaseStatusRestoring, linodego.DatabaseStatusFailed,
124+
linodego.DatabaseStatusDegraded, linodego.DatabaseStatusUpdating, linodego.DatabaseStatusBackingUp,
125+
}
126+
127+
for _, status := range statuses {
128+
t.Run(string(status), func(t *testing.T) {
129+
exists := slices.ContainsFunc(expectedStatuses, func(s string) bool {
130+
return s == string(status)
131+
})
132+
assert.True(t, exists, fmt.Sprintf("Expected status %s to exist", status))
133+
})
134+
}
135+
}
136+
137+
func TestDatabaseMaintenanceFrequencyAssertions(t *testing.T) {
138+
expectedFrequencies := []string{"weekly", "monthly"}
139+
140+
frequencies := []linodego.DatabaseMaintenanceFrequency{
141+
linodego.DatabaseMaintenanceFrequencyWeekly,
142+
linodego.DatabaseMaintenanceFrequencyMonthly,
143+
}
144+
145+
for _, freq := range frequencies {
146+
t.Run(string(freq), func(t *testing.T) {
147+
exists := slices.ContainsFunc(expectedFrequencies, func(f string) bool {
148+
return f == string(freq)
149+
})
150+
assert.True(t, exists, fmt.Sprintf("Expected frequency %s to exist", freq))
151+
})
152+
}
153+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"id": "mysql-8",
3+
"engine": "mysql",
4+
"version": "8.0",
5+
"supported_versions": ["5.7", "8.0"]
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"day_of_week": 1,
3+
"duration": 2,
4+
"frequency": "weekly",
5+
"hour_of_day": 3
6+
}
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"data": [
3+
{
4+
"id": "g6-standard-1",
5+
"label": "Standard Plan",
6+
"memory": 4096,
7+
"vcpus": 2,
8+
"disk": 80
9+
},
10+
{
11+
"id": "g6-standard-2",
12+
"label": "High Performance Plan",
13+
"memory": 8192,
14+
"vcpus": 4,
15+
"disk": 160
16+
}
17+
],
18+
"page": 1,
19+
"pages": 1,
20+
"results": 2
21+
}
22+
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"id": 123,
3+
"status": "active",
4+
"label": "test-db",
5+
"region": "us-east",
6+
"type": "g6-standard-1",
7+
"engine": "mysql",
8+
"version": "8.0",
9+
"cluster_size": 3,
10+
"platform": "rdbms-default",
11+
"created": "2023-01-01T12:00:00"
12+
}
13+
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"data": [
3+
{
4+
"id": 123,
5+
"status": "active",
6+
"label": "test-db",
7+
"region": "us-east",
8+
"type": "g6-standard-1",
9+
"engine": "mysql",
10+
"version": "8.0",
11+
"cluster_size": 3,
12+
"platform": "rdbms-default",
13+
"created": "2023-01-01T12:00:00"
14+
}
15+
],
16+
"page": 1,
17+
"pages": 1,
18+
"results": 1
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"region": "us-east",
3+
"available": true,
4+
"plan": "Linode 2GB"
5+
}

test/unit/fixtures/region_get.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"id": "us-east",
3+
"country": "US",
4+
"capabilities": ["Linodes", "Block Storage", "Object Storage"],
5+
"status": "ok",
6+
"label": "Newark, NJ",
7+
"site_type": "standard",
8+
"resolvers": {
9+
"ipv4": "8.8.8.8",
10+
"ipv6": "2001:4860:4860::8888"
11+
},
12+
"placement_group_limits": {
13+
"maximum_pgs_per_customer": 5,
14+
"maximum_linodes_per_pg": 10
15+
}
16+
}
17+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"data": [
3+
{
4+
"region": "us-east",
5+
"available": true,
6+
"plan": "Linode 2GB"
7+
}
8+
],
9+
"page": 1,
10+
"pages": 1,
11+
"results": 1
12+
}
13+

test/unit/fixtures/regions_list.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"data": [
3+
{
4+
"id": "us-east",
5+
"country": "US",
6+
"capabilities": ["Linodes", "Block Storage", "Object Storage"],
7+
"status": "ok",
8+
"label": "US East",
9+
"site_type": "standard",
10+
"resolvers": {
11+
"ipv4": "8.8.8.8",
12+
"ipv6": "2001:4860:4860::8888"
13+
},
14+
"placement_group_limits": {
15+
"maximum_pgs_per_customer": 10,
16+
"maximum_linodes_per_pg": 5
17+
}
18+
}
19+
],
20+
"page": 1,
21+
"pages": 1,
22+
"results": 1
23+
}
24+
25+
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"id": 123,
3+
"username": "testuser",
4+
"label": "new-stackscript",
5+
"description": "Test Description",
6+
"ordinal": 1,
7+
"logo_url": "https://example.com/logo.png",
8+
"images": ["linode/ubuntu20.04"],
9+
"deployments_total": 10,
10+
"deployments_active": 5,
11+
"is_public": true,
12+
"mine": true,
13+
"rev_note": "Initial revision",
14+
"script": "#!/bin/bash\necho Hello",
15+
"user_defined_fields": [],
16+
"user_gravatar_id": "abcdef123456"
17+
}
18+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"id": 123,
3+
"label": "Updated Stackscript",
4+
"rev_note": "Updated revision"
5+
}
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"data": [
3+
{
4+
"id": 123,
5+
"username": "testuser",
6+
"label": "Test Stackscript",
7+
"description": "A test Stackscript",
8+
"images": ["linode/ubuntu20.04"],
9+
"deployments_total": 10,
10+
"deployments_active": 5,
11+
"is_public": true,
12+
"mine": true,
13+
"rev_note": "Initial version",
14+
"script": "#!/bin/bash\necho Hello",
15+
"user_defined_fields": [],
16+
"user_gravatar_id": "abc123"
17+
},
18+
{
19+
"id": 456,
20+
"username": "anotheruser",
21+
"label": "Another Stackscript",
22+
"description": "Another test Stackscript",
23+
"images": ["linode/debian10"],
24+
"deployments_total": 3,
25+
"deployments_active": 1,
26+
"is_public": false,
27+
"mine": false,
28+
"rev_note": "Another version",
29+
"script": "#!/bin/bash\necho Another",
30+
"user_defined_fields": [],
31+
"user_gravatar_id": "xyz456"
32+
}
33+
],
34+
"page": 1,
35+
"pages": 1,
36+
"results": 2
37+
}

0 commit comments

Comments
 (0)