From 2ebf9d3d00530a38cda9390a9dce1ff2718d4e5d Mon Sep 17 00:00:00 2001 From: Ryan Richard Date: Sat, 3 Aug 2024 14:28:45 -0700 Subject: [PATCH] minor test refactor --- test/integration/concierge_tls_spec_test.go | 67 +++++++++++--------- test/integration/supervisor_tls_spec_test.go | 20 +++--- 2 files changed, 44 insertions(+), 43 deletions(-) diff --git a/test/integration/concierge_tls_spec_test.go b/test/integration/concierge_tls_spec_test.go index 783a17b29..85d8103db 100644 --- a/test/integration/concierge_tls_spec_test.go +++ b/test/integration/concierge_tls_spec_test.go @@ -163,51 +163,42 @@ func TestTLSSpecKubeBuilderValidationConcierge_Parallel(t *testing.T) { indentedTLSYAML := strings.ReplaceAll(tc.tlsYAML, "\n", "\n ") t.Run("apply webhook authenticator", func(t *testing.T) { - webhookResourceName := "test-webhook-authenticator-" + testlib.RandHex(t, 7) - webhookYamlBytes := []byte(fmt.Sprintf(webhookAuthenticatorYamlTemplate, - env.APIGroupSuffix, webhookResourceName, env.TestWebhook.Endpoint, indentedTLSYAML)) + resourceName := "test-webhook-authenticator-" + testlib.RandHex(t, 7) + yamlBytes := []byte(fmt.Sprintf(webhookAuthenticatorYamlTemplate, + env.APIGroupSuffix, resourceName, env.TestWebhook.Endpoint, indentedTLSYAML)) - performKubectlApply( - t, - webhookYamlBytes, + stdOut, stdErr, err := performKubectlApply(t, resourceName, yamlBytes) + requireKubectlApplyResult(t, stdOut, stdErr, err, fmt.Sprintf(`webhookauthenticator.authentication.concierge.%s`, env.APIGroupSuffix), tc.expectedErrorSnippets, "WebhookAuthenticator", - webhookResourceName, + resourceName, ) }) t.Run("apply jwt authenticator", func(t *testing.T) { _, supervisorIssuer := env.InferSupervisorIssuerURL(t) - jwtAuthenticatorResourceName := "test-jwt-authenticator-" + testlib.RandHex(t, 7) - jwtAuthenticatorYamlBytes := []byte(fmt.Sprintf(jwtAuthenticatorYamlTemplate, - env.APIGroupSuffix, jwtAuthenticatorResourceName, supervisorIssuer, indentedTLSYAML)) + resourceName := "test-jwt-authenticator-" + testlib.RandHex(t, 7) + yamlBytes := []byte(fmt.Sprintf(jwtAuthenticatorYamlTemplate, + env.APIGroupSuffix, resourceName, supervisorIssuer, indentedTLSYAML)) - performKubectlApply( - t, - jwtAuthenticatorYamlBytes, + stdOut, stdErr, err := performKubectlApply(t, resourceName, yamlBytes) + requireKubectlApplyResult(t, stdOut, stdErr, err, fmt.Sprintf(`jwtauthenticator.authentication.concierge.%s`, env.APIGroupSuffix), tc.expectedErrorSnippets, "JWTAuthenticator", - jwtAuthenticatorResourceName, + resourceName, ) }) }) } } -func performKubectlApply( - t *testing.T, - yamlBytes []byte, - expectedSuccessPrefix string, - expectedErrorSnippets []string, - resourceType string, - resourceName string, -) { +func performKubectlApply(t *testing.T, resourceName string, yamlBytes []byte) (string, string, error) { t.Helper() - yamlFilepath := filepath.Join(t.TempDir(), fmt.Sprintf("tls-spec-validation-%s.yaml", resourceName)) + yamlFilepath := filepath.Join(t.TempDir(), fmt.Sprintf("test-perform-kubectl-apply-%s.yaml", resourceName)) require.NoError(t, os.WriteFile(yamlFilepath, yamlBytes, 0600)) @@ -227,17 +218,31 @@ func performKubectlApply( require.NoError(t, exec.Command("kubectl", []string{"delete", "--ignore-not-found", "-f", yamlFilepath}...).Run()) }) - if len(expectedErrorSnippets) > 0 { - actualErrorString := strings.TrimSuffix(stdErr.String(), "\n") - for i, snippet := range expectedErrorSnippets { + return stdOut.String(), stdErr.String(), err +} + +func requireKubectlApplyResult( + t *testing.T, + kubectlStdOut string, + kubectlStdErr string, + kubectlErr error, + wantSuccessPrefix string, + wantErrorSnippets []string, + wantResourceType string, + wantResourceName string, +) { + if len(wantErrorSnippets) > 0 { + require.Error(t, kubectlErr) + actualErrorString := strings.TrimSuffix(kubectlStdErr, "\n") + for i, snippet := range wantErrorSnippets { if i == 0 { - snippet = fmt.Sprintf(snippet, resourceType, resourceName) + snippet = fmt.Sprintf(snippet, wantResourceType, wantResourceName) } require.Contains(t, actualErrorString, snippet) } - return + } else { + require.Empty(t, kubectlStdErr) + require.Regexp(t, regexp.QuoteMeta(wantSuccessPrefix)+regexp.QuoteMeta(fmt.Sprintf("/%s created\n", wantResourceName)), kubectlStdOut) + require.NoError(t, kubectlErr) } - require.Empty(t, stdErr.String()) - require.Regexp(t, regexp.QuoteMeta(expectedSuccessPrefix)+regexp.QuoteMeta(fmt.Sprintf("/%s created\n", resourceName)), stdOut.String()) - require.NoError(t, err) } diff --git a/test/integration/supervisor_tls_spec_test.go b/test/integration/supervisor_tls_spec_test.go index 12990eedc..6eb978b63 100644 --- a/test/integration/supervisor_tls_spec_test.go +++ b/test/integration/supervisor_tls_spec_test.go @@ -223,9 +223,8 @@ func TestTLSSpecKubeBuilderValidationSupervisor_Parallel(t *testing.T) { yamlBytes := []byte(fmt.Sprintf(oidcIDPTemplate, env.APIGroupSuffix, resourceName, env.SupervisorUpstreamOIDC.Issuer, indentedTLSYAML)) - performKubectlApply( - t, - yamlBytes, + stdOut, stdErr, err := performKubectlApply(t, resourceName, yamlBytes) + requireKubectlApplyResult(t, stdOut, stdErr, err, fmt.Sprintf(`oidcidentityprovider.idp.supervisor.%s`, env.APIGroupSuffix), tc.expectedErrorSnippets, "OIDCIdentityProvider", @@ -238,9 +237,8 @@ func TestTLSSpecKubeBuilderValidationSupervisor_Parallel(t *testing.T) { yamlBytes := []byte(fmt.Sprintf(ldapIDPTemplate, env.APIGroupSuffix, resourceName, env.SupervisorUpstreamLDAP.Host, indentedTLSYAML)) - performKubectlApply( - t, - yamlBytes, + stdOut, stdErr, err := performKubectlApply(t, resourceName, yamlBytes) + requireKubectlApplyResult(t, stdOut, stdErr, err, fmt.Sprintf(`ldapidentityprovider.idp.supervisor.%s`, env.APIGroupSuffix), tc.expectedErrorSnippets, "LDAPIdentityProvider", @@ -253,9 +251,8 @@ func TestTLSSpecKubeBuilderValidationSupervisor_Parallel(t *testing.T) { yamlBytes := []byte(fmt.Sprintf(activeDirectoryIDPTemplate, env.APIGroupSuffix, resourceName, env.SupervisorUpstreamLDAP.Host, indentedTLSYAML)) - performKubectlApply( - t, - yamlBytes, + stdOut, stdErr, err := performKubectlApply(t, resourceName, yamlBytes) + requireKubectlApplyResult(t, stdOut, stdErr, err, fmt.Sprintf(`activedirectoryidentityprovider.idp.supervisor.%s`, env.APIGroupSuffix), tc.expectedErrorSnippets, "ActiveDirectoryIdentityProvider", @@ -271,9 +268,8 @@ func TestTLSSpecKubeBuilderValidationSupervisor_Parallel(t *testing.T) { yamlBytes := []byte(fmt.Sprintf(githubIDPTemplate, env.APIGroupSuffix, resourceName, indentedTLSYAMLForGitHub)) - performKubectlApply( - t, - yamlBytes, + stdOut, stdErr, err := performKubectlApply(t, resourceName, yamlBytes) + requireKubectlApplyResult(t, stdOut, stdErr, err, fmt.Sprintf(`githubidentityprovider.idp.supervisor.%s`, env.APIGroupSuffix), tc.expectedGitHubErrorSnippets, "GitHubIdentityProvider",