Path: blob/main/components/content-service/pkg/storage/storage_test.go
2501 views
// Copyright (c) 2021 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 storage56import (7"errors"8"testing"910"golang.org/x/xerrors"11)1213func TestBlobObjectName(t *testing.T) {14tests := []struct {15Name string16Input string17ExpectedBlobName string18ExpectedError error19}{20{21Name: "simple name",22Input: "my-simple-name",23ExpectedBlobName: "blobs/my-simple-name",24},25{26Name: "name with slash",27Input: "my-object-name/with-slash",28ExpectedBlobName: "blobs/my-object-name/with-slash",29},30{31Name: "name with whitespace",32Input: "name with whitespace",33ExpectedError: invalidNameError("name with whitespace"),34},35{36Name: "name with invalid char",37Input: "ä-is-invalid",38ExpectedError: invalidNameError("ä-is-invalid"),39},40}4142for _, test := range tests {43t.Run(test.Name, func(t *testing.T) {44actualBlobName, err := blobObjectName(test.Input)45if actualBlobName != test.ExpectedBlobName {46t.Fatalf("unexpected object name: is '%s' but expected '%s'", actualBlobName, test.ExpectedBlobName)47}48if !equivalentError(err, test.ExpectedError) {49t.Fatalf("unexpected error: is '%v' but expected '%v'", err, test.ExpectedError)50}51})52}53}5455func invalidNameError(name string) error {56return xerrors.Errorf(`blob name '%s' needs to match regex '^[a-zA-Z0-9._\-\/]+$'`, name)57}5859func equivalentError(e1 error, e2 error) bool {60if e1 == e2 {61return true62}63if errors.Is(e1, e2) {64return true65}66if e1 == nil || e2 == nil {67return false68}69if e1.Error() == e2.Error() {70return true71}72return false73}747576