Convert test/17-config-utils.spec.coffee to javascript

Change-type: patch
This commit is contained in:
Pagan Gazzard 2020-03-31 16:46:04 +01:00
parent dcb01cb863
commit d3cf650ac4
2 changed files with 142 additions and 120 deletions

View File

@ -1,120 +0,0 @@
{ expect } = require './lib/chai-config'
{ stub } = require 'sinon'
{ fs } = require 'mz'
configUtils = require '../src/config/utils'
{ ExtlinuxConfigBackend, RPiConfigBackend } = require '../src/config/backend'
extlinuxBackend = new ExtlinuxConfigBackend()
rpiBackend = new RPiConfigBackend()
describe 'Config Utilities', ->
describe 'Boot config utilities', ->
describe 'Env <-> Config', ->
it 'correctly transforms environments to boot config objects', ->
bootConfig = configUtils.envToBootConfig(rpiBackend, {
HOST_CONFIG_initramfs: 'initramf.gz 0x00800000'
HOST_CONFIG_dtparam: '"i2c=on","audio=on"'
HOST_CONFIG_dtoverlay: '"ads7846","lirc-rpi,gpio_out_pin=17,gpio_in_pin=13"'
HOST_CONFIG_foobar: 'baz'
})
expect(bootConfig).to.deep.equal({
initramfs: 'initramf.gz 0x00800000'
dtparam: [ 'i2c=on', 'audio=on' ]
dtoverlay: [ 'ads7846', 'lirc-rpi,gpio_out_pin=17,gpio_in_pin=13' ]
foobar: 'baz'
})
describe 'TX2 boot config utilities', ->
it 'should parse a extlinux.conf file', ->
text = '''
DEFAULT primary
# Comment
TIMEOUT 30
MENU TITLE Boot Options
LABEL primary
MENU LABEL primary Image
LINUX /Image
APPEND ${cbootargs} ${resin_kernel_root} ro rootwait
'''
parsed = ExtlinuxConfigBackend.parseExtlinuxFile(text)
expect(parsed.globals).to.have.property('DEFAULT').that.equals('primary')
expect(parsed.globals).to.have.property('TIMEOUT').that.equals('30')
expect(parsed.globals).to.have.property('MENU TITLE').that.equals('Boot Options')
expect(parsed.labels).to.have.property('primary')
primary = parsed.labels.primary
expect(primary).to.have.property('MENU LABEL').that.equals('primary Image')
expect(primary).to.have.property('LINUX').that.equals('/Image')
expect(primary).to.have.property('APPEND').that.equals('${cbootargs} ${resin_kernel_root} ro rootwait')
it 'should parse multiple service entries', ->
text = '''
DEFAULT primary
# Comment
TIMEOUT 30
MENU TITLE Boot Options
LABEL primary
LINUX test1
APPEND test2
LABEL secondary
LINUX test3
APPEND test4
'''
parsed = ExtlinuxConfigBackend.parseExtlinuxFile(text)
expect(parsed.labels).to.have.property('primary').that.deep.equals({
LINUX: 'test1'
APPEND: 'test2'
})
expect(parsed.labels).to.have.property('secondary').that.deep.equals({
LINUX: 'test3'
APPEND: 'test4'
})
it 'should parse configuration options from an extlinux.conf file', ->
text = '''
DEFAULT primary
# Comment
TIMEOUT 30
MENU TITLE Boot Options
LABEL primary
MENU LABEL primary Image
LINUX /Image
APPEND ${cbootargs} ${resin_kernel_root} ro rootwait isolcpus=3
'''
stub(fs, 'readFile').resolves(text)
parsed = extlinuxBackend.getBootConfig()
expect(parsed).to.eventually.have.property('isolcpus').that.equals('3')
fs.readFile.restore()
text = '''
DEFAULT primary
# Comment
TIMEOUT 30
MENU TITLE Boot Options
LABEL primary
MENU LABEL primary Image
LINUX /Image
APPEND ${cbootargs} ${resin_kernel_root} ro rootwait isolcpus=3,4,5
'''
stub(fs, 'readFile').resolves(text)
parsed = extlinuxBackend.getBootConfig()
fs.readFile.restore()
expect(parsed).to.eventually.have.property('isolcpus').that.equals('3,4,5')

View File

@ -0,0 +1,142 @@
import { expect } from './lib/chai-config';
import { stub } from 'sinon';
import { fs } from 'mz';
import * as configUtils from '../src/config/utils';
import { ExtlinuxConfigBackend, RPiConfigBackend } from '../src/config/backend';
const extlinuxBackend = new ExtlinuxConfigBackend();
const rpiBackend = new RPiConfigBackend();
describe('Config Utilities', () =>
describe('Boot config utilities', function() {
describe('Env <-> Config', () =>
it('correctly transforms environments to boot config objects', function() {
const bootConfig = configUtils.envToBootConfig(rpiBackend, {
HOST_CONFIG_initramfs: 'initramf.gz 0x00800000',
HOST_CONFIG_dtparam: '"i2c=on","audio=on"',
HOST_CONFIG_dtoverlay:
'"ads7846","lirc-rpi,gpio_out_pin=17,gpio_in_pin=13"',
HOST_CONFIG_foobar: 'baz',
});
expect(bootConfig).to.deep.equal({
initramfs: 'initramf.gz 0x00800000',
dtparam: ['i2c=on', 'audio=on'],
dtoverlay: ['ads7846', 'lirc-rpi,gpio_out_pin=17,gpio_in_pin=13'],
foobar: 'baz',
});
}));
describe('TX2 boot config utilities', function() {
it('should parse a extlinux.conf file', function() {
const text = `\
DEFAULT primary
# Comment
TIMEOUT 30
MENU TITLE Boot Options
LABEL primary
MENU LABEL primary Image
LINUX /Image
APPEND \${cbootargs} \${resin_kernel_root} ro rootwait\
`;
// @ts-ignore accessing private method
const parsed = ExtlinuxConfigBackend.parseExtlinuxFile(text);
expect(parsed.globals)
.to.have.property('DEFAULT')
.that.equals('primary');
expect(parsed.globals)
.to.have.property('TIMEOUT')
.that.equals('30');
expect(parsed.globals)
.to.have.property('MENU TITLE')
.that.equals('Boot Options');
expect(parsed.labels).to.have.property('primary');
const { primary } = parsed.labels;
expect(primary)
.to.have.property('MENU LABEL')
.that.equals('primary Image');
expect(primary)
.to.have.property('LINUX')
.that.equals('/Image');
expect(primary)
.to.have.property('APPEND')
.that.equals('${cbootargs} ${resin_kernel_root} ro rootwait');
});
it('should parse multiple service entries', function() {
const text = `\
DEFAULT primary
# Comment
TIMEOUT 30
MENU TITLE Boot Options
LABEL primary
LINUX test1
APPEND test2
LABEL secondary
LINUX test3
APPEND test4\
`;
// @ts-ignore accessing private method
const parsed = ExtlinuxConfigBackend.parseExtlinuxFile(text);
expect(parsed.labels)
.to.have.property('primary')
.that.deep.equals({
LINUX: 'test1',
APPEND: 'test2',
});
expect(parsed.labels)
.to.have.property('secondary')
.that.deep.equals({
LINUX: 'test3',
APPEND: 'test4',
});
});
it('should parse configuration options from an extlinux.conf file', function() {
let text = `\
DEFAULT primary
# Comment
TIMEOUT 30
MENU TITLE Boot Options
LABEL primary
MENU LABEL primary Image
LINUX /Image
APPEND \${cbootargs} \${resin_kernel_root} ro rootwait isolcpus=3\
`;
let readFileStub = stub(fs, 'readFile').resolves(text);
let parsed = extlinuxBackend.getBootConfig();
expect(parsed)
.to.eventually.have.property('isolcpus')
.that.equals('3');
readFileStub.restore();
text = `\
DEFAULT primary
# Comment
TIMEOUT 30
MENU TITLE Boot Options
LABEL primary
MENU LABEL primary Image
LINUX /Image
APPEND \${cbootargs} \${resin_kernel_root} ro rootwait isolcpus=3,4,5\
`;
readFileStub = stub(fs, 'readFile').resolves(text);
parsed = extlinuxBackend.getBootConfig();
readFileStub.restore();
expect(parsed)
.to.eventually.have.property('isolcpus')
.that.equals('3,4,5');
});
});
}));