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 1 commit
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
6 changes: 6 additions & 0 deletions test/unit/fixtures/region_availability_get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"region": "us-east",
"plan": "standard-2",
"available": true
}

16 changes: 16 additions & 0 deletions test/unit/fixtures/region_get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"id": "us-east",
"country": "US",
"capabilities": ["Linodes", "Block Storage", "Object Storage"],
"status": "ok",
"label": "US East",
"site_type": "standard",
"resolvers": {
"ipv4": "192.168.1.1",
"ipv6": "2600::1"
},
"placement_group_limits": {
"maximum_pgs_per_customer": 10,
"maximum_linodes_per_pg": 5
}
}
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",
"plan": "standard-2",
"available": true
}
],
"page": 1,
"pages": 1,
"results": 1
}

23 changes: 23 additions & 0 deletions test/unit/fixtures/regions_list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"data": [
{
"id": "us-east",
"country": "US",
"capabilities": ["Linodes", "Block Storage", "Object Storage"],
"status": "ok",
"label": "US East",
"site_type": "standard",
"resolvers": {
"ipv4": "192.168.1.1",
"ipv6": "2600::1"
},
"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
}
93 changes: 93 additions & 0 deletions test/unit/region_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package unit

import (
"context"
"fmt"
"testing"

"github.com/linode/linodego"
"github.com/stretchr/testify/assert"
"golang.org/x/exp/slices"
)

func TestListRegions(t *testing.T) {
// Load the fixture data for regions
fixtureData, err := fixtures.GetFixture("regions_list")
assert.NoError(t, err)

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

base.MockGet("regions", fixtureData)

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

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

// Check if a specific region exists using slices.ContainsFunc
exists := slices.ContainsFunc(regions, func(region linodego.Region) bool {
return region.ID == "us-east"
})
assert.True(t, exists, "Expected region list to contain 'us-east'")
}

func TestGetRegion(t *testing.T) {
// Load the fixture data for a specific region
fixtureData, err := fixtures.GetFixture("region_get")
assert.NoError(t, err)

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

regionID := "us-east"
base.MockGet(fmt.Sprintf("regions/%s", regionID), fixtureData)

region, err := base.Client.GetRegion(context.Background(), regionID)
assert.NoError(t, err)
assert.NotNil(t, region, "Expected region object to be returned")
assert.Equal(t, "us-east", region.ID, "Expected region ID to be 'us-east'")
vshanthe marked this conversation as resolved.
Show resolved Hide resolved
}

func TestListRegionsAvailability(t *testing.T) {
// Load the fixture data for region availability
fixtureData, err := fixtures.GetFixture("regions_availability_list")
assert.NoError(t, err)

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

base.MockGet("regions/availability", fixtureData)

availability, err := base.Client.ListRegionsAvailability(context.Background(), &linodego.ListOptions{})
assert.NoError(t, err)
assert.NotEmpty(t, availability, "Expected non-empty region availability list")

// Check if a specific region availability exists using slices.ContainsFunc
exists := slices.ContainsFunc(availability, func(a linodego.RegionAvailability) bool {
return a.Region == "us-east" && a.Available
})
assert.True(t, exists, "Expected region availability list to contain 'us-east' with available status")
}

func TestGetRegionAvailability(t *testing.T) {
// Load the fixture data for a specific region availability
fixtureData, err := fixtures.GetFixture("region_availability_get")
assert.NoError(t, err)

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

regionID := "us-east"
base.MockGet(fmt.Sprintf("regions/%s/availability", regionID), fixtureData)

availability, err := base.Client.GetRegionAvailability(context.Background(), regionID)
assert.NoError(t, err)
assert.NotNil(t, availability, "Expected region availability object to be returned")
assert.Equal(t, "us-east", availability.Region, "Expected region ID to be 'us-east'")
assert.True(t, availability.Available, "Expected region to be available")
}
122 changes: 122 additions & 0 deletions test/unit/stackscripts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package unit

import (
"context"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"github.com/linode/linodego"
"golang.org/x/exp/slices"
)

func TestListStackscripts(t *testing.T) {
// Mock the API response to match the expected structure for a paginated response
fixtureData, err := fixtures.GetFixture("stackscripts_list")
assert.NoError(t, err)

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

// Mock the request with a correct paginated structure
base.MockGet("linode/stackscripts", fixtureData)

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

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

// Check if a specific stackscript exists using slices.ContainsFunc
exists := slices.ContainsFunc(stackscripts, func(stackscript linodego.Stackscript) bool {
return stackscript.Label == "Test Stackscript"
})

assert.True(t, exists, "Expected stackscripts list to contain 'Test Stackscript'")
}

func TestCreateStackscript(t *testing.T) {
// Load the fixture data for stackscript creation
fixtureData, err := fixtures.GetFixture("stackscript_get")
assert.NoError(t, err)

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

base.MockPost("linode/stackscripts", fixtureData)

opts := linodego.StackscriptCreateOptions{
Label: "new-stackscript",
Description: "A new stackscript",
Images: []string{"linode/ubuntu20.04"},
IsPublic: true,
RevNote: "Initial revision",
Script: "#!/bin/bash\necho Hello",
}

stackscript, err := base.Client.CreateStackscript(context.Background(), opts)
assert.NoError(t, err, "Expected no error when creating stackscript")

// Verify the created stackscript's label
assert.Equal(t, "new-stackscript", stackscript.Label, "Expected created stackscript label to match input")
}

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

stackscriptID := 123
base.MockDelete(fmt.Sprintf("linode/stackscripts/%d", stackscriptID), nil)

err := base.Client.DeleteStackscript(context.Background(), stackscriptID)
assert.NoError(t, err, "Expected no error when deleting stackscript")
}

func TestGetStackscript(t *testing.T) {
// Load the fixture data for a single stackscript
fixtureData, err := fixtures.GetFixture("stackscript_get")
assert.NoError(t, err)

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

stackscriptID := 123
base.MockGet(fmt.Sprintf("linode/stackscripts/%d", stackscriptID), fixtureData)

stackscript, err := base.Client.GetStackscript(context.Background(), stackscriptID)
assert.NoError(t, err)

// Verify the stackscript's label
assert.Equal(t, "new-stackscript", stackscript.Label, "Expected stackscript label to match fixture")
}

func TestUpdateStackscript(t *testing.T) {
// Load the fixture data for stackscript update
fixtureData, err := fixtures.GetFixture("stackscript_revision")
assert.NoError(t, err)

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

stackscriptID := 123
base.MockPut(fmt.Sprintf("linode/stackscripts/%d", stackscriptID), fixtureData)

opts := linodego.StackscriptUpdateOptions{
Label: "Updated Stackscript",
Description: "Updated description",
Images: []string{"linode/ubuntu20.04"},
IsPublic: false,
RevNote: "Updated revision",
Script: "#!/bin/bash\necho Hello Updated",
}

updatedStackscript, err := base.Client.UpdateStackscript(context.Background(), stackscriptID, opts)
assert.NoError(t, err)

// Verify the updated stackscript's label
assert.Equal(t, "Updated Stackscript", updatedStackscript.Label, "Expected updated stackscript label to match input")
}
Loading