-
Notifications
You must be signed in to change notification settings - Fork 2
/
agent.spec.ts
194 lines (175 loc) · 4.45 KB
/
agent.spec.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
import * as Docker from 'dockerode';
import { trace, logger, expect } from '~/test-utils';
import { Agent, UNDEFINED } from 'mahler';
import { planner } from './planner';
import type { Device } from './state';
const docker = new Docker();
describe('orchestrator/agent', () => {
const cleanup = async () => {
const containers = await docker.listContainers({ all: true });
await Promise.all(
containers
.filter(({ Names }) =>
Names.some(
(name) => name.startsWith(`/r0_`) || name.startsWith('/r1_'),
),
)
.map(({ Id }) => docker.getContainer(Id).remove({ force: true })),
);
await docker.pruneImages({ filters: { dangling: { false: true } } });
};
beforeEach(async () => {
await cleanup();
});
after(async () => {
await cleanup();
});
it('can execute a single service plan', async () => {
const agent = Agent.from<Device>({
initial: {
name: 'test-device',
uuid: 'd0',
apps: {},
keys: {},
images: {},
},
planner,
opts: { minWaitMs: 1000, trace },
});
agent.seek({
apps: {
a0: {
name: 'test-app',
releases: {
r0: {
services: {
main: {
image: 'alpine:latest',
command: ['sleep', 'infinity'],
status: 'running',
},
},
},
},
},
},
});
expect(await agent.wait(), 'starting a single container should succeed')
.to.have.property('success')
.that.equals(true);
const device = agent.state();
expect(device.apps.a0).to.not.be.undefined;
expect(device.apps.a0.releases).to.not.be.undefined;
expect(device.apps.a0.releases.r0).to.not.be.undefined;
expect(device.apps.a0.releases.r0.services).to.not.be.undefined;
const service = device.apps.a0.releases.r0.services.main;
expect(service).to.not.be.undefined;
expect(service.containerId).to.not.be.undefined;
expect(
(await docker.getContainer(service.containerId!).inspect()).State,
'container is running after plan is executed',
)
.to.have.property('Running')
.that.equals(true);
// Update the target
agent.seek({
apps: {
a0: {
name: 'test-app',
releases: {
r0: {
services: {
main: {
status: 'stopped',
},
},
},
},
},
},
});
expect(await agent.wait(), 'stopping the container should succeed')
.to.have.property('success')
.that.equals(true);
expect(
(await docker.getContainer(service.containerId!).inspect()).State,
'container is stopped after the plan is executed',
)
.to.have.property('Running')
.that.equals(false);
logger.info('Restarting container');
agent.seek({
apps: {
a0: {
name: 'test-app',
releases: {
r0: {
services: {
main: {
status: 'running',
},
},
},
},
},
},
});
expect(await agent.wait(), 'starting the container should succeed')
.to.have.property('success')
.that.equals(true);
expect(
(await docker.getContainer(service.containerId!).inspect()).State,
'container is running after the plan is executed',
)
.to.have.property('Running')
.that.equals(true);
// Update to a new release
logger.info('Update to a new release');
agent.seek({
apps: {
a0: {
name: 'test-app',
releases: {
r0: UNDEFINED,
r1: {
services: {
main: {
image: 'alpine:3.13',
status: 'running',
command: ['sleep', 'infinity'],
},
},
},
},
},
},
});
expect(await agent.wait(), 'updating to a new release should succeed')
.to.have.property('success')
.that.equals(true);
const newDevice = agent.state();
const newService = newDevice.apps.a0.releases.r1.services.main;
expect(newService).to.not.be.undefined;
expect(newService.containerId).to.not.be.undefined;
expect(newDevice.apps.a0.releases.r0).to.be.undefined;
expect(
newService.containerId,
'the container is recreated during execution',
).to.not.equal(service.containerId);
expect((await docker.getContainer(newService.containerId!).inspect()).State)
.to.have.property('Running')
.that.equals(true);
// Update to a new release
logger.info('Uninstall app');
agent.seek({
apps: {
a0: UNDEFINED,
},
});
expect(await agent.wait(), 'delete the release should succeed')
.to.have.property('success')
.that.equals(true);
expect(agent.state().apps).to.be.empty;
await expect(docker.getContainer('r1_main').inspect()).to.be.rejected;
});
});