Skip to content

Commit

Permalink
Add unit test to TestGetContainers and enhance Test_containerOverride…
Browse files Browse the repository at this point in the history
…sHandler to check for error strings

Signed-off-by: Parthvi Vala <pvala@redhat.com>
  • Loading branch information
valaparthvi committed Dec 7, 2022
1 parent 6d90363 commit c0fab84
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 18 deletions.
63 changes: 53 additions & 10 deletions pkg/devfile/generator/generators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ func TestGetContainers(t *testing.T) {

containerNames := []string{"testcontainer1", "testcontainer2", "testcontainer3"}
containerImages := []string{"image1", "image2", "image3"}
defaultPullPolicy := corev1.PullAlways
defaultEnv := []corev1.EnvVar{
{Name: "PROJECTS_ROOT", Value: "/projects"},
{Name: "PROJECT_SOURCE", Value: "/projects/test-project"},
}

trueMountSources := true
falseMountSources := false

Expand Down Expand Up @@ -94,16 +100,17 @@ func TestGetContainers(t *testing.T) {
}

tests := []struct {
name string
eventCommands EventCommands
containerComponents []v1.Component
filteredComponents []v1.Component
filterOptions common.DevfileOptions
wantContainerName string
wantContainerImage string
wantContainerEnv []corev1.EnvVar
wantContainerVolMount []corev1.VolumeMount
wantErr *string
name string
eventCommands EventCommands
containerComponents []v1.Component
filteredComponents []v1.Component
filterOptions common.DevfileOptions
wantContainerName string
wantContainerImage string
wantContainerEnv []corev1.EnvVar
wantContainerVolMount []corev1.VolumeMount
wantContainerOverrideData *corev1.Container
wantErr *string
}{
{
name: "Container with default project root",
Expand Down Expand Up @@ -297,6 +304,39 @@ func TestGetContainers(t *testing.T) {
name: "Simulating error case, check if error matches",
wantErr: &errMatches,
},
{
name: "container with container-overrides",
containerComponents: []v1.Component{
{
Name: containerNames[0],
ComponentUnion: v1.ComponentUnion{
Container: &v1.ContainerComponent{
Container: v1.Container{
Image: containerImages[0],
},
},
},
Attributes: attributes.Attributes{}.FromMap(map[string]interface{}{
"container-overrides": map[string]interface{}{"securityContext": map[string]int64{"runAsGroup": 3000}},
}, nil),
// Attributes: attributes.Attributes{
// "container-overrides": apiextensionsv1.JSON{Raw: []byte("{\"spec\": {\"securityContext\": {\"runAsGroup\": \"3000\"}}}")},
// },
},
},
wantContainerName: containerNames[0],
wantContainerImage: containerImages[0],
wantContainerEnv: defaultEnv,
wantContainerOverrideData: &corev1.Container{
Name: containerNames[0],
Image: containerImages[0],
Env: defaultEnv,
ImagePullPolicy: defaultPullPolicy,
SecurityContext: &corev1.SecurityContext{
RunAsGroup: pointer.Int64(3000),
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -354,6 +394,9 @@ func TestGetContainers(t *testing.T) {
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)
}
if tt.wantContainerOverrideData != nil && !reflect.DeepEqual(container, *tt.wantContainerOverrideData) {
t.Errorf("TestGetContainers() error: Container override mismatch - got: %+v, wanted: %+v", container, tt.wantContainerOverrideData)
}
}
} else {
assert.Regexp(t, *tt.wantErr, err.Error(), "TestGetContainers(): Error message does not match")
Expand Down
20 changes: 12 additions & 8 deletions pkg/devfile/generator/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1737,10 +1737,11 @@ func Test_containerOverridesHandler(t *testing.T) {
container *corev1.Container
}
tests := []struct {
name string
args args
want *corev1.Container
wantErr bool
name string
args args
want *corev1.Container
wantErr bool
errString string
}{
{
name: "Override the resource requirements of the container component",
Expand Down Expand Up @@ -1815,8 +1816,9 @@ func Test_containerOverridesHandler(t *testing.T) {
},
container: getContainer(containerParams{Name: name, Image: image, Command: command, Args: argsSlice}),
},
want: nil,
wantErr: true,
want: nil,
wantErr: true,
errString: "cannot use container-overrides to override container name, image, command, args, ports, volumeMounts, env",
},
{
name: "Invalid JSON for container-overrides",
Expand All @@ -1828,15 +1830,17 @@ func Test_containerOverridesHandler(t *testing.T) {
},
container: getContainer(containerParams{Name: name, Image: image, Command: command, Args: argsSlice}),
},
want: nil,
wantErr: true,
want: nil,
wantErr: true,
errString: "failed to parse container-overrides attribute on component component3",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := containerOverridesHandler(tt.args.comp, tt.args.container)
if tt.wantErr {
assert.NotNil(t, err, tt.name)
assert.Contains(t, err.Error(), tt.errString, "containerOverridesHandler() error does not match")
} else {
assert.Nil(t, err, tt.name)
}
Expand Down

0 comments on commit c0fab84

Please sign in to comment.