All files / test composer.spec.ts

100% Statements 31/31
100% Branches 2/2
100% Functions 10/10
100% Lines 31/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 651x   1x             1x   1x     9x 9x   9x   9x 5x     9x 9x   9x 9x     1x 1x 1x 1x     1x 1x     1x 1x     1x 1x     1x 1x     1x 1x     1x 1x     1x 1x      
import { expect } from 'chai';
import * as DockerModels from '../lib/dockerModels';
import { compose, createNetworkMap, NetworkMap } from '../lib/index';
 
// https://journal.artfuldev.com/write-tests-for-typescript-projects-with-mocha-and-chai-in-typescript-86e053bdb2b6
// if you used the '@types/mocha' method to install mocha type definitions, uncomment the following line
// import 'mocha';
// Why? Seems to work fine without it
 
import * as fs from 'fs';
 
const fileBaseDir = './test/expected_io';
 
function testFilePair(fileBaseName: string) {
  const jsonFileContents = fs.readFileSync(fileBaseDir + '/' + fileBaseName + '.json', 'utf-8');
  const ymlFileContents = fs.readFileSync(fileBaseDir + '/' + fileBaseName + '.yml', 'utf-8');
 
  let netFileContents = '[]\n';
 
  if (fs.existsSync(fileBaseDir + '/' + fileBaseName + '_net.json')) {
    netFileContents = fs.readFileSync(fileBaseDir + '/' + fileBaseName + '_net.json', 'utf-8');
}
 
  const services: DockerModels.Service[] = JSON.parse(jsonFileContents);
  const networkMap: NetworkMap = createNetworkMap(JSON.parse(netFileContents));
 
  const result = compose(services, networkMap);
  expect(result).to.equal(ymlFileContents);
}
 
describe('compose()', () => {
  it('should return the service name without stack prefix and image name with tag', () => {
    testFilePair('service_name_1');
    testFilePair('service_name_2');
  });
 
  it('should return labels set using the labels/ key', () => {
    testFilePair('labels_1');
  });
 
  it('should return labels set using the /deploy/labels key', () => {
    testFilePair('labels_2');
  });
 
  it('should return overlay networks', () => {
    testFilePair('networks_1');
  });
 
  it('should return external networks', () => {
    testFilePair('networks_2');
  });
 
  it('should return environment variables', () => {
    testFilePair('environment_1');
  });
 
  it('should return constraints', () => {
    testFilePair('constraints_1');
  });
 
  it('should return bind mounts', () => {
    testFilePair('volumes_1');
  });
});