Skip to content

Commit

Permalink
refactor: move from io/ioutil to io and os packages
Browse files Browse the repository at this point in the history
The io/ioutil package has been deprecated as of Go 1.16 [1]. This commit
replaces the existing io/ioutil functions with their new definitions in
io and os packages.

[1]: https://golang.org/doc/go1.16#ioutil
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
  • Loading branch information
Juneezee authored and squeed committed Dec 12, 2022
1 parent e18f632 commit f024754
Show file tree
Hide file tree
Showing 19 changed files with 85 additions and 99 deletions.
9 changes: 4 additions & 5 deletions libcni/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -260,7 +259,7 @@ func (c *CNIConfig) cacheAdd(result types.Result, config []byte, netName string,
return err
}

return ioutil.WriteFile(fname, newBytes, 0600)
return os.WriteFile(fname, newBytes, 0600)
}

func (c *CNIConfig) cacheDel(netName string, rt *RuntimeConf) error {
Expand All @@ -279,7 +278,7 @@ func (c *CNIConfig) getCachedConfig(netName string, rt *RuntimeConf) ([]byte, *R
if err != nil {
return nil, nil, err
}
bytes, err = ioutil.ReadFile(fname)
bytes, err = os.ReadFile(fname)
if err != nil {
// Ignore read errors; the cached result may not exist on-disk
return nil, nil, nil
Expand Down Expand Up @@ -307,7 +306,7 @@ func (c *CNIConfig) getLegacyCachedResult(netName, cniVersion string, rt *Runtim
if err != nil {
return nil, err
}
data, err := ioutil.ReadFile(fname)
data, err := os.ReadFile(fname)
if err != nil {
// Ignore read errors; the cached result may not exist on-disk
return nil, nil
Expand Down Expand Up @@ -335,7 +334,7 @@ func (c *CNIConfig) getCachedResult(netName, cniVersion string, rt *RuntimeConf)
if err != nil {
return nil, err
}
fdata, err := ioutil.ReadFile(fname)
fdata, err := os.ReadFile(fname)
if err != nil {
// Ignore read errors; the cached result may not exist on-disk
return nil, nil
Expand Down
65 changes: 32 additions & 33 deletions libcni/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net"
"os"
"path/filepath"
Expand Down Expand Up @@ -61,7 +60,7 @@ func stringInList(s string, list []string) bool {
}

func newPluginInfo(cniVersion, configValue, prevResult string, injectDebugFilePath bool, result string, runtimeConfig map[string]interface{}, capabilities []string) pluginInfo {
debugFile, err := ioutil.TempFile("", "cni_debug")
debugFile, err := os.CreateTemp("", "cni_debug")
Expect(err).NotTo(HaveOccurred())
Expect(debugFile.Close()).To(Succeed())
debugFilePath := debugFile.Name()
Expand Down Expand Up @@ -153,7 +152,7 @@ var _ = Describe("Invoking plugins", func() {

BeforeEach(func() {
var err error
cacheDirPath, err = ioutil.TempDir("", "cni_cachedir")
cacheDirPath, err = os.MkdirTemp("", "cni_cachedir")
Expect(err).NotTo(HaveOccurred())
})

Expand All @@ -173,7 +172,7 @@ var _ = Describe("Invoking plugins", func() {
)

BeforeEach(func() {
debugFile, err := ioutil.TempFile("", "cni_debug")
debugFile, err := os.CreateTemp("", "cni_debug")
Expect(err).NotTo(HaveOccurred())
Expect(debugFile.Close()).To(Succeed())
debugFilePath = debugFile.Name()
Expand Down Expand Up @@ -296,7 +295,7 @@ var _ = Describe("Invoking plugins", func() {
)

BeforeEach(func() {
debugFile, err := ioutil.TempFile("", "cni_debug")
debugFile, err := os.CreateTemp("", "cni_debug")
Expect(err).NotTo(HaveOccurred())
Expect(debugFile.Close()).To(Succeed())
debugFilePath = debugFile.Name()
Expand Down Expand Up @@ -441,7 +440,7 @@ var _ = Describe("Invoking plugins", func() {
// Make the results directory inaccessible by making it a
// file instead of a directory
tmpPath := filepath.Join(cacheDirPath, "results")
err := ioutil.WriteFile(tmpPath, []byte("afdsasdfasdf"), 0600)
err := os.WriteFile(tmpPath, []byte("afdsasdfasdf"), 0600)
Expect(err).NotTo(HaveOccurred())

result, err := cniConfig.AddNetwork(ctx, netConfig, runtimeConfig)
Expand All @@ -461,7 +460,7 @@ var _ = Describe("Invoking plugins", func() {
"ips": [{"address": "10.1.2.3/24"}],
"dns": {}
}`
err = ioutil.WriteFile(cacheFile, []byte(cachedJson), 0600)
err = os.WriteFile(cacheFile, []byte(cachedJson), 0600)
Expect(err).NotTo(HaveOccurred())

err = cniConfig.CheckNetwork(ctx, netConfig, runtimeConfig)
Expand Down Expand Up @@ -541,7 +540,7 @@ var _ = Describe("Invoking plugins", func() {

Context("containing only a cached result", func() {
It("only passes a prevResult to the plugin", func() {
err := ioutil.WriteFile(cacheFile, []byte(`{
err := os.WriteFile(cacheFile, []byte(`{
"cniVersion": "1.0.0",
"ips": [{"address": "10.1.2.3/24"}],
"dns": {}
Expand Down Expand Up @@ -579,7 +578,7 @@ var _ = Describe("Invoking plugins", func() {
}`))
Expect(err).NotTo(HaveOccurred())

err = ioutil.WriteFile(cacheFile, []byte(ipResult), 0600)
err = os.WriteFile(cacheFile, []byte(ipResult), 0600)
Expect(err).NotTo(HaveOccurred())

debug.ReportResult = ipResult
Expand All @@ -605,7 +604,7 @@ var _ = Describe("Invoking plugins", func() {

Context("is invalid JSON", func() {
It("returns an error", func() {
err := ioutil.WriteFile(cacheFile, []byte("adfadsfasdfasfdsafaf"), 0600)
err := os.WriteFile(cacheFile, []byte("adfadsfasdfasfdsafaf"), 0600)
Expect(err).NotTo(HaveOccurred())

err = cniConfig.CheckNetwork(ctx, netConfig, runtimeConfig)
Expand All @@ -615,7 +614,7 @@ var _ = Describe("Invoking plugins", func() {

Context("version doesn't match the config version", func() {
It("succeeds when the cached result can be converted", func() {
err := ioutil.WriteFile(cacheFile, []byte(`{
err := os.WriteFile(cacheFile, []byte(`{
"cniVersion": "0.3.1",
"ips": [{"version": "4", "address": "10.1.2.3/24"}],
"dns": {}
Expand All @@ -627,7 +626,7 @@ var _ = Describe("Invoking plugins", func() {
})

It("returns an error when the cached result cannot be converted", func() {
err := ioutil.WriteFile(cacheFile, []byte(`{
err := os.WriteFile(cacheFile, []byte(`{
"cniVersion": "0.4567.0",
"ips": [{"version": "4", "address": "10.1.2.3/24"}],
"dns": {}
Expand All @@ -651,7 +650,7 @@ var _ = Describe("Invoking plugins", func() {
"ips": [{"address": "10.1.2.3/24"}],
"dns": {}
}`
err = ioutil.WriteFile(cacheFile, []byte(cachedJson), 0600)
err = os.WriteFile(cacheFile, []byte(cachedJson), 0600)
Expect(err).NotTo(HaveOccurred())

err = cniConfig.DelNetwork(ctx, netConfig, runtimeConfig)
Expand Down Expand Up @@ -722,7 +721,7 @@ var _ = Describe("Invoking plugins", func() {
})

It("deletes the cached result and config after the first DEL", func() {
err := ioutil.WriteFile(resultCacheFile, []byte(`{
err := os.WriteFile(resultCacheFile, []byte(`{
"cniVersion": "0.4.0",
"ips": [{"version": "4", "address": "10.1.2.3/24"}],
"dns": {}
Expand All @@ -731,7 +730,7 @@ var _ = Describe("Invoking plugins", func() {

err = cniConfig.DelNetwork(ctx, netConfig, runtimeConfig)
Expect(err).NotTo(HaveOccurred())
_, err = ioutil.ReadFile(resultCacheFile)
_, err = os.ReadFile(resultCacheFile)
Expect(err).To(HaveOccurred())

err = cniConfig.DelNetwork(ctx, netConfig, runtimeConfig)
Expand All @@ -750,7 +749,7 @@ var _ = Describe("Invoking plugins", func() {

Context("less than 0.4.0", func() {
It("does not pass a prevResult to the plugin", func() {
err := ioutil.WriteFile(cacheFile, []byte(`{
err := os.WriteFile(cacheFile, []byte(`{
"cniVersion": "0.3.1",
"ips": [{"version": "4", "address": "10.1.2.3/24"}],
"dns": {}
Expand All @@ -776,7 +775,7 @@ var _ = Describe("Invoking plugins", func() {

Context("equal to 0.4.0", func() {
It("passes a prevResult to the plugin", func() {
err := ioutil.WriteFile(cacheFile, []byte(`{
err := os.WriteFile(cacheFile, []byte(`{
"cniVersion": "0.4.0",
"ips": [{"version": "4", "address": "10.1.2.3/24"}],
"dns": {}
Expand Down Expand Up @@ -805,7 +804,7 @@ var _ = Describe("Invoking plugins", func() {

Context("result is invalid JSON", func() {
It("returns an error", func() {
err := ioutil.WriteFile(cacheFile, []byte("adfadsfasdfasfdsafaf"), 0600)
err := os.WriteFile(cacheFile, []byte("adfadsfasdfasfdsafaf"), 0600)
Expect(err).NotTo(HaveOccurred())

err = cniConfig.DelNetwork(ctx, netConfig, runtimeConfig)
Expand All @@ -815,7 +814,7 @@ var _ = Describe("Invoking plugins", func() {

Context("result version doesn't match the config version", func() {
It("succeeds when the cached result can be converted", func() {
err := ioutil.WriteFile(cacheFile, []byte(`{
err := os.WriteFile(cacheFile, []byte(`{
"cniVersion": "0.3.1",
"ips": [{"version": "4", "address": "10.1.2.3/24"}],
"dns": {}
Expand All @@ -827,7 +826,7 @@ var _ = Describe("Invoking plugins", func() {
})

It("returns an error when the cached result cannot be converted", func() {
err := ioutil.WriteFile(cacheFile, []byte(`{
err := os.WriteFile(cacheFile, []byte(`{
"cniVersion": "0.4567.0",
"ips": [{"version": "4", "address": "10.1.2.3/24"}],
"dns": {}
Expand Down Expand Up @@ -1163,7 +1162,7 @@ var _ = Describe("Invoking plugins", func() {
})
It("should not have written cache files", func() {
resultCacheFile := resultCacheFilePath(cacheDirPath, netConfigList.Name, runtimeConfig)
_, err := ioutil.ReadFile(resultCacheFile)
_, err := os.ReadFile(resultCacheFile)
Expect(err).To(HaveOccurred())
})
})
Expand All @@ -1173,7 +1172,7 @@ var _ = Describe("Invoking plugins", func() {
// Make the results directory inaccessible by making it a
// file instead of a directory
tmpPath := filepath.Join(cacheDirPath, "results")
err := ioutil.WriteFile(tmpPath, []byte("afdsasdfasdf"), 0600)
err := os.WriteFile(tmpPath, []byte("afdsasdfasdf"), 0600)
Expect(err).NotTo(HaveOccurred())

result, err := cniConfig.AddNetworkList(ctx, netConfigList, runtimeConfig)
Expand All @@ -1188,7 +1187,7 @@ var _ = Describe("Invoking plugins", func() {
cacheFile := resultCacheFilePath(cacheDirPath, netConfigList.Name, runtimeConfig)
err := os.MkdirAll(filepath.Dir(cacheFile), 0700)
Expect(err).NotTo(HaveOccurred())
err = ioutil.WriteFile(cacheFile, []byte(ipResult), 0600)
err = os.WriteFile(cacheFile, []byte(ipResult), 0600)
Expect(err).NotTo(HaveOccurred())

err = cniConfig.CheckNetworkList(ctx, netConfigList, runtimeConfig)
Expand Down Expand Up @@ -1241,7 +1240,7 @@ var _ = Describe("Invoking plugins", func() {
"ips": [{"version": "4", "address": "10.1.2.3/24"}],
"dns": {}
}`
err := ioutil.WriteFile(cacheFile, []byte(ipResult), 0600)
err := os.WriteFile(cacheFile, []byte(ipResult), 0600)
Expect(err).NotTo(HaveOccurred())

netConfigList, plugins = makePluginList("0.4.0", ipResult, rcMap)
Expand Down Expand Up @@ -1312,7 +1311,7 @@ var _ = Describe("Invoking plugins", func() {
cacheFile := resultCacheFilePath(cacheDirPath, netConfigList.Name, runtimeConfig)
err := os.MkdirAll(filepath.Dir(cacheFile), 0700)
Expect(err).NotTo(HaveOccurred())
err = ioutil.WriteFile(cacheFile, []byte("adfadsfasdfasfdsafaf"), 0600)
err = os.WriteFile(cacheFile, []byte("adfadsfasdfasfdsafaf"), 0600)
Expect(err).NotTo(HaveOccurred())

err = cniConfig.CheckNetworkList(ctx, netConfigList, runtimeConfig)
Expand Down Expand Up @@ -1365,7 +1364,7 @@ var _ = Describe("Invoking plugins", func() {
"ips": [{"version": "4", "address": "10.1.2.3/24"}],
"dns": {}
}`
err := ioutil.WriteFile(cacheFile, []byte(ipResult), 0600)
err := os.WriteFile(cacheFile, []byte(ipResult), 0600)
Expect(err).NotTo(HaveOccurred())

netConfigList, plugins = makePluginList("0.4.0", ipResult, rcMap)
Expand Down Expand Up @@ -1397,7 +1396,7 @@ var _ = Describe("Invoking plugins", func() {
"ips": [{"version": "4", "address": "10.1.2.3/24"}],
"dns": {}
}`
err := ioutil.WriteFile(cacheFile, []byte(ipResult), 0600)
err := os.WriteFile(cacheFile, []byte(ipResult), 0600)
Expect(err).NotTo(HaveOccurred())

netConfigList, plugins = makePluginList("0.3.1", ipResult, rcMap)
Expand Down Expand Up @@ -1445,7 +1444,7 @@ var _ = Describe("Invoking plugins", func() {
cacheFile := resultCacheFilePath(cacheDirPath, netConfigList.Name, runtimeConfig)
err := os.MkdirAll(filepath.Dir(cacheFile), 0700)
Expect(err).NotTo(HaveOccurred())
err = ioutil.WriteFile(cacheFile, []byte("adfadsfasdfasfdsafaf"), 0600)
err = os.WriteFile(cacheFile, []byte("adfadsfasdfasfdsafaf"), 0600)
Expect(err).NotTo(HaveOccurred())

err = cniConfig.DelNetworkList(ctx, netConfigList, runtimeConfig)
Expand Down Expand Up @@ -1487,7 +1486,7 @@ var _ = Describe("Invoking plugins", func() {
)

BeforeEach(func() {
debugFile, err := ioutil.TempFile("", "cni_debug")
debugFile, err := os.CreateTemp("", "cni_debug")
Expect(err).NotTo(HaveOccurred())
Expect(debugFile.Close()).To(Succeed())
debugFilePath = debugFile.Name()
Expand Down Expand Up @@ -1679,7 +1678,7 @@ var _ = Describe("Invoking plugins", func() {
netNS := "/some/netns/path"

BeforeEach(func() {
debugFile, err := ioutil.TempFile("", "cni_debug")
debugFile, err := os.CreateTemp("", "cni_debug")
Expect(err).NotTo(HaveOccurred())
Expect(debugFile.Close()).To(Succeed())
debugFilePath = debugFile.Name()
Expand Down Expand Up @@ -1728,7 +1727,7 @@ var _ = Describe("Invoking plugins", func() {
Expect(err).NotTo(HaveOccurred())

resultsDir := filepath.Join(cacheDirPath, "results")
files, err := ioutil.ReadDir(resultsDir)
files, err := os.ReadDir(resultsDir)
Expect(err).NotTo(HaveOccurred())
Expect(len(files)).To(Equal(2))
var foundFirst, foundSecond bool
Expand All @@ -1740,7 +1739,7 @@ var _ = Describe("Invoking plugins", func() {
NetworkName string `json:"networkName"`
}

data, err := ioutil.ReadFile(filepath.Join(resultsDir, f.Name()))
data, err := os.ReadFile(filepath.Join(resultsDir, f.Name()))
Expect(err).NotTo(HaveOccurred())
cc := &cachedConfig{}
err = json.Unmarshal(data, cc)
Expand Down Expand Up @@ -1853,7 +1852,7 @@ var _ = Describe("Invoking plugins", func() {
err := os.MkdirAll(filepath.Dir(resultCacheFile), 0700)
Expect(err).NotTo(HaveOccurred())

err = ioutil.WriteFile(resultCacheFile, []byte("adfadsfasdfasfdsafaf"), 0600)
err = os.WriteFile(resultCacheFile, []byte("adfadsfasdfasfdsafaf"), 0600)
Expect(err).NotTo(HaveOccurred())

cachedConfig, newRt, err := cniConfig.GetNetworkCachedConfig(netConfig, runtimeConfig)
Expand Down
3 changes: 1 addition & 2 deletions libcni/backwards_compatibility_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package libcni_test
import (
"context"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand All @@ -36,7 +35,7 @@ var _ = Describe("Backwards compatibility", func() {

BeforeEach(func() {
var err error
cacheDirPath, err = ioutil.TempDir("", "cni_cachedir")
cacheDirPath, err = os.MkdirTemp("", "cni_cachedir")
Expect(err).NotTo(HaveOccurred())
})

Expand Down
7 changes: 3 additions & 4 deletions libcni/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package libcni
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -55,7 +54,7 @@ func ConfFromBytes(bytes []byte) (*NetworkConfig, error) {
}

func ConfFromFile(filename string) (*NetworkConfig, error) {
bytes, err := ioutil.ReadFile(filename)
bytes, err := os.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("error reading %s: %w", filename, err)
}
Expand Down Expand Up @@ -140,7 +139,7 @@ func ConfListFromBytes(bytes []byte) (*NetworkConfigList, error) {
}

func ConfListFromFile(filename string) (*NetworkConfigList, error) {
bytes, err := ioutil.ReadFile(filename)
bytes, err := os.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("error reading %s: %w", filename, err)
}
Expand All @@ -149,7 +148,7 @@ func ConfListFromFile(filename string) (*NetworkConfigList, error) {

func ConfFiles(dir string, extensions []string) ([]string, error) {
// In part, adapted from rkt/networking/podenv.go#listFiles
files, err := ioutil.ReadDir(dir)
files, err := os.ReadDir(dir)
switch {
case err == nil: // break
case os.IsNotExist(err):
Expand Down
Loading

0 comments on commit f024754

Please sign in to comment.