1
0
mirror of https://github.com/coreos/prometheus-operator.git synced 2026-02-05 06:45:27 +01:00

chore: adding golden files support on webconfig tests (#5776)

* feat: adding golden files support on webconfig tests

I'm adding golden file supports on webconfig package, solves #5741

---------

Signed-off-by: Nicolas Takashi <nicolas.tcs@hotmail.com>
Co-authored-by: Simon Pasquier <spasquie@redhat.com>
This commit is contained in:
Nicolas Takashi
2023-08-03 17:24:27 +01:00
committed by GitHub
parent 6a98ac4405
commit b0aa730488
13 changed files with 70 additions and 42 deletions

1
.gitattributes vendored
View File

@@ -4,3 +4,4 @@ example/prometheus-operator-crd-full/* linguist-generated=true
example/prometheus-operator-crd/* linguist-generated=true example/prometheus-operator-crd/* linguist-generated=true
example/jsonnet/prometheus-operator/* linguist-generated=true example/jsonnet/prometheus-operator/* linguist-generated=true
Documentation/api.md linguist-generated=true Documentation/api.md linguist-generated=true
**/testdata/*.golden linguist-generated=true

View File

@@ -61,6 +61,7 @@ This is a rough outline of what a contributor's workflow looks like:
- Make sure your commit messages are in the proper format (see below). - Make sure your commit messages are in the proper format (see below).
- Push your changes to a topic branch in your fork of the repository. - Push your changes to a topic branch in your fork of the repository.
- Make sure the tests pass, and add any new tests as appropriate. - Make sure the tests pass, and add any new tests as appropriate.
- If the tests are checking long strings such as YAML, JSON or any other complex content, ensure you're using [golden files](https://pkg.go.dev/gotest.tools/v3/golden).
- Submit a pull request to the original repository. - Submit a pull request to the original repository.
Many files (documentation, manifests, ...) in this repository are auto-generated. For instance, `bundle.yaml` is generated from the *Jsonnet* files in `/jsonnet/prometheus-operator`. Before submitting a pull request, make sure that you've executed `make generate` and committed the generated changes. Many files (documentation, manifests, ...) in this repository are auto-generated. For instance, `bundle.yaml` is generated from the *Jsonnet* files in `/jsonnet/prometheus-operator`. Before submitting a pull request, make sure that you've executed `make generate` and committed the generated changes.

View File

@@ -346,6 +346,10 @@ test-unit:
test-long: test-long:
go test $(TEST_RUN_ARGS) $(pkgs) -count=1 -v go test $(TEST_RUN_ARGS) $(pkgs) -count=1 -v
.PHONY: test-unit-update-golden
test-unit-update-golden:
./scripts/update-golden-files.sh
test/instrumented-sample-app/certs/cert.pem test/instrumented-sample-app/certs/key.pem: test/instrumented-sample-app/certs/cert.pem test/instrumented-sample-app/certs/key.pem:
cd test/instrumented-sample-app && make generate-certs cd test/instrumented-sample-app && make generate-certs

1
go.mod
View File

@@ -122,6 +122,7 @@ require (
google.golang.org/appengine v1.6.7 // indirect google.golang.org/appengine v1.6.7 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect
gotest.tools/v3 v3.5.0
k8s.io/kube-openapi v0.0.0-20230525220651-2546d827e515 // indirect k8s.io/kube-openapi v0.0.0-20230525220651-2546d827e515 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.3.0 // indirect sigs.k8s.io/structured-merge-diff/v4 v4.3.0 // indirect

2
go.sum
View File

@@ -902,6 +902,8 @@ gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools/v3 v3.5.0 h1:Ljk6PdHdOhAb5aDMWXjDLMMhph+BpztA4v1QdqEW2eY=
gotest.tools/v3 v3.5.0/go.mod h1:isy3WKz7GK6uNw/sbHzfKBLvlvXwUyV06n6brMxxopU=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

View File

@@ -20,6 +20,7 @@ import (
"testing" "testing"
"github.com/kylelemons/godebug/pretty" "github.com/kylelemons/godebug/pretty"
"gotest.tools/v3/golden"
v1 "k8s.io/api/core/v1" v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake" "k8s.io/client-go/kubernetes/fake"
@@ -34,12 +35,12 @@ func TestCreateOrUpdateWebConfigSecret(t *testing.T) {
tc := []struct { tc := []struct {
name string name string
webConfigFileFields monitoringv1.WebConfigFileFields webConfigFileFields monitoringv1.WebConfigFileFields
expectedData string golden string
}{ }{
{ {
name: "tls config not defined", name: "tls config not defined",
webConfigFileFields: monitoringv1.WebConfigFileFields{}, webConfigFileFields: monitoringv1.WebConfigFileFields{},
expectedData: "", golden: "tls_config_not_defined.golden",
}, },
{ {
name: "minimal TLS config with certificate from secret", name: "minimal TLS config with certificate from secret",
@@ -61,10 +62,7 @@ func TestCreateOrUpdateWebConfigSecret(t *testing.T) {
}, },
}, },
}, },
expectedData: `tls_server_config: golden: "minimal_TLS_config_with_certificate_from_secret.golden",
cert_file: /web_certs_path_prefix/secret/test-secret-cert/tls.crt
key_file: /web_certs_path_prefix/secret/test-secret-key/tls.key
`,
}, },
{ {
name: "minimal TLS config with certificate from configmap", name: "minimal TLS config with certificate from configmap",
@@ -86,10 +84,7 @@ func TestCreateOrUpdateWebConfigSecret(t *testing.T) {
}, },
}, },
}, },
expectedData: `tls_server_config: golden: "minimal_TLS_config_with_certificate_from_configmap.golden",
cert_file: /web_certs_path_prefix/configmap/test-configmap-cert/tls.crt
key_file: /web_certs_path_prefix/secret/test-secret-key/tls.key
`,
}, },
{ {
name: "minimal TLS config with client CA from configmap", name: "minimal TLS config with client CA from configmap",
@@ -119,11 +114,7 @@ func TestCreateOrUpdateWebConfigSecret(t *testing.T) {
}, },
}, },
}, },
expectedData: `tls_server_config: golden: "minimal_TLS_config_with_client_CA_from configmap.golden",
cert_file: /web_certs_path_prefix/configmap/test-configmap-cert/tls.crt
key_file: /web_certs_path_prefix/secret/test-secret-key/tls.key
client_ca_file: /web_certs_path_prefix/configmap/test-configmap-ca/tls.client_ca
`,
}, },
{ {
name: "TLS config with all parameters from secrets", name: "TLS config with all parameters from secrets",
@@ -159,21 +150,7 @@ func TestCreateOrUpdateWebConfigSecret(t *testing.T) {
CurvePreferences: []string{"curve-1", "curve-2"}, CurvePreferences: []string{"curve-1", "curve-2"},
}, },
}, },
expectedData: `tls_server_config: golden: "TLS_config_with_all_parameters_from secrets.golden",
cert_file: /web_certs_path_prefix/secret/test-secret-cert/tls.crt
key_file: /web_certs_path_prefix/secret/test-secret-key/tls.keySecret
client_auth_type: RequireAnyClientCert
client_ca_file: /web_certs_path_prefix/secret/test-secret-ca/tls.ca
min_version: TLS11
max_version: TLS13
cipher_suites:
- cipher-1
- cipher-2
prefer_server_cipher_suites: false
curve_preferences:
- curve-1
- curve-2
`,
}, },
{ {
name: "HTTP config with all parameters", name: "HTTP config with all parameters",
@@ -189,15 +166,7 @@ func TestCreateOrUpdateWebConfigSecret(t *testing.T) {
}, },
}, },
}, },
expectedData: `http_server_config: golden: "HTTP_config_with_all_parameters.golden",
http2: false
headers:
Content-Security-Policy: test
Strict-Transport-Security: test
X-Content-Type-Options: nosniff
X-Frame-Options: sameorigin
X-XSS-Protection: test
`,
}, },
} }
@@ -221,9 +190,7 @@ func TestCreateOrUpdateWebConfigSecret(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
if tt.expectedData != string(secret.Data["web-config.yaml"]) { golden.Assert(t, string(secret.Data["web-config.yaml"]), tt.golden)
t.Fatalf("Got %s\nwant %s\n", secret.Data["web-config.yaml"], tt.expectedData)
}
}) })
} }
} }

View File

@@ -0,0 +1,8 @@
http_server_config:
http2: false
headers:
Content-Security-Policy: test
Strict-Transport-Security: test
X-Content-Type-Options: nosniff
X-Frame-Options: sameorigin
X-XSS-Protection: test

View File

@@ -0,0 +1,14 @@
tls_server_config:
cert_file: /web_certs_path_prefix/secret/test-secret-cert/tls.crt
key_file: /web_certs_path_prefix/secret/test-secret-key/tls.keySecret
client_auth_type: RequireAnyClientCert
client_ca_file: /web_certs_path_prefix/secret/test-secret-ca/tls.ca
min_version: TLS11
max_version: TLS13
cipher_suites:
- cipher-1
- cipher-2
prefer_server_cipher_suites: false
curve_preferences:
- curve-1
- curve-2

View File

@@ -0,0 +1,3 @@
tls_server_config:
cert_file: /web_certs_path_prefix/configmap/test-configmap-cert/tls.crt
key_file: /web_certs_path_prefix/secret/test-secret-key/tls.key

View File

@@ -0,0 +1,3 @@
tls_server_config:
cert_file: /web_certs_path_prefix/secret/test-secret-cert/tls.crt
key_file: /web_certs_path_prefix/secret/test-secret-key/tls.key

View File

@@ -0,0 +1,4 @@
tls_server_config:
cert_file: /web_certs_path_prefix/configmap/test-configmap-cert/tls.crt
key_file: /web_certs_path_prefix/secret/test-secret-key/tls.key
client_ca_file: /web_certs_path_prefix/configmap/test-configmap-ca/tls.client_ca

0
pkg/webconfig/testdata/tls_config_not_defined.golden generated vendored Normal file
View File

20
scripts/update-golden-files.sh Executable file
View File

@@ -0,0 +1,20 @@
#!/bin/bash
# This script updates the golden files for unit tests that import the 'gotest.tools/v3/golden' dependency in a Go project.
# It lists all packages in the project, checks for the dependency in test imports, and runs unit tests with '-update' to update golden files.
dependency="gotest.tools/v3/golden"
# List all packages in the project
packages=$(go list ./...)
# Loop through each package and check if it imports the specific dependency
for pkg in $packages; do
# Use 'go list' with 'XTestImports' template to get the imports from test binaries
imports=$(go list -f '{{join .TestImports "\n"}}{{"\n"}}{{join .XTestImports "\n"}}' "$pkg")
# Check if the dependency is in the imports
if echo "$imports" | grep -q "$dependency"; then
# If the dependency is found, run the unit tests updating the golden files
go test "$pkg" -update -timeout 30s
fi
done