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

Analyzer rule #63

Closed
wants to merge 3 commits into from
Closed
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,4 @@ Not sure where to get started? You can :

- Visit our [Community Page](https://tracetest.io/community).
- See our contributing guidelines [here](./CONTRIBUTING.md).
//
1 change: 1 addition & 0 deletions api/linters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ components:
type: string
enabled:
type: boolean
default: false
minimumScore:
type: integer
plugins:
Expand Down
70 changes: 35 additions & 35 deletions cli/cmd/resource_apply_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,41 +87,41 @@ func (p applyParameters) Validate(cmd *cobra.Command, args []string) []error {
}

func (p applyParameters) validateGitParameters() []error {
gitErrors := make([]error, 0)

// Add specific validation checks for Git parameters
if p.GitRepo == "" {
gitErrors = append(gitErrors, paramError{
Parameter: "git-repo",
Message: "Git repository is required",
})
}
gitErrors := make([]error, 0)

// Add specific validation checks for Git parameters
if p.GitRepo == "" {
gitErrors = append(gitErrors, paramError{
Parameter: "git-repo",
Message: "Git repository is required",
})
}
if p.GitUsername == "" {
gitErrors = append(gitErrors, paramError{
Parameter: "gitusername",
Message: "Git username is required",
})
}

if p.GitToken == "" {
gitErrors = append(gitErrors, paramError{
Parameter: "gittoken",
Message: "Git token is required",
})
}

if p.Branch == "" {
gitErrors = append(gitErrors, paramError{
Parameter: "branch",
Message: "Branch name is required",
})
}

if p.GitFile == "" {
gitErrors = append(gitErrors, paramError{
Parameter: "gitfile",
Message: "Git file name is required",
})
}
gitErrors = append(gitErrors, paramError{
Parameter: "gitusername",
Message: "Git username is required",
})
}

if p.GitToken == "" {
gitErrors = append(gitErrors, paramError{
Parameter: "gittoken",
Message: "Git token is required",
})
}

if p.Branch == "" {
gitErrors = append(gitErrors, paramError{
Parameter: "branch",
Message: "Branch name is required",
})
}

if p.GitFile == "" {
gitErrors = append(gitErrors, paramError{
Parameter: "gitfile",
Message: "Git file name is required",
})
}
return gitErrors
}
142 changes: 71 additions & 71 deletions cli/cmd/resource_run_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package cmd

import (
"context"
"strings"
"fmt"
logger "github.com/sirupsen/logrus"
"strings"

"github.com/kubeshop/tracetest/cli/openapi"
"github.com/kubeshop/tracetest/cli/runner"
Expand Down Expand Up @@ -53,9 +53,9 @@ func init() {
JUnitOuptutFile: runParams.JUnitOuptutFile,
RequiredGates: runParams.RequriedGates,
GitRepo: runParams.GitRepo,
GitUsername: runParams.GitUsername,
GitToken: runParams.GitToken,
Branch: runParams.Branch,
GitUsername: runParams.GitUsername,
GitToken: runParams.GitToken,
Branch: runParams.Branch,
GitFile: runParams.GitFile,
}

Expand Down Expand Up @@ -113,9 +113,9 @@ type runParameters struct {
JUnitOuptutFile string
RequriedGates []string
GitRepo string
GitUsername string
GitToken string
Branch string
GitUsername string
GitToken string
Branch string
GitFile string
}

Expand All @@ -127,88 +127,88 @@ func (p runParameters) Validate(cmd *cobra.Command, args []string) []error {
if p.GitRepo != "" || p.GitUsername != "" || p.GitToken != "" || p.Branch != "" || p.GitFile != "" {

// Log Git parameters for debugging
logger.Infof("Git Repo: %s, Git Username: %s, Branch: %s, Git File: %s", p.GitRepo,p.GitUsername,p.Branch,p.GitFile)
logger.Infof("Git Repo: %s, Git Username: %s, Branch: %s, Git File: %s", p.GitRepo, p.GitUsername, p.Branch, p.GitFile)

// Call the validateGitParameters function
gitErrors := p.validateGitParameters()
errs = append(errs, gitErrors...)

} else {
if p.DefinitionFile == "" && p.ID == "" {
// Check for either DefinitionFile or ID
errs = append(errs, paramError{
Parameter: "resource",
Message: "you must specify a definition file or resource ID",
})
// Check for either DefinitionFile or ID
errs = append(errs, paramError{
Parameter: "resource",
Message: "you must specify a definition file or resource ID",
})
}
if p.DefinitionFile != "" && p.ID != "" {
errs = append(errs, paramError{
Parameter: "resource",
Message: "you cannot specify both a definition file and resource ID",
})
}
errs = append(errs, paramError{
Parameter: "resource",
Message: "you cannot specify both a definition file and resource ID",
})
}
}

// Check for incompatibility between JUnit and SkipResultWait options
if p.JUnitOuptutFile != "" && p.SkipResultWait {
errs = append(errs, paramError{
Parameter: "junit",
Message: "--junit option is incompatible with --skip-result-wait option",
})
}

// Validate required gates
for _, rg := range p.RequriedGates {
_, err := openapi.NewSupportedGatesFromValue(rg)
if err != nil {
errs = append(errs, paramError{
Parameter: "required-gates",
Message: fmt.Sprintf("invalid option '%s'. "+validRequiredGatesMsg(), rg),
})
}
}
if p.JUnitOuptutFile != "" && p.SkipResultWait {
errs = append(errs, paramError{
Parameter: "junit",
Message: "--junit option is incompatible with --skip-result-wait option",
})
}

// Validate required gates
for _, rg := range p.RequriedGates {
_, err := openapi.NewSupportedGatesFromValue(rg)
if err != nil {
errs = append(errs, paramError{
Parameter: "required-gates",
Message: fmt.Sprintf("invalid option '%s'. "+validRequiredGatesMsg(), rg),
})
}
}
logger.Debug("Exiting resource_run_validate()")
return errs
return errs
}

func (p runParameters) validateGitParameters() []error {
gitErrors := make([]error, 0)
gitErrors := make([]error, 0)
logger.Debug("Entering resource_run_git_validate")

// Add specific validation checks for Git parameters
if p.GitRepo == "" {
gitErrors = append(gitErrors, paramError{
Parameter: "git-repo",
Message: "Git repository is required",
})
}
// Add specific validation checks for Git parameters
if p.GitRepo == "" {
gitErrors = append(gitErrors, paramError{
Parameter: "git-repo",
Message: "Git repository is required",
})
}
if p.GitUsername == "" {
gitErrors = append(gitErrors, paramError{
Parameter: "gitusername",
Message: "Git username is required",
})
}

if p.GitToken == "" {
gitErrors = append(gitErrors, paramError{
Parameter: "gittoken",
Message: "Git token is required",
})
}

if p.Branch == "" {
gitErrors = append(gitErrors, paramError{
Parameter: "branch",
Message: "Branch name is required",
})
}

if p.GitFile == "" {
gitErrors = append(gitErrors, paramError{
Parameter: "gitfile",
Message: "Git file name is required",
})
}
gitErrors = append(gitErrors, paramError{
Parameter: "gitusername",
Message: "Git username is required",
})
}

if p.GitToken == "" {
gitErrors = append(gitErrors, paramError{
Parameter: "gittoken",
Message: "Git token is required",
})
}

if p.Branch == "" {
gitErrors = append(gitErrors, paramError{
Parameter: "branch",
Message: "Branch name is required",
})
}

if p.GitFile == "" {
gitErrors = append(gitErrors, paramError{
Parameter: "gitfile",
Message: "Git file name is required",
})
}
logger.Debug("Exiting resource_run_git_validate")
return gitErrors
}
}
44 changes: 22 additions & 22 deletions cli/cmd/resource_run_cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package cmd

import (
"context"
"os"
"testing"
"errors"
"log"
"fmt"
"log"
"os"
"testing"

"github.com/stretchr/testify/assert"
)
Expand All @@ -21,10 +21,10 @@ func runResourceCommand(ctx context.Context, params *runParameters) (int, error)
}

if params.GitRepo == "https://github.com/InvalidRepo/test" {
return 1, errors.New("simulated error") // Simulating an error scenario
}
return 1, errors.New("simulated error") // Simulating an error scenario
}

return 0, nil
return 0, nil
}

// Mock the exitCodeSuccess variable
Expand Down Expand Up @@ -60,21 +60,21 @@ func TestRunCmd_Positive(t *testing.T) {
}

func TestRunCmd_Negative(t *testing.T) {
// Set up test parameters for a scenario where the command should fail
runParams := &runParameters{
GitRepo: "https://github.com/InvalidRepo/test", // Using an intentionally invalid repository
GitUsername: "InvalidUser",
GitToken: "invalidToken",
Branch: "main",
GitFile: "ml-server.yaml",
}

// Execute the Resource Run command
exitCode, err := runResourceCommand(context.Background(), runParams)

// Assertions
assert.Error(t, err, "Resource Run command should encounter an error")
assert.NotEqual(t, exitCodeSuccess, exitCode, "Resource Run command should not exit with success code")
// Set up test parameters for a scenario where the command should fail
runParams := &runParameters{
GitRepo: "https://github.com/InvalidRepo/test", // Using an intentionally invalid repository
GitUsername: "InvalidUser",
GitToken: "invalidToken",
Branch: "main",
GitFile: "ml-server.yaml",
}

// Execute the Resource Run command
exitCode, err := runResourceCommand(context.Background(), runParams)

// Assertions
assert.Error(t, err, "Resource Run command should encounter an error")
assert.NotEqual(t, exitCodeSuccess, exitCode, "Resource Run command should not exit with success code")

}

Expand All @@ -88,4 +88,4 @@ func TestMain(m *testing.M) {

code := m.Run()
os.Exit(code)
}
}
22 changes: 11 additions & 11 deletions cli/gitutil/git_clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ func CloneGitFile(ctx context.Context, gitParams RunParameters) ([]byte, error)
// Set up Basic Authentication using custom BasicAuth struct
auth := &http.BasicAuth{
Username: gitParams.GitUsername,
Password: gitParams.GitToken,
Password: gitParams.GitToken,
}

repo, err := git.PlainCloneContext(ctx, "/tmp/new_01", false, &git.CloneOptions{
URL: gitParams.GitRepo,
URL: gitParams.GitRepo,
Auth: auth,
})
if err != nil {
Expand Down Expand Up @@ -62,14 +62,14 @@ func CloneGitFile(ctx context.Context, gitParams RunParameters) ([]byte, error)
// CleanupClonedRepo deletes the cloned repository file.
func CleanupClonedRepo(filePath string) error {
// Get the directory containing the file
dirPath := filepath.Dir(filePath)
dirPath := filepath.Dir(filePath)

// Remove the directory containing the file
if err := os.RemoveAll(dirPath); err != nil {
logger.Info("Failed to delete directory:", err)
return err
}
// Remove the directory containing the file
if err := os.RemoveAll(dirPath); err != nil {
logger.Info("Failed to delete directory:", err)
return err
}

logger.Info("Cloned repository file and its directory deleted successfully.")
return nil
}
logger.Info("Cloned repository file and its directory deleted successfully.")
return nil
}
Loading
Loading