Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update unit tests to check error messages #107

Merged
merged 4 commits into from
Jul 15, 2021
Merged
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/devfile/library
go 1.13

require (
github.com/devfile/api/v2 v2.0.0-20210630155427-a72fb9eb4afa
github.com/devfile/api/v2 v2.0.0-20210713153530-f78ab9de1a30
github.com/fatih/color v1.7.0
github.com/gobwas/glob v0.2.3
github.com/golang/mock v1.5.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7Do
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/devfile/api/v2 v2.0.0-20210630155427-a72fb9eb4afa h1:Emb+OilQp9eLktgIKc/Kwq5erHwoDL7PbbAEvQDEJMk=
github.com/devfile/api/v2 v2.0.0-20210630155427-a72fb9eb4afa/go.mod h1:QNzaIVQnCsYfXed+QZOn1uvEQFzyhvpi/uc3g/b2ws0=
github.com/devfile/api/v2 v2.0.0-20210713153530-f78ab9de1a30 h1:DL1o2U9NuS2KpVHva5h3tbIReJfF1LkSTJFj+JRiqJg=
github.com/devfile/api/v2 v2.0.0-20210713153530-f78ab9de1a30/go.mod h1:QNzaIVQnCsYfXed+QZOn1uvEQFzyhvpi/uc3g/b2ws0=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/docker/docker v0.7.3-0.20190327010347-be7ac8be2ae0/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
Expand Down
73 changes: 44 additions & 29 deletions pkg/devfile/generator/generators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package generator

import (
"fmt"
"github.com/stretchr/testify/assert"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -47,6 +48,8 @@ func TestGetContainers(t *testing.T) {
},
}

errMatches := "an expected error"

tests := []struct {
name string
containerComponents []v1.Component
Expand All @@ -56,7 +59,7 @@ func TestGetContainers(t *testing.T) {
wantContainerImage string
wantContainerEnv []corev1.EnvVar
wantContainerVolMount []corev1.VolumeMount
wantErr bool
wantErr *string
}{
{
name: "Container with default project root",
Expand Down Expand Up @@ -202,6 +205,10 @@ func TestGetContainers(t *testing.T) {
},
},
},
{
name: "Simulating error case, check if error matches",
wantErr: &errMatches,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -221,6 +228,9 @@ func TestGetContainers(t *testing.T) {
} else {
mockGetComponents.Return(tt.filteredComponents, nil).AnyTimes()
}
if tt.wantErr != nil {
mockGetComponents.Return(nil, fmt.Errorf(*tt.wantErr))
}
mockDevfileData.EXPECT().GetProjects(common.DevfileOptions{}).Return(projects, nil).AnyTimes()

devObj := parser.DevfileObj{
Expand All @@ -229,23 +239,25 @@ func TestGetContainers(t *testing.T) {

containers, err := GetContainers(devObj, tt.filterOptions)
// Unexpected error
if (err != nil) != tt.wantErr {
t.Errorf("TestGetContainers() error = %v, wantErr %v", err, tt.wantErr)
if (err != nil) != (tt.wantErr != nil) {
t.Errorf("TestGetContainers() error: %v, wantErr %v", err, tt.wantErr)
} else if err == nil {
for _, container := range containers {
if container.Name != tt.wantContainerName {
t.Errorf("TestGetContainers error: Name mismatch - got: %s, wanted: %s", container.Name, tt.wantContainerName)
t.Errorf("TestGetContainers() error: Name mismatch - got: %s, wanted: %s", container.Name, tt.wantContainerName)
}
if container.Image != tt.wantContainerImage {
t.Errorf("TestGetContainers error: Image mismatch - got: %s, wanted: %s", container.Image, tt.wantContainerImage)
t.Errorf("TestGetContainers() error: Image mismatch - got: %s, wanted: %s", container.Image, tt.wantContainerImage)
}
if len(container.Env) > 0 && !reflect.DeepEqual(container.Env, tt.wantContainerEnv) {
t.Errorf("TestGetContainers error: Env mismatch - got: %+v, wanted: %+v", container.Env, tt.wantContainerEnv)
t.Errorf("TestGetContainers() error: Env mismatch - got: %+v, wanted: %+v", container.Env, tt.wantContainerEnv)
}
if len(container.VolumeMounts) > 0 && !reflect.DeepEqual(container.VolumeMounts, tt.wantContainerVolMount) {
t.Errorf("TestGetContainers error: Vol Mount mismatch - got: %+v, wanted: %+v", container.VolumeMounts, tt.wantContainerVolMount)
t.Errorf("TestGetContainers() error: Vol Mount mismatch - got: %+v, wanted: %+v", container.VolumeMounts, tt.wantContainerVolMount)
}
}
} else {
assert.Regexp(t, *tt.wantErr, err.Error(), "TestGetContainers(): Error message does not match")
}
})
}
Expand All @@ -259,12 +271,14 @@ func TestGetVolumesAndVolumeMounts(t *testing.T) {
volumeName string
}

errMatches := "an expected error"

tests := []struct {
name string
components []v1.Component
volumeNameToVolInfo map[string]VolumeInfo
wantContainerToVol map[string][]testVolumeMountInfo
wantErr bool
wantErr *string
}{
{
name: "One volume mounted",
Expand All @@ -289,7 +303,6 @@ func TestGetVolumesAndVolumeMounts(t *testing.T) {
},
},
},
wantErr: false,
},
{
name: "One volume mounted at diff locations",
Expand Down Expand Up @@ -332,7 +345,6 @@ func TestGetVolumesAndVolumeMounts(t *testing.T) {
},
},
},
wantErr: false,
},
{
name: "One volume mounted at diff container components",
Expand Down Expand Up @@ -388,12 +400,10 @@ func TestGetVolumesAndVolumeMounts(t *testing.T) {
},
},
},
wantErr: false,
},
{
name: "Invalid case simulating no container components",
components: nil,
wantErr: true,
name: "Simulating error case, check if error matches",
wantErr: &errMatches,
},
}

Expand Down Expand Up @@ -424,9 +434,9 @@ func TestGetVolumesAndVolumeMounts(t *testing.T) {
return
}

if tt.wantErr {
if tt.wantErr != nil {
// simulate error condition
mockGetComponents.Return(nil, fmt.Errorf("mock error"))
mockGetComponents.Return(nil, fmt.Errorf(*tt.wantErr))

}

Expand All @@ -436,8 +446,8 @@ func TestGetVolumesAndVolumeMounts(t *testing.T) {
}

pvcVols, err := GetVolumesAndVolumeMounts(devObj, volumeParams, common.DevfileOptions{})
if tt.wantErr == (err == nil) {
t.Errorf("TestGetVolumesAndVolumeMounts() error = %v, wantErr %v", err, tt.wantErr)
if (err != nil) != (tt.wantErr != nil) {
t.Errorf("TestGetVolumesAndVolumeMounts() error: %v, wantErr %v", err, tt.wantErr)
} else if err == nil {
// check if the pvc volumes returned are correct
for _, volInfo := range tt.volumeNameToVolInfo {
Expand All @@ -449,14 +459,14 @@ func TestGetVolumesAndVolumeMounts(t *testing.T) {
}

if !matched {
t.Errorf("TestGetVolumesAndVolumeMounts error - could not find volume details %s in the actual result", volInfo.VolumeName)
t.Errorf("TestGetVolumesAndVolumeMounts() error: could not find volume details %s in the actual result", volInfo.VolumeName)
}
}

// check the volume mounts of the containers
for _, container := range containers {
if volMounts, ok := tt.wantContainerToVol[container.Name]; !ok {
t.Errorf("TestGetVolumesAndVolumeMounts error - did not find the expected container %s", container.Name)
t.Errorf("TestGetVolumesAndVolumeMounts() error: did not find the expected container %s", container.Name)
return
} else {
for _, expectedVolMount := range volMounts {
Expand All @@ -468,11 +478,13 @@ func TestGetVolumesAndVolumeMounts(t *testing.T) {
}

if !matched {
t.Errorf("TestGetVolumesAndVolumeMounts error - could not find volume mount details for path %s in the actual result for container %s", expectedVolMount.mountPath, container.Name)
t.Errorf("TestGetVolumesAndVolumeMounts() error: could not find volume mount details for path %s in the actual result for container %s", expectedVolMount.mountPath, container.Name)
}
}
}
}
} else {
assert.Regexp(t, *tt.wantErr, err.Error(), "TestGetVolumesAndVolumeMounts(): Error message does not match")
}
})
}
Expand Down Expand Up @@ -506,7 +518,7 @@ func TestGetVolumeMountPath(t *testing.T) {
path := GetVolumeMountPath(tt.volumeMount)

if path != tt.wantPath {
t.Errorf("TestGetVolumeMountPath error: mount path mismatch, expected: %v got: %v", tt.wantPath, path)
t.Errorf("TestGetVolumeMountPath() error: mount path mismatch, expected: %v got: %v", tt.wantPath, path)
}
})
}
Expand Down Expand Up @@ -584,12 +596,14 @@ func TestGetInitContainers(t *testing.T) {
longContainerName := "thisisaverylongcontainerandkuberneteshasalimitforanamesize-exec2"
trimmedLongContainerName := util.TruncateString(longContainerName, containerNameMaxLen)

errMatches := "an expected error"

tests := []struct {
name string
eventCommands []string
wantInitContainer map[string]corev1.Container
longName bool
wantErr bool
wantErr *string
}{
{
name: "Composite and Exec events",
Expand All @@ -611,13 +625,13 @@ func TestGetInitContainers(t *testing.T) {
},
},
{
name: "Simulate error condition",
name: "Simulate error case, check if error matches",
eventCommands: []string{
"apply1",
"apply3",
"apply2",
},
wantErr: true,
wantErr: &errMatches,
},
{
name: "Long Container Name",
Expand Down Expand Up @@ -662,18 +676,19 @@ func TestGetInitContainers(t *testing.T) {
mockDevfileData.EXPECT().GetEvents().Return(preStartEvents).AnyTimes()
mockGetCommands.Return(append(applyCommands, compCommands...), nil).AnyTimes()

if tt.wantErr {
mockGetCommands.Return(nil, fmt.Errorf("mock error")).AnyTimes()
if tt.wantErr != nil {
mockGetCommands.Return(nil, fmt.Errorf(*tt.wantErr)).AnyTimes()
}

devObj := parser.DevfileObj{
Data: mockDevfileData,
}

initContainers, err := GetInitContainers(devObj)
if (err != nil) != tt.wantErr {
t.Errorf("TestGetInitContainers() error = %v, wantErr %v", err, tt.wantErr)
if (err != nil) != (tt.wantErr != nil) {
t.Errorf("TestGetInitContainers() error: %v, wantErr %v", err, tt.wantErr)
} else if err != nil {
assert.Regexp(t, *tt.wantErr, err.Error(), "TestGetInitContainers: Error message does not match")
return
}

Expand Down
Loading