This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: Update tests to use Go Testing
Update the existing tests to use go testing framework and improve coverage to 80% Part of #1704 and # 1489 Signed-off-by: Sneha Chhabria <[email protected]>
- Loading branch information
1 parent
73079be
commit 0ea4311
Showing
12 changed files
with
296 additions
and
307 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,160 +1,147 @@ | ||
package main | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
"testing" | ||
|
||
tassert "github.com/stretchr/testify/assert" | ||
|
||
"github.com/openservicemesh/osm/pkg/certificate/providers" | ||
) | ||
|
||
var _ = Describe("Test validateCertificateManagerOptions", func() { | ||
var ( | ||
testCaBundleSecretName = "test-secret" | ||
) | ||
|
||
Context("tresor certProviderKind is passed in", func() { | ||
certProviderKind = providers.TresorKind.String() | ||
|
||
err := validateCertificateManagerOptions() | ||
|
||
It("should not error", func() { | ||
Expect(err).To(BeNil()) | ||
}) | ||
}) | ||
Context("vault certProviderKind is passed in and vaultToken is not empty", func() { | ||
certProviderKind = providers.VaultKind.String() | ||
vaultOptions.VaultToken = "anythinghere" | ||
|
||
err := validateCertificateManagerOptions() | ||
|
||
It("should not error", func() { | ||
Expect(err).To(BeNil()) | ||
}) | ||
}) | ||
Context("vault certProviderKind is passed in but vaultToken is empty", func() { | ||
certProviderKind = providers.VaultKind.String() | ||
vaultOptions.VaultToken = "" | ||
|
||
err := validateCertificateManagerOptions() | ||
|
||
It("should error", func() { | ||
Expect(err).To(HaveOccurred()) | ||
|
||
}) | ||
}) | ||
Context("cert-manager certProviderKind is passed in with valid caBundleSecretName and certmanagerIssuerName", func() { | ||
certProviderKind = providers.CertManagerKind.String() | ||
caBundleSecretName = testCaBundleSecretName | ||
certManagerOptions.IssuerName = "test-issuer" | ||
|
||
err := validateCertificateManagerOptions() | ||
|
||
It("should not error", func() { | ||
Expect(err).To(BeNil()) | ||
}) | ||
}) | ||
Context("cert-manager certProviderKind is passed in with caBundleSecretName but no certmanagerIssureName", func() { | ||
certProviderKind = providers.CertManagerKind.String() | ||
caBundleSecretName = testCaBundleSecretName | ||
certManagerOptions.IssuerName = "" | ||
|
||
err := validateCertificateManagerOptions() | ||
|
||
It("should error", func() { | ||
Expect(err).To(HaveOccurred()) | ||
}) | ||
}) | ||
Context("cert-manager certProviderKind is passed in without caBundleSecretName but no certmanagerIssureName", func() { | ||
certProviderKind = providers.CertManagerKind.String() | ||
caBundleSecretName = "" | ||
certManagerOptions.IssuerName = "" | ||
|
||
err := validateCertificateManagerOptions() | ||
|
||
It("should error", func() { | ||
Expect(err).To(HaveOccurred()) | ||
func TestValidateCertificateManagerOptions(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
certProvider string | ||
vaultToken string | ||
caBundleSecretName string | ||
issuerName string | ||
expectError bool | ||
}{ | ||
{ | ||
name: "Cert Provider : Tresor", | ||
certProvider: providers.TresorKind.String(), | ||
expectError: false, | ||
}, | ||
{ | ||
name: "Cert Provider : Vault and token is not empty", | ||
certProvider: providers.VaultKind.String(), | ||
vaultToken: "anythinghere", | ||
expectError: false, | ||
}, | ||
{ | ||
name: "Cert Provider : Vault and token is empty", | ||
certProvider: providers.VaultKind.String(), | ||
vaultToken: "", | ||
expectError: true, | ||
}, | ||
{ | ||
name: "Cert Provider : Cert-Manager with valid caBundleSecretName and certmanagerIssuerName", | ||
certProvider: providers.CertManagerKind.String(), | ||
caBundleSecretName: "test-secret", | ||
issuerName: "test-issuer", | ||
expectError: false, | ||
}, | ||
{ | ||
name: "Cert Provider : Cert-Manager with valid caBundleSecretName and no certmanagerIssuerName", | ||
certProvider: providers.CertManagerKind.String(), | ||
caBundleSecretName: "test-secret", | ||
issuerName: "", | ||
expectError: true, | ||
}, | ||
{ | ||
name: "Cert Provider : Cert-Manager with no caBundleSecretName and no certmanagerIssuerName", | ||
certProvider: providers.CertManagerKind.String(), | ||
issuerName: "", | ||
caBundleSecretName: "", | ||
expectError: true, | ||
}, | ||
{ | ||
name: "Cert Provider : InvalidProvider", | ||
certProvider: "InvalidProvider", | ||
expectError: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
assert := tassert.New(t) | ||
certProviderKind = tc.certProvider | ||
vaultOptions.VaultToken = tc.vaultToken | ||
certManagerOptions.IssuerName = tc.issuerName | ||
caBundleSecretName = tc.caBundleSecretName | ||
err := validateCertificateManagerOptions() | ||
assert.Equal(err != nil, tc.expectError) | ||
}) | ||
}) | ||
|
||
Context("invalid kind is passed in", func() { | ||
certProviderKind = "invalidkind" | ||
|
||
err := validateCertificateManagerOptions() | ||
|
||
It("should error", func() { | ||
Expect(err).To(HaveOccurred()) | ||
}) | ||
}) | ||
}) | ||
|
||
var _ = Describe("Test validateCLIParams", func() { | ||
var ( | ||
testMeshName = "test-mesh-name" | ||
testOsmNamespace = "test-namespace" | ||
testvalidatorWebhookConfigName = "test-webhook-name" | ||
testCABundleSecretName = "test-ca-bundle" | ||
) | ||
|
||
Context("none of the necessary CLI params are empty", func() { | ||
certProviderKind = providers.TresorKind.String() | ||
meshName = testMeshName | ||
osmNamespace = testOsmNamespace | ||
validatorWebhookConfigName = testvalidatorWebhookConfigName | ||
caBundleSecretName = testCABundleSecretName | ||
|
||
err := validateCLIParams() | ||
|
||
It("should not error", func() { | ||
Expect(err).To(BeNil()) | ||
}) | ||
}) | ||
Context("mesh name is empty", func() { | ||
certProviderKind = providers.TresorKind.String() | ||
meshName = "" | ||
osmNamespace = testOsmNamespace | ||
validatorWebhookConfigName = testvalidatorWebhookConfigName | ||
|
||
err := validateCLIParams() | ||
|
||
It("should error", func() { | ||
Expect(err).To(HaveOccurred()) | ||
}) | ||
}) | ||
Context("osmNamespace is empty", func() { | ||
certProviderKind = providers.TresorKind.String() | ||
meshName = testMeshName | ||
osmNamespace = "" | ||
validatorWebhookConfigName = testvalidatorWebhookConfigName | ||
|
||
err := validateCLIParams() | ||
|
||
It("should error", func() { | ||
Expect(err).To(HaveOccurred()) | ||
}) | ||
}) | ||
Context("validatorWebhookConfigName is empty", func() { | ||
certProviderKind = providers.TresorKind.String() | ||
meshName = testMeshName | ||
osmNamespace = testOsmNamespace | ||
validatorWebhookConfigName = "" | ||
|
||
err := validateCLIParams() | ||
|
||
It("should error", func() { | ||
Expect(err).To(HaveOccurred()) | ||
}) | ||
}) | ||
Context("caBundleSecretName is empty", func() { | ||
certProviderKind = providers.TresorKind.String() | ||
meshName = testMeshName | ||
osmNamespace = testOsmNamespace | ||
validatorWebhookConfigName = testvalidatorWebhookConfigName | ||
caBundleSecretName = "" | ||
|
||
err := validateCLIParams() | ||
|
||
It("should error", func() { | ||
Expect(err).To(HaveOccurred()) | ||
} | ||
} | ||
|
||
func TestValidateCLIParams(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
certProvider string | ||
meshName string | ||
osmNamespace string | ||
validatorWebhookConfigName string | ||
caBundleSecretName string | ||
expectError bool | ||
}{ | ||
{ | ||
name: "none of the necessary CLI params are empty", | ||
certProvider: providers.TresorKind.String(), | ||
meshName: "test-mesh", | ||
osmNamespace: "test-ns", | ||
validatorWebhookConfigName: "test-webhook", | ||
caBundleSecretName: "test-secret", | ||
expectError: false, | ||
}, | ||
{ | ||
name: "mesh name is empty", | ||
certProvider: providers.TresorKind.String(), | ||
meshName: "", | ||
osmNamespace: "test-ns", | ||
validatorWebhookConfigName: "test-webhook", | ||
caBundleSecretName: "test-secret", | ||
expectError: true, | ||
}, | ||
{ | ||
name: "osm namespace is empty", | ||
certProvider: providers.TresorKind.String(), | ||
meshName: "test-mesh", | ||
osmNamespace: "", | ||
validatorWebhookConfigName: "test-webhook", | ||
caBundleSecretName: "test-secret", | ||
expectError: true, | ||
}, | ||
{ | ||
name: "validator webhook is empty", | ||
certProvider: providers.TresorKind.String(), | ||
meshName: "test-mesh", | ||
osmNamespace: "test-ns", | ||
validatorWebhookConfigName: "", | ||
caBundleSecretName: "test-secret", | ||
expectError: true, | ||
}, | ||
{ | ||
name: "cabundle is empty", | ||
certProvider: providers.TresorKind.String(), | ||
meshName: "test-mesh", | ||
osmNamespace: "test-ns", | ||
validatorWebhookConfigName: "test-webhook", | ||
caBundleSecretName: "", | ||
expectError: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
assert := tassert.New(t) | ||
certProviderKind = tc.certProvider | ||
meshName = tc.meshName | ||
osmNamespace = tc.osmNamespace | ||
validatorWebhookConfigName = tc.validatorWebhookConfigName | ||
caBundleSecretName = tc.caBundleSecretName | ||
err := validateCLIParams() | ||
assert.Equal(err != nil, tc.expectError) | ||
}) | ||
}) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,23 @@ | ||
package catalog | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
tassert "github.com/stretchr/testify/assert" | ||
|
||
"github.com/openservicemesh/osm/pkg/tests" | ||
) | ||
|
||
var _ = Describe("Test catalog proxy register/unregister", func() { | ||
mc := newFakeMeshCatalog() | ||
|
||
Context("Test ListMonitoredNamespaces", func() { | ||
It("lists monitored namespaces", func() { | ||
actual := mc.ListMonitoredNamespaces() | ||
listExpectedNs := tests.GetUnique([]string{ | ||
tests.BookstoreV1Service.Namespace, | ||
tests.BookbuyerService.Namespace, | ||
tests.BookwarehouseService.Namespace, | ||
}) | ||
|
||
Expect(actual).To(Equal(listExpectedNs)) | ||
}) | ||
}) | ||
|
||
Context("Test ListSMIPolicies", func() { | ||
It("lists available SMI Spec policies", func() { | ||
trafficSplits, serviceAccounts, routeGroups, trafficTargets := mc.ListSMIPolicies() | ||
|
||
Expect(trafficSplits[0].Spec.Service).To(Equal("bookstore-apex")) | ||
Expect(serviceAccounts[0].String()).To(Equal("default/bookstore")) | ||
Expect(routeGroups[0].Name).To(Equal("bookstore-service-routes")) | ||
Expect(trafficTargets[0].Name).To(Equal(tests.TrafficTargetName)) | ||
|
||
}) | ||
}) | ||
}) | ||
func TestListSMIPolicies(t *testing.T) { | ||
assert := tassert.New(t) | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
mockCatalog := newFakeMeshCatalog() | ||
|
||
trafficSplits, serviceAccounts, routeGroups, trafficTargets := mockCatalog.ListSMIPolicies() | ||
assert.Equal(trafficSplits[0].Spec.Service, "bookstore-apex") | ||
assert.Equal(serviceAccounts[0].String(), "default/bookstore") | ||
assert.Equal(routeGroups[0].Name, "bookstore-service-routes") | ||
assert.Equal(trafficTargets[0].Name, tests.TrafficTargetName) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.