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 #4559

Merged
merged 12 commits into from
May 12, 2024
6 changes: 3 additions & 3 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
uses: github/codeql-action/init@v3
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
Expand All @@ -54,7 +54,7 @@ jobs:
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v1
uses: github/codeql-action/autobuild@v3

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl
Expand All @@ -68,4 +68,4 @@ jobs:
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
uses: github/codeql-action/analyze@v3
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 @@ -519,7 +519,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([]byte(bodyBytes)))
c.Set("role", tt.mockRole)
c.Set("uid", tt.mockUID)
c.Set("username", tt.mockUsername)
Expand Down Expand Up @@ -595,7 +595,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
41 changes: 22 additions & 19 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,14 +127,19 @@ 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(file *os.File) {
err := file.Close()
if err != nil {
log.Errorf("failed to close the file %v", err)
}
}(file)

list, err := file.Readdirnames(0) // 0 to read all files and folders
if err != nil {
return nil, fmt.Errorf("failed to read the file %v", err)
}

var nodeselector string
var nodeSelector string
if infra.NodeSelector != nil {
selector := strings.Split(*infra.NodeSelector, ",")
selectorList := make(map[string]string)
Expand All @@ -145,18 +148,17 @@ func ManifestParser(infra dbChaosInfra.ChaosInfra, rootPath string, config *Subs
selectorList[kv[0]] = kv[1]
}

nodeSelector := struct {
NodeSelector map[string]string `yaml:"nodeSelector" json:"nodeSelector"`
}{
NodeSelector: selectorList,
}

byt, err := yaml.Marshal(nodeSelector)
byt, err := yaml.Marshal(
struct {
NodeSelector map[string]string `yaml:"nodeSelector" json:"nodeSelector"`
}{
NodeSelector: selectorList,
})
if err != nil {
return nil, fmt.Errorf("failed to marshal the node selector %v", err)
}

nodeselector = string(utils.AddRootIndent(byt, 6))
nodeSelector = string(utils.AddRootIndent(byt, 6))
}

var tolerations string
Expand All @@ -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 Expand Up @@ -209,7 +211,7 @@ func ManifestParser(infra dbChaosInfra.ChaosInfra, rootPath string, config *Subs
}

if infra.NodeSelector != nil {
newContent = strings.Replace(newContent, "#{NODE_SELECTOR}", nodeselector, -1)
newContent = strings.Replace(newContent, "#{NODE_SELECTOR}", nodeSelector, -1)
}
generatedYAML = append(generatedYAML, newContent)
}
Expand Down Expand Up @@ -250,7 +252,8 @@ func SendExperimentToSubscriber(projectID string, workflow *model.ChaosExperimen
var workflowObj unstructured.Unstructured
err := yaml.Unmarshal([]byte(workflow.ExperimentManifest), &workflowObj)
if err != nil {
fmt.Errorf("error while parsing experiment manifest %v", err)
log.Errorf("error while parsing experiment manifest %v", err)
return
}

SendRequestToSubscriber(SubscriberRequests{
Expand Down
52 changes: 32 additions & 20 deletions chaoscenter/graphql/server/pkg/chaoshub/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
Expand All @@ -19,9 +18,7 @@ import (
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 @@ -39,9 +36,9 @@ func GetChartsPath(chartsInput model.CloningInput, projectID string, isDefault b
}

// 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(chartsPath)
Fixed Show fixed Hide fixed
if err != nil {
log.Error("file reading error", err)
return nil, err
Expand All @@ -50,7 +47,7 @@ func GetChartsData(ChartsPath string) ([]*model.Chart, error) {
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 @@ -79,14 +76,16 @@ func GetExperimentData(experimentFilePath string) (*model.Chart, error) {
return nil, err
}
var chartData *model.Chart
json.Unmarshal(e, &chartData)
if err = json.Unmarshal(e, &chartData); err != nil {
return nil, err
}
return chartData, nil
}

// ReadExperimentFile is used for reading experiment file from given path
func ReadExperimentFile(path string) (ChaosChart, error) {
var experiment ChaosChart
experimentFile, err := ioutil.ReadFile(path)
experimentFile, err := os.ReadFile(path)
Fixed Show fixed Hide fixed
if err != nil {
return experiment, fmt.Errorf("file path of the, err: %+v", err)
}
Expand All @@ -99,7 +98,7 @@ func ReadExperimentFile(path string) (ChaosChart, error) {
// 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 @@ -112,7 +111,7 @@ func ReadExperimentYAMLFile(path string) (string, error) {
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 @@ -162,8 +161,8 @@ func DownloadRemoteHub(hubDetails model.CreateRemoteChaosHub, projectID string)
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(hubPath)
Fixed Show fixed Hide fixed
if err != nil {
log.Error(err)
return err
Expand Down Expand Up @@ -191,14 +190,14 @@ func DownloadRemoteHub(hubDetails model.CreateRemoteChaosHub, projectID string)
contentLength := download.Header.Get("content-length")
length, err := strconv.Atoi(contentLength)
if length > maxSize {
_ = os.Remove(hubpath)
_ = os.Remove(hubPath)
Fixed Show fixed Hide fixed
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(hubPath)
Fixed Show fixed Hide fixed
return fmt.Errorf("err: Invalid file type %s", contentType)
}

Expand All @@ -210,30 +209,38 @@ func DownloadRemoteHub(hubDetails model.CreateRemoteChaosHub, projectID string)
}

//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(hubPath)
Fixed Show fixed Hide fixed
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(zipReader *zip.ReadCloser) {
err := zipReader.Close()
if err != nil {
log.Error(err)
}
}(zipReader)
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 @@ -365,7 +372,12 @@ func DefaultChaosHubIconHandler() gin.HandlerFunc {
}
}

defer img.Close()
defer func(img *os.File) {
err := img.Close()
if err != nil {
log.WithError(err).Error("error while closing the file")
}
}(img)

c.Writer.Header().Set("Content-Type", "image/png")
c.Writer.WriteHeader(responseStatusCode)
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
9 changes: 4 additions & 5 deletions chaoscenter/graphql/server/pkg/chaoshub/ops/gitops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ package chaoshubops_test

import (
"fmt"
"io/ioutil"
"io"
"os"
"testing"
"time"

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

"github.com/gin-gonic/gin"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/google/uuid"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model"
chaosHubOps "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaoshub/ops"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
Expand All @@ -25,7 +24,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