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]: Removing deprecated packages from being used #500

Merged
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
3 changes: 1 addition & 2 deletions signalfx/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package signalfx
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
Expand Down Expand Up @@ -216,7 +215,7 @@ func signalfxConfigure(data *schema.ResourceData) (interface{}, error) {
}

func readConfigFile(configPath string, config *signalfxConfig) error {
configFile, err := ioutil.ReadFile(configPath)
configFile, err := os.ReadFile(configPath)
if err != nil {
return fmt.Errorf("failed to open config file. %s", err.Error())
}
Expand Down
44 changes: 17 additions & 27 deletions signalfx/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package signalfx
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/davecgh/go-spew/spew"
Expand Down Expand Up @@ -32,8 +32,10 @@ func resetGlobals() {
HomeConfigPath = OldHomeConfigPath
}

func createTempConfigFile(content string, name string) (*os.File, error) {
tmpfile, err := ioutil.TempFile(os.TempDir(), name)
func createTempConfigFile(tb testing.TB, content string, name string) (*os.File, error) {
tb.Helper()

tmpfile, err := os.Create(filepath.Join(tb.TempDir(), name))
if err != nil {
return nil, fmt.Errorf("Error creating temporary test file. err: %s", err.Error())
}
Expand Down Expand Up @@ -86,17 +88,11 @@ func TestProviderConfigureFromNothing(t *testing.T) {

func TestProviderConfigureFromTerraform(t *testing.T) {
defer resetGlobals()
tmpfileSystem, err := createTempConfigFile(`{"useless_config":"foo","auth_token":"ZZZ"}`, "signalfx.conf")
tmpfileSystem, err := createTempConfigFile(t, `{"useless_config":"foo","auth_token":"ZZZ"}`, "signalfx.conf")
if err != nil {
t.Fatal(err.Error())
}
defer os.Remove(tmpfileSystem.Name())
SystemConfigPath = tmpfileSystem.Name()
tmpfileHome, err := createTempConfigFile(`{"auth_token":"WWW"}`, "signalfx.conf")
if err != nil {
t.Fatal(err.Error())
}
defer os.Remove(tmpfileHome.Name())

old := os.Getenv("SFX_AUTH_TOKEN")
os.Setenv("SFX_AUTH_TOKEN", "YYY")
Expand Down Expand Up @@ -152,13 +148,13 @@ func TestProviderConfigureFromTerraformOnly(t *testing.T) {

func TestProviderConfigureFromEnvironment(t *testing.T) {
defer resetGlobals()
tmpfileSystem, err := createTempConfigFile(`{"useless_config":"foo","auth_token":"ZZZ"}`, "signalfx.conf")
tmpfileSystem, err := createTempConfigFile(t, `{"useless_config":"foo","auth_token":"ZZZ"}`, "signalfx.conf")
if err != nil {
t.Fatal(err.Error())
}
defer os.Remove(tmpfileSystem.Name())

SystemConfigPath = tmpfileSystem.Name()
tmpfileHome, err := createTempConfigFile(`{"auth_token":"WWW"}`, "signalfx.conf")
tmpfileHome, err := createTempConfigFile(t, `{"auth_token":"WWW"}`, "signalfx.conf")
if err != nil {
t.Fatal(err.Error())
}
Expand Down Expand Up @@ -223,22 +219,20 @@ func TestProviderConfigureFromEnvironmentOnly(t *testing.T) {

func TestSignalFxConfigureFromHomeFile(t *testing.T) {
defer resetGlobals()
tmpfileSystem, err := createTempConfigFile(`{"useless_config":"foo","auth_token":"ZZZ"}`, "signalfx.conf")
tmpfileSystem, err := createTempConfigFile(t, `{"useless_config":"foo","auth_token":"ZZZ"}`, "signalfx.conf")
if err != nil {
t.Fatal(err.Error())
}
defer os.Remove(tmpfileSystem.Name())

old := os.Getenv("SFX_AUTH_TOKEN")
defer os.Setenv("SFX_AUTH_TOKEN", old)
os.Unsetenv("SFX_AUTH_TOKEN")

SystemConfigPath = tmpfileSystem.Name()
tmpfileHome, err := createTempConfigFile(`{"auth_token":"WWW"}`, "signalfx.conf")
tmpfileHome, err := createTempConfigFile(t, `{"auth_token":"WWW"}`, "signalfx.conf")
if err != nil {
t.Fatal(err.Error())
}
defer os.Remove(tmpfileHome.Name())
HomeConfigPath = tmpfileHome.Name()
raw := make(map[string]interface{})

Expand All @@ -254,13 +248,12 @@ func TestSignalFxConfigureFromHomeFile(t *testing.T) {

func TestSignalFxConfigureFromNetrcFile(t *testing.T) {
defer resetGlobals()
tmpfileSystem, err := createTempConfigFile(`{"useless_config":"foo","auth_token":"ZZZ"}`, "signalfx.conf")
tmpfileSystem, err := createTempConfigFile(t, `{"useless_config":"foo","auth_token":"ZZZ"}`, "signalfx.conf")
if err != nil {
t.Fatal(err.Error())
}
defer os.Remove(tmpfileSystem.Name())
SystemConfigPath = tmpfileSystem.Name()
tmpfileHome, err := createTempConfigFile(`machine api.signalfx.com login auth_login password WWW`, ".netrc")
tmpfileHome, err := createTempConfigFile(t, `machine api.signalfx.com login auth_login password WWW`, ".netrc")
if err != nil {
t.Fatal(err.Error())
}
Expand Down Expand Up @@ -297,7 +290,7 @@ func TestSignalFxConfigureFromNetrcFile(t *testing.T) {
func TestSignalFxConfigureFromHomeFileOnly(t *testing.T) {
defer resetGlobals()
SystemConfigPath = "filedoesnotexist"
tmpfileHome, err := createTempConfigFile(`{"auth_token":"WWW"}`, "signalfx.conf")
tmpfileHome, err := createTempConfigFile(t, `{"auth_token":"WWW"}`, "signalfx.conf")
if err != nil {
t.Fatal(err.Error())
}
Expand Down Expand Up @@ -345,11 +338,10 @@ func TestSignalFxConfigureFromSystemFileOnly(t *testing.T) {
defer os.Setenv("SFX_CUSTOM_APP_URL", old)
os.Unsetenv("SFX_CUSTOM_APP_URL")

tmpfileSystem, err := createTempConfigFile(`{"useless_config":"foo","auth_token":"ZZZ"}`, "signalfx.conf")
tmpfileSystem, err := createTempConfigFile(t, `{"useless_config":"foo","auth_token":"ZZZ"}`, "signalfx.conf")
if err != nil {
t.Fatal(err.Error())
}
defer os.Remove(tmpfileSystem.Name())
SystemConfigPath = tmpfileSystem.Name()
HomeConfigPath = "filedoesnotexist"
raw := make(map[string]interface{})
Expand Down Expand Up @@ -377,23 +369,21 @@ func TestReadConfigFileFileNotFound(t *testing.T) {

func TestReadConfigFileParseError(t *testing.T) {
config := signalfxConfig{}
tmpfile, err := createTempConfigFile(`{"auth_tok`, "signalfx.conf")
tmpfile, err := createTempConfigFile(t, `{"auth_tok`, "signalfx.conf")
if err != nil {
t.Fatal(err.Error())
}
defer os.Remove(tmpfile.Name())

err = readConfigFile(tmpfile.Name(), &config)
assert.Contains(t, err.Error(), "failed to parse config file")
}

func TestReadConfigFileSuccess(t *testing.T) {
config := signalfxConfig{}
tmpfile, err := createTempConfigFile(`{"useless_config":"foo","auth_token":"XXX"}`, "signalfx.conf")
tmpfile, err := createTempConfigFile(t, `{"useless_config":"foo","auth_token":"XXX"}`, "signalfx.conf")
if err != nil {
t.Fatal(err.Error())
}
defer os.Remove(tmpfile.Name())

err = readConfigFile(tmpfile.Name(), &config)
assert.Nil(t, err)
Expand Down
4 changes: 2 additions & 2 deletions signalfx/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"math"
"net/http"
"net/url"
Expand Down Expand Up @@ -110,7 +110,7 @@ func sendRequest(method string, url string, token string, payload []byte) (int,
return -1, nil, fmt.Errorf("Failed sending %s request to Signalfx: %s", method, err.Error())
}

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
defer resp.Body.Close()

if err != nil {
Expand Down
Loading