diff --git a/.gitattributes b/.gitattributes index 75781d27b..6744c68fd 100644 --- a/.gitattributes +++ b/.gitattributes @@ -4,3 +4,4 @@ example/prometheus-operator-crd-full/* linguist-generated=true example/prometheus-operator-crd/* linguist-generated=true example/jsonnet/prometheus-operator/* linguist-generated=true Documentation/api.md linguist-generated=true +**/testdata/*.golden linguist-generated=true diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index ccb3193b7..64332b7f0 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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). - 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. +- 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. 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. diff --git a/Makefile b/Makefile index 52126a0e6..ed0350bd6 100644 --- a/Makefile +++ b/Makefile @@ -346,6 +346,10 @@ test-unit: test-long: 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: cd test/instrumented-sample-app && make generate-certs diff --git a/go.mod b/go.mod index 41ec2f605..221cd2f4c 100644 --- a/go.mod +++ b/go.mod @@ -122,6 +122,7 @@ require ( google.golang.org/appengine v1.6.7 // indirect gopkg.in/inf.v0 v0.9.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 sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect sigs.k8s.io/structured-merge-diff/v4 v4.3.0 // indirect diff --git a/go.sum b/go.sum index e16a0f05a..45a013c1c 100644 --- a/go.sum +++ b/go.sum @@ -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.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= 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-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/pkg/webconfig/config_test.go b/pkg/webconfig/config_test.go index 36c28b4d3..e1145aa60 100644 --- a/pkg/webconfig/config_test.go +++ b/pkg/webconfig/config_test.go @@ -20,6 +20,7 @@ import ( "testing" "github.com/kylelemons/godebug/pretty" + "gotest.tools/v3/golden" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes/fake" @@ -34,12 +35,12 @@ func TestCreateOrUpdateWebConfigSecret(t *testing.T) { tc := []struct { name string webConfigFileFields monitoringv1.WebConfigFileFields - expectedData string + golden string }{ { name: "tls config not defined", webConfigFileFields: monitoringv1.WebConfigFileFields{}, - expectedData: "", + golden: "tls_config_not_defined.golden", }, { name: "minimal TLS config with certificate from secret", @@ -61,10 +62,7 @@ func TestCreateOrUpdateWebConfigSecret(t *testing.T) { }, }, }, - expectedData: `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 -`, + golden: "minimal_TLS_config_with_certificate_from_secret.golden", }, { name: "minimal TLS config with certificate from configmap", @@ -86,10 +84,7 @@ func TestCreateOrUpdateWebConfigSecret(t *testing.T) { }, }, }, - expectedData: `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 -`, + golden: "minimal_TLS_config_with_certificate_from_configmap.golden", }, { name: "minimal TLS config with client CA from configmap", @@ -119,11 +114,7 @@ func TestCreateOrUpdateWebConfigSecret(t *testing.T) { }, }, }, - expectedData: `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 -`, + golden: "minimal_TLS_config_with_client_CA_from configmap.golden", }, { name: "TLS config with all parameters from secrets", @@ -159,21 +150,7 @@ func TestCreateOrUpdateWebConfigSecret(t *testing.T) { CurvePreferences: []string{"curve-1", "curve-2"}, }, }, - expectedData: `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 -`, + golden: "TLS_config_with_all_parameters_from secrets.golden", }, { name: "HTTP config with all parameters", @@ -189,15 +166,7 @@ func TestCreateOrUpdateWebConfigSecret(t *testing.T) { }, }, }, - expectedData: `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 -`, + golden: "HTTP_config_with_all_parameters.golden", }, } @@ -221,9 +190,7 @@ func TestCreateOrUpdateWebConfigSecret(t *testing.T) { t.Fatal(err) } - if tt.expectedData != string(secret.Data["web-config.yaml"]) { - t.Fatalf("Got %s\nwant %s\n", secret.Data["web-config.yaml"], tt.expectedData) - } + golden.Assert(t, string(secret.Data["web-config.yaml"]), tt.golden) }) } } diff --git a/pkg/webconfig/testdata/HTTP_config_with_all_parameters.golden b/pkg/webconfig/testdata/HTTP_config_with_all_parameters.golden new file mode 100644 index 000000000..f9a2191c4 --- /dev/null +++ b/pkg/webconfig/testdata/HTTP_config_with_all_parameters.golden @@ -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 diff --git a/pkg/webconfig/testdata/TLS_config_with_all_parameters_from secrets.golden b/pkg/webconfig/testdata/TLS_config_with_all_parameters_from secrets.golden new file mode 100644 index 000000000..01d7e829a --- /dev/null +++ b/pkg/webconfig/testdata/TLS_config_with_all_parameters_from secrets.golden @@ -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 diff --git a/pkg/webconfig/testdata/minimal_TLS_config_with_certificate_from_configmap.golden b/pkg/webconfig/testdata/minimal_TLS_config_with_certificate_from_configmap.golden new file mode 100644 index 000000000..9e1010b9a --- /dev/null +++ b/pkg/webconfig/testdata/minimal_TLS_config_with_certificate_from_configmap.golden @@ -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 diff --git a/pkg/webconfig/testdata/minimal_TLS_config_with_certificate_from_secret.golden b/pkg/webconfig/testdata/minimal_TLS_config_with_certificate_from_secret.golden new file mode 100644 index 000000000..6ec4e9bab --- /dev/null +++ b/pkg/webconfig/testdata/minimal_TLS_config_with_certificate_from_secret.golden @@ -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 diff --git a/pkg/webconfig/testdata/minimal_TLS_config_with_client_CA_from configmap.golden b/pkg/webconfig/testdata/minimal_TLS_config_with_client_CA_from configmap.golden new file mode 100644 index 000000000..7d61b6e13 --- /dev/null +++ b/pkg/webconfig/testdata/minimal_TLS_config_with_client_CA_from configmap.golden @@ -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 diff --git a/pkg/webconfig/testdata/tls_config_not_defined.golden b/pkg/webconfig/testdata/tls_config_not_defined.golden new file mode 100644 index 000000000..e69de29bb diff --git a/scripts/update-golden-files.sh b/scripts/update-golden-files.sh new file mode 100755 index 000000000..029245d44 --- /dev/null +++ b/scripts/update-golden-files.sh @@ -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