1 package alizer
2
3 import (
4 "context"
5 "path/filepath"
6 "reflect"
7 "runtime"
8 "testing"
9
10 "github.com/golang/mock/gomock"
11 "github.com/redhat-developer/odo/pkg/api"
12 "github.com/redhat-developer/odo/pkg/registry"
13 )
14
15
16
17 func GetTestProjectPath(folder string) string {
18 _, b, _, _ := runtime.Caller(0)
19 basepath := filepath.Dir(b)
20 return filepath.Join(basepath, "..", "..", "tests/examples/source/", folder)
21 }
22
23 var types = []api.DevfileStack{
24 {
25 Name: "java-maven",
26 Language: "java",
27 ProjectType: "maven",
28 Tags: []string{"Java", "Maven"},
29 Registry: api.Registry{
30 Name: "registry1",
31 },
32 },
33 {
34 Name: "java-quarkus",
35 Language: "java",
36 ProjectType: "quarkus",
37 Tags: []string{"Java", "Quarkus"},
38 Registry: api.Registry{
39 Name: "registry1",
40 },
41 },
42 {
43 Name: "java-wildfly",
44 Language: "java",
45 ProjectType: "wildfly",
46 Tags: []string{"Java", "WildFly"},
47 Registry: api.Registry{
48 Name: "registry2",
49 },
50 },
51 {
52 Name: "nodejs",
53 Language: "JavaScript",
54 ProjectType: "nodejs",
55 Tags: []string{"NodeJS", "Express", "ubi8"},
56 Registry: api.Registry{
57 Name: "registry2",
58 },
59 },
60 {
61 Name: "python",
62 Language: "python",
63 ProjectType: "python",
64 Tags: []string{"Python", "pip"},
65 Registry: api.Registry{
66 Name: "registry3",
67 },
68 },
69 }
70 var list = registry.DevfileStackList{
71 Items: types,
72 }
73
74 func TestDetectFramework(t *testing.T) {
75
76 type args struct {
77 path string
78 }
79 tests := []struct {
80 name string
81 args args
82 wantedDevfile string
83 wantedRegistry string
84 wantErr bool
85 }{
86 {
87 name: "Detect Node.JS example",
88 args: args{
89 path: GetTestProjectPath("nodejs"),
90 },
91 wantedDevfile: "nodejs",
92 wantedRegistry: "registry2",
93 wantErr: false,
94 },
95 {
96 name: "Detect java openjdk example",
97 args: args{
98 path: GetTestProjectPath("openjdk"),
99 },
100 wantedDevfile: "java-maven",
101 wantedRegistry: "registry1",
102 wantErr: false,
103 },
104 {
105 name: "Detect python example",
106 args: args{
107 path: GetTestProjectPath("python"),
108 },
109 wantedDevfile: "python",
110 wantedRegistry: "registry3",
111 wantErr: false,
112 },
113 }
114 for _, tt := range tests {
115 t.Run(tt.name, func(t *testing.T) {
116 ctrl := gomock.NewController(t)
117 registryClient := registry.NewMockClient(ctrl)
118 ctx := context.Background()
119 registryClient.EXPECT().ListDevfileStacks(ctx, "", "", "", false, false).Return(list, nil)
120 alizerClient := NewAlizerClient(registryClient)
121
122
123 detected, err := alizerClient.DetectFramework(ctx, tt.args.path)
124 if !tt.wantErr == (err != nil) {
125 t.Errorf("unexpected error %v, wantErr %v", err, tt.wantErr)
126 return
127 }
128
129 if detected.Type.Name != tt.wantedDevfile {
130 t.Errorf("unexpected devfile %v, wantedDevfile %v", detected, tt.wantedDevfile)
131 }
132 if detected.Registry.Name != tt.wantedRegistry {
133 t.Errorf("unexpected registry %v, wantedRegistry %v", detected.Registry, tt.wantedRegistry)
134 }
135 })
136 }
137 }
138
139 func TestDetectName(t *testing.T) {
140
141 type args struct {
142 path string
143 }
144 tests := []struct {
145 name string
146 args args
147 wantedName string
148 wantErr bool
149 }{
150 {
151 name: "Case 1: Detect Node.JS name through package.json",
152 args: args{
153 path: GetTestProjectPath("nodejs"),
154 },
155 wantedName: "node-echo",
156 wantErr: false,
157 },
158 {
159
160
161
162 name: "Case 2: Detect Python name through DIRECTORY name",
163 args: args{
164 path: GetTestProjectPath("python"),
165 },
166
167 wantedName: "python",
168 wantErr: false,
169 },
170 {
171
172
173
174
175 name: "Case 3: Detect Java name through pom.xml",
176 args: args{
177 path: GetTestProjectPath("wildfly"),
178 },
179 wantedName: "insultapp",
180 wantErr: false,
181 },
182 }
183 for _, tt := range tests {
184 t.Run(tt.name, func(t *testing.T) {
185
186 ctrl := gomock.NewController(t)
187 registryClient := registry.NewMockClient(ctrl)
188 alizerClient := NewAlizerClient(registryClient)
189
190 name, err := alizerClient.DetectName(tt.args.path)
191
192 if !tt.wantErr == (err != nil) {
193 t.Errorf("unexpected error %v, wantErr %v", err, tt.wantErr)
194 return
195 }
196
197 if name != tt.wantedName {
198 t.Errorf("unexpected name %q, wanted: %q", name, tt.wantedName)
199 }
200 })
201 }
202 }
203
204 func TestAlizer_DetectPorts(t *testing.T) {
205 type fields struct {
206 registryClient registry.Client
207 }
208 type args struct {
209 path string
210 }
211 tests := []struct {
212 name string
213 fields fields
214 args args
215 want []int
216 wantErr bool
217 }{
218 {
219 name: "Detect Node.JS example",
220 args: args{
221 path: GetTestProjectPath("nodejs"),
222 },
223 want: []int{8080},
224 wantErr: false,
225 },
226 }
227 for _, tt := range tests {
228 t.Run(tt.name, func(t *testing.T) {
229 o := &Alizer{
230 registryClient: tt.fields.registryClient,
231 }
232 got, err := o.DetectPorts(tt.args.path)
233 if (err != nil) != tt.wantErr {
234 t.Errorf("Alizer.DetectPorts() error = %v, wantErr %v", err, tt.wantErr)
235 return
236 }
237 if !reflect.DeepEqual(got, tt.want) {
238 t.Errorf("Alizer.DetectPorts() = %v, want %v", got, tt.want)
239 }
240 })
241 }
242 }
243
View as plain text