Path: blob/main/components/content-service/pkg/storage/suite_test.go
2501 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package storage_test56import (7"context"8"testing"910"github.com/gitpod-io/gitpod/content-service/pkg/storage"11)1213type TestablePresignedAccess interface {14storage.PresignedAccess1516ForTestCreateObj(ctx context.Context, bucket, path, content string) error17ForTestReset(ctx context.Context) error18}1920func SuiteTestPresignedAccess(t *testing.T, ps TestablePresignedAccess) {21tests := []struct {22Name string23Test func(t *testing.T, ps TestablePresignedAccess)24}{25{26Name: "happy path",27Test: testPresignedHappyPath,28},29}30for _, test := range tests {31t.Run(test.Name, func(t *testing.T) {32test.Test(t, ps)33})34}35}3637func testPresignedHappyPath(t *testing.T, ps TestablePresignedAccess) {38ctx := context.Background()39failOnErr(t, ps.ForTestReset(ctx))4041const (42bucket = "test-bucket"43path = "foo/bar.txt"44)4546failOnErr(t, ps.ForTestCreateObj(ctx, bucket, path, "hello world"))4748tests := []struct {49Name string50Test func(t *testing.T, os TestablePresignedAccess)51}{52{53Name: "ObjectExists",54Test: func(t *testing.T, os TestablePresignedAccess) {55exists, err := ps.ObjectExists(ctx, bucket, path)56failOnErr(t, err)57if !exists {58t.Errorf("expected %s/%s to exist - it did not", bucket, path)59}60},61},62{63Name: "ObjectHash",64Test: func(t *testing.T, os TestablePresignedAccess) {6566hash, err := ps.ObjectHash(ctx, bucket, path)67failOnErr(t, err)68if hash == "" {69t.Errorf("expected non-empty object hash")70}71},72},73{74Name: "DiskUsage",75Test: func(t *testing.T, os TestablePresignedAccess) {76size, err := ps.DiskUsage(ctx, bucket, path)77failOnErr(t, err)78if size == 0 {79t.Errorf("expected non-zero disk usage")80}81},82},83{84Name: "SignDownload",85Test: func(t *testing.T, os TestablePresignedAccess) {86nfo, err := ps.SignDownload(ctx, bucket, path, &storage.SignedURLOptions{})87failOnErr(t, err)88switch {89case nfo == nil:90t.Errorf("expected non-nil download info")91case nfo.URL == "":92t.Errorf("expected non-empty download URL")93case nfo.Size == 0:94t.Errorf("expected non-zero object size")95}96},97},98{99Name: "SignUpload",100Test: func(t *testing.T, os TestablePresignedAccess) {101nfo, err := ps.SignUpload(ctx, bucket, path, &storage.SignedURLOptions{})102failOnErr(t, err)103switch {104case nfo == nil:105t.Errorf("expected non-nil download info")106case nfo.URL == "":107t.Errorf("expected non-empty download URL")108}109},110},111}112113for _, test := range tests {114t.Run(test.Name, func(t *testing.T) {115test.Test(t, ps)116})117}118}119120func failOnErr(t *testing.T, err error) {121if err != nil {122t.Fatal(err)123}124}125126127