-
Notifications
You must be signed in to change notification settings - Fork 174
ROX-13421: Enable roxctl netpol generate and add tech-preview messages #3740
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
Changes from all commits
2155276
7828a9f
a05206a
1f4eaa5
0d290a4
574b9f3
39dfa0d
83ce0d3
094bb0b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| load "../helpers.bash" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [not blocking the merge]
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope, there is no way for this - we have been exploring this some time ago. The only way to avoid copy-paste is to write a code generator and generate tests dynamically.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't it work to parameterize the specific parts with env vars and
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, unfortunately sourcing would not work. There was a thread about this somewhere in the Bats GH/Wiki. Maybe I will give it a look if they got any new feats regarding this. |
||
|
|
||
| out_dir="" | ||
| templated_fragment='"{{ printf "%s" ._thing.image }}"' | ||
|
|
||
| setup_file() { | ||
| command -v yq >/dev/null || skip "Tests in this file require yq" | ||
|
|
@@ -23,11 +24,146 @@ teardown() { | |
| rm -f "$ofile" | ||
| } | ||
|
|
||
| @test "roxctl-release generate netpol should not respect ROX_ROXCTL_NETPOL_GENERATE feature-flag at runtime" { | ||
| export ROX_ROXCTL_NETPOL_GENERATE='true' | ||
| @test "roxctl-release generate netpol should return error on empty or non-existing directory" { | ||
| run roxctl-release generate netpol "$out_dir" | ||
| assert_failure | ||
| assert_line --partial 'unknown command "generate"' | ||
| assert_line --partial "error generating network policies" | ||
| assert_line --partial "no such file or directory" | ||
|
|
||
| run roxctl-release generate netpol | ||
| assert_failure | ||
| assert_line --partial "accepts 1 arg(s), received 0" | ||
| } | ||
|
|
||
| @test "roxctl-release generate netpol generates network policies" { | ||
| assert_file_exist "${test_data}/np-guard/scenario-minimal-service/frontend.yaml" | ||
| assert_file_exist "${test_data}/np-guard/scenario-minimal-service/backend.yaml" | ||
| echo "Writing network policies to ${ofile}" >&3 | ||
| run roxctl-release generate netpol "${test_data}/np-guard/scenario-minimal-service" | ||
| assert_success | ||
|
|
||
| echo "$output" > "$ofile" | ||
| assert_file_exist "$ofile" | ||
| yaml_valid "$ofile" | ||
|
|
||
| # There must be at least 2 yaml documents in the output | ||
| # yq version 4.16.2 has problems with handling 'document_index', thus we use 'di' | ||
| run yq e 'di' "${ofile}" | ||
| assert_line '0' | ||
| assert_line '1' | ||
|
|
||
| # Ensure that both yaml docs are of kind 'NetworkPolicy' | ||
| run yq e '.kind | ({"match": ., "doc": di})' "${ofile}" | ||
| assert_line --index 0 'match: NetworkPolicy' | ||
| assert_line --index 1 'doc: 0' | ||
| assert_line --index 2 'match: NetworkPolicy' | ||
| assert_line --index 3 'doc: 1' | ||
| } | ||
|
|
||
| @test "roxctl-release generate netpol produces no output when all yamls are templated" { | ||
| mkdir -p "$out_dir" | ||
| write_yaml_to_file "$templated_fragment" "$(mktemp "$out_dir/templated-XXXXXX.yaml")" | ||
|
|
||
| echo "Analyzing a corrupted yaml file '$templatedYaml'" >&3 | ||
| run roxctl-release generate netpol "$out_dir/" | ||
| assert_failure | ||
| assert_output --partial 'YAML document is malformed' | ||
| assert_output --partial 'no relevant Kubernetes resources found' | ||
| } | ||
|
|
||
| @test "roxctl-release generate netpol produces errors when some yamls are templated" { | ||
| mkdir -p "$out_dir" | ||
| write_yaml_to_file "$templated_fragment" "$(mktemp "$out_dir/templated-XXXXXX.yaml")" | ||
|
|
||
| assert_file_exist "${test_data}/np-guard/scenario-minimal-service/frontend.yaml" | ||
| assert_file_exist "${test_data}/np-guard/scenario-minimal-service/backend.yaml" | ||
| cp "${test_data}/np-guard/scenario-minimal-service/frontend.yaml" "$out_dir/frontend.yaml" | ||
| cp "${test_data}/np-guard/scenario-minimal-service/backend.yaml" "$out_dir/backend.yaml" | ||
|
|
||
| echo "Analyzing a directory where 1/3 of yaml files are templated '$out_dir/'" >&3 | ||
| run roxctl-release generate netpol "$out_dir/" --remove --output-file=/dev/null | ||
| assert_failure | ||
| assert_output --partial 'YAML document is malformed' | ||
| refute_output --partial 'no relevant Kubernetes resources found' | ||
| } | ||
|
|
||
| @test "roxctl-release generate netpol produces warnings (or errors for --strict) when yamls are not K8s resources" { | ||
| mkdir -p "$out_dir" | ||
| assert_file_exist "${test_data}/np-guard/empty-yamls/empty.yaml" | ||
| assert_file_exist "${test_data}/np-guard/empty-yamls/empty2.yaml" | ||
| cp "${test_data}/np-guard/empty-yamls/empty.yaml" "$out_dir/empty.yaml" | ||
| cp "${test_data}/np-guard/empty-yamls/empty2.yaml" "$out_dir/empty2.yaml" | ||
|
|
||
| run roxctl-release generate netpol "$out_dir/" --remove --output-file=/dev/null | ||
| assert_success | ||
| assert_output --partial 'Yaml document is not a K8s resource' | ||
| assert_output --partial 'no relevant Kubernetes resources found' | ||
|
|
||
| run roxctl-release generate netpol "$out_dir/" --remove --output-file=/dev/null --strict | ||
| assert_failure | ||
| assert_output --partial 'Yaml document is not a K8s resource' | ||
| assert_output --partial 'no relevant Kubernetes resources found' | ||
| assert_output --partial 'ERROR:' | ||
| assert_output --partial 'there were warnings during execution' | ||
| } | ||
|
|
||
| @test "roxctl-release generate netpol stops on first error when run with --fail" { | ||
| mkdir -p "$out_dir" | ||
| write_yaml_to_file "$templated_fragment" "$(mktemp "$out_dir/templated-01-XXXXXX-file1.yaml")" | ||
| write_yaml_to_file "$templated_fragment" "$(mktemp "$out_dir/templated-02-XXXXXX-file2.yaml")" | ||
|
|
||
| run roxctl-release generate netpol "$out_dir/" --remove --output-file=/dev/null --fail | ||
| assert_failure | ||
| assert_output --partial 'YAML document is malformed' | ||
| assert_output --partial 'file1.yaml' | ||
| refute_output --partial 'file2.yaml' | ||
| } | ||
|
|
||
| write_yaml_to_file() { | ||
| image="${1}" | ||
| templatedYaml="${2:-/dev/null}" | ||
| cat >"$templatedYaml" <<-EOF | ||
| --- | ||
| apiVersion: apps/v1 | ||
| kind: Deployment | ||
| metadata: | ||
| name: frontend | ||
| spec: | ||
| selector: | ||
| matchLabels: | ||
| app: frontend | ||
| template: | ||
| metadata: | ||
| labels: | ||
| app: frontend | ||
| spec: | ||
| containers: | ||
| - name: server | ||
| image: $image | ||
| ports: | ||
| - containerPort: 8080 | ||
| env: | ||
| - name: PORT | ||
| value: "8080" | ||
| resources: | ||
| requests: | ||
| cpu: 100m | ||
| memory: 64Mi | ||
| limits: | ||
| cpu: 200m | ||
| memory: 128Mi | ||
| --- | ||
| apiVersion: v1 | ||
| kind: Service | ||
| metadata: | ||
| name: frontend | ||
| spec: | ||
| type: ClusterIP | ||
| selector: | ||
| app: frontend | ||
| ports: | ||
| - name: http | ||
| port: 80 | ||
| targetPort: 8080 | ||
| EOF | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.