1 package common
2
3 import (
4 "testing"
5
6 "github.com/devfile/library/v2/pkg/devfile/generator"
7
8 corev1 "k8s.io/api/core/v1"
9 )
10
11 func TestGetFirstContainerWithSourceVolume(t *testing.T) {
12 tests := []struct {
13 name string
14 containers []corev1.Container
15 want string
16 wantSourcePath string
17 wantErr bool
18 }{
19 {
20 name: "Case: One container, Project Source Env",
21 containers: []corev1.Container{
22 {
23 Name: "test",
24 Env: []corev1.EnvVar{
25 {
26 Name: "RANDOMENV",
27 Value: "/mypath2",
28 },
29 {
30 Name: generator.EnvProjectsSrc,
31 Value: "/mypath",
32 },
33 },
34 },
35 },
36 want: "test",
37 wantSourcePath: "/mypath",
38 wantErr: false,
39 },
40 {
41 name: "Case: Multiple containers, multiple Project Source Env",
42 containers: []corev1.Container{
43 {
44 Name: "test1",
45 Env: []corev1.EnvVar{
46 {
47 Name: "RANDOMENV",
48 Value: "/mypath1",
49 },
50 {
51 Name: generator.EnvProjectsSrc,
52 Value: "/mypath1",
53 },
54 },
55 },
56 {
57 Name: "test2",
58 Env: []corev1.EnvVar{
59 {
60 Name: "RANDOMENV",
61 Value: "/mypath2",
62 },
63 {
64 Name: generator.EnvProjectsSrc,
65 Value: "/mypath2",
66 },
67 },
68 },
69 },
70 want: "test1",
71 wantSourcePath: "/mypath1",
72 wantErr: false,
73 },
74 {
75 name: "Case: Multiple containers, no Project Source Env",
76 containers: []corev1.Container{
77 {
78 Name: "test1",
79 Env: []corev1.EnvVar{
80 {
81 Name: "RANDOMENV",
82 Value: "/mypath1",
83 },
84 },
85 },
86 {
87 Name: "test2",
88 Env: []corev1.EnvVar{
89 {
90 Name: "RANDOMENV",
91 Value: "/mypath2",
92 },
93 },
94 },
95 },
96 wantErr: true,
97 },
98 }
99 for _, tt := range tests {
100 t.Run(tt.name, func(t *testing.T) {
101 container, syncFolder, err := GetFirstContainerWithSourceVolume(tt.containers)
102 if container != tt.want {
103 t.Errorf("expected %s, actual %s", tt.want, container)
104 }
105 if syncFolder != tt.wantSourcePath {
106 t.Errorf("expected %s, actual %s", tt.wantSourcePath, syncFolder)
107 }
108 if !tt.wantErr == (err != nil) {
109 t.Errorf("expected %v, actual %v", tt.wantErr, err)
110 }
111 })
112 }
113 }
114
View as plain text