forked from cloud66-oss/starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
api_test.go
269 lines (239 loc) · 9.89 KB
/
api_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package main
import (
"encoding/json"
"github.com/go-resty/resty"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"io/ioutil"
"os"
)
var _ = Describe("Running Starter in daemon mode", func() {
Context("ping the service", func() {
It("should respond with ok", func() {
resp, err := resty.R().Get("http://127.0.0.1:9090/ping")
Expect(err).NotTo(HaveOccurred())
Expect(string(resp.Body())).To(Equal(`"ok"`))
})
})
Context("get the version", func() {
It("should respond with version number", func() {
resp, err := resty.R().Get("http://127.0.0.1:9090/version")
Expect(err).NotTo(HaveOccurred())
Expect(string(resp.Body())).To(Equal(`"test"`))
})
})
Context("get the list of files starter is using to analyse the codebase", func() {
It("should respond with all the supported files", func() {
resp, err := resty.R().Get("http://127.0.0.1:9090/analyze/supported")
Expect(err).NotTo(HaveOccurred())
Expect(string(resp.Body())).To(Equal(`{"Languages":[{"Name":"ruby","Files":["Gemfile","Procfile","config/database.yml"],"SupportedVersion":null},{"Name":"node","Files":["package.json","Procfile",".meteor/release"],"SupportedVersion":["4.6"]},{"Name":"php","Files":["composer.json"],"SupportedVersion":null}]}`))
})
})
Context("get the list of base Dockerfiles starter is supporting", func() {
It("should respond with all the supported Dockerfiles", func() {
resp, err := resty.R().Get("http://127.0.0.1:9090/analyze/dockerfiles")
Expect(err).NotTo(HaveOccurred())
dockerfiles := []Dockerfile{}
path := "test/ruby/Dockerfile.base"
rubyDockerFile, err := ioutil.ReadFile(path)
dockerfile := Dockerfile{}
dockerfile.Language = "ruby"
dockerfile.Base = string(rubyDockerFile)
dockerfiles = append(dockerfiles, dockerfile)
path = "test/node/Dockerfile.base"
nodeDockerFile, err := ioutil.ReadFile(path)
dockerfile = Dockerfile{}
dockerfile.Language = "node"
dockerfile.Base = string(nodeDockerFile)
dockerfiles = append(dockerfiles, dockerfile)
path = "test/php/Dockerfile.base"
phpDockerFile, err := ioutil.ReadFile(path)
dockerfile = Dockerfile{}
dockerfile.Language = "php"
dockerfile.Base = string(phpDockerFile)
dockerfiles = append(dockerfiles, dockerfile)
b, err := json.Marshal(dockerfiles)
Expect(err).NotTo(HaveOccurred())
Expect(string(resp.Body())).To(Equal(string(b)))
})
})
Context("analyse a ruby project", func() {
It("should respond with program langange ruby", func() {
path := "test/ruby/rails_mysql/src"
resp, err := resty.R().SetBody(`{"path":"` + path + `", "generate":"dockerfile "}`).Post("http://127.0.0.1:9090/analyze")
Expect(err).NotTo(HaveOccurred())
dockerfile, err := ioutil.ReadFile(path + "/Dockerfile")
Expect(err).NotTo(HaveOccurred())
analysis := analysisResult{}
analysis.Language = "ruby"
analysis.Framework = "rails"
analysis.Ok = true
analysis.Warnings = nil
analysis.Dockerfile = string(dockerfile)
analysis.StartCommands = nil
analysis.BuildCommands = []string{}
analysis.DeployCommands = []string{}
analysis.Databases = []string{}
b, err := json.Marshal(analysis)
Expect(string(resp.Body())).To(Equal(string(b)))
os.Remove(path + "/Dockerfile")
})
})
Context("analyse a ruby project and request a dockerfile, docker-compose.yml and service.yml", func() {
It("should respond with a dockerfile, docker-compose.yml and service.yml", func() {
path := "test/ruby/rails_mysql/src"
resp, err := resty.R().SetBody(`{"path":"` + path + `", "generate":"dockerfile,service,docker-compose "}`).Post("http://127.0.0.1:9090/analyze")
Expect(err).NotTo(HaveOccurred())
dockerfile, err := ioutil.ReadFile(path + "/Dockerfile")
Expect(err).NotTo(HaveOccurred())
serviceyml, err := ioutil.ReadFile(path + "/service.yml")
Expect(err).NotTo(HaveOccurred())
dockercomposeyml, err := ioutil.ReadFile(path + "/docker-compose.yml")
Expect(err).NotTo(HaveOccurred())
analysis := analysisResult{}
analysis.Ok = true
analysis.Warnings = nil
analysis.Language = "ruby"
analysis.Framework = "rails"
analysis.Dockerfile = string(dockerfile)
analysis.Service = string(serviceyml)
analysis.DockerCompose = string(dockercomposeyml)
analysis.BuildCommands = []string{}
analysis.DeployCommands = []string{}
analysis.Databases = []string{}
b, err := json.Marshal(analysis)
Expect(string(resp.Body())).To(Equal(string(b)))
os.Remove(path + "/Dockerfile")
os.Remove(path + "/service.yml")
os.Remove(path + "/docker-compose.yml")
})
})
Context("analyse a ruby project through upload files and request a dockerfile, docker-compose.yml and service.yml and s", func() {
It("should respond with a dockerfile, docker-compose.yml and service.yml", func() {
path := "test/ruby/rails_mysql/src"
expected := "test/ruby/rails_mysql/expected/api"
resp, err := resty.R().
SetFile("source", path+"/source.zip").
SetFormData(map[string]string{
"git_repo": "fake.git",
"git_branch": "master",
}).Post("http://127.0.0.1:9090/analyze/upload")
Expect(err).NotTo(HaveOccurred())
dockerfile, err := ioutil.ReadFile(expected + "/Dockerfile")
Expect(err).NotTo(HaveOccurred())
dockercomposeyml, err := ioutil.ReadFile(expected + "/docker-compose.yml")
Expect(err).NotTo(HaveOccurred())
serviceyml, err := ioutil.ReadFile(expected + "/service.yml")
Expect(err).NotTo(HaveOccurred())
analysis := analysisResult{}
analysis.Ok = true
analysis.Warnings = nil
analysis.Language = "ruby"
analysis.Framework = "rails"
analysis.Dockerfile = string(dockerfile)
analysis.DockerCompose = string(dockercomposeyml)
analysis.Service = string(serviceyml)
analysis.BuildCommands = []string{}
analysis.DeployCommands = []string{}
analysis.Databases = []string{}
b, err := json.Marshal(analysis)
Expect(string(resp.Body())).To(Equal(string(b)))
})
})
Context("analyse a ruby project and request a dockerfile and service.yml", func() {
It("should respond with a dockerfile and service.yml", func() {
path := "test/ruby/rails_mysql/src"
resp, err := resty.R().SetBody(`{"path":"` + path + `", "generate":"dockerfile,service"}`).Post("http://127.0.0.1:9090/analyze")
Expect(err).NotTo(HaveOccurred())
dockerfile, err := ioutil.ReadFile(path + "/Dockerfile")
Expect(err).NotTo(HaveOccurred())
serviceyml, err := ioutil.ReadFile(path + "/service.yml")
Expect(err).NotTo(HaveOccurred())
analysis := analysisResult{}
analysis.Ok = true
analysis.Language = "ruby"
analysis.Framework = "rails"
analysis.Warnings = nil
analysis.Dockerfile = string(dockerfile)
analysis.Service = string(serviceyml)
analysis.BuildCommands = []string{}
analysis.DeployCommands = []string{}
analysis.Databases = []string{}
b, err := json.Marshal(analysis)
Expect(string(resp.Body())).To(Equal(string(b)))
os.Remove(path + "/Dockerfile")
os.Remove(path + "/service.yml")
})
})
Context("analyse a ruby project and only request a dockerfile", func() {
It("should respond with a dockerfile", func() {
path := "test/ruby/rails_mysql/src"
resp, err := resty.R().SetBody(`{"path":"` + path + `", "generate":"dockerfile"}`).Post("http://127.0.0.1:9090/analyze")
Expect(err).NotTo(HaveOccurred())
file, err := ioutil.ReadFile(path + "/Dockerfile")
Expect(err).NotTo(HaveOccurred())
analysis := analysisResult{}
analysis.Ok = true
analysis.Language = "ruby"
analysis.Framework = "rails"
analysis.Warnings = nil
analysis.Dockerfile = string(file)
analysis.BuildCommands = []string{}
analysis.DeployCommands = []string{}
analysis.Databases = []string{}
b, err := json.Marshal(analysis)
Expect(string(resp.Body())).To(Equal(string(b)))
os.Remove(path + "/Dockerfile")
})
})
Context("analyse a node project only request a dockerfile", func() {
It("should respond with a dockerfile", func() {
path := "test/node/express_procfile/src"
resp, err := resty.R().SetBody(`{"path":"` + path + `", "generate":"dockerfile"}`).Post("http://127.0.0.1:9090/analyze")
Expect(err).NotTo(HaveOccurred())
file, err := ioutil.ReadFile(path + "/Dockerfile")
Expect(err).NotTo(HaveOccurred())
analysis := analysisResult{}
analysis.Ok = true
analysis.Language = "node"
analysis.LanguageVersion = "4.6"
analysis.Framework = "express"
analysis.FrameworkVersion = "4.13.0"
analysis.SupportedLanguageVersions = []string{"4.6"}
analysis.Warnings = nil
analysis.Dockerfile = string(file)
analysis.StartCommands = []string{"node server.js"}
analysis.BuildCommands = []string{}
analysis.DeployCommands = []string{}
analysis.Databases = []string{}
b, err := json.Marshal(analysis)
Expect(string(resp.Body())).To(Equal(string(b)))
os.Remove(path + "/Dockerfile")
})
})
Context("analyse a node project with databaze only request a dockerfile", func() {
It("should respond with a dockerfile", func() {
path := "test/node/express_mysql_pg/src"
resp, err := resty.R().SetBody(`{"path":"` + path + `", "generate":"dockerfile"}`).Post("http://127.0.0.1:9090/analyze")
Expect(err).NotTo(HaveOccurred())
file, err := ioutil.ReadFile(path + "/Dockerfile")
Expect(err).NotTo(HaveOccurred())
analysis := analysisResult{}
analysis.Ok = true
analysis.Language = "node"
analysis.LanguageVersion = "4.6"
analysis.Framework = "express"
analysis.FrameworkVersion = "4.14.0"
analysis.SupportedLanguageVersions = []string{"4.6"}
analysis.Warnings = []string{"No Procfile was detected. It is strongly advised to add one in order to specify the commands to run your services."}
analysis.Dockerfile = string(file)
analysis.StartCommands = []string{"npm start"}
analysis.BuildCommands = []string{}
analysis.DeployCommands = []string{}
analysis.Databases = []string{"mysql", "postgresql"}
b, err := json.Marshal(analysis)
Expect(string(resp.Body())).To(Equal(string(b)))
os.Remove(path + "/Dockerfile")
})
})
})