Skip to content

Commit

Permalink
Replace deprecated ioutil functions with corresponding io or os ones
Browse files Browse the repository at this point in the history
  • Loading branch information
deadlycoconuts committed Sep 13, 2022
1 parent 45ab9f1 commit 1ad6d18
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 34 deletions.
4 changes: 2 additions & 2 deletions api/e2e/test/04_undeploy_router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package e2e
import (
"context"
"fmt"
"io/ioutil"
"io"
"net/http"
"testing"
"time"
Expand Down Expand Up @@ -42,7 +42,7 @@ func TestUndeployRouter(t *testing.T) {
response, err := globalTestContext.httpClient.Do(req)
require.NoError(t, err)
assert.Equal(t, http.StatusOK, response.StatusCode)
responseBody, err := ioutil.ReadAll(response.Body)
responseBody, err := io.ReadAll(response.Body)
defer response.Body.Close()
require.NoError(t, err)
t.Log("Undeploy Response:", string(responseBody))
Expand Down
4 changes: 2 additions & 2 deletions api/e2e/test/06_deploy_valid_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package e2e
import (
"context"
"fmt"
"io/ioutil"
"io"
"net/http"
"testing"

Expand Down Expand Up @@ -42,7 +42,7 @@ func TestDeployValidConfig(t *testing.T) {
response, err := globalTestContext.httpClient.Do(req)
require.NoError(t, err)
assert.Equal(t, http.StatusAccepted, response.StatusCode, readBody(t, response))
responseBody, err := ioutil.ReadAll(response.Body)
responseBody, err := io.ReadAll(response.Body)
require.NoError(t, err)
t.Log("Deploy Response:", string(responseBody))

Expand Down
12 changes: 6 additions & 6 deletions api/e2e/test/helpers_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"testing"
"time"
Expand Down Expand Up @@ -34,7 +34,7 @@ func getRouter(
}
defer resp.Body.Close()

respBytes, err := ioutil.ReadAll(resp.Body)
respBytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
Expand All @@ -61,7 +61,7 @@ func getRouterByName(
defer resp.Body.Close()

routers := make([]*models.Router, 0)
data, err := ioutil.ReadAll(resp.Body)
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -96,7 +96,7 @@ func getRouterVersion(
}
defer resp.Body.Close()

respBytes, err := ioutil.ReadAll(resp.Body)
respBytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -143,7 +143,7 @@ func waitDeployVersion(
}

func getPodLogs(t *testing.T, resp *http.Response) []service.PodLog {
data, err := ioutil.ReadAll(resp.Body)
data, err := io.ReadAll(resp.Body)
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -175,7 +175,7 @@ func withDeployedRouter(
require.NoError(t, err)
defer resp.Body.Close()

responsePayload, err := ioutil.ReadAll(resp.Body)
responsePayload, err := io.ReadAll(resp.Body)
require.NoError(t, err)

created := models.Router{}
Expand Down
11 changes: 6 additions & 5 deletions api/e2e/test/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ package e2e

import (
"bytes"
"io/ioutil"
"io"
"net/http"
"os"
"testing"
"text/template"

Expand All @@ -16,7 +17,7 @@ func readBody(t *testing.T, resp *http.Response) string {
if resp.Body == nil {
return ""
}
data, err := ioutil.ReadAll(resp.Body)
data, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
Expand All @@ -25,7 +26,7 @@ func readBody(t *testing.T, resp *http.Response) string {

// make payload to create new router from a template file
func makeRouterPayload(payloadTemplateFile string, args TestContext) []byte {
data, err := ioutil.ReadFile(payloadTemplateFile)
data, err := os.ReadFile(payloadTemplateFile)
if err != nil {
panic(err)
}
Expand All @@ -52,7 +53,7 @@ func withRouterResponse(
body string,
assertion func(response *http.Response, responsePayload []byte)) {

req, err := http.NewRequest(method, url, ioutil.NopCloser(bytes.NewReader([]byte(body))))
req, err := http.NewRequest(method, url, io.NopCloser(bytes.NewReader([]byte(body))))
require.NoError(t, err)

req.Header = headers
Expand All @@ -61,7 +62,7 @@ func withRouterResponse(
require.NoError(t, err)
defer resp.Body.Close()

responseBytes, err := ioutil.ReadAll(resp.Body)
responseBytes, err := io.ReadAll(resp.Body)
require.NoError(t, err)

assertion(resp, responseBytes)
Expand Down
4 changes: 2 additions & 2 deletions api/turing/internal/testutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package testutils

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

Expand All @@ -19,7 +19,7 @@ func ReadFile(filepath string) ([]byte, error) {
}
defer fileObj.Close()
// Read contents
byteValue, err := ioutil.ReadAll(fileObj)
byteValue, err := io.ReadAll(fileObj)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions api/turing/middleware/openapi_validation_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package middleware

import (
"io/ioutil"
"net/http"
"os"
"strings"
"testing"
)
Expand Down Expand Up @@ -136,7 +136,7 @@ func TestOpenAPIValidationValidate(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d, err := ioutil.ReadFile("../../api/openapi.bundle.yaml")
d, err := os.ReadFile("../../api/openapi.bundle.yaml")
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -189,7 +189,7 @@ func TestNewOpenAPIValidation(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d, err := ioutil.ReadFile(tt.openapiYamlFile)
d, err := os.ReadFile(tt.openapiYamlFile)
if err != nil {
t.Error(err)
}
Expand Down
4 changes: 2 additions & 2 deletions api/turing/service/alert_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package service
import (
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"strings"
Expand Down Expand Up @@ -519,7 +519,7 @@ func newMockGitlabServer() (mockGitlab *httptest.Server, requestRecords map[stri
records := make(map[string]string)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
record := fmt.Sprintf("%s %s", r.Method, r.URL.Path)
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if body != nil && err == nil {
records[record] = string(body)
} else {
Expand Down
5 changes: 2 additions & 3 deletions api/turing/service/mlp_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package service

import (
"context"
"io/ioutil"
"net/http"
"os"
"reflect"
Expand Down Expand Up @@ -48,12 +47,12 @@ func testSetupEnvForGoogleCredentials(t *testing.T) (reset func()) {
"token_uri": "https://oauth2.googleapis.com/token"
}`)

file, err := ioutil.TempFile("", "dummy-service-account")
file, err := os.CreateTemp("", "dummy-service-account")
if err != nil {
t.Fatal(err)
}

err = ioutil.WriteFile(file.Name(), serviceAccountKey, 0644)
err = os.WriteFile(file.Name(), serviceAccountKey, 0644)
if err != nil {
t.Fatal(err)
}
Expand Down
14 changes: 7 additions & 7 deletions api/turing/service/pod_log_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package service
import (
"bytes"
"errors"
"io/ioutil"
"io"
"testing"
"time"

Expand Down Expand Up @@ -119,40 +119,40 @@ func TestPodLogServiceListPodLogs(t *testing.T) {
controller.
On("ListPodLogs", mock.Anything, "namespace", "json-payload",
&corev1.PodLogOptions{Container: "user-container", Timestamps: true, SinceTime: &sinceTimeV1Minus1Sec}).
Return(ioutil.NopCloser(bytes.NewBufferString(`2020-07-07T06:59:59Z {"foo":"bar", "baz": 5}
Return(io.NopCloser(bytes.NewBufferString(`2020-07-07T06:59:59Z {"foo":"bar", "baz": 5}
2020-07-07T07:00:05Z {"foo":"bar", "baz": 5}
2020-07-07T07:00:10Z {"foo":"bar", "baz": 10}`)), nil)
controller.
On("ListPodLogs", mock.Anything, "namespace", "json-payload",
&corev1.PodLogOptions{Container: "user-container", Timestamps: true}).
Return(ioutil.NopCloser(bytes.NewBufferString(`2020-07-07T06:59:59Z {"foo":"bar", "baz": 5}
Return(io.NopCloser(bytes.NewBufferString(`2020-07-07T06:59:59Z {"foo":"bar", "baz": 5}
2020-07-07T07:00:05Z {"foo":"bar", "baz": 5}
2020-07-07T07:00:10Z {"foo":"bar", "baz": 10}`)), nil)
controller.
On("ListPodLogs", mock.Anything, "namespace", "json-payload",
&corev1.PodLogOptions{Container: "user-container", Timestamps: true, TailLines: &tailLines}).
Return(ioutil.NopCloser(bytes.NewBufferString(`2020-07-07T07:00:05Z {"foo":"bar", "baz": 5}
Return(io.NopCloser(bytes.NewBufferString(`2020-07-07T07:00:05Z {"foo":"bar", "baz": 5}
2020-07-07T07:00:10Z {"foo":"bar", "baz": 10}`)), nil)
controller.
On("ListPodLogs", mock.Anything, "namespace", "text-payload",
&corev1.PodLogOptions{Container: "user-container", Timestamps: true, SinceTime: &sinceTimeV1Minus1Sec}).
Return(ioutil.NopCloser(bytes.NewBufferString(`2020-07-07T08:00:05Z line1
Return(io.NopCloser(bytes.NewBufferString(`2020-07-07T08:00:05Z line1
2020-07-07T08:00:10Z line2
invalidtimestamp line3
2020-07-07T08:00:00Z `)), nil)
controller.
On("ListPodLogs", mock.Anything, "namespace", "text-payload",
&corev1.PodLogOptions{Container: "user-container", Timestamps: true, TailLines: &tailLines}).
Return(ioutil.NopCloser(bytes.NewBufferString(`2020-07-07T08:00:05Z line1
Return(io.NopCloser(bytes.NewBufferString(`2020-07-07T08:00:05Z line1
2020-07-07T08:00:10Z line2
invalidtimestamp line3
2020-07-07T08:00:00Z `)), nil)
controller.
On("ListPodLogs", mock.Anything, "namespace", "text-payload",
&corev1.PodLogOptions{Container: "user-container", Timestamps: true}).
Return(ioutil.NopCloser(bytes.NewBufferString(`2020-07-07T08:00:05Z line1
Return(io.NopCloser(bytes.NewBufferString(`2020-07-07T08:00:05Z line1
2020-07-07T08:00:10Z line2
invalidtimestamp line3
Expand Down
3 changes: 1 addition & 2 deletions api/turing/utils/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package utils
import (
"bytes"
"fmt"
"io/ioutil"
"os"

"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -83,7 +82,7 @@ func MergeMaps(originalMap, override map[string]interface{}) (map[string]interfa
}

func readYAML(filepath string) (map[string]interface{}, error) {
file, err := ioutil.ReadFile(filepath)
file, err := os.ReadFile(filepath)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 1ad6d18

Please sign in to comment.