blob: 68ad4f9080ac00db54e4be712b907a6774a9b722 [file] [log] [blame]
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gcs
import (
"context"
"fmt"
"path"
"sort"
"testing"
"cos.googlesource.com/cos/tools.git/src/pkg/fakes"
"github.com/google/go-cmp/cmp"
)
// TestListGCSBucket is testing the functionality to lists all the objectNames in gcsBucket with prefix.
func TestListGCSBucket(t *testing.T) {
fakeGCS := fakes.GCSForTest(t)
fakeBucket := "cos-tools"
fakeGCSClient := fakeGCS.Client
ctx := context.Background()
var fakeData = []struct {
artifactPath string
artifactContent string
}{
{
"10000.00.00/lakitu/gpu_535_version",
"535.1.1",
},
{
"10000.00.00/lakitu/gpu_525_version",
"525.1.1",
},
{
"10000.00.00/lakitu/cpu_410_version",
"empty",
},
{
"10000.00.00/lakitu/empty_file",
"",
},
}
for _, data := range fakeData {
fakeGCS.Objects[path.Join("/", fakeBucket, data.artifactPath)] = []byte(data.artifactContent)
}
defer fakeGCS.Close()
var testCases = []struct {
prefix string
expected []string
}{
{
"10000",
[]string{"10000.00.00/lakitu/gpu_535_version", "10000.00.00/lakitu/gpu_525_version", "10000.00.00/lakitu/cpu_410_version", "10000.00.00/lakitu/empty_file"},
},
{
"10000.00.00/lakitu/cpu_",
[]string{"10000.00.00/lakitu/cpu_410_version"},
},
{
"10000.00.00/lakitu/empty_file",
[]string{"10000.00.00/lakitu/empty_file"},
},
{
"999",
nil,
},
}
for index, tc := range testCases {
t.Run(fmt.Sprintf("Test %d: List artifacts with prefix:%s.", index, tc.prefix), func(t *testing.T) {
actualResult, err := ListGCSBucket(ctx, fakeGCSClient, fakeBucket, tc.prefix)
if err != nil {
t.Fatalf("TestListArtifacts Failed: fail to list the artifact with prefix: %s from the fake GCS client in bucket: %s with error: %v", tc.prefix, fakeBucket, err)
}
sort.Strings(actualResult)
sort.Strings(tc.expected)
if !cmp.Equal(actualResult, tc.expected) {
t.Errorf("TestListArtifacts Failed: listing the artifact with prefix: %s from the fake GCS client in bucket: %s, the expected result is: %s, but the actual result is: %s.", tc.prefix, fakeBucket, tc.expected, actualResult)
}
})
}
}