Skip to content

Commit

Permalink
all: fix test lint issues detecte by tenv and thelper linters (#3441)
Browse files Browse the repository at this point in the history
  • Loading branch information
peczenyj committed Jun 3, 2024
1 parent e0e5901 commit 19db7d5
Show file tree
Hide file tree
Showing 36 changed files with 380 additions and 72 deletions.
32 changes: 14 additions & 18 deletions blob/azureblob/azureblob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ type harness struct {
}

func newHarness(ctx context.Context, t *testing.T) (drivertest.Harness, error) {
t.Helper()

var key string
if *setup.Record {
name := os.Getenv("AZURE_STORAGE_ACCOUNT")
Expand Down Expand Up @@ -401,21 +403,21 @@ func TestOpenerFromEnv(t *testing.T) {
},
}
for _, test := range tests {
os.Setenv("AZURE_STORAGE_ACCOUNT", test.accountName)
os.Setenv("AZURE_STORAGE_KEY", test.accountKey)
os.Setenv("AZURE_STORAGE_SAS_TOKEN", test.sasToken)
os.Setenv("AZURE_STORAGE_CONNECTION_STRING", test.connectionString)
os.Setenv("AZURE_STORAGE_DOMAIN", test.domain)
os.Setenv("AZURE_STORAGE_PROTOCOL", test.protocol)
t.Setenv("AZURE_STORAGE_ACCOUNT", test.accountName)
t.Setenv("AZURE_STORAGE_KEY", test.accountKey)
t.Setenv("AZURE_STORAGE_SAS_TOKEN", test.sasToken)
t.Setenv("AZURE_STORAGE_CONNECTION_STRING", test.connectionString)
t.Setenv("AZURE_STORAGE_DOMAIN", test.domain)
t.Setenv("AZURE_STORAGE_PROTOCOL", test.protocol)
if test.isCDN {
os.Setenv("AZURE_STORAGE_IS_CDN", "true")
t.Setenv("AZURE_STORAGE_IS_CDN", "true")
} else {
os.Setenv("AZURE_STORAGE_IS_CDN", "")
t.Setenv("AZURE_STORAGE_IS_CDN", "")
}
if test.isLocalEmulator {
os.Setenv("AZURE_STORAGE_IS_LOCAL_EMULATOR", "true")
t.Setenv("AZURE_STORAGE_IS_LOCAL_EMULATOR", "true")
} else {
os.Setenv("AZURE_STORAGE_IS_LOCAL_EMULATOR", "")
t.Setenv("AZURE_STORAGE_IS_LOCAL_EMULATOR", "")
}

got := newCredInfoFromEnv()
Expand Down Expand Up @@ -603,14 +605,8 @@ func TestNewServiceURL(t *testing.T) {
}

func TestOpenBucketFromURL(t *testing.T) {
prevAccount := os.Getenv("AZURE_STORAGE_ACCOUNT")
prevKey := os.Getenv("AZURE_STORAGE_KEY")
os.Setenv("AZURE_STORAGE_ACCOUNT", "my-account")
os.Setenv("AZURE_STORAGE_KEY", "bXlrZXk=") // mykey base64 encoded
defer func() {
os.Setenv("AZURE_STORAGE_ACCOUNT", prevAccount)
os.Setenv("AZURE_STORAGE_KEY", prevKey)
}()
t.Setenv("AZURE_STORAGE_ACCOUNT", "my-account")
t.Setenv("AZURE_STORAGE_KEY", "bXlrZXk=") // mykey base64 encoded

tests := []struct {
URL string
Expand Down
2 changes: 2 additions & 0 deletions blob/blob_fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ var fsFiles = []string{
}

func initBucket(t *testing.T, files []string) *blob.Bucket {
t.Helper()

ctx := context.Background()

b := memblob.OpenBucket(nil)
Expand Down
64 changes: 62 additions & 2 deletions blob/drivertest/drivertest.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ func (verifyAsFailsOnNil) ListObjectCheck(o *blob.ListObject) error {

// RunConformanceTests runs conformance tests for driver implementations of blob.
func RunConformanceTests(t *testing.T, newHarness HarnessMaker, asTests []AsTest) {
t.Helper()

t.Run("TestNonexistentBucket", func(t *testing.T) {
testNonexistentBucket(t, newHarness)
})
Expand Down Expand Up @@ -273,6 +275,8 @@ func RunConformanceTests(t *testing.T, newHarness HarnessMaker, asTests []AsTest

// RunBenchmarks runs benchmarks for driver implementations of blob.
func RunBenchmarks(b *testing.B, bkt *blob.Bucket) {
b.Helper()

b.Run("BenchmarkRead", func(b *testing.B) {
benchmarkRead(b, bkt)
})
Expand All @@ -283,6 +287,8 @@ func RunBenchmarks(b *testing.B, bkt *blob.Bucket) {

// testNonexistentBucket tests the functionality of IsAccessible.
func testNonexistentBucket(t *testing.T, newHarness HarnessMaker) {
t.Helper()

ctx := context.Background()
h, err := newHarness(ctx, t)
if err != nil {
Expand Down Expand Up @@ -331,11 +337,15 @@ func testNonexistentBucket(t *testing.T, newHarness HarnessMaker) {

// testList tests the functionality of List.
func testList(t *testing.T, newHarness HarnessMaker) {
t.Helper()

const keyPrefix = "blob-for-list"
content := []byte("hello")

keyForIndex := func(i int) string { return fmt.Sprintf("%s-%d", keyPrefix, i) }
gotIndices := func(t *testing.T, objs []*driver.ListObject) []int {
t.Helper()

var got []int
for _, obj := range objs {
if !strings.HasPrefix(obj.Key, keyPrefix) {
Expand Down Expand Up @@ -407,6 +417,8 @@ func testList(t *testing.T, newHarness HarnessMaker) {
// from List. The very first time the test is run against a Bucket, it may be
// flaky due to this race.
init := func(t *testing.T) (driver.Bucket, func()) {
t.Helper()

h, err := newHarness(ctx, t)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -560,6 +572,8 @@ func testList(t *testing.T, newHarness HarnessMaker) {

// testListWeirdKeys tests the functionality of List on weird keys.
func testListWeirdKeys(t *testing.T, newHarness HarnessMaker) {
t.Helper()

const keyPrefix = "list-weirdkeys-"
content := []byte("hello")
ctx := context.Background()
Expand All @@ -577,6 +591,8 @@ func testListWeirdKeys(t *testing.T, newHarness HarnessMaker) {
// from List. The very first time the test is run against a Bucket, it may be
// flaky due to this race.
init := func(t *testing.T) (*blob.Bucket, func()) {
t.Helper()

h, err := newHarness(ctx, t)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -658,6 +674,8 @@ func doList(ctx context.Context, b *blob.Bucket, prefix, delim string, recurse b

// testListDelimiters tests the functionality of List using Delimiters.
func testListDelimiters(t *testing.T, newHarness HarnessMaker) {
t.Helper()

const keyPrefix = "blob-for-delimiters-"
content := []byte("hello")

Expand Down Expand Up @@ -876,6 +894,8 @@ func testListDelimiters(t *testing.T, newHarness HarnessMaker) {
// from List. The very first time the test is run against a Bucket, it may be
// flaky due to this race.
init := func(t *testing.T, delim string) (driver.Bucket, *blob.Bucket, func()) {
t.Helper()

h, err := newHarness(ctx, t)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -982,6 +1002,8 @@ func testListDelimiters(t *testing.T, newHarness HarnessMaker) {
// less than the delimiter.
// See https://github.com/google/go-cloud/issues/3089.
func testDirsWithCharactersBeforeDelimiter(t *testing.T, newHarness HarnessMaker) {
t.Helper()

const keyPrefix = "blob-for-dirs-with-chars-before-delimiter/"
content := []byte("hello")

Expand Down Expand Up @@ -1058,6 +1080,8 @@ func testDirsWithCharactersBeforeDelimiter(t *testing.T, newHarness HarnessMaker
}

func iterToSetOfKeys(ctx context.Context, t *testing.T, iter *blob.ListIterator) map[string]bool {
t.Helper()

retval := map[string]bool{}
for {
if item, err := iter.Next(ctx); err == io.EOF {
Expand All @@ -1073,6 +1097,8 @@ func iterToSetOfKeys(ctx context.Context, t *testing.T, iter *blob.ListIterator)

// testRead tests the functionality of NewReader, NewRangeReader, and Reader.
func testRead(t *testing.T, newHarness HarnessMaker) {
t.Helper()

const key = "blob-for-reading"
content := []byte("abcdefghijklmnopqurstuvwxyz")
contentSize := int64(len(content))
Expand Down Expand Up @@ -1143,6 +1169,8 @@ func testRead(t *testing.T, newHarness HarnessMaker) {

// Creates a blob for sub-tests below.
init := func(t *testing.T, skipCreate bool) (*blob.Bucket, func()) {
t.Helper()

h, err := newHarness(ctx, t)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -1218,6 +1246,8 @@ func testRead(t *testing.T, newHarness HarnessMaker) {

// testAttributes tests Attributes.
func testAttributes(t *testing.T, newHarness HarnessMaker) {
t.Helper()

const (
dirKey = "someDir"
key = dirKey + "/blob-for-attributes"
Expand All @@ -1233,6 +1263,8 @@ func testAttributes(t *testing.T, newHarness HarnessMaker) {

// Creates a blob for sub-tests below.
init := func(t *testing.T) (*blob.Bucket, func()) {
t.Helper()

h, err := newHarness(ctx, t)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -1348,16 +1380,20 @@ func testAttributes(t *testing.T, newHarness HarnessMaker) {
}

// loadTestData loads test data, inlined using go-bindata.
func loadTestData(t testing.TB, name string) []byte {
func loadTestData(tb testing.TB, name string) []byte {
tb.Helper()

data, err := Asset(name)
if err != nil {
t.Fatal(err)
tb.Fatal(err)
}
return data
}

// testWrite tests the functionality of NewWriter and Writer.
func testWrite(t *testing.T, newHarness HarnessMaker) {
t.Helper()

const key = "blob-for-reading"
const existingContent = "existing content"
smallText := loadTestData(t, "test-small.txt")
Expand Down Expand Up @@ -1576,6 +1612,8 @@ func testWrite(t *testing.T, newHarness HarnessMaker) {

// testCanceledWrite tests the functionality of canceling an in-progress write.
func testCanceledWrite(t *testing.T, newHarness HarnessMaker) {
t.Helper()

const key = "blob-for-canceled-write"
content := []byte("hello world")
cancelContent := []byte("going to cancel")
Expand Down Expand Up @@ -1689,6 +1727,8 @@ func testCanceledWrite(t *testing.T, newHarness HarnessMaker) {

// testMetadata tests writing and reading the key/value metadata for a blob.
func testMetadata(t *testing.T, newHarness HarnessMaker) {
t.Helper()

const key = "blob-for-metadata"
hello := []byte("hello")

Expand Down Expand Up @@ -1812,6 +1852,8 @@ func testMetadata(t *testing.T, newHarness HarnessMaker) {

// testMD5 tests reading MD5 hashes via List and Attributes.
func testMD5(t *testing.T, newHarness HarnessMaker) {
t.Helper()

ctx := context.Background()

// Define two blobs with different content; we'll write them and then verify
Expand Down Expand Up @@ -1888,6 +1930,8 @@ func testMD5(t *testing.T, newHarness HarnessMaker) {

// testCopy tests the functionality of Copy.
func testCopy(t *testing.T, newHarness HarnessMaker) {
t.Helper()

const (
srcKey = "blob-for-copying-src"
dstKey = "blob-for-copying-dest"
Expand Down Expand Up @@ -2018,6 +2062,8 @@ func testCopy(t *testing.T, newHarness HarnessMaker) {

// testDelete tests the functionality of Delete.
func testDelete(t *testing.T, newHarness HarnessMaker) {
t.Helper()

const key = "blob-for-deleting"

ctx := context.Background()
Expand Down Expand Up @@ -2089,6 +2135,8 @@ func testDelete(t *testing.T, newHarness HarnessMaker) {
// testConcurrentWriteAndRead tests that concurrent writing to multiple blob
// keys and concurrent reading from multiple blob keys works.
func testConcurrentWriteAndRead(t *testing.T, newHarness HarnessMaker) {
t.Helper()

ctx := context.Background()
h, err := newHarness(ctx, t)
if err != nil {
Expand Down Expand Up @@ -2171,6 +2219,8 @@ func testConcurrentWriteAndRead(t *testing.T, newHarness HarnessMaker) {
// these are implemented in the concrete type, but drivers that implement Reader.Download
// and/or Writer.Upload will have those called directly.
func testUploadDownload(t *testing.T, newHarness HarnessMaker) {
t.Helper()

const key = "blob-for-upload-download"
const contents = "up and down"
ctx := context.Background()
Expand Down Expand Up @@ -2205,6 +2255,8 @@ func testUploadDownload(t *testing.T, newHarness HarnessMaker) {

// testKeys tests a variety of weird keys.
func testKeys(t *testing.T, newHarness HarnessMaker) {
t.Helper()

const keyPrefix = "weird-keys"
content := []byte("hello")
ctx := context.Background()
Expand Down Expand Up @@ -2326,6 +2378,8 @@ func testKeys(t *testing.T, newHarness HarnessMaker) {

// testSignedURL tests the functionality of SignedURL.
func testSignedURL(t *testing.T, newHarness HarnessMaker) {
t.Helper()

const key = "blob-for-signing"
const contents = "hello world"

Expand Down Expand Up @@ -2557,6 +2611,8 @@ func testSignedURL(t *testing.T, newHarness HarnessMaker) {

// testAs tests the various As functions, using AsTest.
func testAs(t *testing.T, newHarness HarnessMaker, st AsTest) {
t.Helper()

const (
dir = "mydir"
key = dir + "/as-test"
Expand Down Expand Up @@ -2672,6 +2728,8 @@ func testAs(t *testing.T, newHarness HarnessMaker, st AsTest) {
}

func benchmarkRead(b *testing.B, bkt *blob.Bucket) {
b.Helper()

ctx := context.Background()
const key = "readbenchmark-blob"

Expand Down Expand Up @@ -2707,6 +2765,8 @@ func benchmarkRead(b *testing.B, bkt *blob.Bucket) {
}

func benchmarkWriteReadDelete(b *testing.B, bkt *blob.Bucket) {
b.Helper()

ctx := context.Background()
const baseKey = "writereaddeletebenchmark-blob-"

Expand Down
10 changes: 10 additions & 0 deletions blob/fileblob/fileblob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ type harness struct {
}

func newHarness(ctx context.Context, t *testing.T, prefix string, metadataHow metadataOption, noTempDir bool) (drivertest.Harness, error) {
t.Helper()

if metadataHow == MetadataDontWrite {
// Skip tests for if no metadata gets written.
// For these it is currently undefined whether any gets read (back).
Expand Down Expand Up @@ -172,13 +174,17 @@ func (h *harness) Close() {

func TestConformance(t *testing.T) {
newHarnessNoPrefix := func(ctx context.Context, t *testing.T) (drivertest.Harness, error) {
t.Helper()

return newHarness(ctx, t, "", MetadataInSidecar, false)
}
drivertest.RunConformanceTests(t, newHarnessNoPrefix, []drivertest.AsTest{verifyAs{}})
}

func TestConformanceNoTempDir(t *testing.T) {
newHarnessNoTmpDir := func(ctx context.Context, t *testing.T) (drivertest.Harness, error) {
t.Helper()

return newHarness(ctx, t, "", MetadataInSidecar, true)
}
drivertest.RunConformanceTests(t, newHarnessNoTmpDir, []drivertest.AsTest{verifyAs{}})
Expand All @@ -187,13 +193,17 @@ func TestConformanceNoTempDir(t *testing.T) {
func TestConformanceWithPrefix(t *testing.T) {
const prefix = "some/prefix/dir/"
newHarnessWithPrefix := func(ctx context.Context, t *testing.T) (drivertest.Harness, error) {
t.Helper()

return newHarness(ctx, t, prefix, MetadataInSidecar, false)
}
drivertest.RunConformanceTests(t, newHarnessWithPrefix, []drivertest.AsTest{verifyAs{prefix: prefix}})
}

func TestConformanceSkipMetadata(t *testing.T) {
newHarnessSkipMetadata := func(ctx context.Context, t *testing.T) (drivertest.Harness, error) {
t.Helper()

return newHarness(ctx, t, "", MetadataDontWrite, false)
}
drivertest.RunConformanceTests(t, newHarnessSkipMetadata, []drivertest.AsTest{verifyAs{}})
Expand Down
2 changes: 2 additions & 0 deletions blob/gcsblob/gcsblob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ type harness struct {
}

func newHarness(ctx context.Context, t *testing.T) (drivertest.Harness, error) {
t.Helper()

opts := &Options{GoogleAccessID: serviceAccountID}
if *setup.Record {
if *pathToPrivateKey == "" {
Expand Down
Loading

0 comments on commit 19db7d5

Please sign in to comment.