Skip to content

Commit

Permalink
Remove deprecated ioutil
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanmorgan committed Sep 5, 2024
1 parent fbd256f commit 3e6d9bb
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 73 deletions.
4 changes: 2 additions & 2 deletions auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ func TestLoadAuthSchemes(t *testing.T) {
})

t.Run("should load multiple auth schemes", func(t *testing.T) {
myauth, err := createBasicAuthFile("foo:bar")
myauth, err := createBasicAuthFile("foo:bar", t)
if err != nil {
t.Fatalf("could not create file on disk %s", err)
}

myotherauth, err := createBasicAuthFile("bar:foo")
myotherauth, err := createBasicAuthFile("bar:foo", t)
if err != nil {
t.Fatalf("could not create file on disk %s", err)
}
Expand Down
25 changes: 10 additions & 15 deletions auth/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package auth
import (
"encoding/base64"
"fmt"
"io/ioutil"
"net/http"
"os"
"reflect"
Expand Down Expand Up @@ -37,16 +36,12 @@ func (rw *responseWriter) WriteHeader(statusCode int) {
rw.code = statusCode
}

func createBasicAuthFile(contents string) (string, error) {
dir, err := ioutil.TempDir("", "basicauth")

if err != nil {
return "", fmt.Errorf("could not create temp dir: %s", err)
}
func createBasicAuthFile(contents string, t *testing.T) (string, error) {
dir := t.TempDir()

filename := fmt.Sprintf("%s/%s", dir, uuid.NewUUID())

err = ioutil.WriteFile(filename, []byte(contents), 0666)
err := os.WriteFile(filename, []byte(contents), 0666)

if err != nil {
return "", fmt.Errorf("could not write password file: %s", err)
Expand All @@ -55,10 +50,10 @@ func createBasicAuthFile(contents string) (string, error) {
return filename, nil
}

func createBasicAuth(user string, password string) (AuthScheme, error) {
func createBasicAuth(user string, password string, t *testing.T) (AuthScheme, error) {
contents := fmt.Sprintf("%s:%s", user, password)

filename, err := createBasicAuthFile(contents)
filename, err := createBasicAuthFile(contents, t)

a, err := newBasicAuth(config.BasicAuth{
File: filename,
Expand All @@ -75,7 +70,7 @@ func createBasicAuth(user string, password string) (AuthScheme, error) {
func TestNewBasicAuth(t *testing.T) {

t.Run("should create a basic auth scheme from the supplied config", func(t *testing.T) {
filename, err := createBasicAuthFile("foo:bar")
filename, err := createBasicAuthFile("foo:bar", t)

if err != nil {
t.Error(err)
Expand All @@ -91,7 +86,7 @@ func TestNewBasicAuth(t *testing.T) {
})

t.Run("should log a warning when credentials are malformed", func(t *testing.T) {
filename, err := createBasicAuthFile("foosdlijdgohdgdbar")
filename, err := createBasicAuthFile("foosdlijdgohdgdbar", t)

if err != nil {
t.Error(err)
Expand All @@ -108,7 +103,7 @@ func TestNewBasicAuth(t *testing.T) {
}

func TestBasic_Authorised(t *testing.T) {
basicAuth, err := createBasicAuth("foo", "bar")
basicAuth, err := createBasicAuth("foo", "bar", t)
creds := []byte("foo:bar")

if err != nil {
Expand Down Expand Up @@ -171,7 +166,7 @@ func TestBasic_Authorised(t *testing.T) {
}

func TestBasic_Authorised_should_fail_without_htpasswd_file(t *testing.T) {
filename, err := createBasicAuthFile("foo:bar")
filename, err := createBasicAuthFile("foo:bar", t)
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -213,7 +208,7 @@ func TestBasic_Authorised_should_fail_without_htpasswd_file(t *testing.T) {
}

func TestBasic_Authorized_should_set_www_realm_header(t *testing.T) {
basicAuth, err := createBasicAuth("foo", "bar")
basicAuth, err := createBasicAuth("foo", "bar", t)

if err != nil {
t.Fatal(err)
Expand Down
4 changes: 2 additions & 2 deletions cert/file_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cert
import (
"crypto/tls"
"crypto/x509"
"io/ioutil"
"os"

"github.com/fabiolb/fabio/exit"
)
Expand All @@ -26,7 +26,7 @@ func (s FileSource) LoadClientCAs() (*x509.CertPool, error) {
if s.ClientAuthFile == "" {
return nil, nil
}
pemBlock, err := ioutil.ReadFile(path)
pemBlock, err := os.ReadFile(path)
return map[string][]byte{path: pemBlock}, err
})
}
Expand Down
6 changes: 3 additions & 3 deletions cert/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"net/url"
Expand Down Expand Up @@ -34,7 +34,7 @@ func loadURL(listURL string) (pemBlocks map[string][]byte, err error) {
return nil, err
}
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
return io.ReadAll(resp.Body)
}

// fetch the file with the list of filenames
Expand Down Expand Up @@ -88,7 +88,7 @@ func loadPath(root string) (pemBlocks map[string][]byte, err error) {
return nil
}

buf, err := ioutil.ReadFile(path)
buf, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("cert: %s", err)
}
Expand Down
23 changes: 7 additions & 16 deletions cert/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"encoding/pem"
"fmt"
"io"
"io/ioutil"
"log"
"math/big"
"net/http"
Expand Down Expand Up @@ -193,23 +192,23 @@ func TestStaticSource(t *testing.T) {
}

func TestFileSource(t *testing.T) {
dir := tempDir()
dir := t.TempDir()
defer os.RemoveAll(dir)
certPEM, keyPEM := makePEM("localhost", time.Minute)
certFile, keyFile := saveCert(dir, "localhost", certPEM, keyPEM)
testSource(t, FileSource{CertFile: certFile, KeyFile: keyFile}, makeCertPool(certPEM), 0)
}

func TestPathSource(t *testing.T) {
dir := tempDir()
dir := t.TempDir()
defer os.RemoveAll(dir)
certPEM, keyPEM := makePEM("localhost", time.Minute)
saveCert(dir, "localhost", certPEM, keyPEM)
testSource(t, PathSource{CertPath: dir}, makeCertPool(certPEM), 10*time.Millisecond)
}

func TestHTTPSource(t *testing.T) {
dir := tempDir()
dir := t.TempDir()
defer os.RemoveAll(dir)
certPEM, keyPEM := makePEM("localhost", time.Minute)
certFile, keyFile := saveCert(dir, "localhost", certPEM, keyPEM)
Expand Down Expand Up @@ -261,7 +260,7 @@ func TestConsulSource(t *testing.T) {
return false
}

n, err := io.Copy(ioutil.Discard, resp.Body)
n, err := io.Copy(io.Discard, resp.Body)
return err == nil && n > 10
}

Expand Down Expand Up @@ -582,7 +581,7 @@ func testSource(t *testing.T, source Source, rootCAs *x509.CertPool, sleep time.
// disable log output for the next call to prevent
// confusing log messages since they are expected
// http: TLS handshake error from 127.0.0.1:55044: remote error: bad certificate
log.SetOutput(ioutil.Discard)
log.SetOutput(io.Discard)
defer log.SetOutput(os.Stderr)

// fail calls https://localhost.org/ for which certificate validation
Expand Down Expand Up @@ -642,7 +641,7 @@ func roundtrip(serverName string, srvConfig *tls.Config, client *http.Client) (c
}
defer resp.Body.Close()

data, err := ioutil.ReadAll(resp.Body)
data, err := io.ReadAll(resp.Body)
if err != nil {
return 0, "", err
}
Expand Down Expand Up @@ -675,16 +674,8 @@ func http20Client(rootCAs *x509.CertPool) (*http.Client, error) {
return &http.Client{Transport: t}, nil
}

func tempDir() string {
dir, err := ioutil.TempDir("", "fabio")
if err != nil {
panic(err.Error())
}
return dir
}

func writeFile(filename string, data []byte) {
if err := ioutil.WriteFile(filename, data, 0644); err != nil {
if err := os.WriteFile(filename, data, 0644); err != nil {
panic(err.Error())
}
}
Expand Down
3 changes: 1 addition & 2 deletions cert/vault_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cert
import (
"encoding/json"
"errors"
"io/ioutil"
"log"
"os"
"strings"
Expand Down Expand Up @@ -186,7 +185,7 @@ func getVaultToken(c string) string {
return token
}
if cArray[0] == "file" {
b, err := ioutil.ReadFile(cArray[1]) // just pass the file name
b, err := os.ReadFile(cArray[1]) // just pass the file name
if err != nil {
log.Printf("[WARN] vault: Failed to fetch token from %s", c)
} else {
Expand Down
3 changes: 1 addition & 2 deletions config/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"crypto/tls"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -1183,7 +1182,7 @@ func TestLoad(t *testing.T) {
}

case tt.path != "":
if err := ioutil.WriteFile(tt.path, []byte(tt.data), 0600); err != nil {
if err := os.WriteFile(tt.path, []byte(tt.data), 0600); err != nil {
t.Fatalf("error writing file: %s", err)
}
defer os.Remove(tt.path)
Expand Down
6 changes: 3 additions & 3 deletions logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package logger

import (
"bytes"
"io/ioutil"
"io"
"net/http"
"net/url"
"sort"
Expand Down Expand Up @@ -221,7 +221,7 @@ func BenchmarkLog(b *testing.B) {
sort.Strings(keys)
format := strings.Join(keys, " ")

l, err := New(ioutil.Discard, format)
l, err := New(io.Discard, format)
if err != nil {
b.Fatal(err)
}
Expand All @@ -243,7 +243,7 @@ func BenchmarkLog(b *testing.B) {

b.ResetTimer()
for i := 0; i < b.N; i++ {
t.Execute(ioutil.Discard, e)
t.Execute(io.Discard, e)
}
})
}
Expand Down
12 changes: 6 additions & 6 deletions proxy/gzip/gzip_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package gzip
import (
"bytes"
"compress/gzip"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"regexp"
Expand All @@ -31,15 +31,15 @@ func Test_GzipHandler_CompressableType(t *testing.T) {
assertEqual(resp.Header.Get("Content-Type"), "text/plain; charset=utf-8")
assertEqual(resp.Header.Get("Content-Encoding"), "gzip")

gzBytes, err := ioutil.ReadAll(resp.Body)
gzBytes, err := io.ReadAll(resp.Body)
assertEqual(err, nil)
assertEqual(resp.Header.Get("Content-Length"), strconv.Itoa(len(gzBytes)))

reader, err := gzip.NewReader(bytes.NewBuffer(gzBytes))
assertEqual(err, nil)
defer reader.Close()

bytes, err := ioutil.ReadAll(reader)
bytes, err := io.ReadAll(reader)
assertEqual(err, nil)

assertEqual(string(bytes), "Hello World")
Expand All @@ -63,7 +63,7 @@ func Test_GzipHandler_NotCompressingTwice(t *testing.T) {
assertEqual(err, nil)
defer reader.Close()

bytes, err := ioutil.ReadAll(reader)
bytes, err := io.ReadAll(reader)
assertEqual(err, nil)

assertEqual(string(bytes), "Hello World")
Expand All @@ -83,7 +83,7 @@ func Test_GzipHandler_CompressableType_NoAccept(t *testing.T) {

assertEqual(resp.Header.Get("Content-Encoding"), "")

bytes, err := ioutil.ReadAll(resp.Body)
bytes, err := io.ReadAll(resp.Body)
assertEqual(err, nil)

assertEqual(string(bytes), "Hello World")
Expand All @@ -103,7 +103,7 @@ func Test_GzipHandler_NonCompressableType(t *testing.T) {

assertEqual(resp.Header.Get("Content-Encoding"), "")

bytes, err := ioutil.ReadAll(resp.Body)
bytes, err := io.ReadAll(resp.Body)
assertEqual(err, nil)

assertEqual(bytes, []byte{42})
Expand Down
4 changes: 2 additions & 2 deletions proxy/http_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"io"
"net"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -750,7 +750,7 @@ func mustDo(req *http.Request) (*http.Response, []byte) {
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions proxy/inetaf_tcpproxy_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"context"
"crypto/x509"
"fmt"
"io/ioutil"
"io"
"net"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -146,7 +146,7 @@ route add tcproute example2.com/ tcp://%s opts "proto=tcp"`
t.Errorf("error on request %s", err)
return
}
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
t.Errorf("error reading body: %s", err)
Expand Down
Loading

0 comments on commit 3e6d9bb

Please sign in to comment.