Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: address io/ioutil depreciation #1086

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"regexp"
"testing"
Expand Down Expand Up @@ -354,7 +354,7 @@ func TestServeWithTLS(t *testing.T) {
tlsConf := &tls.Config{}
if tc.clientCACert != "" {
caCertPool := x509.NewCertPool()
caCert, err := ioutil.ReadFile(tc.clientCACert)
caCert, err := os.ReadFile(tc.clientCACert)
require.NoError(t, err)
caCertPool.AppendCertsFromPEM(caCert)
tlsConf = &tls.Config{
Expand Down Expand Up @@ -549,11 +549,11 @@ func TestServeWithMutualTLS_MultipleCA(t *testing.T) {
cleanup := testutils.MakeTempDir(t, tmpDir)
defer cleanup()
for _, src := range caFiles {
input, err := ioutil.ReadFile(src)
input, err := os.ReadFile(src)
file := filepath.Base(src)
dest := filepath.Join(tmpDir, file)
require.NoError(t, err)
err = ioutil.WriteFile(dest, input, 0644)
err = os.WriteFile(dest, input, 0644)
require.NoError(t, err)
}

Expand Down
13 changes: 6 additions & 7 deletions api/oapigen/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions api/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"testing"
"time"
Expand Down Expand Up @@ -82,7 +82,7 @@ func TestRequest(t *testing.T) {
require.NoError(t, err)
bytesR := bytes.NewBuffer(b)
mockResp := &http.Response{
Body: ioutil.NopCloser(bytesR),
Body: io.NopCloser(bytesR),
StatusCode: tc.httpStatus,
}
hc.On("Do", mock.Anything).Return(mockResp, tc.httpError).Once()
Expand Down
4 changes: 2 additions & 2 deletions api/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package api
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"sort"
"strings"
Expand Down Expand Up @@ -101,7 +101,7 @@ func (h *taskHandler) updateTask(w http.ResponseWriter, r *http.Request) {
return
}

body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
logger.Trace("unable to read request body from update", "error", err)
jsonErrorResponse(ctx, w, http.StatusInternalServerError, err)
Expand Down
4 changes: 2 additions & 2 deletions command/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
package command

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

"github.com/mitchellh/cli"
"github.com/stretchr/testify/assert"
)

func Test_Commands(t *testing.T) {
cf := Commands(ioutil.Discard, ioutil.Discard)
cf := Commands(io.Discard, io.Discard)

// map of commands to synopsis
expectedCommands := map[string]cli.Command{
Expand Down
3 changes: 1 addition & 2 deletions command/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"net/url"
"strings"

Expand Down Expand Up @@ -82,7 +81,7 @@ func (m *meta) defaultFlagSet(name string) *flag.FlagSet {
m.tls.sslVerify = m.flags.Bool(FlagSSLVerify, true, fmt.Sprintf("Boolean to verify SSL or not. Set to true to verify SSL. "+
"\n\t\tThis can also be specified using the %s environment variable.", api.EnvTLSSSLVerify))

m.flags.SetOutput(ioutil.Discard)
m.flags.SetOutput(io.Discard)

return m.flags
}
Expand Down
7 changes: 3 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package config
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -489,7 +488,7 @@ func fromFile(path string) (*Config, error) {
}

logger := logging.Global().Named(logSystemName)
content, err := ioutil.ReadFile(path)
content, err := os.ReadFile(path)
if err != nil {
logger.Error("failed reading config file from disk", filePathLogKey, path)
return nil, err
Expand Down Expand Up @@ -533,7 +532,7 @@ func fromPath(path string) (*Config, error) {
}

// Ensure the given filepath has at least one config file
files, err := ioutil.ReadDir(path)
files, err := os.ReadDir(path)
if err != nil {
logger.Error("failed listing directory", filePathLogKey, path)
return nil, err
Expand Down Expand Up @@ -591,7 +590,7 @@ func stringFromEnv(list []string, def string) *string {

func stringFromFile(list []string, def string) *string {
for _, s := range list {
c, err := ioutil.ReadFile(s)
c, err := os.ReadFile(s)
if err == nil {
return String(strings.TrimSpace(string(c)))
}
Expand Down
6 changes: 3 additions & 3 deletions driver/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"io"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -145,7 +145,7 @@ func NewTerraform(config *TerraformConfig) (*Terraform, error) {
postApply: h,
resolver: hcat.NewResolver(),
watcher: config.Watcher,
fileReader: ioutil.ReadFile,
fileReader: os.ReadFile,
logger: logger,
}, nil
}
Expand Down Expand Up @@ -496,7 +496,7 @@ func (tf *Terraform) inspectTask(ctx context.Context, returnPlan bool) (InspectP
if tf.logClient {
tfLogger = log.New(log.Writer(), "", log.Flags())
} else {
tfLogger = log.New(ioutil.Discard, "", 0)
tfLogger = log.New(io.Discard, "", 0)
}
defer tf.client.SetStdout(tfLogger.Writer())
}
Expand Down
3 changes: 1 addition & 2 deletions e2e/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -160,14 +159,14 @@

output, err := runSubcommand(t, tc.input, subcmd...)
assert.NoError(t, err)
assert.Contains(t, output, tc.outputContains)

Check failure on line 162 in e2e/command_test.go

View workflow job for this annotation

GitHub Actions / E2E Tests (0)

command_test.go:162: Error Trace: /home/runner/work/consul-terraform-sync/consul-terraform-sync/e2e/command_test.go:162 Error: "==> Inspecting changes to resource if enabling 'disabled_task'...\n\n Generating plan that Consul-Terraform-Sync will use Terraform to execute\n\nWarning: 'port' option is deprecated and will be removed in a later version. It is preferred to use the 'http-addr' option instead.\n\n \nTerraform used the selected providers to generate the following execution\nplan. Resource actions are indicated with the following symbols:\n + create\n\nTerraform will perform the following actions:\n\n # module.disabled_task.local_file.address[\"api.node-dfb442ae-6af0-1b2f-78c8-9b0d6e257373.dc1\"] will be created\n + resource \"local_file\" \"address\" {\n + content = \"1.2.3.4\"\n + content_base64sha256 = (known after apply)\n + content_base64sha512 = (known after apply)\n + content_md5 = (known after apply)\n + content_sha1 = (known after apply)\n + content_sha256 = (known after apply)\n + content_sha512 = (known after apply)\n + directory_permission = \"0777\"\n + file_permission = \"0777\"\n + filename = \"resources/api.txt\"\n + id = (known after apply)\n }\n\n # module.disabled_task.local_file.address[\"web.node-dfb442ae-6af0-1b2f-78c8-9b0d6e257373.dc1\"] will be created\n + resource \"local_file\" \"address\" {\n + content = \"5.6.7.8\"\n + content_base64sha256 = (known after apply)\n + content_base64sha512 = (known after apply)\n + content_md5 = (known after apply)\n + content_sha1 = (known after apply)\n + content_sha256 = (known after apply)\n + content_sha512 = (known after apply)\n + directory_permission = \"0777\"\n + file_permission = \"0777\"\n + filename = \"resources/web.txt\"\n + id = (known after apply)\n }\n\nPlan: 2 to add, 0 to change, 0 to destroy.\n\n==> 'disabled_task' enable complete!\n" does not contain "Cancelled enabling task" Test: TestE2E_EnableTaskCommand/user_does_not_approve_plan

// confirm that the task's final enabled state
taskStatuses, err := cts.Status().Task(disabledTaskName, nil)
require.NoError(t, err)
status, ok := taskStatuses[disabledTaskName]
require.True(t, ok)
assert.Equal(t, tc.expectEnabled, status.Enabled)

Check failure on line 169 in e2e/command_test.go

View workflow job for this annotation

GitHub Actions / E2E Tests (0)

command_test.go:169: Error Trace: /home/runner/work/consul-terraform-sync/consul-terraform-sync/e2e/command_test.go:169 Error: Not equal: expected: false actual : true Test: TestE2E_EnableTaskCommand/user_does_not_approve_plan

if !tc.expectEnabled {
// only check for events if we expect the task to be enabled
Expand All @@ -177,7 +176,7 @@
// check that there was an initial event
eventCountBase := 1
eventCountNow := eventCount(t, disabledTaskName, cts.Port())
require.Equal(t, eventCountBase, eventCountNow,

Check failure on line 179 in e2e/command_test.go

View workflow job for this annotation

GitHub Actions / E2E Tests (0)

command_test.go:179: Error Trace: /home/runner/work/consul-terraform-sync/consul-terraform-sync/e2e/command_test.go:179 Error: Not equal: expected: 1 actual : 0 Test: TestE2E_EnableTaskCommand/happy_path

Check failure on line 179 in e2e/command_test.go

View workflow job for this annotation

GitHub Actions / E2E Tests (0)

command_test.go:179: Error Trace: /home/runner/work/consul-terraform-sync/consul-terraform-sync/e2e/command_test.go:179 Error: Not equal: expected: 1 actual : 0 Test: TestE2E_EnableTaskCommand/auto_approve
"event count did not increment once. task was not triggered as expected")

// make a change in Consul and confirm a new event is triggered
Expand Down Expand Up @@ -631,7 +630,7 @@
// If required by test case, verify contents of generated variables file
for _, v := range tc.tfVarsFiles {
expectedFilePath := filepath.Join(tempDir, taskName, "variables.auto.tfvars")
b, err := ioutil.ReadFile(expectedFilePath)
b, err := os.ReadFile(expectedFilePath)
require.NoError(t, err)
assert.Contains(t, string(b), v)
}
Expand Down
1 change: 1 addition & 0 deletions e2e/test_modules/incompatible_w_cts/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
# SPDX-License-Identifier: MPL-2.0

variable "test" {
default = "test"
}

6 changes: 3 additions & 3 deletions templates/hcltmpl/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package hcltmpl

import (
"context"
"io/ioutil"
"io"
"testing"
"time"

Expand Down Expand Up @@ -77,8 +77,8 @@ func TestLoadDynamicConfig_ConsulKV(t *testing.T) {
srv, err := testutil.NewTestServerConfigT(tb,
func(c *testutil.TestServerConfig) {
c.LogLevel = "warn"
c.Stdout = ioutil.Discard
c.Stderr = ioutil.Discard
c.Stdout = io.Discard
c.Stderr = io.Discard
})
require.NoError(t, err)
clients := hcat.NewClientSet()
Expand Down
4 changes: 2 additions & 2 deletions templates/tftmpl/golden_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"bytes"
"flag"
"io"
"io/ioutil"
"os"
"testing"

"github.com/hashicorp/consul-terraform-sync/templates/hcltmpl"
Expand Down Expand Up @@ -514,7 +514,7 @@ func TestNewFiles(t *testing.T) {
func checkGoldenFile(t *testing.T, goldenFile string, actual string) {
// update golden files if necessary
if *update {
if err := ioutil.WriteFile(goldenFile, []byte(actual), 0644); err != nil {
if err := os.WriteFile(goldenFile, []byte(actual), 0644); err != nil {
require.NoError(t, err)
}
}
Expand Down
14 changes: 7 additions & 7 deletions templates/tftmpl/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package tftmpl

import (
"context"
"io/ioutil"
"io"
"log"
"os"
"path/filepath"
Expand All @@ -27,7 +27,7 @@ import (
)

func TestInitRootModule(t *testing.T) {
dir, err := ioutil.TempDir(".", "consul-terraform-sync-tftmpl-")
dir, err := os.MkdirTemp(".", "consul-terraform-sync-tftmpl-")
require.NoError(t, err)
defer os.RemoveAll(dir)

Expand Down Expand Up @@ -233,12 +233,12 @@ func TestRenderTFVarsTmpl(t *testing.T) {
t.Parallel()

// Setup Consul server
log.SetOutput(ioutil.Discard)
log.SetOutput(io.Discard)
srv, err := testutil.NewTestServerConfigT(tb,
func(c *testutil.TestServerConfig) {
c.LogLevel = "warn"
c.Stdout = ioutil.Discard
c.Stderr = ioutil.Discard
c.Stdout = io.Discard
c.Stderr = io.Discard

// Hardcode node info so it doesn't change per run
c.NodeName = "worker-01"
Expand Down Expand Up @@ -275,8 +275,8 @@ func TestRenderTFVarsTmpl(t *testing.T) {
func(c *testutil.TestServerConfig) {
c.Bootstrap = false
c.LogLevel = "warn"
c.Stdout = ioutil.Discard
c.Stderr = ioutil.Discard
c.Stdout = io.Discard
c.Stderr = io.Discard

// Hardcode node info so it doesn't change per run
c.NodeName = "worker-02"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package tmplfunc

import (
"io/ioutil"
"io"
"regexp"
"testing"
"time"
Expand Down Expand Up @@ -182,8 +182,8 @@ func TestCatalogServicesRegistrationQuery_Fetch(t *testing.T) {
func(c *testutil.TestServerConfig) {
c.Bootstrap = false
c.LogLevel = "warn"
c.Stdout = ioutil.Discard
c.Stderr = ioutil.Discard
c.Stdout = io.Discard
c.Stderr = io.Discard
c.NodeMeta = map[string]string{"k": "v"}
})

Expand All @@ -195,8 +195,8 @@ func TestCatalogServicesRegistrationQuery_Fetch(t *testing.T) {
c.Datacenter = "dc2"
c.Bootstrap = true
c.LogLevel = "warn"
c.Stdout = ioutil.Discard
c.Stderr = ioutil.Discard
c.Stdout = io.Discard
c.Stderr = io.Discard
})
require.NoError(t, err, "failed to start consul server 3")
defer srv3.Stop()
Expand Down
10 changes: 5 additions & 5 deletions templates/tftmpl/tmplfunc/services_regex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package tmplfunc

import (
"fmt"
"io/ioutil"
"io"
"regexp"
"sort"
"testing"
Expand Down Expand Up @@ -155,8 +155,8 @@ func TestServicesRegexQuery_Fetch(t *testing.T) {
func(c *testutil.TestServerConfig) {
c.Bootstrap = false
c.LogLevel = "warn"
c.Stdout = ioutil.Discard
c.Stderr = ioutil.Discard
c.Stdout = io.Discard
c.Stderr = io.Discard
c.NodeMeta = nodeMeta
})
consulSrv2 := &dep.HealthService{
Expand All @@ -175,8 +175,8 @@ func TestServicesRegexQuery_Fetch(t *testing.T) {
c.Datacenter = "dc2"
c.Bootstrap = true
c.LogLevel = "warn"
c.Stdout = ioutil.Discard
c.Stderr = ioutil.Discard
c.Stdout = io.Discard
c.Stderr = io.Discard
})
require.NoError(t, err, "failed to start consul server 3")
defer srv3.Stop()
Expand Down
Loading
Loading