Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions k8s/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package k8s

import (
"fmt"
"testing"

"github.com/arschles/assert"
"github.com/juju/loggo"
)

func init() {
logger.SetLogLevel(loggo.TRACE)
}

func TestResourceAPIVersion(t *testing.T) {
const (
ver = "testver"
)
assert.Equal(t, resourceAPIVersion(ver), fmt.Sprintf("%s/%s", resourceAPIVersionBase, ver), "api version")
}
45 changes: 45 additions & 0 deletions k8s/service_catalog_entry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package k8s

import (
"fmt"
"testing"

"github.com/arschles/assert"
"github.com/deis/steward/mode"
"github.com/pborman/uuid"
"k8s.io/client-go/1.4/pkg/api"
)

func TestNewServiceCatalogEntry(t *testing.T) {
brokerName := "testBroker"
objectMeta := api.ObjectMeta{Name: "testOM", Namespace: "testNS"}
info := mode.ServiceInfo{Name: "testServiceInfo", ID: uuid.New(), Description: "testDescrInfo"}
plan := mode.ServicePlan{Name: "testPlan", ID: uuid.New(), Description: "testDescrPlan"}

entry := NewServiceCatalogEntry(brokerName, objectMeta, info, plan)
assert.Equal(t, entry.TypeMeta.Kind, ServiceCatalogEntryKind, "kind")
assert.Equal(t, entry.TypeMeta.APIVersion, resourceAPIVersion(apiVersionV1), "API version")
assert.Equal(t, entry.ObjectMeta.Name, fmt.Sprintf("%s-%s-%s", brokerName, info.Name, plan.Name), "object meta name")
assert.Equal(t, entry.ObjectMeta.Labels["broker"], brokerName, "broker name label")
assert.Equal(t, entry.ObjectMeta.Labels["service-id"], info.ID, "service ID label")
assert.Equal(t, entry.ObjectMeta.Labels["plan-id"], plan.ID, "plan ID label")
assert.Equal(t, entry.ObjectMeta.Labels["plan-name"], plan.Name, "plan name label")
assert.Equal(t, entry.Info, info, "service info")
assert.Equal(t, entry.Plan, plan, "service plan")
assert.Equal(t, entry.Description, fmt.Sprintf("%s (%s)", info.Description, plan.Description), "service description")
}

func TestCanonicalize(t *testing.T) {
strs := []string{
"a_b_c",
"a.b.c",
"a:b:c",
"a/b/c",
`a\b\c`,
"a b c",
}
for _, str := range strs {
cleaned := canonicalize(str)
assert.Equal(t, cleaned, "a-b-c", "canonicalized string")
}
}
1 change: 1 addition & 0 deletions k8s/service_catalog_lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (scl *ServiceCatalogLookup) Set(entry *ServiceCatalogEntry) {
scl.lookup[catalogKey(entry.Info.ID, entry.Plan.ID)] = entry
}

// Len gets the number of items in scl
func (scl *ServiceCatalogLookup) Len() int {
return len(scl.lookup)
}
Expand Down
47 changes: 47 additions & 0 deletions k8s/service_catalog_lookup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,55 @@ import (
"github.com/arschles/assert"
"github.com/deis/steward/mode"
"github.com/pborman/uuid"
"k8s.io/client-go/1.4/pkg/api"
)

func TestFetchServiceCatalogLookup(t *testing.T) {
iface := &FakeServiceCatalogInteractor{
ListRet: &ServiceCatalogEntryList{},
}
lookup, err := FetchServiceCatalogLookup(iface)
assert.NoErr(t, err)
assert.Equal(t, lookup.Len(), 0, "number of items in the catalog")
iface.ListRet.Items = []*ServiceCatalogEntry{
NewServiceCatalogEntry(
"testBroker1",
api.ObjectMeta{},
mode.ServiceInfo{
ID: uuid.New(),
Name: "testSvc1",
},
mode.ServicePlan{
ID: uuid.New(),
Name: "testPlan1",
},
),
NewServiceCatalogEntry(
"testBroker2",
api.ObjectMeta{},
mode.ServiceInfo{
ID: uuid.New(),
Name: "testSvc2",
},
mode.ServicePlan{
ID: uuid.New(),
Name: "testPlan2",
},
),
}
lookup, err = FetchServiceCatalogLookup(iface)
assert.NoErr(t, err)
assert.Equal(t, lookup.Len(), len(iface.ListRet.Items), "number of items in the catalog")
for _, entry := range iface.ListRet.Items {
assert.Equal(
t,
lookup.Get(entry.Info.ID, entry.Plan.ID),
entry,
fmt.Sprintf("service %s, plan %s", entry.Info.ID, entry.Plan.ID),
)
}
}

func TestServiceCatalogLookupCatalogKey(t *testing.T) {
svcID := uuid.New()
planID := uuid.New()
Expand Down
19 changes: 19 additions & 0 deletions k8s/service_plan_claim_action_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package k8s

import (
"testing"

"github.com/arschles/assert"
)

func TestStringIsServicePlanClaimAction(t *testing.T) {
assert.True(
t,
StringIsServicePlanClaimAction("testaction", ServicePlanClaimAction("testaction")),
"'testaction' was not reported as the equivalent ServicePlanClaimAction",
)
}

func TestServicePlanClaimActionStringer(t *testing.T) {
assert.Equal(t, ServicePlanClaimAction("testaction").String(), "testaction", "string value")
}
2 changes: 1 addition & 1 deletion k8s/service_plan_claim_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const (
StatusFailed ServicePlanClaimStatus = "failed"
)

// Status is the type representing the current status of a claim. It implements fmt.Stringer
// ServicePlanClaimStatus is the type representing the current status of a claim. It implements fmt.Stringer
type ServicePlanClaimStatus string

// StringIsStatus returns true if s == st.String()
Expand Down
19 changes: 19 additions & 0 deletions k8s/service_plan_claim_status_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package k8s

import (
"testing"

"github.com/arschles/assert"
)

func TestStringIsStatus(t *testing.T) {
assert.True(
t,
StringIsStatus("teststatus", ServicePlanClaimStatus("teststatus")),
"equivalent statuses were not reported equal",
)
}

func TestServicePlanClaimStringer(t *testing.T) {
assert.Equal(t, ServicePlanClaimStatus("teststatus").String(), "teststatus", "status string")
}
80 changes: 80 additions & 0 deletions k8s/service_plan_claim_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package k8s

import (
"strings"
"testing"

"github.com/arschles/assert"
"github.com/deis/steward/mode"
"github.com/pborman/uuid"
)

type jsonObjectEqualer mode.JSONObject

func (j jsonObjectEqualer) Equal(e assert.Equaler) bool {
je, ok := e.(jsonObjectEqualer)
if !ok {
return false
}
thisObj := mode.JSONObject(j)
otherObj := mode.JSONObject(je)
if len(thisObj) != len(otherObj) {
return false
}

for key, val := range thisObj {
if val != otherObj[key] {
return false
}
}
return true
}

type servicePlanClaimEqualer ServicePlanClaim

func (s servicePlanClaimEqualer) Equal(e assert.Equaler) bool {
thisObj := ServicePlanClaim(s)
spce, ok := e.(servicePlanClaimEqualer)
if !ok {
return false
}
otherObj := ServicePlanClaim(spce)
if thisObj.TargetName != otherObj.TargetName ||
thisObj.ServiceID != otherObj.ServiceID ||
thisObj.PlanID != otherObj.PlanID ||
thisObj.ClaimID != otherObj.ClaimID ||
thisObj.Action != otherObj.Action ||
thisObj.Status != otherObj.Status ||
thisObj.StatusDescription != otherObj.StatusDescription ||
thisObj.InstanceID != otherObj.InstanceID ||
thisObj.BindID != otherObj.BindID ||
!jsonObjectEqualer(thisObj.Extra).Equal(jsonObjectEqualer(otherObj.Extra)) {
return false
}
return true
}

func TestErrDataMapMissingKey(t *testing.T) {
err := errDataMapMissingKey{key: "testKey"}
assert.True(t, strings.Contains(err.Error(), err.key), "error string didn't contain error key %s", err.key)
}

func TestServicePlanClaimMapRoundTrip(t *testing.T) {
claim := ServicePlanClaim{
TargetName: "testTarget",
ServiceID: uuid.New(),
PlanID: uuid.New(),
ClaimID: uuid.New(),
Action: "testAction",
Status: "testStatus",
StatusDescription: "testStatusDescription",
InstanceID: uuid.New(),
BindID: uuid.New(),
Extra: mode.JSONObject(map[string]string{"key1": "val1"}),
}
m := claim.ToMap()
parsedClaim, err := ServicePlanClaimFromMap(m)
assert.NoErr(t, err)
// note that servicePlanClaimEqualer is an instance of assert.Equals, and assert.Equal checks for instances of that type (https://godoc.org/github.com/arschles/assert#Equaler). This feature allows us to define what "deep equals" means (instead of using reflect.DeepEqual)
assert.Equal(t, servicePlanClaimEqualer(*parsedClaim), servicePlanClaimEqualer(claim), "parsed claim")
}