From a0535f5a2ec3c2fa40f5caa42ee46d17ffa5b46e Mon Sep 17 00:00:00 2001 From: Eduard Serra Date: Tue, 29 Jun 2021 13:42:37 -0700 Subject: [PATCH] pkg/cert/providers: add ValidateCertManagerOptions test Unit test for `ValidateCertManagerOptions` and `ValidateVaultOptions` Signed-off-by: Eduard Serra --- pkg/certificate/providers/config_test.go | 136 +++++++++++++++++++++++ 1 file changed, 136 insertions(+) diff --git a/pkg/certificate/providers/config_test.go b/pkg/certificate/providers/config_test.go index f4cb70f3a4..3a9487b300 100644 --- a/pkg/certificate/providers/config_test.go +++ b/pkg/certificate/providers/config_test.go @@ -210,3 +210,139 @@ func TestGetCertificateFromKubernetes(t *testing.T) { assert.Equal(testElement.expectNilVal, cert == nil) } } + +func TestValidateCertManagerOptions(t *testing.T) { + assert := tassert.New(t) + + testCases := []struct { + testName string + options CertManagerOptions + expectErr bool + }{ + { + testName: "Empty issuer", + options: CertManagerOptions{ + IssuerName: "", + IssuerKind: "test-kind", + IssuerGroup: "test-group", + }, + expectErr: true, + }, + { + testName: "Empty kind", + options: CertManagerOptions{ + IssuerName: "test-name", + IssuerKind: "", + IssuerGroup: "test-group", + }, + expectErr: true, + }, + { + testName: "Empty group", + options: CertManagerOptions{ + IssuerName: "test-name", + IssuerKind: "test-kind", + IssuerGroup: "", + }, + expectErr: true, + }, + { + testName: "Valid cert manager opts", + options: CertManagerOptions{ + IssuerName: "test-name", + IssuerKind: "test-kind", + IssuerGroup: "test-group", + }, + expectErr: false, + }, + } + + for _, t := range testCases { + err := ValidateCertManagerOptions(t.options) + if t.expectErr { + assert.Error(err, "test '%s' didn't error as expected", t.testName) + } else { + assert.NoError(err, "test '%s' didn't succeed as expected", t.testName) + } + } +} + +func TestValidateVaultOptions(t *testing.T) { + assert := tassert.New(t) + + testCases := []struct { + testName string + options VaultOptions + expectErr bool + }{ + { + testName: "invalid proto", + options: VaultOptions{ + VaultProtocol: "ftp", + VaultHost: "vault-host", + VaultToken: "vault-token", + VaultRole: "vault-role", + }, + expectErr: true, + }, + { + testName: "Empty host", + options: VaultOptions{ + VaultProtocol: "http", + VaultHost: "", + VaultToken: "vault-token", + VaultRole: "vault-role", + }, + expectErr: true, + }, + { + testName: "Empty token", + options: VaultOptions{ + VaultProtocol: "https", + VaultHost: "vault-host", + VaultToken: "", + VaultRole: "vault-role", + }, + expectErr: true, + }, + { + testName: "Empty role", + options: VaultOptions{ + VaultProtocol: "http", + VaultHost: "vault-host", + VaultToken: "vault-token", + VaultRole: "", + }, + expectErr: true, + }, + { + testName: "Empty role", + options: VaultOptions{ + VaultProtocol: "https", + VaultHost: "vault-host", + VaultToken: "vault-token", + VaultRole: "", + }, + expectErr: true, + }, + { + testName: "Valid config", + options: VaultOptions{ + VaultProtocol: "https", + VaultHost: "vault-host", + VaultToken: "vault-token", + VaultRole: "role", + }, + expectErr: false, + }, + } + + for _, t := range testCases { + err := ValidateVaultOptions(t.options) + if t.expectErr { + assert.Error(err, "test '%s' didn't error as expected", t.testName) + } else { + assert.NoError(err, "test '%s' didn't succeed as expected", t.testName) + } + } +}