1 package state
2
3 import (
4 "context"
5 "encoding/json"
6 "fmt"
7 "testing"
8
9 "github.com/google/go-cmp/cmp"
10
11 "github.com/redhat-developer/odo/pkg/api"
12 odocontext "github.com/redhat-developer/odo/pkg/odo/context"
13 "github.com/redhat-developer/odo/pkg/testingutil/filesystem"
14 )
15
16 func TestState_SetForwardedPorts(t *testing.T) {
17
18 forwardedPort1 := api.ForwardedPort{
19 ContainerName: "acontainer",
20 LocalAddress: "localhost",
21 LocalPort: 20001,
22 ContainerPort: 3000,
23 }
24
25 type fields struct {
26 fs func() filesystem.Filesystem
27 getSecondsFromEpoch func() int64
28 getpid func() int
29 }
30 type args struct {
31 fwPorts []api.ForwardedPort
32 }
33 tests := []struct {
34 name string
35 fields fields
36 args args
37 wantErr bool
38 checkState func(fs filesystem.Filesystem) error
39 }{
40
41 {
42 name: "set forwarded ports",
43 fields: fields{
44 fs: func() filesystem.Filesystem {
45 return filesystem.NewFakeFs()
46 },
47 getSecondsFromEpoch: func() int64 {
48 return 13000
49 },
50 getpid: func() int {
51 return 100
52 },
53 },
54 args: args{
55 fwPorts: []api.ForwardedPort{forwardedPort1},
56 },
57 wantErr: false,
58 checkState: func(fs filesystem.Filesystem) error {
59 jsonContent, err := fs.ReadFile(_filepath)
60 if err != nil {
61 return err
62 }
63 var content Content
64 err = json.Unmarshal(jsonContent, &content)
65 if err != nil {
66 return err
67 }
68 expected := []api.ForwardedPort{forwardedPort1}
69 if diff := cmp.Diff(expected, content.ForwardedPorts); diff != "" {
70 return fmt.Errorf("forwarded ports is %+v, should be %+v, diff: %s", content.ForwardedPorts, expected, diff)
71 }
72 return nil
73 },
74 },
75 }
76 for _, tt := range tests {
77 t.Run(tt.name, func(t *testing.T) {
78 fs := tt.fields.fs()
79 o := State{
80 fs: fs,
81 }
82 ctx := context.Background()
83 ctx = odocontext.WithPID(ctx, 1)
84 if err := o.SetForwardedPorts(ctx, tt.args.fwPorts); (err != nil) != tt.wantErr {
85 t.Errorf("State.SetForwardedPorts() error = %v, wantErr %v", err, tt.wantErr)
86 }
87 if check := tt.checkState(fs); check != nil {
88 t.Error(check)
89 }
90 })
91 }
92 }
93
94 func TestState_SaveExit(t *testing.T) {
95 type fields struct {
96 fs func() filesystem.Filesystem
97 getSecondsFromEpoch func() int64
98 getpid func() int
99 }
100 tests := []struct {
101 name string
102 fields fields
103 wantErr bool
104 checkState func(fs filesystem.Filesystem) error
105 }{
106 {
107 name: "save exit",
108 fields: fields{
109 fs: func() filesystem.Filesystem {
110 return filesystem.NewFakeFs()
111 },
112 getSecondsFromEpoch: func() int64 {
113 return 13000
114 },
115 getpid: func() int {
116 return 100
117 },
118 },
119 wantErr: false,
120 checkState: func(fs filesystem.Filesystem) error {
121 jsonContent, err := fs.ReadFile(_filepath)
122 if err != nil {
123 return err
124 }
125 var content Content
126 err = json.Unmarshal(jsonContent, &content)
127 if err != nil {
128 return err
129 }
130 if len(content.ForwardedPorts) != 0 {
131 return fmt.Errorf("Forwarded ports is %+v, should be empty", content.ForwardedPorts)
132 }
133 return nil
134 },
135 },
136 }
137 for _, tt := range tests {
138 t.Run(tt.name, func(t *testing.T) {
139 fs := tt.fields.fs()
140 o := State{
141 fs: fs,
142 }
143 ctx := context.Background()
144 ctx = odocontext.WithPID(ctx, 1)
145 _ = o.SetForwardedPorts(ctx, nil)
146 if err := o.SaveExit(ctx); (err != nil) != tt.wantErr {
147 t.Errorf("State.SaveExit() error = %v, wantErr %v", err, tt.wantErr)
148 }
149 if check := tt.checkState(fs); check != nil {
150 t.Error(check)
151 }
152 })
153 }
154 }
155
156 func TestState_GetForwardedPorts(t *testing.T) {
157 contentPodman := Content{
158 Platform: "podman",
159 ForwardedPorts: []api.ForwardedPort{
160 {
161 ContainerName: "acontainer",
162 LocalAddress: "localhost",
163 LocalPort: 20001,
164 ContainerPort: 3000,
165 },
166 },
167 }
168 contentCluster := Content{
169 Platform: "cluster",
170 ForwardedPorts: []api.ForwardedPort{
171 {
172 ContainerName: "acontainer",
173 LocalAddress: "localhost",
174 LocalPort: 20002,
175 ContainerPort: 3000,
176 },
177 },
178 }
179 type fields struct {
180 content Content
181 fs func(t *testing.T) filesystem.Filesystem
182 }
183 tests := []struct {
184 name string
185 fields fields
186 want []api.ForwardedPort
187 wantErr bool
188 }{
189 {
190 name: "get forwarded ports, only deployed on podman",
191 fields: fields{
192 content: Content{},
193 fs: func(t *testing.T) filesystem.Filesystem {
194 fs := filesystem.NewFakeFs()
195 jsonContent, err := json.Marshal(contentPodman)
196 if err != nil {
197 t.Errorf("Error marshaling data")
198 }
199 pid := 1
200 err = fs.WriteFile(getFilename(pid), jsonContent, 0644)
201 if err != nil {
202 t.Errorf("Error saving content to file")
203 }
204 return fs
205 },
206 },
207 want: contentPodman.ForwardedPorts,
208 wantErr: false,
209 },
210 {
211 name: "get forwarded ports, only deployed on cluster",
212 fields: fields{
213 content: Content{},
214 fs: func(t *testing.T) filesystem.Filesystem {
215 fs := filesystem.NewFakeFs()
216 jsonContent, err := json.Marshal(contentCluster)
217 if err != nil {
218 t.Errorf("Error marshaling data")
219 }
220 pid := 1
221 err = fs.WriteFile(getFilename(pid), jsonContent, 0644)
222 if err != nil {
223 t.Errorf("Error saving content to file")
224 }
225 return fs
226 },
227 },
228 want: contentCluster.ForwardedPorts,
229 wantErr: false,
230 },
231 {
232 name: "get forwarded ports, deployed on both podman and cluster",
233 fields: fields{
234 content: Content{},
235 fs: func(t *testing.T) filesystem.Filesystem {
236 fs := filesystem.NewFakeFs()
237
238 pidCluster := 1
239 jsonContentCluster, err := json.Marshal(contentCluster)
240 if err != nil {
241 t.Errorf("Error marshaling data")
242 }
243 err = fs.WriteFile(getFilename(pidCluster), jsonContentCluster, 0644)
244 if err != nil {
245 t.Errorf("Error saving content to file")
246 }
247
248 pidPodman := 2
249 jsonContentPodman, err := json.Marshal(contentPodman)
250 if err != nil {
251 t.Errorf("Error marshaling data")
252 }
253 err = fs.WriteFile(getFilename(pidPodman), jsonContentPodman, 0644)
254 if err != nil {
255 t.Errorf("Error saving content to file")
256 }
257
258 return fs
259 },
260 },
261 want: append(append([]api.ForwardedPort{}, contentCluster.ForwardedPorts...), contentPodman.ForwardedPorts...),
262 wantErr: false,
263 },
264 }
265 for _, tt := range tests {
266 t.Run(tt.name, func(t *testing.T) {
267 o := &State{
268 content: tt.fields.content,
269 fs: tt.fields.fs(t),
270 }
271 ctx := context.Background()
272 ctx = odocontext.WithPID(ctx, 1)
273 got, err := o.GetForwardedPorts(ctx)
274 if (err != nil) != tt.wantErr {
275 t.Errorf("State.GetForwardedPorts() error = %v, wantErr %v", err, tt.wantErr)
276 return
277 }
278 if diff := cmp.Diff(tt.want, got); diff != "" {
279 t.Errorf("State.GetForwardedPorts() mismatch (-want +got):\n%s", diff)
280 }
281 })
282 }
283 }
284
View as plain text