Skip to content
This repository has been archived by the owner on Jun 26, 2024. It is now read-only.

Commit

Permalink
refactor: ✅ transform TestRetriever into a table test
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Sutton Lopes committed Jun 16, 2020
1 parent fe7bf94 commit 24e3e84
Showing 1 changed file with 97 additions and 35 deletions.
132 changes: 97 additions & 35 deletions pkg/controller/servicebindingrequest/retriever_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,38 @@ package servicebindingrequest

import (
"fmt"
"strings"
"testing"

"github.com/stretchr/testify/require"
v1 "k8s.io/api/core/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"

"github.com/redhat-developer/service-binding-operator/test/mocks"
)

func TestRetriever(t *testing.T) {
func toIndexTemplate(obj *unstructured.Unstructured, fieldPath string) string {
gvk := obj.GetObjectKind().GroupVersionKind()
name := obj.GetName()
parts := strings.Split(fieldPath, ".")
var newParts []string
for _, part := range parts {
newParts = append(newParts, fmt.Sprintf(`%q`, part))
}
indexArg := strings.Join(newParts, " ")
return fmt.Sprintf(
`{{ index . %q %q %q %q %s }}`, gvk.Version, gvk.Group, gvk.Kind, name, indexArg)
}

func TestRetrieverProcessServiceContexts(t *testing.T) {

logf.SetLogger(logf.ZapLogger(true))

ns := "testing"
backingServiceNs := "backing-service-ns"
crName := "db-testing"
crId := "db_testing"

f := mocks.NewFake(t, ns)
f.AddMockedUnstructuredCSV("csv")
Expand All @@ -26,47 +42,93 @@ func TestRetriever(t *testing.T) {
cr, err := mocks.UnstructuredDatabaseCRMock(backingServiceNs, crName)
require.NoError(t, err)

crInSameNamespace, err := mocks.UnstructuredDatabaseCRMock(ns, crName)
require.NoError(t, err)
fakeDynClient := f.FakeDynClient()

crId := "db_testing"
crInSameNamespaceId := "db_testing_other_ns"
serviceCtxs := serviceContextList{
type testCase struct {
dataMapping []corev1.EnvVar
envVarPrefix string
expected map[string][]byte
name string
svcCtxs serviceContextList
}

testCases := []testCase{
{
service: cr,
id: &crId,
name: "access with index should return correct value",
envVarPrefix: "SERVICE_BINDING",
svcCtxs: serviceContextList{
{service: cr},
},
dataMapping: []corev1.EnvVar{
{Name: "SAME_NAMESPACE", Value: toIndexTemplate(cr, "metadata.name")},
},
expected: map[string][]byte{
"SERVICE_BINDING_SAME_NAMESPACE": []byte(cr.GetName()),
},
},
{
service: crInSameNamespace,
id: &crInSameNamespaceId,
name: "direct access with apiVersion and kind should return correct value",
envVarPrefix: "SERVICE_BINDING",
svcCtxs: serviceContextList{
{service: cr},
},
dataMapping: []corev1.EnvVar{
{
Name: "DIRECT_ACCESS",
Value: `{{ .v1alpha1.postgresql_baiju_dev.Database.db_testing.metadata.name }}`,
},
},
expected: map[string][]byte{
"SERVICE_BINDING_DIRECT_ACCESS": []byte(cr.GetName()),
},
},
{
name: "direct access with declared id should return correct value",
envVarPrefix: "SERVICE_BINDING",
svcCtxs: serviceContextList{
{
service: cr,
id: &crId,
},
},
dataMapping: []corev1.EnvVar{
{
Name: "ID_ACCESS",
Value: `{{ .db_testing.metadata.name }}`,
},
},
expected: map[string][]byte{
"SERVICE_BINDING_ID_ACCESS": []byte(cr.GetName()),
},
},
{
name: "direct access without declared id should return <no value>",
envVarPrefix: "SERVICE_BINDING",
svcCtxs: serviceContextList{
{
service: cr,
},
},
dataMapping: []corev1.EnvVar{
{
Name: "ID_ACCESS",
Value: `{{ .db_testing.metadata.name }}`,
},
},
expected: map[string][]byte{
"SERVICE_BINDING_ID_ACCESS": []byte("<no value>"),
},
},
}

fakeDynClient := f.FakeDynClient()

toTmpl := func(obj *unstructured.Unstructured) string {
gvk := obj.GetObjectKind().GroupVersionKind()
name := obj.GetName()
return fmt.Sprintf(`{{ index . %q %q %q %q "metadata" "name" }}`, gvk.Version, gvk.Group, gvk.Kind, name)
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got, _, err := NewRetriever(fakeDynClient).ProcessServiceContexts(
tc.envVarPrefix, tc.svcCtxs, tc.dataMapping)
require.NoError(t, err)
require.Equal(t, tc.expected, got)
})
}

actual, _, err := NewRetriever(fakeDynClient).ProcessServiceContexts(
"SERVICE_BINDING",
serviceCtxs,
[]v1.EnvVar{
{Name: "SAME_NAMESPACE", Value: toTmpl(crInSameNamespace)},
{Name: "OTHER_NAMESPACE", Value: toTmpl(cr)},
{Name: "DIRECT_ACCESS", Value: `{{ .v1alpha1.postgresql_baiju_dev.Database.db_testing.metadata.name }}`},
{Name: "ID_ACCESS", Value: `{{ .db_testing.metadata.name }}`},
},
)
require.NoError(t, err)
require.Equal(t, map[string][]byte{
"SERVICE_BINDING_SAME_NAMESPACE": []byte(crInSameNamespace.GetName()),
"SERVICE_BINDING_OTHER_NAMESPACE": []byte(cr.GetName()),
"SERVICE_BINDING_DIRECT_ACCESS": []byte(cr.GetName()),
"SERVICE_BINDING_ID_ACCESS": []byte(cr.GetName()),
}, actual)
}

func TestBuildServiceEnvVars(t *testing.T) {
Expand Down

0 comments on commit 24e3e84

Please sign in to comment.