1 package backend
2
3 import (
4 "context"
5 "errors"
6 "path/filepath"
7 "runtime"
8 "testing"
9
10 "github.com/golang/mock/gomock"
11 "github.com/google/go-cmp/cmp"
12
13 "github.com/devfile/alizer/pkg/apis/model"
14
15 "github.com/redhat-developer/odo/pkg/alizer"
16 "github.com/redhat-developer/odo/pkg/api"
17 "github.com/redhat-developer/odo/pkg/init/asker"
18 "github.com/redhat-developer/odo/pkg/testingutil/filesystem"
19 )
20
21
22
23 func GetTestProjectPath(folder string) string {
24 _, b, _, _ := runtime.Caller(0)
25 basepath := filepath.Dir(b)
26 return filepath.Join(basepath, "..", "..", "..", "tests/examples/source/", folder)
27 }
28
29 func TestAlizerBackend_SelectDevfile(t *testing.T) {
30 type fields struct {
31 askerClient func(ctrl *gomock.Controller) asker.Asker
32 alizerClient func(ctrl *gomock.Controller) alizer.Client
33 }
34 type args struct {
35 flags map[string]string
36 fs filesystem.Filesystem
37 dir string
38 }
39 tests := []struct {
40 name string
41 fields fields
42 args args
43 wantLocation *api.DetectionResult
44 wantErr bool
45 }{
46 {
47 name: "error while trying to detect devfile",
48 fields: fields{
49 askerClient: func(ctrl *gomock.Controller) asker.Asker {
50 askerClient := asker.NewMockAsker(ctrl)
51 askerClient.EXPECT().AskCorrect().Return(true, nil).Times(0)
52 return askerClient
53 },
54 alizerClient: func(ctrl *gomock.Controller) alizer.Client {
55 alizerClient := alizer.NewMockClient(ctrl)
56 alizerClient.EXPECT().DetectFramework(gomock.Any(), gomock.Any()).Return(alizer.DetectedFramework{}, errors.New("unable to detect framework"))
57 return alizerClient
58 },
59 },
60 args: args{
61 fs: filesystem.DefaultFs{},
62 dir: GetTestProjectPath("nodejs"),
63 },
64 wantErr: true,
65 },
66 {
67 name: "error while trying to detect ports",
68 fields: fields{
69 askerClient: func(ctrl *gomock.Controller) asker.Asker {
70 askerClient := asker.NewMockAsker(ctrl)
71 askerClient.EXPECT().AskCorrect().Return(true, nil).Times(0)
72 return askerClient
73 },
74 alizerClient: func(ctrl *gomock.Controller) alizer.Client {
75 alizerClient := alizer.NewMockClient(ctrl)
76 alizerClient.EXPECT().DetectFramework(gomock.Any(), gomock.Any()).Return(alizer.DetectedFramework{}, nil)
77 alizerClient.EXPECT().DetectPorts(gomock.Any()).Return(nil, errors.New("unable to detect ports"))
78 return alizerClient
79 },
80 },
81 args: args{
82 fs: filesystem.DefaultFs{},
83 dir: GetTestProjectPath("nodejs"),
84 },
85 wantErr: true,
86 },
87 {
88 name: "error while asking consent to user",
89 fields: fields{
90 askerClient: func(ctrl *gomock.Controller) asker.Asker {
91 askerClient := asker.NewMockAsker(ctrl)
92 askerClient.EXPECT().AskCorrect().Return(false, errors.New("error while prompting user"))
93 return askerClient
94 },
95 alizerClient: func(ctrl *gomock.Controller) alizer.Client {
96 alizerClient := alizer.NewMockClient(ctrl)
97 alizerClient.EXPECT().DetectFramework(gomock.Any(), gomock.Any()).Return(alizer.DetectedFramework{
98 Type: model.DevfileType{
99 Name: "a-devfile-name",
100 },
101 DefaultVersion: "1.0.0",
102 Registry: api.Registry{
103 Name: "a-registry",
104 },
105 }, nil)
106 alizerClient.EXPECT().DetectPorts(gomock.Any()).Return(nil, nil)
107 return alizerClient
108 },
109 },
110 args: args{
111 fs: filesystem.DefaultFs{},
112 dir: GetTestProjectPath("nodejs"),
113 },
114 wantErr: true,
115 },
116 {
117 name: "devfile found and accepted",
118 fields: fields{
119 askerClient: func(ctrl *gomock.Controller) asker.Asker {
120 askerClient := asker.NewMockAsker(ctrl)
121 askerClient.EXPECT().AskCorrect().Return(true, nil)
122 return askerClient
123 },
124 alizerClient: func(ctrl *gomock.Controller) alizer.Client {
125 alizerClient := alizer.NewMockClient(ctrl)
126 alizerClient.EXPECT().DetectFramework(gomock.Any(), gomock.Any()).Return(alizer.DetectedFramework{
127 Type: model.DevfileType{
128 Name: "a-devfile-name",
129 },
130 DefaultVersion: "1.0.0",
131 Registry: api.Registry{
132 Name: "a-registry",
133 },
134 }, nil)
135 alizerClient.EXPECT().DetectPorts(gomock.Any()).Return(nil, nil)
136 return alizerClient
137 },
138 },
139 args: args{
140 fs: filesystem.DefaultFs{},
141 dir: GetTestProjectPath("nodejs"),
142 },
143 wantLocation: &api.DetectionResult{
144 Devfile: "a-devfile-name",
145 DevfileRegistry: "a-registry",
146 DevfileVersion: "1.0.0",
147 },
148 },
149 {
150 name: "devfile found but not accepted",
151 fields: fields{
152 askerClient: func(ctrl *gomock.Controller) asker.Asker {
153 askerClient := asker.NewMockAsker(ctrl)
154 askerClient.EXPECT().AskCorrect().Return(false, nil)
155 return askerClient
156 },
157 alizerClient: func(ctrl *gomock.Controller) alizer.Client {
158 alizerClient := alizer.NewMockClient(ctrl)
159 alizerClient.EXPECT().DetectFramework(gomock.Any(), gomock.Any()).Return(alizer.DetectedFramework{}, nil)
160 alizerClient.EXPECT().DetectPorts(gomock.Any()).Return(nil, nil)
161 return alizerClient
162 },
163 },
164 args: args{
165 fs: filesystem.DefaultFs{},
166 dir: GetTestProjectPath("nodejs"),
167 },
168 wantLocation: nil,
169 },
170 {
171 name: "devfile and ports detected and accepted",
172 fields: fields{
173 askerClient: func(ctrl *gomock.Controller) asker.Asker {
174 askerClient := asker.NewMockAsker(ctrl)
175 askerClient.EXPECT().AskCorrect().Return(true, nil)
176 return askerClient
177 },
178 alizerClient: func(ctrl *gomock.Controller) alizer.Client {
179 alizerClient := alizer.NewMockClient(ctrl)
180 alizerClient.EXPECT().DetectFramework(gomock.Any(), gomock.Any()).Return(alizer.DetectedFramework{
181 Type: model.DevfileType{
182 Name: "a-devfile-name",
183 },
184 DefaultVersion: "1.0.0",
185 Registry: api.Registry{
186 Name: "a-registry",
187 },
188 }, nil)
189 alizerClient.EXPECT().DetectPorts(gomock.Any()).Return([]int{1234, 5678}, nil)
190 return alizerClient
191 },
192 },
193 args: args{
194 fs: filesystem.DefaultFs{},
195 dir: GetTestProjectPath("nodejs"),
196 },
197 wantLocation: &api.DetectionResult{
198 Devfile: "a-devfile-name",
199 DevfileRegistry: "a-registry",
200 ApplicationPorts: []int{1234, 5678},
201 DevfileVersion: "1.0.0",
202 },
203 },
204
205 }
206 for _, tt := range tests {
207 t.Run(tt.name, func(t *testing.T) {
208 ctrl := gomock.NewController(t)
209 o := &AlizerBackend{
210 askerClient: tt.fields.askerClient(ctrl),
211 alizerClient: tt.fields.alizerClient(ctrl),
212 }
213 ctx := context.Background()
214 gotLocation, err := o.SelectDevfile(ctx, tt.args.flags, tt.args.fs, tt.args.dir)
215 if (err != nil) != tt.wantErr {
216 t.Errorf("AlizerBackend.SelectDevfile() error = %v, wantErr %v", err, tt.wantErr)
217 return
218 }
219 if diff := cmp.Diff(tt.wantLocation, gotLocation); diff != "" {
220 t.Errorf("AlizerBackend.SelectDevfile() wantLocation mismatch (-want +got):\n%s", diff)
221 }
222 })
223 }
224 }
225
View as plain text