-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathbinary-test.ts
195 lines (165 loc) · 6.57 KB
/
binary-test.ts
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
/**********************************************************************************
* MIT License *
* *
* Copyright (c) 2021 Hyperjump Technology *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy *
* of this software and associated documentation files (the "Software"), to deal *
* in the Software without restriction, including without limitation the rights *
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
* copies of the Software, and to permit persons to whom the Software is *
* furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in all *
* copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
* SOFTWARE. *
**********************************************************************************/
import { prepareEnvironment } from '@gmrchk/cli-testing-library'
import { expect } from 'chai'
let monikaPath: string
before(async () => {
const { spawn, cleanup } = await prepareEnvironment()
const { getStdout, waitForText } = await spawn('which', 'monika', '.')
await waitForText('/monika')
monikaPath = getStdout()
.join('\r\n')
.replace('{{homedir}}', process.env.HOME || '')
console.log('Setup: Using monika path', monikaPath)
await cleanup()
})
describe('Binary CLI Testing', () => {
it('shows version', async () => {
// arrange
const { spawn, cleanup } = await prepareEnvironment()
// act
const { getStdout, waitForText } = await spawn(monikaPath, '-v')
await waitForText('@hyperjumptech/monika')
const stdout = getStdout().join('\r\n')
// assert
expect(stdout).to.contain('@hyperjumptech/monika')
await cleanup()
})
it('shows initializing file when no config', async () => {
// arrange
const { spawn, cleanup, exists, removeFile } = await prepareEnvironment()
const assertText1 = 'No Monika configuration available, initializing...'
const assertText2 =
'monika.yml file has been created in this directory. You can change the URL to probe and other configurations in that monika.yml file.'
// act
const prevConfig = await exists('./monika.yml')
if (prevConfig) {
await removeFile('./monika.yml')
}
const { getStdout, waitForText } = await spawn(monikaPath, '-r 1')
await waitForText(assertText2)
const stdout = getStdout().join('\r\n')
// assert
expect(stdout).to.contain(assertText1)
expect(stdout).to.contain(assertText2)
await cleanup()
})
it('shows starting message with valid json config', async () => {
// arrange
const { spawn, cleanup } = await prepareEnvironment()
// act
const { getStdout, waitForText } = await spawn(
monikaPath,
'-r 1 -c ./monika.example.json'
)
await waitForText('Starting Monika.')
const stdout = getStdout().join('\r\n')
// assert
expect(stdout).to.contain('Starting Monika.')
await cleanup()
})
it('shows starting message with valid yml config', async () => {
// arrange
const { spawn, cleanup } = await prepareEnvironment()
// act
const { getStdout, waitForText } = await spawn(
monikaPath,
'-r 1 -c ./monika.example.yml'
)
await waitForText('Starting Monika.')
const stdout = getStdout().join('\r\n')
// assert
expect(stdout).to.contain('Starting Monika.')
await cleanup()
})
it('Detect config file changes successfully', async () => {
// arrange
const { spawn, cleanup, writeFile } = await prepareEnvironment()
const initFile = `
probes:
- id: '1'
name: Google
interval: 1
requests:
- url: https://google.com
notifications:
- id: unique-id-monika-notif,
type: desktop
`
const changeFile = `
probes:
- id: '1'
name: Google
interval: 1
requests:
- url: https://google.com
- id: '2'
name: Github
interval: 1
requests:
- url: https://github.com
notifications:
- id: unique-id-monika-notif,
type: desktop
`
const updatedString = 'WARN: config file update detected'
// act
// write initial yml
await writeFile('./cli-test.yml', initFile)
// run monika
const { getStdout, waitForText, kill, wait } = await spawn(
monikaPath,
'-c cli-test.yml'
)
try {
await waitForText('GET https://google.com')
// write new yml containing github url
await writeFile('./cli-test.yml', changeFile)
await wait(5000)
// assert
const stdout = getStdout().join('\r\n')
const stdoutBeforeConfigChange = stdout.split(updatedString)[0]
const stdoutAfterConfigChange = stdout.split(updatedString)[1]
// expect starting monika
expect(stdoutBeforeConfigChange).to.contain('Using config file')
expect(stdoutBeforeConfigChange).to.contain('cli-test.yml')
expect(stdoutBeforeConfigChange).to.contain(
'Starting Monika. Probes: 1. Notifications: 1'
)
expect(stdoutBeforeConfigChange).to.contain('GET https://google.com')
// expect new yml including url github
expect(stdoutAfterConfigChange).to.contain('Using config file:')
expect(stdoutAfterConfigChange).to.contain('cli-test.yml')
expect(stdoutAfterConfigChange).to.contain(
'Restarting Monika. Probes: 2. Notifications: 1'
)
expect(stdoutAfterConfigChange).to.contain('GET https://github.com')
} catch {
console.error('catch error waiting for change file')
}
// Stop monika
kill('SIGINT')
await cleanup()
})
})