Skip to content

Commit ae087ca

Browse files
feat: support flags set and set-string for test cmd (#2519)
* feat: support flags set and set-string for test cmd Signed-off-by: ShutingZhao <[email protected]> * fix: pass env flags in Signed-off-by: ShutingZhao <[email protected]> * fix: codegen files Signed-off-by: ShutingZhao <[email protected]> * fix: pass env flags in Signed-off-by: ShutingZhao <[email protected]> --------- Signed-off-by: ShutingZhao <[email protected]> Signed-off-by: Charles-Edouard Brétéché <[email protected]> Co-authored-by: Charles-Edouard Brétéché <[email protected]>
1 parent 731a9bd commit ae087ca

File tree

9 files changed

+115
-7
lines changed

9 files changed

+115
-7
lines changed

Makefile

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,9 @@ build-ko: $(KO)
272272
# TEST #
273273
########
274274

275+
SET_FLAGS ?= --set env=poc --set clusterDirectory=my-cluster
276+
SET_STRING_FLAGS ?= --set-string image.tag=01
277+
275278
.PHONY: tests
276279
tests: ## Run tests
277280
tests: $(CLI_BIN)
@@ -283,13 +286,12 @@ tests: $(CLI_BIN)
283286
e2e-tests: ## Run e2e tests
284287
e2e-tests: $(CLI_BIN)
285288
@echo Running e2e tests... >&2
286-
@./$(CLI_BIN) test ./testdata/e2e --remarshal --config ./testdata/e2e/config.yaml --values ./testdata/e2e/values.yaml
289+
@./$(CLI_BIN) test ./testdata/e2e --remarshal --config ./testdata/e2e/config.yaml --values ./testdata/e2e/values.yaml $(SET_FLAGS) $(SET_STRING_FLAGS)
287290

288-
.PHONY: e2e-tests-no-cluster
289291
e2e-tests-no-cluster: ## Run e2e tests with --no-cluster
290292
e2e-tests-no-cluster: $(CLI_BIN)
291293
@echo Running e2e tests with --no-cluster... >&2
292-
@./$(CLI_BIN) test testdata/e2e/examples/script-env --no-cluster --remarshal --config ./testdata/e2e/config.yaml --values ./testdata/e2e/values.yaml
294+
@./$(CLI_BIN) test testdata/e2e/examples/script-env --no-cluster --remarshal --config ./testdata/e2e/config.yaml --values ./testdata/e2e/values.yaml $(SET_FLAGS) $(SET_STRING_FLAGS)
293295
@./$(CLI_BIN) test testdata/e2e/examples/dynamic-clusters --no-cluster --remarshal --config ./testdata/e2e/config.yaml --values ./testdata/e2e/values.yaml
294296

295297
.PHONY: e2e-tests-ko
@@ -304,7 +306,7 @@ e2e-tests-ko: build-ko
304306
--user $(id -u):$(id -g) \
305307
--name chainsaw \
306308
--rm \
307-
ko.local/github.com/kyverno/chainsaw:$(KO_TAGS) test /chainsaw --remarshal --config /chainsaw/config.yaml --values /chainsaw/values.yaml --selector !no-ko-test
309+
ko.local/github.com/kyverno/chainsaw:$(KO_TAGS) test /chainsaw --remarshal --config /chainsaw/config.yaml --values /chainsaw/values.yaml --selector !no-ko-test $(SET_FLAGS) $(SET_STRING_FLAGS)
308310

309311
########
310312
# KIND #

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ require (
2424
github.com/xeipuuv/gojsonschema v1.2.0
2525
go.uber.org/multierr v1.11.0
2626
gopkg.in/yaml.v3 v3.0.1
27+
helm.sh/helm/v4 v4.0.0-beta.1
2728
k8s.io/api v0.34.1
2829
k8s.io/apimachinery v0.34.1
2930
k8s.io/apiserver v0.34.1

go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,8 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq
282282
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
283283
github.com/onsi/ginkgo/v2 v2.22.0 h1:Yed107/8DjTr0lKCNt7Dn8yQ6ybuDRQoMGrNFKzMfHg=
284284
github.com/onsi/ginkgo/v2 v2.22.0/go.mod h1:7Du3c42kxCUegi0IImZ1wUQzMBVecgIHjR1C+NkhLQo=
285-
github.com/onsi/gomega v1.36.1 h1:bJDPBO7ibjxcbHMgSCoo4Yj18UWbKDlLwX1x9sybDcw=
286-
github.com/onsi/gomega v1.36.1/go.mod h1:PvZbdDc8J6XJEpDK4HCuRBm8a6Fzp9/DmhC9C7yFlog=
285+
github.com/onsi/gomega v1.37.0 h1:CdEG8g0S133B4OswTDC/5XPSzE1OeP29QOioj2PID2Y=
286+
github.com/onsi/gomega v1.37.0/go.mod h1:8D9+Txp43QWKhM24yyOBEdpkzN8FvJyAwecBgsU4KU0=
287287
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
288288
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
289289
github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 h1:GFCKgmp0tecUJ0sJuv4pzYCqS9+RGSn52M3FUwPs+uo=
@@ -491,6 +491,8 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
491491
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
492492
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
493493
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
494+
helm.sh/helm/v4 v4.0.0-beta.1 h1:QYGfJfP19sa1uT3gL+O/bchkMsbJEaRtYqcYRypaCxE=
495+
helm.sh/helm/v4 v4.0.0-beta.1/go.mod h1:4ryT7BLyPpgnILQmnRdD9XA19J7NJz+2uTSDo7SZW1w=
494496
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
495497
k8s.io/api v0.34.1 h1:jC+153630BMdlFukegoEL8E/yT7aLyQkIVuwhmwDgJM=
496498
k8s.io/api v0.34.1/go.mod h1:SB80FxFtXn5/gwzCoN6QCtPD7Vbu5w2n1S0J5gFfTYk=

pkg/commands/test/command.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/kyverno/chainsaw/pkg/version"
2424
"github.com/kyverno/pkg/ext/output/color"
2525
"github.com/spf13/cobra"
26+
"helm.sh/helm/v4/pkg/strvals"
2627
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2728
"k8s.io/apimachinery/pkg/labels"
2829
"k8s.io/client-go/rest"
@@ -63,6 +64,8 @@ type options struct {
6364
noCluster bool
6465
pauseOnFailure bool
6566
values []string
67+
set []string
68+
setString []string
6669
clusters []string
6770
remarshal bool
6871
shardIndex int
@@ -339,6 +342,17 @@ func Command() *cobra.Command {
339342
if err != nil {
340343
return err
341344
}
345+
// merge --set into values
346+
for _, s := range options.set {
347+
if err := strvals.ParseInto(s, values); err != nil {
348+
return fmt.Errorf("failed parsing --set data: %w", err)
349+
}
350+
}
351+
for _, s := range options.setString {
352+
if err := strvals.ParseIntoString(s, values); err != nil {
353+
return fmt.Errorf("failed parsing --set-string data: %w", err)
354+
}
355+
}
342356
// run tests
343357
fprintln(stdOut, "Running tests...")
344358
// setup test context
@@ -443,6 +457,8 @@ func Command() *cobra.Command {
443457
cmd.Flags().StringSliceVar(&options.selector, "selector", nil, "Selector (label query) to filter on")
444458
// external values
445459
cmd.Flags().StringSliceVar(&options.values, "values", nil, "Values passed to the tests")
460+
cmd.Flags().StringArrayVar(&options.set, "set", nil, "set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)")
461+
cmd.Flags().StringArrayVar(&options.setString, "set-string", nil, "set STRING values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)")
446462
// sharding
447463
cmd.Flags().IntVar(&options.shardIndex, "shard-index", 0, "Current shard index (if `--shard-count` > 0)")
448464
cmd.Flags().IntVar(&options.shardCount, "shard-count", 0, "Number of shards")

pkg/commands/test/command_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import (
77
"path/filepath"
88
"testing"
99

10+
chainsawvalues "github.com/kyverno/chainsaw/pkg/loaders/values"
1011
"github.com/stretchr/testify/assert"
12+
"helm.sh/helm/v4/pkg/strvals"
1113
)
1214

1315
func TestChainsawCommand(t *testing.T) {
@@ -146,6 +148,8 @@ func TestChainsawCommand(t *testing.T) {
146148
"--include-test-regex=^.*$",
147149
"--exclude-test-regex=^.*$",
148150
"--force-termination-grace-period=5s",
151+
"--set=env=prod",
152+
"--set-string=image.tag=01",
149153
},
150154
wantErr: false,
151155
out: filepath.Join(basePath, "all_flags.txt"),
@@ -182,3 +186,80 @@ func TestChainsawCommand(t *testing.T) {
182186
})
183187
}
184188
}
189+
190+
func TestCommandHasSetFlags(t *testing.T) {
191+
cmd := Command()
192+
193+
if f := cmd.Flags().Lookup("set"); f == nil {
194+
t.Fatalf("expected --set flag to be registered")
195+
}
196+
if f := cmd.Flags().Lookup("set-string"); f == nil {
197+
t.Fatalf("expected --set-string flag to be registered")
198+
}
199+
}
200+
201+
func TestValuesMergeWithSetFlags(t *testing.T) {
202+
dir := t.TempDir()
203+
valuesFile := filepath.Join(dir, "values.yaml")
204+
if err := os.WriteFile(valuesFile, []byte(
205+
"env: poc\n"+
206+
"nested:\n a: 1\n"+
207+
"arr:\n - 1\n - 2\n"), 0o600); err != nil {
208+
t.Fatalf("failed to write values file: %v", err)
209+
}
210+
211+
vals, err := chainsawvalues.Load(valuesFile)
212+
if err != nil {
213+
t.Fatalf("failed to load values: %v", err)
214+
}
215+
216+
// simulate --set
217+
if err := strvals.ParseInto("env=prod,nested.b=two,arr={3,4},newkey=true", vals); err != nil {
218+
t.Fatalf("ParseInto failed: %v", err)
219+
}
220+
// simulate --set-string
221+
if err := strvals.ParseIntoString("num=08", vals); err != nil {
222+
t.Fatalf("ParseIntoString failed: %v", err)
223+
}
224+
225+
if got, want := vals["env"], "prod"; got != want {
226+
t.Fatalf("env: got %v want %v", got, want)
227+
}
228+
nested, ok := vals["nested"].(map[string]any)
229+
if !ok {
230+
t.Fatalf("nested not a map: %T", vals["nested"])
231+
}
232+
switch v := nested["a"].(type) {
233+
case int64:
234+
if v != 1 {
235+
t.Fatalf("nested.a: got %v want %v", v, 1)
236+
}
237+
case int:
238+
if v != 1 {
239+
t.Fatalf("nested.a: got %v want %v", v, 1)
240+
}
241+
case float64:
242+
if v != 1 {
243+
t.Fatalf("nested.a: got %v want %v", v, 1)
244+
}
245+
default:
246+
t.Fatalf("nested.a: unexpected type %T with value %v", v, v)
247+
}
248+
if got, want := nested["b"], "two"; got != want {
249+
t.Fatalf("nested.b: got %v want %v", got, want)
250+
}
251+
// arrays are replaced by --set list literal
252+
arr, ok := vals["arr"].([]any)
253+
if !ok {
254+
t.Fatalf("arr not a list: %T", vals["arr"])
255+
}
256+
if len(arr) != 2 || arr[0] != int64(3) || arr[1] != int64(4) {
257+
t.Fatalf("arr: got %#v want [3,4]", arr)
258+
}
259+
if got, want := vals["newkey"], true; got != want {
260+
t.Fatalf("newkey: got %v want %v", got, want)
261+
}
262+
if got, want := vals["num"], "08"; got != want {
263+
t.Fatalf("num: got %v want %v", got, want)
264+
}
265+
}

testdata/commands/test/help.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ Flags:
5252
--report-name string The name of the report to create (default "chainsaw-report")
5353
--report-path string The path of the report to create
5454
--selector strings Selector (label query) to filter on
55+
--set stringArray set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)
56+
--set-string stringArray set STRING values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)
5557
--shard-count int Number of shards
5658
--shard-index --shard-count Current shard index (if --shard-count > 0)
5759
--skip-delete If set, do not delete the resources after running the tests

testdata/e2e/examples/values/chainsaw-test.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ spec:
99
- assert:
1010
resource:
1111
($values.foo): bar
12+
($values.env): poc
13+
($values.clusterDirectory): my-cluster
14+
($values.image.tag): "01"

testdata/kuttl/.chainsaw.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
apiVersion: chainsaw.kyverno.io/v1alpha1
33
kind: Configuration
44
metadata:
5-
creationTimestamp: null
65
name: configuration
76
spec:
87
parallel: 4

website/docs/reference/commands/chainsaw_test.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ chainsaw test [flags]... [test directories]...
5757
--report-name string The name of the report to create (default "chainsaw-report")
5858
--report-path string The path of the report to create
5959
--selector strings Selector (label query) to filter on
60+
--set stringArray set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)
61+
--set-string stringArray set STRING values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)
6062
--shard-count int Number of shards
6163
--shard-index --shard-count Current shard index (if --shard-count > 0)
6264
--skip-delete If set, do not delete the resources after running the tests

0 commit comments

Comments
 (0)