Skip to content

Commit

Permalink
Unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
feloy committed Jun 2, 2021
1 parent 5cdf778 commit 09ca0a7
Show file tree
Hide file tree
Showing 2 changed files with 225 additions and 6 deletions.
12 changes: 6 additions & 6 deletions pkg/odo/cli/service/list_operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type clusterInfo struct {

type serviceItem struct {
ClusterInfo *clusterInfo
DevfileInfo *string
InDevfile bool
}

// listOperatorServices lists the operator backed services
Expand Down Expand Up @@ -105,12 +105,12 @@ func mixServices(clusterList []unstructured.Unstructured, devfileList []string)
}
}

for i, item := range devfileList {
for _, item := range devfileList {
name := item
if _, ok := servicesItems[name]; !ok {
servicesItems[name] = &serviceItem{}
}
servicesItems[name].DevfileInfo = &devfileList[i]
servicesItems[name].InDevfile = true
}
return

Expand All @@ -131,7 +131,7 @@ func getOrderedServicesNames(servicesItems map[string]*serviceItem) (orderedName
// getTabularInfo returns information to be displayed in the output for a specific service and a specific current devfile component
func getTabularInfo(serviceItem *serviceItem, devfileComponent string) (managedByOdo, state, duration string) {
clusterItem := serviceItem.ClusterInfo
devfileItem := serviceItem.DevfileInfo
inDevfile := serviceItem.InDevfile
if clusterItem != nil {
// service deployed into cluster
var component string
Expand All @@ -144,7 +144,7 @@ func getTabularInfo(serviceItem *serviceItem, devfileComponent string) (managedB
managedByOdo = "No"
}
duration = time.Since(clusterItem.CreationTimestamp).Truncate(time.Second).String()
if devfileItem != nil {
if inDevfile {
// service deployed into cluster and defined in devfile
state = "Pushed"
} else {
Expand All @@ -160,7 +160,7 @@ func getTabularInfo(serviceItem *serviceItem, devfileComponent string) (managedB
}
}
} else {
if devfileItem != nil {
if inDevfile {
// service not deployed into cluster and defined in devfile
state = "Not pushed"
managedByOdo = fmt.Sprintf("Yes (%s)", devfileComponent)
Expand Down
219 changes: 219 additions & 0 deletions pkg/odo/cli/service/list_operator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
package service

import (
"reflect"
"testing"
"time"

"github.com/ghodss/yaml"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

func TestGetOrderedServicesNames(t *testing.T) {
tests := []struct {
name string
services map[string]*serviceItem
want []string
}{
{
name: "Unordered names",
services: map[string]*serviceItem{
"name3": nil,
"name1": nil,
"name2": nil,
},
want: []string{"name1", "name2", "name3"},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := getOrderedServicesNames(tt.services)
if !reflect.DeepEqual(result, tt.want) {
t.Errorf("Failed %s: got: %q, want: %q", t.Name(), result, tt.want)
}
})
}
}

type getTabularInfoResult struct {
managedByOdo string
state string
durationContent bool
}

func TestGetTabularInfo(t *testing.T) {

tests := []struct {
name string
service *serviceItem
devfileComponent string
want getTabularInfoResult
}{
{
name: "case 1: service in cluster managed by current devfile",
service: &serviceItem{
ClusterInfo: &clusterInfo{
Labels: map[string]string{
"app.kubernetes.io/managed-by": "odo",
"app.kubernetes.io/instance": "component1",
},
},
InDevfile: true,
},
devfileComponent: "component1",
want: getTabularInfoResult{
managedByOdo: "Yes (component1)",
state: "Pushed",
durationContent: true,
},
},
{
name: "case 2: service in cluster not managed by Odo",
service: &serviceItem{
ClusterInfo: &clusterInfo{
Labels: map[string]string{},
},
InDevfile: false,
},
devfileComponent: "component1",
want: getTabularInfoResult{
managedByOdo: "No",
state: "",
durationContent: true,
},
},
{
name: "case 3: service in cluster absent from current devfile",
service: &serviceItem{
ClusterInfo: &clusterInfo{
Labels: map[string]string{
"app.kubernetes.io/managed-by": "odo",
"app.kubernetes.io/instance": "component1",
},
},
InDevfile: false,
},
devfileComponent: "component1",
want: getTabularInfoResult{
managedByOdo: "Yes (component1)",
state: "Deleted locally",
durationContent: true,
},
},
{
name: "case 4: service in cluster maaged by another devfile",
service: &serviceItem{
ClusterInfo: &clusterInfo{
Labels: map[string]string{
"app.kubernetes.io/managed-by": "odo",
"app.kubernetes.io/instance": "component2",
},
},
InDevfile: false,
},
devfileComponent: "component1",
want: getTabularInfoResult{
managedByOdo: "Yes (component2)",
state: "",
durationContent: true,
},
},
{
name: "case 5: service defined in devfile, not in cluster",
service: &serviceItem{
ClusterInfo: nil,
InDevfile: true,
},
devfileComponent: "component1",
want: getTabularInfoResult{
managedByOdo: "Yes (component1)",
state: "Not pushed",
durationContent: false,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
managedByOdo, state, duration := getTabularInfo(tt.service, tt.devfileComponent)
if managedByOdo != tt.want.managedByOdo {
t.Errorf("Failed %s: managedByOdo got: %q, want: %q", t.Name(), managedByOdo, tt.want.managedByOdo)
}
if state != tt.want.state {
t.Errorf("Failed %s: state got: %q, want: %q", t.Name(), state, tt.want.state)
}
if len(duration) > 0 != tt.want.durationContent {
t.Errorf("Failed %s: duration content got: %v, want: %v", t.Name(), len(duration) > 0, tt.want.durationContent)
}
})
}
}

func TestMixServices(t *testing.T) {
atime, _ := time.Parse(time.RFC3339, "2021-06-02T08:39:20Z00:00")
tests := []struct {
name string
clusterListInlined []string
devfileList []string
want map[string]*serviceItem
}{
{
name: "two in cluster and two in devfile, including one in common",
clusterListInlined: []string{`
kind: kind1
metadata:
name: name1
labels:
app.kubernetes.io/managed-by: odo
app.kubernetes.io/instance: component1
creationTimestamp: 2021-06-02T08:39:20Z00:00`,
`
kind: kind2
metadata:
name: name2
labels:
app.kubernetes.io/managed-by: odo
app.kubernetes.io/instance: component2
creationTimestamp: 2021-06-02T08:39:20Z00:00`},
devfileList: []string{"kind1/name1", "kind3/name3"},
want: map[string]*serviceItem{
"kind1/name1": {
ClusterInfo: &clusterInfo{
Labels: map[string]string{
"app.kubernetes.io/managed-by": "odo",
"app.kubernetes.io/instance": "component1",
},
CreationTimestamp: atime,
},
InDevfile: true,
},
"kind2/name2": {
ClusterInfo: &clusterInfo{
Labels: map[string]string{
"app.kubernetes.io/managed-by": "odo",
"app.kubernetes.io/instance": "component2",
},
CreationTimestamp: atime,
},
InDevfile: false,
},
"kind3/name3": {
ClusterInfo: nil,
InDevfile: true,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
us := make([]unstructured.Unstructured, len(tt.clusterListInlined))
for i, clusterInlined := range tt.clusterListInlined {
yaml.Unmarshal([]byte(clusterInlined), &us[i])
}
result := mixServices(us, tt.devfileList)
if !reflect.DeepEqual(result, tt.want) {
t.Errorf("Failed %s", t.Name())
}
})
}
}

0 comments on commit 09ca0a7

Please sign in to comment.