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

Use alternate package for ioutil #538

Merged
merged 1 commit into from
Jan 3, 2024
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
9 changes: 7 additions & 2 deletions cmd/dhcp-sync/dhcp-sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ package dhcp

import (
"fmt"
"io/ioutil"
"net"
"os"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -93,6 +93,9 @@ func syncDHCPD() {
}

n, err := pvmclient.NetworkClient.Get(networkID)
if err != nil {
klog.Fatalf("failed to fetch network by ID: %v", err)
}

ipv4Addr, ipv4Net, err := net.ParseCIDR(*n.Cidr)
if err != nil {
Expand Down Expand Up @@ -144,7 +147,9 @@ func syncDHCPD() {

content := fmt.Sprintf(dhcpdTemplate, subnetStmt.IndentedString(""))
mutex.Lock()
ioutil.WriteFile(file, []byte(content), 0644)
if err = os.WriteFile(file, []byte(content), 0644); err != nil {
klog.Fatalf("cannot write to dhcp.conf file: %v", err)
}
mutex.Unlock()
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/image/info/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package info
import (
"encoding/xml"
"fmt"
"io/ioutil"
"io"
"os"
"path/filepath"

Expand Down Expand Up @@ -97,7 +97,7 @@ pvsadm image info rhcos-46-12152021.ova.gz
}
defer xmlFile.Close()
var version Ovf
byteValue, err := ioutil.ReadAll(xmlFile)
byteValue, err := io.ReadAll(xmlFile)
if err != nil {
return err
}
Expand Down
9 changes: 4 additions & 5 deletions cmd/image/qcow2ova/get-image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package qcow2ova

import (
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
Expand All @@ -43,27 +42,27 @@ func Test_getImage(t *testing.T) {
fmt.Fprintf(w, "Hello World!")
})
mux.HandleFunc("/fail", func(w http.ResponseWriter, req *http.Request) {
http.Error(w, fmt.Sprintf("failed to handle the build"), http.StatusInternalServerError)
http.Error(w, "failed to handle the build", http.StatusInternalServerError)
})

ts := httptest.NewServer(mux)
defer ts.Close()

content := []byte("temporary file's content")
dir, err := ioutil.TempDir("", "example")
dir, err := os.MkdirTemp("", "example")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(dir)

destDir, err := ioutil.TempDir("", "example")
destDir, err := os.MkdirTemp("", "example")
if err != nil {
log.Fatal(err)
}
//defer os.RemoveAll(destDir)

tmpfn := filepath.Join(dir, "tmpfile")
if err := ioutil.WriteFile(tmpfn, content, 0666); err != nil {
if err := os.WriteFile(tmpfn, content, 0666); err != nil {
log.Fatal(err)
}

Expand Down
7 changes: 3 additions & 4 deletions cmd/image/qcow2ova/prep/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package prep

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -97,17 +96,17 @@ func prepare(mnt, volume, dist, rhnuser, rhnpasswd, rootpasswd string) error {
if err != nil {
return err
}
err = ioutil.WriteFile(filepath.Join(mnt, "setup.sh"), []byte(setupStr), 0744)
err = os.WriteFile(filepath.Join(mnt, "setup.sh"), []byte(setupStr), 0744)
if err != nil {
return err
}

err = ioutil.WriteFile(filepath.Join(mnt, "/etc/cloud/cloud.cfg"), []byte(CloudConfig), 0644)
err = os.WriteFile(filepath.Join(mnt, "/etc/cloud/cloud.cfg"), []byte(CloudConfig), 0644)
if err != nil {
return err
}

err = ioutil.WriteFile(filepath.Join(mnt, "/etc/cloud/ds-identify.cfg"), []byte(dsIdentify), 0644)
err = os.WriteFile(filepath.Join(mnt, "/etc/cloud/ds-identify.cfg"), []byte(dsIdentify), 0644)
if err != nil {
return err
}
Expand Down
7 changes: 3 additions & 4 deletions cmd/image/qcow2ova/qcow2ova.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package qcow2ova

import (
"fmt"
"io/ioutil"
"os"
"os/signal"
"path/filepath"
Expand Down Expand Up @@ -98,7 +97,7 @@ Qcow2 images location:
return fmt.Errorf("--prep-template option is not supported for coreos distro")
} else {
klog.Info("Overriding with the user defined image preparation template.")
content, err := ioutil.ReadFile(opt.PrepTemplate)
content, err := os.ReadFile(opt.PrepTemplate)
if err != nil {
return err
}
Expand All @@ -107,7 +106,7 @@ Qcow2 images location:
}
if opt.CloudConfig != "" {
klog.V(2).Info("Overriding with the user defined cloud config.")
content, err := ioutil.ReadFile(opt.CloudConfig)
content, err := os.ReadFile(opt.CloudConfig)
if err != nil {
return err
}
Expand Down Expand Up @@ -172,7 +171,7 @@ Qcow2 images location:
RunE: func(cmd *cobra.Command, args []string) error {
opt := pkg.ImageCMDOptions

tmpDir, err := ioutil.TempDir(opt.TempDir, "qcow2ova")
tmpDir, err := os.MkdirTemp(opt.TempDir, "qcow2ova")
if err != nil {
return fmt.Errorf("failed to create a temprory directory: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/image/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package sync

import (
"errors"
"io/ioutil"
"os"
"strings"
"time"

Expand Down Expand Up @@ -160,7 +160,7 @@ func getResults(results <-chan bool, totalChannels int) bool {
func getSpec(specfileName string) ([]pkg.Spec, error) {
var spec []pkg.Spec
// Unmashalling yaml file
yamlFile, err := ioutil.ReadFile(specfileName)
yamlFile, err := os.ReadFile(specfileName)
if err != nil {
klog.Errorf("ERROR: Read yaml failed : %v", err)
return nil, err
Expand Down
3 changes: 1 addition & 2 deletions cmd/image/sync/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package sync

import (
"errors"
"io/ioutil"
"os"
"strconv"
"testing"
Expand Down Expand Up @@ -74,7 +73,7 @@ func TestGetSpec(t *testing.T) {
spec := mockCreateSpec()

// test case verification section
file, err := ioutil.TempFile("", "spec.*.yaml")
file, err := os.CreateTemp("", "spec.*.yaml")
require.NoError(t, err, "Error creating specfile")
defer file.Close()

Expand Down
3 changes: 1 addition & 2 deletions test/e2e/qcow2ova/qcow2ova.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package qcow2ova

import (
"io/ioutil"
"os"

"github.com/ppc64le-cloud/pvsadm/pkg/utils"
Expand All @@ -37,7 +36,7 @@ var _ = CMDDescribe("pvsadm qcow2ova tests", func() {

BeforeSuite(func() {
var err error
image, err = ioutil.TempFile("", "qcow2ova")
image, err = os.CreateTemp("", "qcow2ova")
Expect(err).NotTo(HaveOccurred())
_, err = image.WriteString("some dummy image")
Expect(err).NotTo(HaveOccurred())
Expand Down
23 changes: 15 additions & 8 deletions test/e2e/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package sync
import (
"errors"
"io/fs"
"io/ioutil"
"os"
"strings"

Expand Down Expand Up @@ -66,13 +65,13 @@ func runSyncCMD(args ...string) (int, string, string) {
// Create Specifications yaml file
func createSpecFile(spec []pkg.Spec) error {
klog.Infof("STEP: Creating Spec file")
dir, err := ioutil.TempDir(".", "spec")
dir, err := os.MkdirTemp(".", "spec")
if err != nil {
klog.Errorf("ERROR: %v", err)
return err
}

file, err := ioutil.TempFile(dir, "spec.*.yaml")
file, err := os.CreateTemp(dir, "spec.*.yaml")
if err != nil {
klog.Errorf("ERROR: %v", err)
return err
Expand Down Expand Up @@ -203,15 +202,15 @@ func createBucket(bucketName string, cos string, region string, storageClass str
func createObjects() error {
klog.Infoln("STEP: Create Required Files")
var content string
dir, err := ioutil.TempDir(".", "objects")
dir, err := os.MkdirTemp(".", "objects")
if err != nil {
klog.Errorf("ERROR: %v", err)
return err
}

ObjectsFolderName = dir
for i := 0; i < NoOfObjects; i++ {
file, err := ioutil.TempFile(ObjectsFolderName, "image-sync-*.txt")
file, err := os.CreateTemp(ObjectsFolderName, "image-sync-*.txt")
if err != nil {
klog.Errorf("ERROR: %v", err)
return err
Expand Down Expand Up @@ -265,7 +264,7 @@ func uploadWorker(s3Cli *client.S3Client, bucketName string, workerId int, filep
func uploadObjects(src pkg.Source) error {
klog.Infoln("STEP: Upload Objects to source Bucket ", src.Bucket)
var filePath string
files, err := ioutil.ReadDir(ObjectsFolderName)
files, err := os.ReadDir(ObjectsFolderName)
if err != nil {
klog.Errorf("ERROR: %v", err)
return err
Expand Down Expand Up @@ -349,15 +348,23 @@ func verifyBucketObjects(tgt pkg.TargetItem, cos string, files []fs.FileInfo, re
// Verify objects copied from source bucket to dest buckets
func verifyObjectsCopied(spec []pkg.Spec) error {
klog.Infoln("STEP: Verify Objects Copied to dest buckets")
files, err := ioutil.ReadDir(ObjectsFolderName)
files, err := os.ReadDir(ObjectsFolderName)
if err != nil {
klog.Errorf("ERROR: %v", err)
return err
}
fileInfos := make([]fs.FileInfo, 0, len(files))
for _, entry := range files {
fileInfo, err := entry.Info()
if err != nil {
return err
}
fileInfos = append(fileInfos, fileInfo)
}

for _, src := range spec {
for _, tgt := range src.Target {
err = verifyBucketObjects(tgt, src.Cos, files, src.Object)
err = verifyBucketObjects(tgt, src.Cos, fileInfos, src.Object)
if err != nil {
klog.Errorf("ERROR: %v", err)
return err
Expand Down