Skip to content

Commit

Permalink
Add testutils to DRY up provider tests
Browse files Browse the repository at this point in the history
  • Loading branch information
doodlesbykumbi committed Oct 13, 2020
1 parent edb41de commit c776af8
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 161 deletions.
50 changes: 50 additions & 0 deletions internal/plugin/v1/testutils/testutils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package testutils

import (
. "github.com/smartystreets/goconvey/convey"

plugin_v1 "github.com/cyberark/secretless-broker/internal/plugin/v1"
)

// CanProvideTestCase captures a test case where a provider is expected to return a value
// and no error
type CanProvideTestCase struct {
Description string
ID string
ExpectedValue string
}

// CanProvide calls GetValues on the provider and ensures that the provider response for
// the given id has the expected value and no error
func CanProvide(provider plugin_v1.Provider, id string, expectedValue string) func() {
return func() {
values, err := provider.GetValues(id)

So(err, ShouldBeNil)
So(values[id], ShouldNotBeNil)
So(values[id].Error, ShouldBeNil)
So(values[id].Value, ShouldNotBeNil)
So(string(values[id].Value), ShouldEqual, expectedValue)
}
}

// ReportsTestCase captures a test case where a provider is expected to return an error
type ReportsTestCase struct {
Description string
ID string
ExpectedErrString string
}

// Reports calls GetValues on the provider and ensures that the provider response for the
// given id has the expected error and no value
func Reports(provider plugin_v1.Provider, id string, expectedErrString string) func() {
return func() {
values, err := provider.GetValues(id)

So(err, ShouldBeNil)
So(values, ShouldContainKey, id)
So(values[id].Value, ShouldBeNil)
So(values[id].Error, ShouldNotBeNil)
So(values[id].Error.Error(), ShouldEqual, expectedErrString)
}
}
68 changes: 25 additions & 43 deletions test/connector/http/conjur/conjur_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
. "github.com/smartystreets/goconvey/convey"

plugin_v1 "github.com/cyberark/secretless-broker/internal/plugin/v1"
"github.com/cyberark/secretless-broker/internal/plugin/v1/testutils"
"github.com/cyberark/secretless-broker/internal/providers"
)

Expand Down Expand Up @@ -47,64 +48,45 @@ func TestConjur_Provider(t *testing.T) {
So(token["payload"], ShouldNotBeNil)
})

Convey("Reports an unknown value", t, func() {
id := "foobar"
values, err := provider.GetValues(id)

So(err, ShouldBeNil)
So(values[id], ShouldNotBeNil)
So(values[id].Error, ShouldNotBeNil)
So(values[id].Error.Error(), ShouldEqual, "404 Not Found. Variable 'foobar' not found in account 'dev'.")
So(values[id].Value, ShouldBeNil)
})
Convey(
"Reports an unknown value",
t,
testutils.Reports(
provider,
"foobar",
"404 Not Found. Variable 'foobar' not found in account 'dev'.",
),
)

Convey("Provides", t, func() {
for _, testCase := range canProvideTestCases {
Convey(
testCase.description,
canProvide(provider, testCase.id, testCase.expectedValue),
testCase.Description,
testutils.CanProvide(provider, testCase.ID, testCase.ExpectedValue),
)
}
})
}

type canProvideTestCase struct {
description string
id string
expectedValue string
}

func canProvide(provider plugin_v1.Provider, id string, expectedValue string) func() {
return func() {
values, err := provider.GetValues(id)

So(err, ShouldBeNil)
So(values[id], ShouldNotBeNil)
So(values[id].Error, ShouldBeNil)
So(values[id].Value, ShouldNotBeNil)
So(string(values[id].Value), ShouldEqual, expectedValue)
}
}

var canProvideTestCases = []canProvideTestCase{
var canProvideTestCases = []testutils.CanProvideTestCase{
{
description: "Can provide a secret to a fully qualified variable",
id: "dev:variable:db/password",
expectedValue: "secret",
Description: "Can provide a secret to a fully qualified variable",
ID: "dev:variable:db/password",
ExpectedValue: "secret",
},
{
description: "Can retrieve a secret value with spaces",
id: "my var",
expectedValue: "othersecret",
Description: "Can retrieve a secret value with spaces",
ID: "my var",
ExpectedValue: "othersecret",
},
{
description: "Can provide the default Conjur account name",
id: "variable:db/password",
expectedValue: "secret",
Description: "Can provide the default Conjur account name",
ID: "variable:db/password",
ExpectedValue: "secret",
},
{
description: "Can provide the default Conjur account name and resource type",
id: "db/password",
expectedValue: "secret",
Description: "Can provide the default Conjur account name and resource type",
ID: "db/password",
ExpectedValue: "secret",
},
}
41 changes: 20 additions & 21 deletions test/providers/keychain/keychain_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

plugin_v1 "github.com/cyberark/secretless-broker/internal/plugin/v1"
"github.com/cyberark/secretless-broker/internal/plugin/v1/testutils"
"github.com/cyberark/secretless-broker/internal/providers"

. "github.com/smartystreets/goconvey/convey"
Expand Down Expand Up @@ -37,25 +38,23 @@ func TestKeychainProvider(t *testing.T) {
So(provider.GetName(), ShouldEqual, name)
})

Convey("Can provide a valid secret value", t, func() {
id := strings.Join([]string{service, account}, "#")

values, err := provider.GetValues(id)
So(err, ShouldBeNil)
So(values[id], ShouldNotBeNil)
So(values[id].Error, ShouldBeNil)
So(values[id].Value, ShouldNotBeNil)
So(string(values[id].Value), ShouldEqual, secret)
})

Convey("Returns an error for an invalid secret value", t, func() {
id := "madeup#secret"

values, err := provider.GetValues(id)
So(err, ShouldBeNil)
So(values[id], ShouldNotBeNil)
So(values[id].Error, ShouldNotBeNil)
So(values[id].Error.Error(), ShouldEqual, "The specified item could not be found in the keychain.")
So(values[id].Value, ShouldBeNil)
})
Convey(
"Can provide a valid secret value",
t,
testutils.CanProvide(
provider,
strings.Join([]string{service, account}, "#"),
secret,
),
)

Convey(
"Returns an error for an invalid secret value",
t,
testutils.CanProvide(
provider,
"madeup#secret",
"The specified item could not be found in the keychain.",
),
)
}
53 changes: 19 additions & 34 deletions test/providers/kubernetessecrets/kubernetes_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
testclient "k8s.io/client-go/kubernetes/fake"

plugin_v1 "github.com/cyberark/secretless-broker/internal/plugin/v1"
"github.com/cyberark/secretless-broker/internal/plugin/v1/testutils"
"github.com/cyberark/secretless-broker/internal/providers"
"github.com/cyberark/secretless-broker/internal/providers/kubernetessecrets"
)
Expand All @@ -21,7 +22,9 @@ func TestKubernetes_Provider(t *testing.T) {
kubernetesProvider *kubernetessecrets.Provider
)

var testSecretsClient = testclient.NewSimpleClientset().CoreV1().Secrets("some-namespace")
var testSecretsClient = testclient.NewSimpleClientset().CoreV1().Secrets(
"some-namespace",
)

_, err = testSecretsClient.Create(&v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -68,50 +71,32 @@ func TestKubernetes_Provider(t *testing.T) {
Convey("Reports", t, func() {
for _, testCase := range reportsTestCases {
Convey(
testCase.description,
reports(provider, testCase.id, testCase.expectedErrString),
testCase.Description,
testutils.Reports(provider, testCase.ID, testCase.ExpectedErrString),
)
}
})
}

type reportsTestCase struct {
description string
id string
expectedErrString string
}

func reports(provider plugin_v1.Provider, id string, expectedErrString string) func() {
return func() {
values, err := provider.GetValues(id)

So(err, ShouldBeNil)
So(values, ShouldContainKey, id)
So(values[id].Value, ShouldBeNil)
So(values[id].Error, ShouldNotBeNil)
So(values[id].Error.Error(), ShouldEqual, expectedErrString)
}
}

var reportsTestCases = []reportsTestCase{
var reportsTestCases = []testutils.ReportsTestCase{
{
description: "Reports when the secret id does not contain a field name",
id: "foobar",
expectedErrString: "Kubernetes secret id must contain secret name and field name in the format secretName#fieldName, received 'foobar'",
Description: "Reports when the secret id does not contain a field name",
ID: "foobar",
ExpectedErrString: "Kubernetes secret id must contain secret name and field name in the format secretName#fieldName, received 'foobar'",
},
{
description: "Reports when the secret id has empty field name",
id: "foobar#",
expectedErrString: "field name missing from Kubernetes secret id 'foobar#'",
Description: "Reports when the secret id has empty field name",
ID: "foobar#",
ExpectedErrString: "field name missing from Kubernetes secret id 'foobar#'",
},
{
description: "Reports when Kubernetes is unable to find secret",
id: "foobar#maybe",
expectedErrString: "could not find Kubernetes secret from 'foobar#maybe'",
Description: "Reports when Kubernetes is unable to find secret",
ID: "foobar#maybe",
ExpectedErrString: "could not find Kubernetes secret from 'foobar#maybe'",
},
{
description: "Reports when Kubernetes is unable to find field name in secret",
id: "database#missing",
expectedErrString: "could not find field 'missing' in Kubernetes secret 'database'",
Description: "Reports when Kubernetes is unable to find field name in secret",
ID: "database#missing",
ExpectedErrString: "could not find field 'missing' in Kubernetes secret 'database'",
},
}
Loading

0 comments on commit c776af8

Please sign in to comment.