2019-07-02 12:51:09 +00:00
|
|
|
import { expect } from 'chai';
|
2020-06-02 16:56:58 +00:00
|
|
|
import { stub, SinonStub } from 'sinon';
|
|
|
|
|
|
|
|
import { docker } from '../src/lib/docker-utils';
|
2020-06-08 12:57:31 +00:00
|
|
|
import * as logger from '../src/logger';
|
2019-07-02 12:51:09 +00:00
|
|
|
|
|
|
|
import Volume from '../src/compose/volume';
|
|
|
|
import logTypes = require('../src/lib/log-types');
|
|
|
|
|
|
|
|
describe('Compose volumes', () => {
|
2020-06-02 16:56:58 +00:00
|
|
|
let createVolumeStub: SinonStub;
|
2020-06-08 12:57:31 +00:00
|
|
|
let logSystemStub: SinonStub;
|
|
|
|
let logMessageStub: SinonStub;
|
2020-06-02 16:56:58 +00:00
|
|
|
before(() => {
|
|
|
|
createVolumeStub = stub(docker, 'createVolume');
|
2020-06-08 12:57:31 +00:00
|
|
|
logSystemStub = stub(logger, 'logSystemEvent');
|
|
|
|
logMessageStub = stub(logger, 'logSystemMessage');
|
2020-06-02 16:56:58 +00:00
|
|
|
});
|
|
|
|
after(() => {
|
|
|
|
createVolumeStub.restore();
|
2020-06-08 12:57:31 +00:00
|
|
|
logSystemStub.restore();
|
|
|
|
logMessageStub.restore();
|
2020-06-02 16:56:58 +00:00
|
|
|
});
|
|
|
|
|
2019-07-02 12:51:09 +00:00
|
|
|
describe('Parsing volumes', () => {
|
|
|
|
it('should correctly parse docker volumes', () => {
|
2020-06-08 12:57:31 +00:00
|
|
|
const volume = Volume.fromDockerVolume({
|
2019-07-02 12:51:09 +00:00
|
|
|
Driver: 'local',
|
|
|
|
Labels: {
|
|
|
|
'io.balena.supervised': 'true',
|
|
|
|
},
|
|
|
|
Mountpoint: '/var/lib/docker/volumes/1032480_one_volume/_data',
|
|
|
|
Name: '1032480_one_volume',
|
|
|
|
Options: {},
|
|
|
|
Scope: 'local',
|
|
|
|
});
|
|
|
|
|
2020-05-15 11:01:51 +00:00
|
|
|
expect(volume).to.have.property('appId').that.equals(1032480);
|
|
|
|
expect(volume).to.have.property('name').that.equals('one_volume');
|
2019-07-02 12:51:09 +00:00
|
|
|
expect(volume)
|
|
|
|
.to.have.property('config')
|
|
|
|
.that.has.property('labels')
|
|
|
|
.that.deep.equals({
|
|
|
|
'io.balena.supervised': 'true',
|
|
|
|
});
|
|
|
|
expect(volume)
|
|
|
|
.to.have.property('config')
|
|
|
|
.that.has.property('driverOpts')
|
|
|
|
.that.deep.equals({});
|
2019-12-02 10:05:31 +00:00
|
|
|
expect(volume)
|
|
|
|
.to.have.property('config')
|
|
|
|
.that.has.property('driver')
|
|
|
|
.that.equals('local');
|
2019-07-02 12:51:09 +00:00
|
|
|
});
|
|
|
|
|
2019-12-02 10:05:31 +00:00
|
|
|
it('should correctly parse compose volumes without an explicit driver', () => {
|
2020-06-08 12:57:31 +00:00
|
|
|
const volume = Volume.fromComposeObject('one_volume', 1032480, {
|
|
|
|
driver_opts: {
|
|
|
|
opt1: 'test',
|
2019-07-02 12:51:09 +00:00
|
|
|
},
|
2020-06-08 12:57:31 +00:00
|
|
|
labels: {
|
|
|
|
'my-label': 'test-label',
|
|
|
|
},
|
|
|
|
});
|
2019-07-02 12:51:09 +00:00
|
|
|
|
2020-05-15 11:01:51 +00:00
|
|
|
expect(volume).to.have.property('appId').that.equals(1032480);
|
|
|
|
expect(volume).to.have.property('name').that.equals('one_volume');
|
2019-07-02 12:51:09 +00:00
|
|
|
expect(volume)
|
|
|
|
.to.have.property('config')
|
|
|
|
.that.has.property('labels')
|
|
|
|
.that.deep.equals({
|
|
|
|
'io.balena.supervised': 'true',
|
|
|
|
'my-label': 'test-label',
|
|
|
|
});
|
|
|
|
expect(volume)
|
|
|
|
.to.have.property('config')
|
|
|
|
.that.has.property('driverOpts')
|
|
|
|
.that.deep.equals({
|
|
|
|
opt1: 'test',
|
|
|
|
});
|
2019-12-02 10:05:31 +00:00
|
|
|
expect(volume)
|
|
|
|
.to.have.property('config')
|
|
|
|
.that.has.property('driver')
|
|
|
|
.that.equals('local');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should correctly parse compose volumes with an explicit driver', () => {
|
2020-06-08 12:57:31 +00:00
|
|
|
const volume = Volume.fromComposeObject('one_volume', 1032480, {
|
|
|
|
driver: 'other',
|
|
|
|
driver_opts: {
|
|
|
|
opt1: 'test',
|
2019-12-02 10:05:31 +00:00
|
|
|
},
|
2020-06-08 12:57:31 +00:00
|
|
|
labels: {
|
|
|
|
'my-label': 'test-label',
|
|
|
|
},
|
|
|
|
});
|
2019-12-02 10:05:31 +00:00
|
|
|
|
2020-05-15 11:01:51 +00:00
|
|
|
expect(volume).to.have.property('appId').that.equals(1032480);
|
|
|
|
expect(volume).to.have.property('name').that.equals('one_volume');
|
2019-12-02 10:05:31 +00:00
|
|
|
expect(volume)
|
|
|
|
.to.have.property('config')
|
|
|
|
.that.has.property('labels')
|
|
|
|
.that.deep.equals({
|
|
|
|
'io.balena.supervised': 'true',
|
|
|
|
'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');
|
2019-07-02 12:51:09 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Generating docker options', () => {
|
|
|
|
afterEach(() => {
|
2020-06-02 16:56:58 +00:00
|
|
|
createVolumeStub.reset();
|
2020-06-08 12:57:31 +00:00
|
|
|
logSystemStub.reset();
|
|
|
|
logMessageStub.reset();
|
2019-07-02 12:51:09 +00:00
|
|
|
});
|
|
|
|
it('should correctly generate docker options', async () => {
|
2020-06-08 12:57:31 +00:00
|
|
|
const volume = Volume.fromComposeObject('one_volume', 1032480, {
|
|
|
|
driver_opts: {
|
|
|
|
opt1: 'test',
|
2019-07-02 12:51:09 +00:00
|
|
|
},
|
2020-06-08 12:57:31 +00:00
|
|
|
labels: {
|
|
|
|
'my-label': 'test-label',
|
|
|
|
},
|
|
|
|
});
|
2019-07-02 12:51:09 +00:00
|
|
|
|
|
|
|
await volume.create();
|
|
|
|
expect(
|
2020-06-02 16:56:58 +00:00
|
|
|
createVolumeStub.calledWith({
|
2019-07-02 12:51:09 +00:00
|
|
|
Labels: {
|
|
|
|
'my-label': 'test-label',
|
|
|
|
'io.balena.supervised': 'true',
|
|
|
|
},
|
|
|
|
Options: {
|
|
|
|
opt1: 'test',
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2020-06-08 12:57:31 +00:00
|
|
|
expect(logSystemStub.calledWith(logTypes.createVolume));
|
2019-07-02 12:51:09 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|