Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50655 views
1
###
2
Using API to run shell command in project
3
###
4
5
api = require('./apitest')
6
{setup, teardown} = api
7
8
expect = require('expect')
9
10
describe 'runs shell command in a project', ->
11
before(setup)
12
after(teardown)
13
14
project_id = undefined
15
16
it "creates test project", (done) ->
17
api.call
18
event : 'create_project'
19
body :
20
title : 'ADVTEST'
21
description : 'Testing advanced API'
22
cb : (err, resp) ->
23
expect(resp?.event).toBe('project_created')
24
project_id = resp.project_id
25
done(err)
26
27
it "does shell built-in", (done) ->
28
@timeout 30000
29
api.call
30
event : 'project_exec'
31
body :
32
project_id: project_id
33
command : 'pwd'
34
cb : (err, resp) ->
35
expect(resp?.event).toBe('project_exec_output')
36
re = new RegExp project_id+'$'
37
expect(resp?.stdout?.trim()).toMatch(re)
38
done(err)
39
40
it "runs command in different working directory", (done) ->
41
api.call
42
event : 'project_exec'
43
body :
44
project_id: project_id
45
command : 'pwd'
46
path : '/etc'
47
cb : (err, resp) ->
48
expect(resp?.event).toBe('project_exec_output')
49
expect(resp?.stdout?.trim()).toBe('/etc')
50
done(err)
51
52
it "runs command with args option", (done) ->
53
api.call
54
event : 'project_exec'
55
body :
56
project_id: project_id
57
command : 'echo'
58
args : ['hello', 'world']
59
cb : (err, resp) ->
60
expect(resp?.event).toBe('project_exec_output')
61
expect(resp?.stdout?.trim()).toBe('hello world')
62
done(err)
63
64
it "limits output to 5 characters", (done) ->
65
api.call
66
event : 'project_exec'
67
body :
68
project_id: project_id
69
command : 'echo'
70
args : ['hello', 'world']
71
max_output: 5
72
cb : (err, resp) ->
73
expect(resp?.event).toBe('project_exec_output')
74
expect(resp?.stdout?.trim()).toMatch('hello')
75
expect(resp?.stdout?.trim()).toNotMatch('world')
76
expect(resp?.stdout?.trim()).toMatch('truncated at 5 characters')
77
done(err)
78
79
it "sets execution timeout", (done) ->
80
@timeout 10000
81
api.call
82
event : 'project_exec'
83
body :
84
project_id: project_id
85
command : 'sleep 5;echo done'
86
timeout : 2
87
cb : (err, resp) ->
88
expect(resp?.event).toBe('error')
89
expect(resp?.error).toMatch('killed command')
90
done(err)
91
92