Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Commit

Permalink
tests: Update tests to use Go Testing
Browse files Browse the repository at this point in the history
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
snehachhabria committed Aug 31, 2021
1 parent 73079be commit 0ea4311
Show file tree
Hide file tree
Showing 12 changed files with 296 additions and 307 deletions.
289 changes: 138 additions & 151 deletions cmd/osm-controller/validate_test.go
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)
})
})
})
}
}
11 changes: 0 additions & 11 deletions pkg/catalog/debugger.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,3 @@ func (mc *MeshCatalog) ListSMIPolicies() ([]*split.TrafficSplit, []identity.K8sS

return trafficSplits, serviceAccounts, trafficSpecs, trafficTargets
}

// ListMonitoredNamespaces returns all namespaces that the mesh is monitoring.
func (mc *MeshCatalog) ListMonitoredNamespaces() []string {
namespaces, err := mc.kubeController.ListMonitoredNamespaces()

if err != nil {
return nil
}

return namespaces
}
46 changes: 16 additions & 30 deletions pkg/catalog/debugger_test.go
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)
}
14 changes: 0 additions & 14 deletions pkg/debugger/mock_debugger_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions pkg/debugger/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ type CertificateManagerDebugger interface {
type MeshCatalogDebugger interface {
// ListSMIPolicies lists the SMI policies detected by OSM.
ListSMIPolicies() ([]*split.TrafficSplit, []identity.K8sServiceAccount, []*spec.HTTPRouteGroup, []*access.TrafficTarget)

// ListMonitoredNamespaces lists the namespaces that the control plan knows about.
ListMonitoredNamespaces() []string
}

// XDSDebugger is an interface providing debugging server with methods introspecting XDS.
Expand Down
Loading

0 comments on commit 0ea4311

Please sign in to comment.