1 package storage
2
3 import (
4 "testing"
5
6 devfilev1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
7 "github.com/devfile/library/v2/pkg/devfile/parser"
8 "github.com/devfile/library/v2/pkg/devfile/parser/data"
9 "github.com/google/go-cmp/cmp"
10
11 "github.com/redhat-developer/odo/pkg/testingutil"
12 )
13
14 func TestEnvInfo_ListStorage(t *testing.T) {
15 type fields struct {
16 devfileObj parser.DevfileObj
17 }
18 tests := []struct {
19 name string
20 fields fields
21 want []LocalStorage
22 wantErr bool
23 }{
24 {
25 name: "case 1: list all the volumes in the devfile along with their respective size and containers",
26 fields: fields{
27 devfileObj: parser.DevfileObj{
28 Data: func() data.DevfileData {
29 devfileData, err := data.NewDevfileData(string(data.APISchemaVersion200))
30 if err != nil {
31 t.Error(err)
32 }
33 err = devfileData.AddComponents([]devfilev1.Component{
34 {
35 Name: "container-0",
36 ComponentUnion: devfilev1.ComponentUnion{
37 Container: &devfilev1.ContainerComponent{
38 Container: devfilev1.Container{
39 VolumeMounts: []devfilev1.VolumeMount{
40 {
41 Name: "volume-0",
42 Path: "/path",
43 },
44 {
45 Name: "volume-1",
46 Path: "/data",
47 },
48 },
49 },
50 },
51 },
52 },
53 {
54 Name: "container-1",
55 ComponentUnion: devfilev1.ComponentUnion{
56 Container: &devfilev1.ContainerComponent{
57 Container: devfilev1.Container{
58 VolumeMounts: []devfilev1.VolumeMount{
59 {
60 Name: "volume-1",
61 Path: "/data",
62 },
63 },
64 },
65 },
66 },
67 },
68 testingutil.GetFakeVolumeComponent("volume-0", "5Gi"),
69 testingutil.GetFakeVolumeComponent("volume-1", "10Gi"),
70 })
71 if err != nil {
72 t.Error(err)
73 }
74 return devfileData
75 }(),
76 },
77 },
78 want: []LocalStorage{
79 {
80 Name: "volume-0",
81 Size: "5Gi",
82 Path: "/path",
83 Container: "container-0",
84 },
85 {
86 Name: "volume-1",
87 Size: "10Gi",
88 Path: "/data",
89 Container: "container-0",
90 },
91 {
92 Name: "volume-1",
93 Size: "10Gi",
94 Path: "/data",
95 Container: "container-1",
96 },
97 },
98 },
99 {
100 name: "case 2: list all the volumes in the devfile with the default size when no size is mentioned",
101 fields: fields{
102 devfileObj: parser.DevfileObj{
103 Data: func() data.DevfileData {
104 devfileData, err := data.NewDevfileData(string(data.APISchemaVersion200))
105 if err != nil {
106 t.Error(err)
107 }
108 err = devfileData.AddComponents([]devfilev1.Component{
109 {
110 Name: "container-0",
111 ComponentUnion: devfilev1.ComponentUnion{
112 Container: &devfilev1.ContainerComponent{
113 Container: devfilev1.Container{
114 VolumeMounts: []devfilev1.VolumeMount{
115 {
116 Name: "volume-0",
117 Path: "/path",
118 },
119 {
120 Name: "volume-1",
121 Path: "/data",
122 },
123 },
124 },
125 },
126 },
127 },
128 testingutil.GetFakeVolumeComponent("volume-0", ""),
129 testingutil.GetFakeVolumeComponent("volume-1", "10Gi"),
130 })
131 if err != nil {
132 t.Error(err)
133 }
134 return devfileData
135 }(),
136 },
137 },
138 want: []LocalStorage{
139 {
140 Name: "volume-0",
141 Size: "1Gi",
142 Path: "/path",
143 Container: "container-0",
144 },
145 {
146 Name: "volume-1",
147 Size: "10Gi",
148 Path: "/data",
149 Container: "container-0",
150 },
151 },
152 },
153 {
154 name: "case 3: list all the volumes in the devfile with the default mount path when no path is mentioned",
155 fields: fields{
156 devfileObj: parser.DevfileObj{
157 Data: func() data.DevfileData {
158 devfileData, err := data.NewDevfileData(string(data.APISchemaVersion200))
159 if err != nil {
160 t.Error(err)
161 }
162 err = devfileData.AddComponents([]devfilev1.Component{
163 {
164 Name: "container-0",
165 ComponentUnion: devfilev1.ComponentUnion{
166 Container: &devfilev1.ContainerComponent{
167 Container: devfilev1.Container{
168 VolumeMounts: []devfilev1.VolumeMount{
169 {
170 Name: "volume-0",
171 },
172 },
173 },
174 },
175 },
176 },
177 testingutil.GetFakeVolumeComponent("volume-0", ""),
178 })
179 if err != nil {
180 t.Error(err)
181 }
182 return devfileData
183 }(),
184 },
185 },
186 want: []LocalStorage{
187 {
188 Name: "volume-0",
189 Size: "1Gi",
190 Path: "/volume-0",
191 Container: "container-0",
192 },
193 },
194 },
195 {
196 name: "case 4: return empty when no volumes is mounted",
197 fields: fields{
198 devfileObj: parser.DevfileObj{
199 Data: func() data.DevfileData {
200 devfileData, err := data.NewDevfileData(string(data.APISchemaVersion200))
201 if err != nil {
202 t.Error(err)
203 }
204 err = devfileData.AddComponents([]devfilev1.Component{
205 {
206 Name: "container-0",
207 ComponentUnion: devfilev1.ComponentUnion{
208 Container: &devfilev1.ContainerComponent{
209 Container: devfilev1.Container{},
210 },
211 },
212 },
213 testingutil.GetFakeVolumeComponent("volume-0", ""),
214 testingutil.GetFakeVolumeComponent("volume-1", "10Gi"),
215 })
216 if err != nil {
217 t.Error(err)
218 }
219 return devfileData
220 }(),
221 },
222 },
223 want: nil,
224 },
225 }
226 for _, tt := range tests {
227 t.Run(tt.name, func(t *testing.T) {
228 got, err := ListStorage(tt.fields.devfileObj)
229 if (err != nil) != tt.wantErr {
230 t.Errorf("ListStorage() error = %v, wantErr %v", err, tt.wantErr)
231 }
232 if diff := cmp.Diff(tt.want, got); diff != "" {
233 t.Errorf("ListStorage() mismatch (-want +got):\n%s", diff)
234 }
235 })
236 }
237 }
238
View as plain text