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

Replace deprecated package #4475

Merged
Merged
Show file tree
Hide file tree
Changes from 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"io"
"log"
"net/http"
"net/http/httptest"
Expand All @@ -26,7 +26,7 @@ import (
// TestMain is the entry point for testing
func TestMain(m *testing.M) {
gin.SetMode(gin.TestMode)
log.SetOutput(ioutil.Discard)
log.SetOutput(io.Discard)
os.Exit(m.Run())
}

Expand Down Expand Up @@ -516,7 +516,7 @@ func TestResetPassword(t *testing.T) {
c := GetTestGinContext(w)
c.Request.Method = http.MethodPost
bodyBytes, _ := json.Marshal(tt.inputBody)
c.Request.Body = ioutil.NopCloser(bytes.NewReader([]byte(bodyBytes)))
c.Request.Body = io.NopCloser(bytes.NewReader(bodyBytes))
c.Set("role", tt.mockRole)
c.Set("uid", tt.mockUID)
c.Set("username", tt.mockUsername)
Expand Down Expand Up @@ -592,7 +592,7 @@ func TestUpdateUserState(t *testing.T) {
c := GetTestGinContext(w)
c.Request.Method = http.MethodPost
bodyBytes, _ := json.Marshal(tc.inputBody)
c.Request.Body = ioutil.NopCloser(bytes.NewReader([]byte(bodyBytes)))
c.Request.Body = io.NopCloser(bytes.NewReader([]byte(bodyBytes)))
c.Set("role", tc.mockRole)
c.Set("uid", tc.mockUID)
c.Set("username", tc.mockUsername)
Expand Down
18 changes: 10 additions & 8 deletions chaoscenter/graphql/server/pkg/chaos_infrastructure/infra_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@ package chaos_infrastructure

import (
"fmt"
"os"
"strings"

"github.com/ghodss/yaml"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model"
store "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/data-store"
dbChaosInfra "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/chaos_infrastructure"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/k8s"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/utils"
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

"io/ioutil"
"os"
"strings"
)

type SubscriberConfigurations struct {
Expand Down Expand Up @@ -65,7 +63,7 @@ func GetK8sInfraYaml(infra dbChaosInfra.ChaosInfra) ([]byte, error) {
} else if infra.InfraScope == NamespaceScope {
respData, err = ManifestParser(infra, "manifests/namespace", &config)
} else {
logrus.Error("INFRA_SCOPE env is empty!")
log.Error("INFRA_SCOPE env is empty!")
}
if err != nil {
return nil, err
Expand Down Expand Up @@ -129,7 +127,11 @@ func ManifestParser(infra dbChaosInfra.ChaosInfra, rootPath string, config *Subs
return nil, fmt.Errorf("failed to open the file %v", err)
}

defer file.Close()
defer func() {
if err := file.Close(); err != nil {
log.Warnf("failed to close file: %v", err)
}
}()

list, err := file.Readdirnames(0) // 0 to read all files and folders
if err != nil {
Expand Down Expand Up @@ -174,7 +176,7 @@ func ManifestParser(infra dbChaosInfra.ChaosInfra, rootPath string, config *Subs
}

for _, fileName := range list {
fileContent, err := ioutil.ReadFile(rootPath + "/" + fileName)
fileContent, err := os.ReadFile(rootPath + "/" + fileName)
if err != nil {
return nil, fmt.Errorf("failed to read the file %v", err)
}
Expand Down
74 changes: 50 additions & 24 deletions chaoscenter/graphql/server/pkg/chaoshub/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"
"strconv"
"strings"
Expand All @@ -17,9 +17,7 @@
chaoshubops "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaoshub/ops"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/chaos_hub"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/utils"

log "github.com/sirupsen/logrus"

"gopkg.in/yaml.v2"
)

Expand All @@ -37,9 +35,9 @@
}

// GetChartsData is used to get details of charts like experiments.
func GetChartsData(ChartsPath string) ([]*model.Chart, error) {
func GetChartsData(chartsPath string) ([]*model.Chart, error) {
var allChartsDetails []ChaosChart
Charts, err := ioutil.ReadDir(ChartsPath)
Charts, err := os.ReadDir(path.Clean(chartsPath))
Dismissed Show dismissed Hide dismissed
if err != nil {
log.Error("file reading error", err)
return nil, err
Expand All @@ -48,7 +46,7 @@
if chart.Name() == "icons" {
continue
}
chartDetails, _ := ReadExperimentFile(ChartsPath + chart.Name() + "/" + chart.Name() + ".chartserviceversion.yaml")
chartDetails, _ := ReadExperimentFile(chartsPath + chart.Name() + "/" + chart.Name() + ".chartserviceversion.yaml")
allChartsDetails = append(allChartsDetails, chartDetails)
}

Expand Down Expand Up @@ -77,14 +75,17 @@
return nil, err
}
var chartData *model.Chart
json.Unmarshal(e, &chartData)
err = json.Unmarshal(e, &chartData)
if err != nil {
return nil, err
}
return chartData, nil
}

// ReadExperimentFile is used for reading experiment file from given path
func ReadExperimentFile(path string) (ChaosChart, error) {
func ReadExperimentFile(givenPath string) (ChaosChart, error) {
var experiment ChaosChart
experimentFile, err := ioutil.ReadFile(path)
experimentFile, err := os.ReadFile(path.Clean(givenPath))
Dismissed Show dismissed Hide dismissed
if err != nil {
return experiment, fmt.Errorf("file path of the, err: %+v", err)
}
Expand All @@ -97,7 +98,7 @@
// ReadExperimentYAMLFile is used for reading experiment/engine file from given path
func ReadExperimentYAMLFile(path string) (string, error) {
var s string
YAMLData, err := ioutil.ReadFile(path)
YAMLData, err := os.ReadFile(path)
if err != nil {
return s, fmt.Errorf("file path of the, err: %+v", err)
}
Expand All @@ -110,7 +111,7 @@
func ListPredefinedWorkflowDetails(name string, projectID string) ([]*model.PredefinedExperimentList, error) {
experimentsPath := DefaultPath + projectID + "/" + name + "/workflows"
var predefinedWorkflows []*model.PredefinedExperimentList
files, err := ioutil.ReadDir(experimentsPath)
files, err := os.ReadDir(experimentsPath)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -160,13 +161,17 @@
return err
}
//create the destination directory where the hub will be downloaded
hubpath := dirPath + "/" + hubDetails.Name + ".zip"
destDir, err := os.Create(hubpath)
hubPath := dirPath + "/" + hubDetails.Name + ".zip"
destDir, err := os.Create(path.Clean(hubPath))
Dismissed Show dismissed Hide dismissed
if err != nil {
log.Error(err)
return err
}
defer destDir.Close()
defer func() {
if err := destDir.Close(); err != nil {
log.Warnf("failed to close dir: %v", err)
}
}()

//download the zip file from the provided url
download, err := http.Get(hubDetails.RepoURL)
Expand All @@ -175,7 +180,11 @@
return err
}

defer download.Body.Close()
defer func() {
if err := download.Body.Close(); err != nil {
log.Warnf("failed to close body: %v", err)
}
}()

if download.StatusCode != http.StatusOK {
return fmt.Errorf("err: " + download.Status)
Expand All @@ -189,14 +198,14 @@
contentLength := download.Header.Get("content-length")
length, err := strconv.Atoi(contentLength)
if length > maxSize {
_ = os.Remove(hubpath)
_ = os.Remove(path.Clean(hubPath))
Dismissed Show dismissed Hide dismissed
return fmt.Errorf("err: File size exceeded the threshold %d", length)
}

//validate the content-type
contentType := download.Header.Get("content-type")
if contentType != "application/zip" {
_ = os.Remove(hubpath)
_ = os.Remove(path.Clean(hubPath))
Dismissed Show dismissed Hide dismissed
return fmt.Errorf("err: Invalid file type %s", contentType)
}

Expand All @@ -208,30 +217,38 @@
}

//unzip the ChaosHub to the default hub directory
err = UnzipRemoteHub(hubpath, hubDetails, projectID)
err = UnzipRemoteHub(hubPath, projectID)
if err != nil {
return err
}

//remove the redundant zip file
err = os.Remove(hubpath)
err = os.Remove(path.Clean(hubPath))
Dismissed Show dismissed Hide dismissed
if err != nil {
return err
}
return nil
}

// UnzipRemoteHub is used to unzip the zip file
func UnzipRemoteHub(zipPath string, hubDetails model.CreateRemoteChaosHub, projectID string) error {
func UnzipRemoteHub(zipPath string, projectID string) error {
extractPath := DefaultPath + projectID
zipReader, err := zip.OpenReader(zipPath)
if err != nil {
log.Error(err)
return err
}
defer zipReader.Close()
defer func() {
if err := zipReader.Close(); err != nil {
log.Warnf("failed to close reader: %v", err)
}
}()

for _, file := range zipReader.File {
CopyZipItems(file, extractPath, file.Name)
err := CopyZipItems(file, extractPath, file.Name)
if err != nil {
return err
}
}
return nil
}
Expand Down Expand Up @@ -260,9 +277,18 @@
if err != nil {
log.Error(err)
}
fileCopy.Close()
defer func() {
if err := fileCopy.Close(); err != nil {
log.Warnf("failed to close file: %v", err)
}
}()

}
fileReader.Close()
defer func() {
if err := fileReader.Close(); err != nil {
log.Warnf("failed to close file: %v", err)
}
}()

return nil
}
Expand Down
14 changes: 6 additions & 8 deletions chaoscenter/graphql/server/pkg/chaoshub/handler/handler_test.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
package handler_test

import (
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaoshub/handler"
chaosHubOps "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaoshub/ops"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/utils"

"io/ioutil"
"io"
"os"
"testing"

"github.com/gin-gonic/gin"
"github.com/google/uuid"

"github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaoshub/handler"
chaosHubOps "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaoshub/ops"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/utils"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)

// TestMain is the entry point for testing
func TestMain(m *testing.M) {
gin.SetMode(gin.TestMode)
log.SetOutput(ioutil.Discard)
log.SetOutput(io.Discard)
os.Exit(m.Run())
}

Expand Down
4 changes: 2 additions & 2 deletions chaoscenter/graphql/server/pkg/chaoshub/ops/gitops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package chaoshubops_test

import (
"fmt"
"io/ioutil"
"io"
"os"
"testing"
"time"
Expand All @@ -25,7 +25,7 @@ var (
// TestMain is the entry point for testing
func TestMain(m *testing.M) {
gin.SetMode(gin.TestMode)
log.SetOutput(ioutil.Discard)
log.SetOutput(io.Discard)
os.Exit(m.Run())
}

Expand Down
Loading
Loading