forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Resolve issues with offline linter + add tests
argoproj#10059 was tested in very specific cases it seems because a couple things are wrong: - Linting directories does not work anymore - Resolving references in a namespace which doesn't show up in the given args, panics In this PR, I fix those issues but I also add functional tests that checks that all of these cases work from the cmd level I did not add to the `argo/lint/lint_test.go` tests and instead put the tests earlier down the line because the current tests do not cover the creation and configuration of the offline client Signed-off-by: Julien Duchesne <julien.duchesne@grafana.com>
- Loading branch information
1 parent
5c3c3b3
commit 89e441e
Showing
8 changed files
with
254 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
package commands | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_OfflineLint(t *testing.T) { | ||
dir := t.TempDir() | ||
|
||
subdir := filepath.Join(dir, "subdir") | ||
require.NoError(t, os.Mkdir(subdir, 0755)) | ||
wftmplPath := filepath.Join(subdir, "wftmpl.yaml") | ||
err := os.WriteFile(wftmplPath, []byte(` | ||
apiVersion: argoproj.io/v1alpha1 | ||
kind: WorkflowTemplate | ||
metadata: | ||
name: hello-world-template-local-arg | ||
namespace: test | ||
spec: | ||
templates: | ||
- name: hello-world | ||
inputs: | ||
parameters: | ||
- name: msg | ||
value: hello world | ||
container: | ||
image: docker/whalesay | ||
command: | ||
- cowsay | ||
args: | ||
- '{{inputs.parameters.msg}}' | ||
`), 0644) | ||
require.NoError(t, err) | ||
|
||
clusterWftmplPath := filepath.Join(subdir, "cluster-workflow-template.yaml") | ||
err = os.WriteFile(clusterWftmplPath, []byte(` | ||
apiVersion: argoproj.io/v1alpha1 | ||
kind: ClusterWorkflowTemplate | ||
metadata: | ||
name: hello-world-cluster | ||
spec: | ||
templates: | ||
- name: hello-world | ||
inputs: | ||
parameters: | ||
- name: msg | ||
value: hello world | ||
container: | ||
image: docker/whalesay | ||
command: | ||
- cowsay | ||
args: | ||
- '{{inputs.parameters.msg}}' | ||
`), 0644) | ||
require.NoError(t, err) | ||
|
||
workflowPath := filepath.Join(subdir, "workflow.yaml") | ||
err = os.WriteFile(workflowPath, []byte(` | ||
apiVersion: argoproj.io/v1alpha1 | ||
kind: Workflow | ||
metadata: | ||
generateName: hello-world-local-arg- | ||
namespace: test | ||
spec: | ||
entrypoint: whalesay | ||
templates: | ||
- name: whalesay | ||
steps: | ||
- - name: hello-world | ||
templateRef: | ||
name: hello-world-template-local-arg | ||
template: hello-world | ||
- name: hello-world-cluster | ||
templateRef: | ||
name: hello-world-cluster | ||
template: hello-world | ||
clusterScope: true | ||
`), 0644) | ||
require.NoError(t, err) | ||
|
||
t.Run("linting a workflow missing references", func(t *testing.T) { | ||
defer func() { logrus.StandardLogger().ExitFunc = nil }() | ||
var fatal bool | ||
logrus.StandardLogger().ExitFunc = func(int) { fatal = true } | ||
|
||
runLint(context.Background(), []string{workflowPath}, true, nil, "pretty", true) | ||
|
||
assert.True(t, fatal, "should have exited") | ||
}) | ||
|
||
t.Run("linting a workflow missing a workflow template ref", func(t *testing.T) { | ||
defer func() { logrus.StandardLogger().ExitFunc = nil }() | ||
var fatal bool | ||
logrus.StandardLogger().ExitFunc = func(int) { fatal = true } | ||
|
||
runLint(context.Background(), []string{workflowPath, clusterWftmplPath}, true, nil, "pretty", true) | ||
|
||
assert.True(t, fatal, "should have exited") | ||
}) | ||
|
||
t.Run("linting a workflow missing a cluster workflow template ref", func(t *testing.T) { | ||
defer func() { logrus.StandardLogger().ExitFunc = nil }() | ||
var fatal bool | ||
logrus.StandardLogger().ExitFunc = func(int) { fatal = true } | ||
|
||
runLint(context.Background(), []string{workflowPath, wftmplPath}, true, nil, "pretty", true) | ||
|
||
assert.True(t, fatal, "should have exited") | ||
}) | ||
|
||
t.Run("linting a workflow template on its own", func(t *testing.T) { | ||
defer func() { logrus.StandardLogger().ExitFunc = nil }() | ||
var fatal bool | ||
logrus.StandardLogger().ExitFunc = func(int) { fatal = true } | ||
|
||
runLint(context.Background(), []string{wftmplPath}, true, nil, "pretty", true) | ||
|
||
assert.False(t, fatal, "should not have exited") | ||
}) | ||
|
||
t.Run("linting a cluster workflow template on its own", func(t *testing.T) { | ||
defer func() { logrus.StandardLogger().ExitFunc = nil }() | ||
var fatal bool | ||
logrus.StandardLogger().ExitFunc = func(int) { fatal = true } | ||
|
||
runLint(context.Background(), []string{clusterWftmplPath}, true, nil, "pretty", true) | ||
|
||
assert.False(t, fatal, "should not have exited") | ||
}) | ||
|
||
t.Run("linting a workflow and templates", func(t *testing.T) { | ||
defer func() { logrus.StandardLogger().ExitFunc = nil }() | ||
var fatal bool | ||
logrus.StandardLogger().ExitFunc = func(int) { fatal = true } | ||
|
||
runLint(context.Background(), []string{workflowPath, wftmplPath, clusterWftmplPath}, true, nil, "pretty", true) | ||
|
||
assert.False(t, fatal, "should not have exited") | ||
}) | ||
|
||
t.Run("linting a directory", func(t *testing.T) { | ||
defer func() { logrus.StandardLogger().ExitFunc = nil }() | ||
var fatal bool | ||
logrus.StandardLogger().ExitFunc = func(int) { fatal = true } | ||
|
||
runLint(context.Background(), []string{dir}, true, nil, "pretty", true) | ||
|
||
assert.False(t, fatal, "should not have exited") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.