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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
package integration
import (
"context"
"slices"
"testing"
"github.com/linode/linodego"
. "github.com/linode/linodego"
)
var objectStorageBucketTestLabel = "go-bucket-test-def"
var testObjectStorageBucketCreateOpts = ObjectStorageBucketCreateOptions{
Cluster: "us-east-1",
Label: objectStorageBucketTestLabel,
}
var testRegionalObjectStorageBucketCreateOpts = ObjectStorageBucketCreateOptions{
Region: "us-east",
Label: objectStorageBucketTestLabel,
}
func TestObjectStorageBucket_Create_smoke(t *testing.T) {
_, bucket, teardown, err := setupObjectStorageBucket(t,
nil,
"fixtures/TestObjectStorageBucket_Create", nil, nil, nil)
defer teardown()
if err != nil {
t.Errorf("Error creating Object Storage Bucket, got error %v", err)
}
expected := testObjectStorageBucketCreateOpts
// when comparing fixtures to random value Label will differ, compare the known prefix
if bucket.Label != expected.Label ||
bucket.Cluster != expected.Cluster {
t.Errorf("Object Storage Bucket did not match CreateOptions")
}
assertDateSet(t, bucket.Created)
}
func TestObjectStorageBucket_Regional(t *testing.T) {
client, teardown := createTestClient(t, "fixtures/TestObjectStorageBucket_Regional")
regions := getRegionsWithCaps(t, client, []string{"Object Storage"})
if len(regions) < 1 {
t.Fatal("Can't get region with Object Storage capability")
}
region := regions[0]
client, bucket, teardown, err := setupObjectStorageBucket(t,
[]objectStorageBucketModifier{
func(opts *ObjectStorageBucketCreateOptions) {
opts.Cluster = ""
opts.Region = region
},
},
"fixtures/TestObjectStorageBucket_Regional",
client, teardown, nil,
)
defer teardown()
if err != nil {
t.Errorf("Error creating Object Storage Bucket, got error %v", err)
}
expected := testObjectStorageBucketCreateOpts
// when comparing fixtures to random value Label will differ, compare the known prefix
if bucket.Label != expected.Label ||
bucket.Region != region {
t.Errorf("Object Storage Bucket did not match CreateOptions")
}
assertDateSet(t, bucket.Created)
bucket, err = client.GetObjectStorageBucket(context.Background(), region, expected.Label)
if err != nil {
t.Errorf("Error getting Object Storage Bucket, %v", err)
}
}
func TestObjectStorageBucket_GetMissing(t *testing.T) {
client, bucket, teardown, err := setupObjectStorageBucket(t,
nil,
"fixtures/TestObjectStorageBucket_GetMissing", nil, nil, nil)
defer teardown()
sameLabel := bucket.Label
differentCluster := "us-west-1"
i, err := client.GetObjectStorageBucket(context.Background(), differentCluster, sameLabel)
if err == nil {
t.Errorf("should have received an error requesting a missing ObjectStorageBucket, got %v", i)
}
e, ok := err.(*Error)
if !ok {
t.Errorf("should have received an Error requesting a missing ObjectStorageBucket, got %v", e)
}
if e.Code != 404 {
t.Errorf("should have received a 404 Code requesting a missing ObjectStorageBucket, got %v", e.Code)
}
}
func TestObjectStorageBucket_GetFound(t *testing.T) {
client, bucket, teardown, err := setupObjectStorageBucket(t,
nil,
"fixtures/TestObjectStorageBucket_GetFound", nil, nil, nil)
defer teardown()
if err != nil {
t.Error(err)
}
i, err := client.GetObjectStorageBucket(context.Background(), bucket.Cluster, bucket.Label)
if err != nil {
t.Errorf("Error getting ObjectStorageBucket, expected struct, got %v and error %v", i, err)
}
if i.Label != bucket.Label {
t.Errorf("Expected a specific ObjectStorageBucket, but got a different one %v", i)
}
expected := testObjectStorageBucketCreateOpts
// when comparing fixtures to random value Label will differ, compare the known prefix
if bucket.Label != expected.Label ||
bucket.Cluster != expected.Cluster {
t.Errorf("Object Storage Bucket did not match CreateOptions")
}
}
func TestObjectStorageBuckets_List_smoke(t *testing.T) {
client, _, teardown, err := setupObjectStorageBucket(t,
nil,
"fixtures/TestObjectStorageBuckets_List", nil, nil, nil)
defer teardown()
if err != nil {
t.Fatalf("Failed to set up test: %v", err)
}
buckets, err := client.ListObjectStorageBuckets(context.Background(), nil)
if err != nil {
t.Fatalf("Error listing Object Storage Buckets: %v", err)
}
if len(buckets) == 0 {
t.Fatalf("Expected at least one Object Storage Bucket, but got none.")
}
for index, bucket := range buckets {
if bucket.Label == "" {
t.Errorf("Bucket at index %d is missing attributes: Label=%q, Full Bucket Data: %+v",
index, bucket.Label, bucket)
}
}
}
func TestObjectStorageBucketsInCluster_List(t *testing.T) {
client, bucket, teardown, err := setupObjectStorageBucket(t,
nil,
"fixtures/TestObjectStorageBucketsInCluster_List", nil, nil, nil)
defer teardown()
i, err := client.ListObjectStorageBucketsInCluster(context.Background(), nil, bucket.Cluster)
if err != nil {
t.Errorf("Error listing ObjectStorageBucketsInCluster, expected struct, got error %v", err)
}
if len(i) == 0 {
t.Errorf("Expected a list of ObjectStorageBucketsInCluster, but got none %v", i)
} else if i[0].Label == "" ||
i[0].Cluster == "" {
t.Errorf("Listed Object Storage Bucket in Cluster did not have attributes %v", i)
}
}
func TestObjectStorageBucket_Access_Get(t *testing.T) {
corsEnabled := false
createOpts := ObjectStorageBucketCreateOptions{
ACL: ACLAuthenticatedRead,
CorsEnabled: &corsEnabled,
}
endpointType := linodego.ObjectStorageEndpointE1
client, bucket, teardown, err := setupObjectStorageBucket(t,
[]objectStorageBucketModifier{
func(opts *ObjectStorageBucketCreateOptions) {
opts.ACL = createOpts.ACL
opts.CorsEnabled = createOpts.CorsEnabled
},
},
"fixtures/TestObjectStorageBucket_Access_Get", nil, nil,
&endpointType,
)
defer teardown()
newBucket, err := client.GetObjectStorageBucketAccess(context.Background(), bucket.Region, bucket.Label)
if err != nil {
t.Errorf("Error getting ObjectStorageBucket access, got error %s", err)
}
newBucketv2, err := client.GetObjectStorageBucketAccessV2(context.Background(), bucket.Region, bucket.Label)
if err != nil {
t.Errorf("Error getting ObjectStorageBucket access, got error %s", err)
}
if newBucket.CorsEnabled != corsEnabled {
t.Errorf("ObjectStorageBucket access CORS does not match update, expected %t, got %t", corsEnabled, newBucket.CorsEnabled)
}
if newBucketv2.CorsEnabled == nil {
t.Errorf("ObjectStorageBucket access CORS does not match update, expected %t, got nil", corsEnabled)
}
if newBucketv2.CorsEnabled != nil && *newBucketv2.CorsEnabled != corsEnabled {
t.Errorf("ObjectStorageBucket access CORS does not match update, expected %t, got %t", corsEnabled, *newBucketv2.CorsEnabled)
}
if newBucket.ACL != createOpts.ACL {
t.Errorf("ObjectStorageBucket access ACL does not match update, expected %s, got %s",
createOpts.ACL,
newBucket.ACL)
}
if newBucketv2.ACL != createOpts.ACL {
t.Errorf("ObjectStorageBucket access ACL does not match update, expected %s, got %s",
createOpts.ACL,
newBucketv2.ACL)
}
}
func TestObjectStorageBucket_Access_Update(t *testing.T) {
endpointType := linodego.ObjectStorageEndpointE1
client, bucket, teardown, err := setupObjectStorageBucket(t,
nil,
"fixtures/TestObjectStorageBucket_Access_Update",
nil, nil, &endpointType,
)
defer teardown()
corsEnabled := false
opts := ObjectStorageBucketUpdateAccessOptions{
ACL: ACLPrivate,
CorsEnabled: &corsEnabled,
}
err = client.UpdateObjectStorageBucketAccess(context.Background(), bucket.Region, bucket.Label, opts)
if err != nil {
t.Errorf("Error updating ObjectStorageBucket access, got error %s", err)
}
newBucket, err := client.GetObjectStorageBucketAccess(context.Background(), bucket.Region, bucket.Label)
if err != nil {
t.Errorf("Error getting ObjectStorageBucket access, got error %s", err)
}
if newBucket.CorsEnabled != corsEnabled {
t.Errorf("ObjectStorageBucket access CORS does not match update, expected %t, got %t", corsEnabled, newBucket.CorsEnabled)
}
if newBucket.ACL != opts.ACL {
t.Errorf("ObjectStorageBucket access ACL does not match update, expected %s, got %s", opts.ACL, newBucket.ACL)
}
}
type objectStorageBucketModifier func(*ObjectStorageBucketCreateOptions)
func setupObjectStorageBucket(
t *testing.T,
bucketModifiers []objectStorageBucketModifier,
fixturesYaml string,
client *Client,
teardown func(),
endpointType *linodego.ObjectStorageEndpointType,
) (*Client, *ObjectStorageBucket, func(), error) {
t.Helper()
if (client == nil) != (teardown == nil) {
t.Fatalf(
"The client and fixtureTeardown variables must either both be nil or both " +
"have a value. They cannot have one set to nil and the other set to a non-nil value.",
)
}
if client == nil {
client, teardown = createTestClient(t, fixturesYaml)
}
createOpts := testRegionalObjectStorageBucketCreateOpts
if endpointType != nil {
endpoints, err := client.ListObjectStorageEndpoints(context.Background(), nil)
if err != nil {
t.Fatalf("Error listing endpoints: %s", err)
} else {
selectedEndpoint := endpoints[slices.IndexFunc(endpoints, func(e linodego.ObjectStorageEndpoint) bool {
return e.EndpointType == linodego.ObjectStorageEndpointE1
})]
createOpts.Region = selectedEndpoint.Region
createOpts.EndpointType = selectedEndpoint.EndpointType
}
}
for _, modifier := range bucketModifiers {
modifier(&createOpts)
}
bucket, err := client.CreateObjectStorageBucket(context.Background(), createOpts)
if err != nil {
t.Fatalf("Error creating test Bucket: %s", err)
}
newTeardown := func() {
if err := client.DeleteObjectStorageBucket(context.Background(), bucket.Cluster, bucket.Label); err != nil {
if t != nil {
t.Errorf("Error deleting test Bucket: %s", err)
}
}
teardown()
}
return client, bucket, newTeardown, err
}
|