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
|
package unit
import (
"context"
"fmt"
"testing"
"github.com/linode/linodego"
"github.com/stretchr/testify/assert"
"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")
}
|