-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.spec.ts
136 lines (107 loc) · 4.22 KB
/
index.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
const execMocks = {
exec: jest.fn(),
};
jest.mock('@cloud-cli/exec', () => {
return execMocks;
});
import vm from './index';
import * as exec from '@cloud-cli/exec';
const inspectOutput = `[{
"CreatedAt": "2023-03-10T10:15:10Z",
"Driver": "local",
"Labels": {
"origin": "test/container",
"version": "534524"
},
"Mountpoint": "/var/lib/docker/volumes/test/_data",
"Name": "test",
"Options": {},
"Scope": "local"
}]`;
describe('volume manager', () => {
describe('list', () => {
it('should list the available volumes', async () => {
const execOutput = {
ok: true,
stdout: `test\ntest2\n`,
};
execMocks.exec = jest.fn().mockResolvedValue(execOutput);
await expect(vm.list()).resolves.toEqual(['test', 'test2']);
expect(exec.exec).toHaveBeenCalledWith('docker', ['volume', 'ls', '--format="{{.Name}}"']);
});
it('should reject in case of error', async () => {
const output = { ok: false };
execMocks.exec = jest.fn().mockResolvedValue(output);
await expect(vm.list()).rejects.toThrowError('Unable to list volumes');
});
});
describe('show', () => {
it('should show details of a volume', async () => {
const execOutput = { ok: true, stdout: inspectOutput };
execMocks.exec = jest.fn().mockResolvedValue(execOutput);
await expect(vm.show({ name: 'test' })).resolves.toEqual({
name: 'test',
createdAt: '2023-03-10T10:15:10Z',
labels: {
origin: 'test/container',
version: '534524',
},
});
expect(exec.exec).toHaveBeenCalledWith('docker', ['volume', 'inspect', 'test']);
});
it('should throw error if volume name is invalid', async () => {
execMocks.exec = jest.fn();
await expect(vm.show({ name: 'In$%valid' })).rejects.toThrowError('Invalid name');
expect(exec.exec).not.toHaveBeenCalled();
});
it('should throw error if volume does not exist', async () => {
const output = { ok: true, stdout: '[]' };
execMocks.exec = jest.fn().mockResolvedValue(output);
await expect(vm.show({ name: 'invalid' })).rejects.toThrowError('Volume not found');
});
});
describe('add', () => {
it('should add a volume', async () => {
const execOutput = { ok: true, stdout: 'test' };
execMocks.exec = jest.fn().mockResolvedValue(execOutput);
await expect(vm.add({ name: 'test' })).resolves.toEqual('');
expect(exec.exec).toHaveBeenCalledWith('docker', ['volume', 'create', 'test']);
});
it('should throw error if volume name is invalid', async () => {
execMocks.exec = jest.fn();
await expect(vm.add({ name: 'In$%valid' })).rejects.toThrowError('Invalid name');
expect(exec.exec).not.toHaveBeenCalled();
});
it('should throw error if volume creation fails', async () => {
const output = { ok: false, stdout: '' };
execMocks.exec = jest.fn().mockResolvedValue(output);
await expect(vm.add({ name: 'failed' })).rejects.toThrowError('Unable to create volume');
});
});
describe('delete', () => {
it('should add a volume', async () => {
const execOutput = { ok: true, stdout: '' };
execMocks.exec = jest.fn().mockResolvedValue(execOutput);
await expect(vm.remove({ name: 'test' })).resolves.toEqual('');
expect(exec.exec).toHaveBeenCalledWith('docker', ['volume', 'rm', 'test']);
});
it('should throw error if volume name is invalid', async () => {
execMocks.exec = jest.fn();
await expect(vm.remove({ name: 'In$%valid' })).rejects.toThrowError('Invalid name');
expect(exec.exec).not.toHaveBeenCalled();
});
it('should throw error if volume removal fails', async () => {
const output = { ok: false, stdout: '' };
execMocks.exec = jest.fn().mockResolvedValue(output);
await expect(vm.remove({ name: 'failed' })).rejects.toThrowError('Unable to remove volume');
});
});
describe('prune', () => {
it('should remove ununsed volumes', async () => {
const execOutput = { ok: true, stdout: '' };
execMocks.exec = jest.fn().mockResolvedValue(execOutput);
await expect(vm.prune()).resolves.toEqual('');
expect(exec.exec).toHaveBeenCalledWith('docker', ['volume', 'prune']);
});
});
});