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 Stackscripts, Database and Region related methods/functions #652

Merged
merged 5 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added test/unit/.DS_Store
Binary file not shown.
153 changes: 153 additions & 0 deletions test/unit/database_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package unit

import (
"context"
"encoding/json"
"fmt"
"slices"
"testing"

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

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

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

base.MockGet("databases/instances", fixtureData)
databases, err := base.Client.ListDatabases(context.Background(), &linodego.ListOptions{})
assert.NoError(t, err)
assert.NotEmpty(t, databases, "Expected non-empty database list")
}

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

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

engineID := "mysql-8"
base.MockGet(fmt.Sprintf("databases/engines/%s", engineID), fixtureData)
databaseEngine, err := base.Client.GetDatabaseEngine(context.Background(), &linodego.ListOptions{}, engineID)
assert.NoError(t, err)
assert.NotNil(t, databaseEngine, "Expected database engine object to be returned")
assert.Equal(t, engineID, databaseEngine.ID, "Expected correct database engine ID")
assert.Equal(t, "mysql", databaseEngine.Engine, "Expected MySQL engine")
assert.Equal(t, "8.0", databaseEngine.Version, "Expected MySQL 8.0 version")
}

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

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

base.MockGet("databases/types", fixtureData)
databaseTypes, err := base.Client.ListDatabaseTypes(context.Background(), &linodego.ListOptions{})
assert.NoError(t, err)
assert.NotEmpty(t, databaseTypes, "Expected non-empty database types list")
}

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

var data []byte
switch v := fixtureData.(type) {
case []byte:
data = v
case string:
data = []byte(v)
case map[string]interface{}:
data, err = json.Marshal(v) // Convert map to JSON string
assert.NoError(t, err, "Failed to marshal fixtureData")
default:
assert.Fail(t, "Unexpected fixtureData type")
}

var db linodego.Database
err = json.Unmarshal(data, &db)
assert.NoError(t, err)
assert.Equal(t, 123, db.ID, "Expected correct database ID")
assert.Equal(t, "active", string(db.Status), "Expected active status")
assert.Equal(t, "mysql", db.Engine, "Expected MySQL engine")
assert.Equal(t, 3, db.ClusterSize, "Expected cluster size 3")
assert.NotNil(t, db.Created, "Expected Created timestamp to be set")
}

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

var data []byte
switch v := fixtureData.(type) {
case []byte:
data = v
case string:
data = []byte(v)
case map[string]interface{}:
data, err = json.Marshal(v)
assert.NoError(t, err, "Failed to marshal fixtureData")
default:
assert.Fail(t, "Unexpected fixtureData type")
}

var window linodego.DatabaseMaintenanceWindow
err = json.Unmarshal(data, &window)
assert.NoError(t, err)
assert.Equal(t, linodego.DatabaseMaintenanceDayMonday, window.DayOfWeek, "Expected Monday as maintenance day")
assert.Equal(t, 2, window.Duration, "Expected 2-hour maintenance window")
assert.Equal(t, linodego.DatabaseMaintenanceFrequencyWeekly, window.Frequency, "Expected weekly frequency")
assert.Equal(t, 3, window.HourOfDay, "Expected maintenance at 3 AM")
}

func TestDatabaseStatusAssertions(t *testing.T) {
expectedStatuses := []string{
"provisioning", "active", "deleting", "deleted",
"suspending", "suspended", "resuming", "restoring",
"failed", "degraded", "updating", "backing_up",
}

statuses := []linodego.DatabaseStatus{
linodego.DatabaseStatusProvisioning, linodego.DatabaseStatusActive, linodego.DatabaseStatusDeleting,
linodego.DatabaseStatusDeleted, linodego.DatabaseStatusSuspending, linodego.DatabaseStatusSuspended,
linodego.DatabaseStatusResuming, linodego.DatabaseStatusRestoring, linodego.DatabaseStatusFailed,
linodego.DatabaseStatusDegraded, linodego.DatabaseStatusUpdating, linodego.DatabaseStatusBackingUp,
}

for _, status := range statuses {
t.Run(string(status), func(t *testing.T) {
exists := slices.ContainsFunc(expectedStatuses, func(s string) bool {
return s == string(status)
})
assert.True(t, exists, fmt.Sprintf("Expected status %s to exist", status))
})
}
}

func TestDatabaseMaintenanceFrequencyAssertions(t *testing.T) {
expectedFrequencies := []string{"weekly", "monthly"}

frequencies := []linodego.DatabaseMaintenanceFrequency{
linodego.DatabaseMaintenanceFrequencyWeekly,
linodego.DatabaseMaintenanceFrequencyMonthly,
}

for _, freq := range frequencies {
t.Run(string(freq), func(t *testing.T) {
exists := slices.ContainsFunc(expectedFrequencies, func(f string) bool {
return f == string(freq)
})
assert.True(t, exists, fmt.Sprintf("Expected frequency %s to exist", freq))
})
}
}
6 changes: 6 additions & 0 deletions test/unit/fixtures/database_engine_get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"id": "mysql-8",
"engine": "mysql",
"version": "8.0",
"supported_versions": ["5.7", "8.0"]
}
7 changes: 7 additions & 0 deletions test/unit/fixtures/database_maintenance_window.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"day_of_week": 1,
"duration": 2,
"frequency": "weekly",
"hour_of_day": 3
}

22 changes: 22 additions & 0 deletions test/unit/fixtures/database_types_list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"data": [
{
"id": "g6-standard-1",
"label": "Standard Plan",
"memory": 4096,
"vcpus": 2,
"disk": 80
},
{
"id": "g6-standard-2",
"label": "High Performance Plan",
"memory": 8192,
"vcpus": 4,
"disk": 160
}
],
"page": 1,
"pages": 1,
"results": 2
}

13 changes: 13 additions & 0 deletions test/unit/fixtures/database_unmarshal.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"id": 123,
"status": "active",
"label": "test-db",
"region": "us-east",
"type": "g6-standard-1",
"engine": "mysql",
"version": "8.0",
"cluster_size": 3,
"platform": "rdbms-default",
"created": "2023-01-01T12:00:00"
}

19 changes: 19 additions & 0 deletions test/unit/fixtures/databases_list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"data": [
{
"id": 123,
"status": "active",
"label": "test-db",
"region": "us-east",
"type": "g6-standard-1",
"engine": "mysql",
"version": "8.0",
"cluster_size": 3,
"platform": "rdbms-default",
"created": "2023-01-01T12:00:00"
}
],
"page": 1,
"pages": 1,
"results": 1
}
5 changes: 5 additions & 0 deletions test/unit/fixtures/region_availability_get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"region": "us-east",
"available": true,
"plan": "Linode 2GB"
}
17 changes: 17 additions & 0 deletions test/unit/fixtures/region_get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"id": "us-east",
"country": "US",
"capabilities": ["Linodes", "Block Storage", "Object Storage"],
"status": "ok",
"label": "Newark, NJ",
"site_type": "standard",
"resolvers": {
"ipv4": "8.8.8.8",
"ipv6": "2001:4860:4860::8888"
},
"placement_group_limits": {
"maximum_pgs_per_customer": 5,
"maximum_linodes_per_pg": 10
}
}

13 changes: 13 additions & 0 deletions test/unit/fixtures/regions_availability_list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"data": [
{
"region": "us-east",
"available": true,
"plan": "Linode 2GB"
}
],
"page": 1,
"pages": 1,
"results": 1
}

25 changes: 25 additions & 0 deletions test/unit/fixtures/regions_list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"data": [
{
"id": "us-east",
"country": "US",
"capabilities": ["Linodes", "Block Storage", "Object Storage"],
"status": "ok",
"label": "US East",
"site_type": "standard",
"resolvers": {
"ipv4": "8.8.8.8",
"ipv6": "2001:4860:4860::8888"
},
"placement_group_limits": {
"maximum_pgs_per_customer": 10,
"maximum_linodes_per_pg": 5
}
}
],
"page": 1,
"pages": 1,
"results": 1
}


18 changes: 18 additions & 0 deletions test/unit/fixtures/stackscript_get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"id": 123,
"username": "testuser",
"label": "new-stackscript",
"description": "Test Description",
"ordinal": 1,
"logo_url": "https://example.com/logo.png",
"images": ["linode/ubuntu20.04"],
"deployments_total": 10,
"deployments_active": 5,
"is_public": true,
"mine": true,
"rev_note": "Initial revision",
"script": "#!/bin/bash\necho Hello",
"user_defined_fields": [],
"user_gravatar_id": "abcdef123456"
}

5 changes: 5 additions & 0 deletions test/unit/fixtures/stackscript_revision.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": 123,
"label": "Updated Stackscript",
"rev_note": "Updated revision"
}
37 changes: 37 additions & 0 deletions test/unit/fixtures/stackscripts_list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"data": [
{
"id": 123,
"username": "testuser",
"label": "Test Stackscript",
"description": "A test Stackscript",
"images": ["linode/ubuntu20.04"],
"deployments_total": 10,
"deployments_active": 5,
"is_public": true,
"mine": true,
"rev_note": "Initial version",
"script": "#!/bin/bash\necho Hello",
"user_defined_fields": [],
"user_gravatar_id": "abc123"
},
{
"id": 456,
"username": "anotheruser",
"label": "Another Stackscript",
"description": "Another test Stackscript",
"images": ["linode/debian10"],
"deployments_total": 3,
"deployments_active": 1,
"is_public": false,
"mine": false,
"rev_note": "Another version",
"script": "#!/bin/bash\necho Another",
"user_defined_fields": [],
"user_gravatar_id": "xyz456"
}
],
"page": 1,
"pages": 1,
"results": 2
}
Loading
Loading