Skip to content

Commit

Permalink
all: fix deprecated code and small lint issues detected by staticcheck (
Browse files Browse the repository at this point in the history
  • Loading branch information
peczenyj authored Jun 3, 2024
1 parent 9cc772b commit 439229c
Show file tree
Hide file tree
Showing 46 changed files with 177 additions and 197 deletions.
3 changes: 1 addition & 2 deletions aws/rds/rds.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"encoding/pem"
"fmt"
"io"
"io/ioutil"
"net/http"

"github.com/google/wire"
Expand Down Expand Up @@ -79,7 +78,7 @@ func (cf *CertFetcher) Fetch(ctx context.Context) ([]*x509.Certificate, error) {
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("fetch RDS certificates: HTTP %s", resp.Status)
}
pemData, err := ioutil.ReadAll(&io.LimitedReader{R: resp.Body, N: 1 << 20}) // limit to 1MiB
pemData, err := io.ReadAll(&io.LimitedReader{R: resp.Body, N: 1 << 20}) // limit to 1MiB
if err != nil {
return nil, fmt.Errorf("fetch RDS certificates: %v", err)
}
Expand Down
3 changes: 1 addition & 2 deletions azure/azuredb/azuredb.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"encoding/pem"
"fmt"
"io"
"io/ioutil"
"net/http"

"golang.org/x/net/context/ctxhttp"
Expand Down Expand Up @@ -70,7 +69,7 @@ func (cf *CertFetcher) Fetch(ctx context.Context) ([]*x509.Certificate, error) {
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("fetch Azure certificates: HTTP %s", resp.Status)
}
pemData, err := ioutil.ReadAll(&io.LimitedReader{R: resp.Body, N: 1 << 20}) // limit to 1MiB
pemData, err := io.ReadAll(&io.LimitedReader{R: resp.Body, N: 1 << 20}) // limit to 1MiB
if err != nil {
return nil, fmt.Errorf("fetch Azure certificates: %v", err)
}
Expand Down
3 changes: 1 addition & 2 deletions blob/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ import (
"fmt"
"hash"
"io"
"io/ioutil"
"log"
"mime"
"net/http"
Expand Down Expand Up @@ -732,7 +731,7 @@ func (b *Bucket) ReadAll(ctx context.Context, key string) (_ []byte, err error)
return nil, err
}
defer r.Close()
return ioutil.ReadAll(r)
return io.ReadAll(r)
}

// Download writes the content of a blob into an io.Writer w.
Expand Down
38 changes: 26 additions & 12 deletions blob/drivertest/drivertest.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
Expand Down Expand Up @@ -2123,34 +2122,49 @@ func testConcurrentWriteAndRead(t *testing.T, newHarness HarnessMaker) {

var wg sync.WaitGroup

errs := make(chan error, numKeys)

// Write all blobs concurrently.
for k := 0; k < numKeys; k++ {
wg.Add(1)
go func(key int) {
if err := b.WriteAll(ctx, blobName(key), keyData[key], nil); err != nil {
t.Fatal(err)
errs <- fmt.Errorf("WriteAll key=%v: %w", key, err)
}
wg.Done()
}(k)
defer b.Delete(ctx, blobName(k))
}
wg.Wait()

close(errs)

for err := range errs {
t.Fatalf("got error from concurrent blob write: %v", err)
}

errs = make(chan error, numKeys)

// Read all blobs concurrently and verify that they contain the expected data.
for k := 0; k < numKeys; k++ {
wg.Add(1)
go func(key int) {
buf, err := b.ReadAll(ctx, blobName(key))
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(buf, keyData[key]) {
t.Errorf("read data mismatch for key %d", key)
errs <- err
} else if !bytes.Equal(buf, keyData[key]) {
errs <- fmt.Errorf("read data mismatch for key %d", key)
}
wg.Done()
}(k)
}
wg.Wait()

close(errs)

for err := range errs {
t.Fatalf("got error from concurrent blob write: %v", err)
}
}

// testUploadDownload tests that Upload and Download work. For many drivers,
Expand Down Expand Up @@ -2275,7 +2289,7 @@ func testKeys(t *testing.T, newHarness HarnessMaker) {
if resp.StatusCode != 200 {
t.Errorf("got status code %d, want 200", resp.StatusCode)
}
got, err := ioutil.ReadAll(resp.Body)
got, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -2463,7 +2477,7 @@ func testSignedURL(t *testing.T, newHarness HarnessMaker) {
success := resp.StatusCode >= 200 && resp.StatusCode < 300
if success != test.wantSuccess {
t.Errorf("PUT to %q with ContentType %q got status code %d, wanted 2xx? %v", test.urlDescription, test.contentType, resp.StatusCode, test.wantSuccess)
gotBody, _ := ioutil.ReadAll(resp.Body)
gotBody, _ := io.ReadAll(resp.Body)
t.Errorf(string(gotBody))
}
}
Expand All @@ -2487,10 +2501,10 @@ func testSignedURL(t *testing.T, newHarness HarnessMaker) {
success := resp.StatusCode >= 200 && resp.StatusCode < 300
if success != test.wantSuccess {
t.Errorf("GET to %q got status code %d, want 2xx? %v", test.urlDescription, resp.StatusCode, test.wantSuccess)
gotBody, _ := ioutil.ReadAll(resp.Body)
gotBody, _ := io.ReadAll(resp.Body)
t.Errorf(string(gotBody))
} else if success {
gotBody, err := ioutil.ReadAll(resp.Body)
gotBody, err := io.ReadAll(resp.Body)
if err != nil {
t.Errorf("GET to %q failed to read response body: %v", test.urlDescription, err)
} else if gotBodyStr := string(gotBody); gotBodyStr != contents {
Expand Down Expand Up @@ -2519,7 +2533,7 @@ func testSignedURL(t *testing.T, newHarness HarnessMaker) {
defer resp.Body.Close()
success := resp.StatusCode >= 200 && resp.StatusCode < 300
if success != test.wantSuccess {
gotBody, _ := ioutil.ReadAll(resp.Body)
gotBody, _ := io.ReadAll(resp.Body)
t.Errorf(string(gotBody))
t.Fatalf("DELETE to %q got status code %d, want 2xx? %v", test.urlDescription, resp.StatusCode, test.wantSuccess)
}
Expand All @@ -2534,7 +2548,7 @@ func testSignedURL(t *testing.T, newHarness HarnessMaker) {
defer resp.Body.Close()
if resp.StatusCode != 404 {
t.Errorf("GET after DELETE got status code %d, want 404", resp.StatusCode)
gotBody, _ := ioutil.ReadAll(resp.Body)
gotBody, _ := io.ReadAll(resp.Body)
t.Errorf(string(gotBody))
}
}
Expand Down
3 changes: 1 addition & 2 deletions blob/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"log"
"os"

Expand Down Expand Up @@ -570,7 +569,7 @@ func ExampleAttributes_As() {
}

func newTempDir() (string, func()) {
dir, err := ioutil.TempDir("", "go-cloud-blob-example")
dir, err := os.MkdirTemp("", "go-cloud-blob-example")
if err != nil {
panic(err)
}
Expand Down
3 changes: 1 addition & 2 deletions blob/fileblob/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package fileblob_test
import (
"context"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -46,7 +45,7 @@ func ExampleOpenBucket() {

func Example_openBucketFromURL() {
// Create a temporary directory.
dir, err := ioutil.TempDir("", "go-cloud-fileblob-example")
dir, err := os.MkdirTemp("", "go-cloud-fileblob-example")
if err != nil {
log.Fatal(err)
}
Expand Down
3 changes: 1 addition & 2 deletions blob/fileblob/fileblob.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ import (
"hash"
"io"
"io/fs"
"io/ioutil"
"net/url"
"os"
"path/filepath"
Expand Down Expand Up @@ -222,7 +221,7 @@ func (o *URLOpener) forParams(ctx context.Context, q url.Values) (*Options, erro
if err != nil {
return nil, err
}
sk, err := ioutil.ReadFile(keyPath)
sk, err := os.ReadFile(keyPath)
if err != nil {
return nil, err
}
Expand Down
46 changes: 16 additions & 30 deletions blob/fileblob/fileblob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -215,22 +214,16 @@ func BenchmarkFileblob(b *testing.B) {
// File-specific unit tests.
func TestNewBucket(t *testing.T) {
t.Run("BucketDirMissing", func(t *testing.T) {
dir, err := ioutil.TempDir("", "fileblob")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
dir := t.TempDir()

_, gotErr := OpenBucket(filepath.Join(dir, "notfound"), nil)
if gotErr == nil {
t.Errorf("got nil want error")
}
})
t.Run("BucketDirMissingWithCreateDir", func(t *testing.T) {
dir, err := ioutil.TempDir("", "fileblob")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
dir := t.TempDir()

b, gotErr := OpenBucket(filepath.Join(dir, "notfound"), &Options{CreateDir: true})
if gotErr != nil {
t.Errorf("got error %v", gotErr)
Expand All @@ -244,11 +237,12 @@ func TestNewBucket(t *testing.T) {
}
})
t.Run("BucketIsFile", func(t *testing.T) {
f, err := ioutil.TempFile("", "fileblob")
dir := t.TempDir()

f, err := os.CreateTemp(dir, "fileblob")
if err != nil {
t.Fatal(err)
}
defer os.Remove(f.Name())
_, gotErr := OpenBucket(f.Name(), nil)
if gotErr == nil {
t.Errorf("got nil want error")
Expand All @@ -257,11 +251,8 @@ func TestNewBucket(t *testing.T) {
}

func TestSignedURLReturnsUnimplementedWithNoURLSigner(t *testing.T) {
dir, err := ioutil.TempDir("", "fileblob")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
dir := t.TempDir()

b, err := OpenBucket(dir, nil)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -352,15 +343,15 @@ func TestOpenBucketFromURL(t *testing.T) {
if err := os.MkdirAll(filepath.Join(dir, subdir), os.ModePerm); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(filepath.Join(dir, "myfile.txt"), []byte("hello world"), 0666); err != nil {
if err := os.WriteFile(filepath.Join(dir, "myfile.txt"), []byte("hello world"), 0666); err != nil {
t.Fatal(err)
}
// To avoid making another temp dir, use the bucket directory to hold the secret key file.
secretKeyPath := filepath.Join(dir, "secret.key")
if err := ioutil.WriteFile(secretKeyPath, []byte("secret key"), 0666); err != nil {
if err := os.WriteFile(secretKeyPath, []byte("secret key"), 0666); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(filepath.Join(dir, subdir, "myfileinsubdir.txt"), []byte("hello world in subdir"), 0666); err != nil {
if err := os.WriteFile(filepath.Join(dir, subdir, "myfileinsubdir.txt"), []byte("hello world in subdir"), 0666); err != nil {
t.Fatal(err)
}
// Convert dir to a URL path, adding a leading "/" if needed on Windows.
Expand Down Expand Up @@ -460,10 +451,8 @@ func TestListAtRoot(t *testing.T) {
}
defer b.Close()

dir, err := ioutil.TempDir("", "fileblob")
if err != nil {
t.Fatalf("Got error creating temp dir: %#v", err)
}
dir := t.TempDir()

f, err := os.Create(filepath.Join(dir, "file.txt"))
if err != nil {
t.Fatalf("Got error creating file: %#v", err)
Expand All @@ -487,11 +476,8 @@ func TestListAtRoot(t *testing.T) {
}

func TestSkipMetadata(t *testing.T) {
dir, err := ioutil.TempDir("", "fileblob*")
if err != nil {
t.Fatalf("Got error creating temp dir: %#v", err)
}
defer os.RemoveAll(dir)
dir := t.TempDir()

dirpath := filepath.ToSlash(dir)
if os.PathSeparator != '/' && !strings.HasPrefix(dirpath, "/") {
dirpath = "/" + dirpath
Expand Down
5 changes: 2 additions & 3 deletions blob/gcsblob/gcsblob.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -248,7 +247,7 @@ func (o *URLOpener) forParams(ctx context.Context, q url.Values) (*Options, *gcp
}
}
if keyPath := q.Get("private_key_path"); keyPath != "" {
pk, err := ioutil.ReadFile(keyPath)
pk, err := os.ReadFile(keyPath)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -345,7 +344,7 @@ type bucket struct {
opts *Options
}

var emptyBody = ioutil.NopCloser(strings.NewReader(""))
var emptyBody = io.NopCloser(strings.NewReader(""))

// reader reads a GCS object. It implements driver.Reader.
type reader struct {
Expand Down
Loading

0 comments on commit 439229c

Please sign in to comment.