2021-05-05 20:32:31 +00:00
|
|
|
import { expect } from 'chai';
|
2022-08-17 23:35:08 +00:00
|
|
|
import Volume from '~/src/compose/volume';
|
2021-05-05 20:32:31 +00:00
|
|
|
|
2022-08-24 20:02:19 +00:00
|
|
|
describe('compose/volume: unit tests', () => {
|
2021-05-05 20:32:31 +00:00
|
|
|
describe('creating a volume from a compose object', () => {
|
|
|
|
it('should use proper defaults when no compose configuration is provided', () => {
|
2021-08-25 21:50:34 +00:00
|
|
|
const volume = Volume.fromComposeObject(
|
|
|
|
'my_volume',
|
|
|
|
1234,
|
|
|
|
'deadbeef',
|
|
|
|
{},
|
|
|
|
);
|
2021-05-05 20:32:31 +00:00
|
|
|
|
|
|
|
expect(volume.name).to.equal('my_volume');
|
|
|
|
expect(volume.appId).to.equal(1234);
|
2021-08-25 21:50:34 +00:00
|
|
|
expect(volume.appUuid).to.equal('deadbeef');
|
2021-05-05 20:32:31 +00:00
|
|
|
expect(volume.config).to.deep.equal({
|
|
|
|
driver: 'local',
|
|
|
|
driverOpts: {},
|
|
|
|
labels: {
|
|
|
|
'io.balena.supervised': 'true',
|
2021-08-25 21:50:34 +00:00
|
|
|
'io.balena.app-uuid': 'deadbeef',
|
2021-05-05 20:32:31 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should correctly parse compose volumes without an explicit driver', () => {
|
2021-08-25 21:50:34 +00:00
|
|
|
const volume = Volume.fromComposeObject(
|
|
|
|
'one_volume',
|
|
|
|
1032480,
|
|
|
|
'deadbeef',
|
|
|
|
{
|
|
|
|
driver_opts: {
|
|
|
|
opt1: 'test',
|
|
|
|
},
|
|
|
|
labels: {
|
|
|
|
'my-label': 'test-label',
|
|
|
|
},
|
2021-05-05 20:32:31 +00:00
|
|
|
},
|
2021-08-25 21:50:34 +00:00
|
|
|
);
|
2021-05-05 20:32:31 +00:00
|
|
|
|
|
|
|
expect(volume).to.have.property('appId').that.equals(1032480);
|
|
|
|
expect(volume).to.have.property('name').that.equals('one_volume');
|
|
|
|
expect(volume)
|
|
|
|
.to.have.property('config')
|
|
|
|
.that.has.property('labels')
|
|
|
|
.that.deep.equals({
|
|
|
|
'io.balena.supervised': 'true',
|
2021-08-25 21:50:34 +00:00
|
|
|
'io.balena.app-uuid': 'deadbeef',
|
2021-05-05 20:32:31 +00:00
|
|
|
'my-label': 'test-label',
|
|
|
|
});
|
|
|
|
expect(volume)
|
|
|
|
.to.have.property('config')
|
|
|
|
.that.has.property('driverOpts')
|
|
|
|
.that.deep.equals({
|
|
|
|
opt1: 'test',
|
|
|
|
});
|
|
|
|
expect(volume)
|
|
|
|
.to.have.property('config')
|
|
|
|
.that.has.property('driver')
|
|
|
|
.that.equals('local');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should correctly parse compose volumes with an explicit driver', () => {
|
2021-08-25 21:50:34 +00:00
|
|
|
const volume = Volume.fromComposeObject(
|
|
|
|
'one_volume',
|
|
|
|
1032480,
|
|
|
|
'deadbeef',
|
|
|
|
{
|
|
|
|
driver: 'other',
|
|
|
|
driver_opts: {
|
|
|
|
opt1: 'test',
|
|
|
|
},
|
|
|
|
labels: {
|
|
|
|
'my-label': 'test-label',
|
|
|
|
},
|
2021-05-05 20:32:31 +00:00
|
|
|
},
|
2021-08-25 21:50:34 +00:00
|
|
|
);
|
2021-05-05 20:32:31 +00:00
|
|
|
|
|
|
|
expect(volume).to.have.property('appId').that.equals(1032480);
|
|
|
|
expect(volume).to.have.property('name').that.equals('one_volume');
|
|
|
|
expect(volume)
|
|
|
|
.to.have.property('config')
|
|
|
|
.that.has.property('labels')
|
|
|
|
.that.deep.equals({
|
|
|
|
'io.balena.supervised': 'true',
|
2021-08-25 21:50:34 +00:00
|
|
|
'io.balena.app-uuid': 'deadbeef',
|
2021-05-05 20:32:31 +00:00
|
|
|
'my-label': 'test-label',
|
|
|
|
});
|
|
|
|
expect(volume)
|
|
|
|
.to.have.property('config')
|
|
|
|
.that.has.property('driverOpts')
|
|
|
|
.that.deep.equals({
|
|
|
|
opt1: 'test',
|
|
|
|
});
|
|
|
|
expect(volume)
|
|
|
|
.to.have.property('config')
|
|
|
|
.that.has.property('driver')
|
|
|
|
.that.equals('other');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('creating a volume instance from a docker volume', () => {
|
|
|
|
it('should correctly parse app id from volume name', () => {
|
|
|
|
const volume = Volume.fromDockerVolume({
|
|
|
|
Driver: 'local',
|
|
|
|
Name: '1234_my_volume',
|
|
|
|
Mountpoint: '/var/lib/docker/volumes/1032480_one_volume/_data',
|
|
|
|
Labels: {},
|
|
|
|
Options: {},
|
|
|
|
Scope: 'local',
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(volume.name).to.equal('my_volume');
|
|
|
|
expect(volume.appId).to.equal(1234);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail if volume name is not properly formatted', () => {
|
|
|
|
expect(() =>
|
|
|
|
Volume.fromDockerVolume({
|
|
|
|
Driver: 'local',
|
|
|
|
Name: 'non_supervised_volume',
|
|
|
|
Mountpoint: '/var/lib/docker/volumes/1032480_one_volume/_data',
|
|
|
|
Labels: {},
|
|
|
|
Options: {},
|
|
|
|
Scope: 'local',
|
|
|
|
}),
|
|
|
|
).to.throw;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should correctly parse docker volumes', () => {
|
|
|
|
const volume = Volume.fromDockerVolume({
|
|
|
|
Driver: 'local',
|
|
|
|
Labels: {
|
|
|
|
'io.balena.supervised': 'true',
|
2021-08-25 21:50:34 +00:00
|
|
|
'io.balena.app-uuid': 'deadbeef',
|
2021-05-05 20:32:31 +00:00
|
|
|
},
|
|
|
|
Mountpoint: '/var/lib/docker/volumes/1032480_one_volume/_data',
|
|
|
|
Name: '1032480_one_volume',
|
|
|
|
Options: {},
|
|
|
|
Scope: 'local',
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(volume).to.have.property('appId').that.equals(1032480);
|
|
|
|
expect(volume).to.have.property('name').that.equals('one_volume');
|
2021-08-25 21:50:34 +00:00
|
|
|
expect(volume).to.have.property('appUuid').that.equals('deadbeef');
|
2021-05-05 20:32:31 +00:00
|
|
|
expect(volume)
|
|
|
|
.to.have.property('config')
|
|
|
|
.that.has.property('labels')
|
|
|
|
.that.deep.equals({
|
|
|
|
'io.balena.supervised': 'true',
|
2021-08-25 21:50:34 +00:00
|
|
|
'io.balena.app-uuid': 'deadbeef',
|
2021-05-05 20:32:31 +00:00
|
|
|
});
|
|
|
|
expect(volume)
|
|
|
|
.to.have.property('config')
|
|
|
|
.that.has.property('driverOpts')
|
|
|
|
.that.deep.equals({});
|
|
|
|
expect(volume)
|
|
|
|
.to.have.property('config')
|
|
|
|
.that.has.property('driver')
|
|
|
|
.that.equals('local');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('comparing volume configuration', () => {
|
|
|
|
it('should ignore name and supervisor labels in the comparison', () => {
|
|
|
|
expect(
|
2021-08-25 21:50:34 +00:00
|
|
|
Volume.fromComposeObject('aaa', 1234, 'deadbeef').isEqualConfig(
|
|
|
|
Volume.fromComposeObject('bbb', 4567, 'deadbeef', {
|
2021-05-05 20:32:31 +00:00
|
|
|
driver: 'local',
|
|
|
|
driver_opts: {},
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
).to.be.true;
|
|
|
|
|
|
|
|
expect(
|
2021-08-25 21:50:34 +00:00
|
|
|
Volume.fromComposeObject('aaa', 1234, 'deadbeef').isEqualConfig(
|
|
|
|
Volume.fromComposeObject('bbb', 4567, 'deadc0de'),
|
|
|
|
),
|
|
|
|
).to.be.true;
|
|
|
|
|
|
|
|
expect(
|
|
|
|
Volume.fromComposeObject('aaa', 1234, 'deadbeef').isEqualConfig(
|
|
|
|
Volume.fromDockerVolume({
|
|
|
|
Name: '1234_aaa',
|
|
|
|
Driver: 'local',
|
|
|
|
Labels: {
|
|
|
|
'io.balena.supervised': 'true',
|
|
|
|
'io.balena.app-uuid': 'deadbeef',
|
|
|
|
},
|
|
|
|
Options: {},
|
|
|
|
Mountpoint: '/var/lib/docker/volumes/1032480_one_volume/_data',
|
|
|
|
Scope: 'local',
|
|
|
|
}),
|
2021-05-05 20:32:31 +00:00
|
|
|
),
|
|
|
|
).to.be.true;
|
|
|
|
|
2021-08-25 21:50:34 +00:00
|
|
|
// the app-uuid should be omitted from the comparison
|
2021-05-05 20:32:31 +00:00
|
|
|
expect(
|
2021-08-25 21:50:34 +00:00
|
|
|
Volume.fromComposeObject('aaa', 1234, 'deadbeef').isEqualConfig(
|
2021-05-05 20:32:31 +00:00
|
|
|
Volume.fromDockerVolume({
|
|
|
|
Name: '1234_aaa',
|
|
|
|
Driver: 'local',
|
|
|
|
Labels: {
|
|
|
|
'io.balena.supervised': 'true',
|
|
|
|
},
|
|
|
|
Options: {},
|
|
|
|
Mountpoint: '/var/lib/docker/volumes/1032480_one_volume/_data',
|
|
|
|
Scope: 'local',
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
).to.be.true;
|
|
|
|
|
|
|
|
expect(
|
2021-08-25 21:50:34 +00:00
|
|
|
Volume.fromComposeObject('aaa', 1234, null as any).isEqualConfig(
|
2021-05-05 20:32:31 +00:00
|
|
|
Volume.fromDockerVolume({
|
|
|
|
Name: '4567_bbb',
|
|
|
|
Driver: 'local',
|
|
|
|
Labels: {
|
|
|
|
'io.balena.supervised': 'true',
|
|
|
|
},
|
|
|
|
Options: {},
|
|
|
|
Mountpoint: '/var/lib/docker/volumes/1032480_one_volume/_data',
|
|
|
|
Scope: 'local',
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
).to.be.true;
|
|
|
|
|
|
|
|
expect(
|
2021-08-25 21:50:34 +00:00
|
|
|
Volume.fromComposeObject('aaa', 1234, 'deadbeef').isEqualConfig(
|
2021-05-05 20:32:31 +00:00
|
|
|
Volume.fromDockerVolume({
|
|
|
|
Name: '1234_aaa',
|
|
|
|
Driver: 'local',
|
|
|
|
Labels: {
|
|
|
|
'some.other.label': '123',
|
|
|
|
'io.balena.supervised': 'true',
|
2021-08-25 21:50:34 +00:00
|
|
|
'io.balena.app-uuid': 'deadbeef',
|
2021-05-05 20:32:31 +00:00
|
|
|
},
|
|
|
|
Options: {},
|
|
|
|
Mountpoint: '/var/lib/docker/volumes/1032480_one_volume/_data',
|
|
|
|
Scope: 'local',
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
).to.be.false;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should compare based on driver configuration and options', () => {
|
|
|
|
expect(
|
2021-08-25 21:50:34 +00:00
|
|
|
Volume.fromComposeObject('aaa', 1234, 'deadbeef').isEqualConfig(
|
|
|
|
Volume.fromComposeObject('aaa', 1234, 'deadbeef', {
|
2021-05-05 20:32:31 +00:00
|
|
|
driver: 'other',
|
|
|
|
driver_opts: {},
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
).to.be.false;
|
|
|
|
|
|
|
|
expect(
|
2021-08-25 21:50:34 +00:00
|
|
|
Volume.fromComposeObject('aaa', 1234, 'deadbeef', {
|
2021-05-05 20:32:31 +00:00
|
|
|
driver: 'other',
|
|
|
|
}).isEqualConfig(
|
2021-08-25 21:50:34 +00:00
|
|
|
Volume.fromComposeObject('aaa', 1234, 'deadbeef', {
|
2021-05-05 20:32:31 +00:00
|
|
|
driver: 'other',
|
|
|
|
driver_opts: {},
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
).to.be.true;
|
|
|
|
|
|
|
|
expect(
|
2021-08-25 21:50:34 +00:00
|
|
|
Volume.fromComposeObject('aaa', 1234, 'deadbeef', {}).isEqualConfig(
|
|
|
|
Volume.fromComposeObject('aaa', 1234, 'deadbeef', {
|
2021-05-05 20:32:31 +00:00
|
|
|
driver_opts: { opt: '123' },
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
).to.be.false;
|
|
|
|
|
|
|
|
expect(
|
2021-08-25 21:50:34 +00:00
|
|
|
Volume.fromComposeObject('aaa', 1234, 'deadbeef', {
|
2021-05-05 20:32:31 +00:00
|
|
|
driver: 'other',
|
|
|
|
labels: { 'some.other.label': '123' },
|
|
|
|
driver_opts: { 'some-opt': '123' },
|
|
|
|
}).isEqualConfig(
|
|
|
|
Volume.fromDockerVolume({
|
|
|
|
Name: '1234_aaa',
|
|
|
|
Driver: 'other',
|
|
|
|
Labels: {
|
|
|
|
'some.other.label': '123',
|
|
|
|
'io.balena.supervised': 'true',
|
|
|
|
},
|
|
|
|
Options: {},
|
|
|
|
Mountpoint: '/var/lib/docker/volumes/1032480_one_volume/_data',
|
|
|
|
Scope: 'local',
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
).to.be.false;
|
|
|
|
|
|
|
|
expect(
|
2021-08-25 21:50:34 +00:00
|
|
|
Volume.fromComposeObject('aaa', 1234, 'deadbeef', {
|
2021-05-05 20:32:31 +00:00
|
|
|
driver: 'other',
|
|
|
|
labels: { 'some.other.label': '123' },
|
|
|
|
driver_opts: { 'some-opt': '123' },
|
|
|
|
}).isEqualConfig(
|
|
|
|
Volume.fromDockerVolume({
|
|
|
|
Name: '1234_aaa',
|
|
|
|
Driver: 'other',
|
|
|
|
Labels: {
|
|
|
|
'some.other.label': '123',
|
|
|
|
'io.balena.supervised': 'true',
|
|
|
|
},
|
|
|
|
Options: { 'some-opt': '123' },
|
|
|
|
Mountpoint: '/var/lib/docker/volumes/1032480_one_volume/_data',
|
|
|
|
Scope: 'local',
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
).to.be.true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|