Skip to content

Commit

Permalink
refactor: move from io/ioutil to io and os package (#3294)
Browse files Browse the repository at this point in the history
* refactor: move from io/ioutil to io and os package

The io/ioutil package has been deprecated as of Go 1.16, see
https://golang.org/doc/go1.16#ioutil. This commit replaces the existing
io/ioutil functions with their new definitions in io and os packages.

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* test(anonymizer): add test for New and SaveMapping

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
  • Loading branch information
Juneezee committed Oct 2, 2021
1 parent 0bdcec8 commit a883aea
Show file tree
Hide file tree
Showing 39 changed files with 178 additions and 128 deletions.
6 changes: 3 additions & 3 deletions cmd/agent/app/agent_test.go
Expand Up @@ -17,7 +17,7 @@ package app

import (
"fmt"
"io/ioutil"
"io"
"net/http"
"strings"
"testing"
Expand Down Expand Up @@ -66,7 +66,7 @@ func TestAgentSamplingEndpoint(t *testing.T) {
}
resp, err := http.Get(url)
require.NoError(t, err)
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
assert.Equal(t, "collector error: no peers available\n", string(body))
})
Expand All @@ -77,7 +77,7 @@ func TestAgentMetricsEndpoint(t *testing.T) {
url := fmt.Sprintf("http://%s/metrics", httpAddr)
resp, err := http.Get(url)
require.NoError(t, err)
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
assert.Contains(t, string(body), "# HELP")
})
Expand Down
8 changes: 4 additions & 4 deletions cmd/all-in-one/all_in_one_test.go
Expand Up @@ -21,7 +21,7 @@ package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"testing"
"time"
Expand Down Expand Up @@ -92,7 +92,7 @@ func getAPITrace(t *testing.T) {
resp, err := httpClient.Do(req)
require.NoError(t, err)

body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)

err = json.Unmarshal(body, &queryResponse)
require.NoError(t, err)
Expand All @@ -115,7 +115,7 @@ func getSamplingStrategy(t *testing.T) {
resp, err := httpClient.Do(req)
require.NoError(t, err)

body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)

err = json.Unmarshal(body, &queryResponse)
require.NoError(t, err)
Expand Down Expand Up @@ -143,7 +143,7 @@ func getServicesAPIV3(t *testing.T) {
require.NoError(t, err)
resp, err := httpClient.Do(req)
require.NoError(t, err)
body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)

var servicesResponse api_v3.GetServicesResponse
jsonpb := runtime.JSONPb{}
Expand Down
5 changes: 2 additions & 3 deletions cmd/anonymizer/app/anonymizer/anonymizer.go
Expand Up @@ -18,7 +18,6 @@ import (
"encoding/json"
"fmt"
"hash/fnv"
"io/ioutil"
"os"
"path/filepath"
"sync"
Expand Down Expand Up @@ -81,7 +80,7 @@ func New(mappingFile string, options Options, logger *zap.Logger) *Anonymizer {
options: options,
}
if _, err := os.Stat(filepath.Clean(mappingFile)); err == nil {
dat, err := ioutil.ReadFile(filepath.Clean(mappingFile))
dat, err := os.ReadFile(filepath.Clean(mappingFile))
if err != nil {
logger.Fatal("Cannot load previous mapping", zap.Error(err))
}
Expand All @@ -108,7 +107,7 @@ func (a *Anonymizer) SaveMapping() {
a.logger.Error("Failed to marshal mapping file", zap.Error(err))
return
}
if err := ioutil.WriteFile(filepath.Clean(a.mappingFile), dat, os.ModePerm); err != nil {
if err := os.WriteFile(filepath.Clean(a.mappingFile), dat, os.ModePerm); err != nil {
a.logger.Error("Failed to write mapping file", zap.Error(err))
return
}
Expand Down
59 changes: 59 additions & 0 deletions cmd/anonymizer/app/anonymizer/anonymizer_test.go
Expand Up @@ -15,10 +15,13 @@
package anonymizer

import (
"os"
"path/filepath"
"testing"
"time"

"github.com/stretchr/testify/assert"
"go.uber.org/zap"

"github.com/jaegertracing/jaeger/model"
)
Expand Down Expand Up @@ -73,6 +76,62 @@ var span2 = &model.Span{
StartTime: time.Unix(300, 0),
}

func TestNew(t *testing.T) {
nopLogger := zap.NewNop()
tempDir := t.TempDir()

file, err := os.CreateTemp(tempDir, "mapping.json")
assert.NoError(t, err)

_, err = file.Write([]byte(`
{
"services": {
"api": "hashed_api"
},
"operations": {
"[api]:delete": "hashed_api_delete"
}
}
`))
assert.NoError(t, err)

anonymizer := New(file.Name(), Options{}, nopLogger)
assert.NotNil(t, anonymizer)
}

func TestAnonymizer_SaveMapping(t *testing.T) {
nopLogger := zap.NewNop()
mapping := mapping{
Services: make(map[string]string),
Operations: make(map[string]string),
}

tests := []struct {
name string
mappingFile string
}{
{
name: "fail to write mapping file",
mappingFile: "",
},
{
name: "save mapping file successfully",
mappingFile: filepath.Join(t.TempDir(), "mapping.json"),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
anonymizer := Anonymizer{
logger: nopLogger,
mapping: mapping,
mappingFile: tt.mappingFile,
}
anonymizer.SaveMapping()
})
}
}

func TestAnonymizer_FilterStandardTags(t *testing.T) {
expected := []model.KeyValue{
model.Bool("error", true),
Expand Down
3 changes: 1 addition & 2 deletions cmd/anonymizer/app/uiconv/extractor_test.go
Expand Up @@ -15,7 +15,6 @@
package uiconv

import (
"io/ioutil"
"os"
"testing"

Expand Down Expand Up @@ -111,7 +110,7 @@ func TestExtractor_TraceScanError(t *testing.T) {
}

func loadJSON(t *testing.T, fileName string, i interface{}) {
b, err := ioutil.ReadFile(fileName)
b, err := os.ReadFile(fileName)
require.NoError(t, err)
err = swag.ReadJSON(b, i)
require.NoError(t, err, "Failed to parse json fixture file %s", fileName)
Expand Down
4 changes: 2 additions & 2 deletions cmd/collector/app/handler/http_handler.go
Expand Up @@ -18,7 +18,7 @@ package handler
import (
"fmt"
"html"
"io/ioutil"
"io"
"mime"
"net/http"

Expand Down Expand Up @@ -62,7 +62,7 @@ func (aH *APIHandler) RegisterRoutes(router *mux.Router) {

// SaveSpan submits the span provided in the request body to the JaegerBatchesHandler
func (aH *APIHandler) SaveSpan(w http.ResponseWriter, r *http.Request) {
bodyBytes, err := ioutil.ReadAll(r.Body)
bodyBytes, err := io.ReadAll(r.Body)
r.Body.Close()
if err != nil {
http.Error(w, fmt.Sprintf(UnableToReadBodyErrFormat, err), http.StatusInternalServerError)
Expand Down
4 changes: 2 additions & 2 deletions cmd/collector/app/handler/http_handler_test.go
Expand Up @@ -19,7 +19,7 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"sync"
Expand Down Expand Up @@ -204,7 +204,7 @@ func postBytes(contentType, urlStr string, bodyBytes []byte) (int, string, error
}
defer res.Body.Close()

body, err := ioutil.ReadAll(res.Body)
body, err := io.ReadAll(res.Body)
if err != nil {
return 0, "", err
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/collector/app/zipkin/http_handler.go
Expand Up @@ -20,7 +20,6 @@ import (
"fmt"
"html"
"io"
"io/ioutil"
"mime"
"net/http"
"strings"
Expand Down Expand Up @@ -77,7 +76,7 @@ func (aH *APIHandler) saveSpans(w http.ResponseWriter, r *http.Request) {
bRead = gz
}

bodyBytes, err := ioutil.ReadAll(bRead)
bodyBytes, err := io.ReadAll(bRead)
if err != nil {
http.Error(w, fmt.Sprintf(handler.UnableToReadBodyErrFormat, err), http.StatusInternalServerError)
return
Expand Down Expand Up @@ -127,7 +126,7 @@ func (aH *APIHandler) saveSpansV2(w http.ResponseWriter, r *http.Request) {
bRead = gz
}

bodyBytes, err := ioutil.ReadAll(bRead)
bodyBytes, err := io.ReadAll(bRead)
if err != nil {
http.Error(w, fmt.Sprintf(handler.UnableToReadBodyErrFormat, err), http.StatusInternalServerError)
return
Expand Down
4 changes: 2 additions & 2 deletions cmd/collector/app/zipkin/http_handler_test.go
Expand Up @@ -20,7 +20,7 @@ import (
"compress/gzip"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"sync"
Expand Down Expand Up @@ -392,7 +392,7 @@ func postBytes(urlStr string, bytesBody []byte, header *http.Header) (int, strin
}
defer res.Body.Close()

body, err := ioutil.ReadAll(res.Body)
body, err := io.ReadAll(res.Body)
if err != nil {
return 0, "", err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/collector/app/zipkin/jsonv2_test.go
Expand Up @@ -16,7 +16,7 @@ package zipkin

import (
"fmt"
"io/ioutil"
"os"
"testing"

"github.com/go-openapi/swag"
Expand Down Expand Up @@ -147,7 +147,7 @@ func TestErrSpans(t *testing.T) {
}

func loadJSON(t *testing.T, fileName string, i interface{}) {
b, err := ioutil.ReadFile(fileName)
b, err := os.ReadFile(fileName)
require.NoError(t, err)
err = swag.ReadJSON(b, i)
require.NoError(t, err, "Failed to parse json fixture file %s", fileName)
Expand Down
4 changes: 2 additions & 2 deletions cmd/collector/app/zipkin/protov2_test.go
Expand Up @@ -17,7 +17,7 @@ package zipkin
import (
"crypto/rand"
"encoding/json"
"io/ioutil"
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -67,7 +67,7 @@ func TestLCFromProtoSpanLocalEndpoint(t *testing.T) {
}

func loadProto(t *testing.T, fname string, spans *zipkinProto.ListOfSpans) {
b, err := ioutil.ReadFile(fname)
b, err := os.ReadFile(fname)
require.NoError(t, err)
err = json.Unmarshal(b, spans)
require.NoError(t, err)
Expand Down
6 changes: 3 additions & 3 deletions cmd/docs/command_test.go
Expand Up @@ -15,7 +15,7 @@
package docs

import (
"io/ioutil"
"os"
"strings"
"testing"

Expand Down Expand Up @@ -43,7 +43,7 @@ func TestOutputFormats(t *testing.T) {
cmd.ParseFlags([]string{test.flag})
err := cmd.Execute()
if err == nil {
f, err := ioutil.ReadFile(test.file)
f, err := os.ReadFile(test.file)
require.NoError(t, err)
assert.True(t, strings.Contains(string(f), "documentation"))
} else {
Expand All @@ -62,7 +62,7 @@ func TestDocsForParent(t *testing.T) {
parent.AddCommand(docs)
err := docs.RunE(docs, []string{})
require.NoError(t, err)
f, err := ioutil.ReadFile("root_command.md")
f, err := os.ReadFile("root_command.md")
require.NoError(t, err)
assert.True(t, strings.Contains(string(f), "some description"))
}
7 changes: 3 additions & 4 deletions cmd/query/app/http_handler_test.go
Expand Up @@ -21,7 +21,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"math"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -239,7 +238,7 @@ func TestWriteJSON(t *testing.T) {
get := func(url string) string {
res, err := http.Get(url)
require.NoError(t, err)
body, err := ioutil.ReadAll(res.Body)
body, err := io.ReadAll(res.Body)
require.NoError(t, err)
return string(body)
}
Expand Down Expand Up @@ -843,15 +842,15 @@ func execJSON(req *http.Request, out interface{}) error {
defer resp.Body.Close()

if resp.StatusCode > 399 {
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
return fmt.Errorf("%d error from server: %s", resp.StatusCode, body)
}

if out == nil {
io.Copy(ioutil.Discard, resp.Body)
io.Copy(io.Discard, resp.Body)
return nil
}

Expand Down
7 changes: 4 additions & 3 deletions cmd/query/app/static_handler.go
Expand Up @@ -19,8 +19,9 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
Expand Down Expand Up @@ -212,7 +213,7 @@ func loadIndexHTML(open func(string) (http.File, error)) ([]byte, error) {
return nil, fmt.Errorf("cannot open index.html: %w", err)
}
defer indexFile.Close()
indexBytes, err := ioutil.ReadAll(indexFile)
indexBytes, err := io.ReadAll(indexFile)
if err != nil {
return nil, fmt.Errorf("cannot read from index.html: %w", err)
}
Expand All @@ -223,7 +224,7 @@ func loadUIConfig(uiConfig string) (*loadedConfig, error) {
if uiConfig == "" {
return nil, nil
}
bytesConfig, err := ioutil.ReadFile(filepath.Clean(uiConfig))
bytesConfig, err := os.ReadFile(filepath.Clean(uiConfig))
if err != nil {
return nil, fmt.Errorf("cannot read UI config file %v: %w", uiConfig, err)
}
Expand Down

0 comments on commit a883aea

Please sign in to comment.