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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
package integration
import (
"bytes"
"context"
"slices"
"testing"
"github.com/dnaeon/go-vcr/recorder"
. "github.com/linode/linodego"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// testImageBytes is a minimal Gzipped image.
// This is necessary because the API will reject invalid images.
var testImageBytes = []byte{
0x1f, 0x8b, 0x08, 0x08, 0xbd, 0x5c, 0x91, 0x60,
0x00, 0x03, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x69, 0x6d, 0x67, 0x00, 0x03, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
}
func TestImage_GetMissing(t *testing.T) {
client, teardown := createTestClient(t, "fixtures/TestImage_GetMissing")
defer teardown()
i, err := client.GetImage(context.Background(), "does-not-exist")
if err == nil {
t.Errorf("should have received an error requesting a missing image, got %v", i)
}
e, ok := err.(*Error)
if !ok {
t.Errorf("should have received an Error requesting a missing image, got %v", e)
}
if e.Code != 404 {
t.Errorf("should have received a 404 Code requesting a missing image, got %v", e.Code)
}
}
func TestImage_GetFound(t *testing.T) {
client, teardown := createTestClient(t, "fixtures/TestImage_GetFound")
defer teardown()
i, err := client.GetImage(context.Background(), "linode/ubuntu22.04")
if i.Created == nil || i.EOL == nil || i.Updated == nil {
t.Errorf("Error parsing time, %v, %v, %v", i.Created, i.EOL, i.Updated)
}
if err != nil {
t.Errorf("Error getting image, expected struct, got %v and error %v", i, err)
}
if i.ID != "linode/ubuntu22.04" {
t.Errorf("Expected a specific image, but got a different one %v", i)
}
}
func TestImages_List_smoke(t *testing.T) {
client, teardown := createTestClient(t, "fixtures/TestImages_List")
defer teardown()
i, err := client.ListImages(context.Background(), nil)
if err != nil {
t.Errorf("Error listing images, expected struct, got error %v", err)
}
if len(i) == 0 {
t.Errorf("Expected a list of images, but got none %v", i)
}
}
func TestImage_Upload(t *testing.T) {
client, teardown := createTestClient(t, "fixtures/TestImage_Upload")
defer teardown()
image, uploadURL, err := client.CreateImageUpload(context.Background(), ImageCreateUploadOptions{
Region: "us-ord",
Label: "linodego-image-test",
Description: "An image that does stuff.",
})
if err != nil {
t.Errorf("Failed to create image upload: %v", err)
}
defer func() {
if err := client.DeleteImage(context.Background(), image.ID); err != nil {
t.Errorf("Failed to delete image %s: %v", image.ID, err)
}
}()
if uploadURL == "" {
t.Errorf("Expected upload URL, got none")
}
if _, err := client.WaitForImageStatus(context.Background(), image.ID, ImageStatusPendingUpload, 60); err != nil {
t.Errorf("Failed to wait for image pending upload status: %v", err)
}
// Because this request currently bypasses the recorder, we should only run it when the recorder is recording
if testingMode != recorder.ModeReplaying {
if err := client.UploadImageToURL(context.Background(), uploadURL, bytes.NewReader(testImageBytes)); err != nil {
t.Errorf("failed to upload image: %v", err)
}
}
if _, err := client.WaitForImageStatus(context.Background(), image.ID, ImageStatusAvailable, 240); err != nil {
t.Errorf("Failed to wait for image available upload status: %v", err)
}
}
func TestImage_CreateUpload(t *testing.T) {
client, teardown := createTestClient(t, "fixtures/TestImage_CreateUpload")
defer teardown()
image, uploadURL, err := client.CreateImageUpload(context.Background(), ImageCreateUploadOptions{
Region: getRegionsWithCaps(t, client, []string{"Metadata"})[0],
Label: "linodego-image-create-upload",
Description: "An image that does stuff.",
CloudInit: true,
Tags: &[]string{"foo", "bar"},
})
if err != nil {
t.Errorf("Failed to create image upload: %v", err)
}
defer func() {
if err := client.DeleteImage(context.Background(), image.ID); err != nil {
t.Errorf("Failed to delete image %s: %v", image.ID, err)
}
}()
assertSliceContains(t, image.Capabilities, "cloud-init")
if uploadURL == "" {
t.Errorf("Expected upload URL, got none")
}
require.NotNil(t, image.Tags)
}
func TestImage_CloudInit(t *testing.T) {
client, instance, teardown, err := setupInstance(
t, "fixtures/TestImage_CloudInit", true,
func(client *Client, options *InstanceCreateOptions) {
options.Region = getRegionsWithCaps(t, client, []string{"Metadata"})[0]
})
if err != nil {
t.Fatal(err)
}
t.Cleanup(teardown)
instanceDisks, err := client.ListInstanceDisks(
context.Background(),
instance.ID,
nil,
)
if err != nil {
t.Fatal(err)
}
image, err := client.CreateImage(context.Background(), ImageCreateOptions{
DiskID: instanceDisks[0].ID,
Label: "linodego-test-cloud-init",
CloudInit: true,
Tags: &[]string{"test1", "test2"},
})
if err != nil {
t.Errorf("Failed to create image: %v", err)
}
t.Cleanup(func() {
if err := client.DeleteImage(context.Background(), image.ID); err != nil {
t.Errorf("Failed to delete image %s: %v", image.ID, err)
}
})
assertSliceContains(t, image.Capabilities, "cloud-init")
slices.Sort(image.Tags)
require.Equal(t, image.Tags, []string{"test1", "test2"})
}
func TestImage_Replicate(t *testing.T) {
client, teardown := createTestClient(t, "fixtures/TestImage_Replicate")
defer teardown()
availableRegions := getRegionsWithCapsAndSiteType(t, client, []string{"Object Storage"}, "core")
image, uploadURL, err := client.CreateImageUpload(context.Background(), ImageCreateUploadOptions{
Region: availableRegions[1],
Label: "linodego-image-replication",
Description: "An image that does stuff.",
})
if err != nil {
t.Errorf("Failed to create image upload: %v", err)
}
defer func() {
if err := client.DeleteImage(context.Background(), image.ID); err != nil {
t.Errorf("Failed to delete image %s: %v", image.ID, err)
}
}()
if uploadURL == "" {
t.Errorf("Expected upload URL, got none")
}
if _, err := client.WaitForImageStatus(context.Background(), image.ID, ImageStatusPendingUpload, 60); err != nil {
t.Errorf("Failed to wait for image pending upload status: %v", err)
}
// Because this request currently bypasses the recorder, we should only run it when the recorder is recording
if testingMode != recorder.ModeReplaying {
if err := client.UploadImageToURL(context.Background(), uploadURL, bytes.NewReader(testImageBytes)); err != nil {
t.Errorf("failed to upload image: %v", err)
}
}
if _, err := client.WaitForImageStatus(context.Background(), image.ID, ImageStatusAvailable, 400); err != nil {
t.Errorf("Failed to wait for image available upload status: %v", err)
}
replicaRegions := availableRegions[:2]
image, err = client.ReplicateImage(context.Background(), image.ID, ImageReplicateOptions{
Regions: replicaRegions,
})
require.NoError(t, err)
require.Len(t, image.Regions, len(replicaRegions))
for _, region := range image.Regions {
assert.Contains(t, replicaRegions, region.Region)
}
}
|