13
13
package storage
14
14
15
15
import (
16
- "fmt"
17
16
"io/ioutil"
18
17
"path/filepath"
19
18
"testing"
@@ -54,29 +53,35 @@ func setupControllerCfg() {
54
53
config .SetupConfigForTesting (testControllerCfg )
55
54
}
56
55
57
- func loadTestCaseOrPanic (t * testing.T , testFilename string ) testCase {
58
- testPath := filepath .Join ("./testdata" , testFilename )
59
- bytes , err := ioutil .ReadFile (testPath )
56
+ func loadTestCaseOrPanic (t * testing.T , testFilepath string ) testCase {
57
+ bytes , err := ioutil .ReadFile (testFilepath )
60
58
if err != nil {
61
59
t .Fatal (err )
62
60
}
63
61
var test testCase
64
62
if err := yaml .Unmarshal (bytes , & test ); err != nil {
65
63
t .Fatal (err )
66
64
}
67
- t .Log (fmt .Sprintf ("Read file:\n %+v\n \n " , test ))
68
65
return test
69
66
}
70
67
71
- func TestRewriteContainerVolumeMounts (t * testing.T ) {
72
- tests := []testCase {
73
- loadTestCaseOrPanic (t , "does-nothing-for-no-storage-needed.yaml" ),
74
- loadTestCaseOrPanic (t , "projects-volume-overriding.yaml" ),
75
- loadTestCaseOrPanic (t , "rewrites-volumes-for-common-pvc-strategy.yaml" ),
76
- loadTestCaseOrPanic (t , "error-duplicate-volumes.yaml" ),
77
- loadTestCaseOrPanic (t , "error-undefined-volume.yaml" ),
78
- loadTestCaseOrPanic (t , "error-undefined-volume-init-container.yaml" ),
68
+ func loadAllTestCasesOrPanic (t * testing.T , fromDir string ) []testCase {
69
+ files , err := ioutil .ReadDir (fromDir )
70
+ if err != nil {
71
+ t .Fatal (err )
79
72
}
73
+ var tests []testCase
74
+ for _ , file := range files {
75
+ if file .IsDir () {
76
+ continue
77
+ }
78
+ tests = append (tests , loadTestCaseOrPanic (t , filepath .Join (fromDir , file .Name ())))
79
+ }
80
+ return tests
81
+ }
82
+
83
+ func TestRewriteContainerVolumeMounts (t * testing.T ) {
84
+ tests := loadAllTestCasesOrPanic (t , "testdata" )
80
85
setupControllerCfg ()
81
86
82
87
for _ , tt := range tests {
@@ -97,60 +102,93 @@ func TestRewriteContainerVolumeMounts(t *testing.T) {
97
102
}
98
103
99
104
func TestNeedsStorage (t * testing.T ) {
100
- boolFalse := false
101
105
boolTrue := true
102
106
tests := []struct {
103
- Name string
104
- Explanation string
105
- Components []devworkspace.ComponentUnion
107
+ Name string
108
+ Explanation string
109
+ NeedsStorage bool
110
+ Components []devworkspace.Component
106
111
}{
107
112
{
108
- Name : "Has volume component" ,
109
- Explanation : "If the devfile has a volume component, it requires storage" ,
110
- Components : []devworkspace.ComponentUnion {
113
+ Name : "Has volume component" ,
114
+ Explanation : "If the devfile has a volume component, it requires storage" ,
115
+ NeedsStorage : true ,
116
+ Components : []devworkspace.Component {
117
+ {
118
+ ComponentUnion : devworkspace.ComponentUnion {
119
+ Volume : & devworkspace.VolumeComponent {},
120
+ },
121
+ },
122
+ },
123
+ },
124
+ {
125
+ Name : "Has ephemeral volume and does not need storage" ,
126
+ Explanation : "Volumes with ephemeral: true do not require storage" ,
127
+ NeedsStorage : false ,
128
+ Components : []devworkspace.Component {
111
129
{
112
- Volume : & devworkspace.VolumeComponent {},
130
+ ComponentUnion : devworkspace.ComponentUnion {
131
+ Volume : & devworkspace.VolumeComponent {
132
+ Volume : devworkspace.Volume {
133
+ Ephemeral : true ,
134
+ },
135
+ },
136
+ },
113
137
},
114
138
},
115
139
},
116
140
{
117
- Name : "Has container component with volume mounts" ,
118
- Explanation : "If a devfile container has volumeMounts, it requires storage" ,
119
- Components : []devworkspace.ComponentUnion {
141
+ Name : "Container has mountSources" ,
142
+ Explanation : "If a devfile container has mountSources set, it requires storage" ,
143
+ NeedsStorage : true ,
144
+ Components : []devworkspace.Component {
120
145
{
121
- Container : & devworkspace.ContainerComponent {
122
- Container : devworkspace.Container {
123
- MountSources : & boolFalse ,
124
- VolumeMounts : []devworkspace.VolumeMount {
125
- {
126
- Name : "test-volumeMount" ,
127
- },
146
+ ComponentUnion : devworkspace.ComponentUnion {
147
+ Container : & devworkspace.ContainerComponent {
148
+ Container : devworkspace.Container {
149
+ MountSources : & boolTrue ,
128
150
},
129
151
},
130
152
},
131
153
},
132
154
},
133
155
},
134
156
{
135
- Name : "Container has mountSources" ,
136
- Explanation : "If a devfile container has mountSources set, it requires storage" ,
137
- Components : []devworkspace.ComponentUnion {
157
+ Name : "Container has mountSources but projects is ephemeral" ,
158
+ Explanation : "When a devfile has an explicit, ephemeral projects volume, containers with mountSources do not need storage" ,
159
+ NeedsStorage : false ,
160
+ Components : []devworkspace.Component {
161
+ {
162
+ ComponentUnion : devworkspace.ComponentUnion {
163
+ Container : & devworkspace.ContainerComponent {
164
+ Container : devworkspace.Container {
165
+ MountSources : & boolTrue ,
166
+ },
167
+ },
168
+ },
169
+ },
138
170
{
139
- Container : & devworkspace.ContainerComponent {
140
- Container : devworkspace.Container {
141
- MountSources : & boolTrue ,
171
+ Name : "projects" ,
172
+ ComponentUnion : devworkspace.ComponentUnion {
173
+ Volume : & devworkspace.VolumeComponent {
174
+ Volume : devworkspace.Volume {
175
+ Ephemeral : true ,
176
+ },
142
177
},
143
178
},
144
179
},
145
180
},
146
181
},
147
182
{
148
- Name : "Container has implicit mountSources" ,
149
- Explanation : "If a devfile container does not have mountSources set, the default is true" ,
150
- Components : []devworkspace.ComponentUnion {
183
+ Name : "Container has implicit mountSources" ,
184
+ Explanation : "If a devfile container does not have mountSources set, the default is true" ,
185
+ NeedsStorage : true ,
186
+ Components : []devworkspace.Component {
151
187
{
152
- Container : & devworkspace.ContainerComponent {
153
- Container : devworkspace.Container {},
188
+ ComponentUnion : devworkspace.ComponentUnion {
189
+ Container : & devworkspace.ContainerComponent {
190
+ Container : devworkspace.Container {},
191
+ },
154
192
},
155
193
},
156
194
},
@@ -159,13 +197,12 @@ func TestNeedsStorage(t *testing.T) {
159
197
for _ , tt := range tests {
160
198
t .Run (tt .Name , func (t * testing.T ) {
161
199
workspace := devworkspace.DevWorkspaceTemplateSpec {}
162
- for idx , comp := range tt .Components {
163
- workspace . Components = append ( workspace . Components , devworkspace. Component {
164
- Name : fmt . Sprintf ( "test-component-%d" , idx ),
165
- ComponentUnion : comp ,
166
- } )
200
+ workspace . Components = tt .Components
201
+ if tt . NeedsStorage {
202
+ assert . True ( t , NeedsStorage ( workspace ), tt . Explanation )
203
+ } else {
204
+ assert . False ( t , NeedsStorage ( workspace ), tt . Explanation )
167
205
}
168
- assert .True (t , NeedsStorage (workspace ), tt .Explanation )
169
206
})
170
207
}
171
208
}
0 commit comments