mirror of
https://github.com/grafana/grafana.git
synced 2026-08-18 17:15:08 -05:00
Handle ioutil deprecations (#53526)
* replace ioutil.ReadFile -> os.ReadFile * replace ioutil.ReadAll -> io.ReadAll * replace ioutil.TempFile -> os.CreateTemp * replace ioutil.NopCloser -> io.NopCloser * replace ioutil.WriteFile -> os.WriteFile * replace ioutil.TempDir -> os.MkdirTemp * replace ioutil.Discard -> io.Discard
This commit is contained in:
@@ -3,8 +3,8 @@ package main
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -22,7 +22,7 @@ func main() {
|
||||
}
|
||||
|
||||
//nolint
|
||||
b, err := ioutil.ReadFile(input)
|
||||
b, err := os.ReadFile(input)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
@@ -101,7 +101,7 @@ func main() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = ioutil.WriteFile(output, out, 0644)
|
||||
err = os.WriteFile(output, out, 0644)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package definitions
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -808,10 +808,10 @@ alertmanager_config: |
|
||||
func Test_GettableUserConfigRoundtrip(t *testing.T) {
|
||||
// raw contains secret fields. We'll unmarshal, re-marshal, and ensure
|
||||
// the fields are not redacted.
|
||||
yamlEncoded, err := ioutil.ReadFile("alertmanager_test_artifact.yaml")
|
||||
yamlEncoded, err := os.ReadFile("alertmanager_test_artifact.yaml")
|
||||
require.Nil(t, err)
|
||||
|
||||
jsonEncoded, err := ioutil.ReadFile("alertmanager_test_artifact.json")
|
||||
jsonEncoded, err := os.ReadFile("alertmanager_test_artifact.json")
|
||||
require.Nil(t, err)
|
||||
|
||||
// test GettableUserConfig (yamlDecode -> jsonEncode)
|
||||
@@ -1031,7 +1031,7 @@ routes:
|
||||
}
|
||||
|
||||
func Test_Marshaling_Validation(t *testing.T) {
|
||||
jsonEncoded, err := ioutil.ReadFile("alertmanager_test_artifact.json")
|
||||
jsonEncoded, err := os.ReadFile("alertmanager_test_artifact.json")
|
||||
require.Nil(t, err)
|
||||
|
||||
var tmp GettableUserConfig
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package channels
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
@@ -92,7 +91,7 @@ Labels:
|
||||
`
|
||||
|
||||
func templateForTests(t *testing.T) *template.Template {
|
||||
f, err := ioutil.TempFile("/tmp", "template")
|
||||
f, err := os.CreateTemp("/tmp", "template")
|
||||
require.NoError(t, err)
|
||||
defer func(f *os.File) {
|
||||
_ = f.Close()
|
||||
|
||||
@@ -2,7 +2,6 @@ package channels
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io/ioutil"
|
||||
"net/url"
|
||||
"os"
|
||||
"testing"
|
||||
@@ -56,7 +55,7 @@ func TestDefaultTemplateString(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
f, err := ioutil.TempFile("/tmp", "template")
|
||||
f, err := os.CreateTemp("/tmp", "template")
|
||||
require.NoError(t, err)
|
||||
defer func(f *os.File) {
|
||||
_ = f.Close()
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
@@ -268,7 +268,7 @@ func TestTeamsNotifier(t *testing.T) {
|
||||
expBody, err := json.Marshal(c.expMsg)
|
||||
require.NoError(t, err)
|
||||
|
||||
body, err := ioutil.ReadAll(clientStub.lastRequest.Body)
|
||||
body, err := io.ReadAll(clientStub.lastRequest.Body)
|
||||
require.NoError(t, err)
|
||||
require.JSONEq(t, string(expBody), string(body))
|
||||
})
|
||||
@@ -311,6 +311,6 @@ func newMockClient(resp *mockResponse) *mockClient {
|
||||
func makeResponse(status int, body string) *http.Response {
|
||||
return &http.Response{
|
||||
StatusCode: status,
|
||||
Body: ioutil.NopCloser(strings.NewReader(body)),
|
||||
Body: io.NopCloser(strings.NewReader(body)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ func PersistTemplates(cfg *api.PostableUserConfig, path string) ([]string, bool,
|
||||
// Check if the template file already exists and if it has changed
|
||||
// We can safely ignore gosec here as we've previously checked the filename is clean
|
||||
// nolint:gosec
|
||||
if tmpl, err := ioutil.ReadFile(file); err == nil && string(tmpl) == content {
|
||||
if tmpl, err := os.ReadFile(file); err == nil && string(tmpl) == content {
|
||||
// Templates file is the same we have, no-op and continue.
|
||||
continue
|
||||
} else if err != nil && !os.IsNotExist(err) {
|
||||
@@ -45,7 +45,7 @@ func PersistTemplates(cfg *api.PostableUserConfig, path string) ([]string, bool,
|
||||
|
||||
// We can safely ignore gosec here as we've previously checked the filename is clean
|
||||
// nolint:gosec
|
||||
if err := ioutil.WriteFile(file, []byte(content), 0644); err != nil {
|
||||
if err := os.WriteFile(file, []byte(content), 0644); err != nil {
|
||||
return nil, false, fmt.Errorf("unable to create Alertmanager template file %q: %s", file, err)
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package notifier
|
||||
import (
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
@@ -71,7 +72,7 @@ func TestPersistTemplates(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
// Write "existing files"
|
||||
for name, content := range tt.existingTemplates {
|
||||
err := ioutil.WriteFile(filepath.Join(dir, name), []byte(content), 0644)
|
||||
err := os.WriteFile(filepath.Join(dir, name), []byte(content), 0644)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
c := &api.PostableUserConfig{TemplateFiles: tt.templates}
|
||||
@@ -87,7 +88,7 @@ func TestPersistTemplates(t *testing.T) {
|
||||
}
|
||||
// Safe to disable, this is a test.
|
||||
// nolint:gosec
|
||||
content, err := ioutil.ReadFile(filepath.Join(dir, f.Name()))
|
||||
content, err := os.ReadFile(filepath.Join(dir, f.Name()))
|
||||
// nolint:gosec
|
||||
require.NoError(t, err)
|
||||
files[f.Name()] = string(content)
|
||||
|
||||
@@ -2,7 +2,6 @@ package notifier
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
@@ -24,7 +23,7 @@ func TestFileStore_FilepathFor_DirectoryNotExist(t *testing.T) {
|
||||
r, err := fs.FilepathFor(context.Background(), filekey)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, filePath, r)
|
||||
f, err := ioutil.ReadFile(filepath.Clean(filePath))
|
||||
f, err := os.ReadFile(filepath.Clean(filePath))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "silence1,silence3", string(f))
|
||||
require.NoError(t, os.Remove(filePath))
|
||||
@@ -44,7 +43,7 @@ func TestFileStore_FilepathFor(t *testing.T) {
|
||||
r, err := fs.FilepathFor(context.Background(), filekey)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, filePath, r)
|
||||
f, err := ioutil.ReadFile(filepath.Clean(filePath))
|
||||
f, err := os.ReadFile(filepath.Clean(filePath))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "silence1,silence2", string(f))
|
||||
require.NoError(t, os.Remove(filePath))
|
||||
@@ -56,7 +55,7 @@ func TestFileStore_FilepathFor(t *testing.T) {
|
||||
r, err := fs.FilepathFor(context.Background(), filekey)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, filePath, r)
|
||||
f, err := ioutil.ReadFile(filepath.Clean(filePath))
|
||||
f, err := os.ReadFile(filepath.Clean(filePath))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "silence1,silence3", string(f))
|
||||
require.NoError(t, os.Remove(filePath))
|
||||
@@ -68,7 +67,7 @@ func TestFileStore_FilepathFor(t *testing.T) {
|
||||
r, err := fs.FilepathFor(context.Background(), filekey)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, filePath, r)
|
||||
_, err = ioutil.ReadFile(filepath.Clean(filePath))
|
||||
_, err = os.ReadFile(filepath.Clean(filePath))
|
||||
require.Error(t, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package sender
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"sync"
|
||||
@@ -71,7 +71,7 @@ func (am *FakeExternalAlertmanager) Alerts() amv2.PostableAlerts {
|
||||
|
||||
func (am *FakeExternalAlertmanager) Handler() func(w http.ResponseWriter, r *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
b, err := ioutil.ReadAll(r.Body)
|
||||
b, err := io.ReadAll(r.Body)
|
||||
require.NoError(am.t, err)
|
||||
|
||||
a := amv2.PostableAlerts{}
|
||||
|
||||
Reference in New Issue
Block a user