|
| 1 | +const {expect, use} = require('chai'); |
| 2 | +const sinon = require('sinon'); |
| 3 | + |
| 4 | +const fs = require('fs-extra'); |
| 5 | +const execa = require('execa'); |
| 6 | +const {errors} = require('../../../lib'); |
| 7 | + |
| 8 | +const {checkUnitFile, checkNodeVersion} = require('../doctor'); |
| 9 | + |
| 10 | +use(require('chai-as-promised')); |
| 11 | + |
| 12 | +describe('Unit: Systemd > doctor checks', function () { |
| 13 | + afterEach(function () { |
| 14 | + sinon.restore(); |
| 15 | + }); |
| 16 | + |
| 17 | + describe('checkUnitFile', function () { |
| 18 | + it('errors when readFile errors', async function () { |
| 19 | + const readFile = sinon.stub(fs, 'readFile').rejects(new Error('test')); |
| 20 | + const ctx = { |
| 21 | + instance: {name: 'test'} |
| 22 | + }; |
| 23 | + |
| 24 | + const expectedPath = '/lib/systemd/system/ghost_test.service'; |
| 25 | + |
| 26 | + await expect(checkUnitFile(ctx)).to.be.rejectedWith(errors.SystemError); |
| 27 | + expect(readFile.calledOnceWithExactly(expectedPath)).to.be.true; |
| 28 | + expect(ctx.systemd).to.deep.equal({unitFilePath: expectedPath}); |
| 29 | + }); |
| 30 | + |
| 31 | + it('adds valid unit file to context', async function () { |
| 32 | + const readFile = sinon.stub(fs, 'readFile').resolves(` |
| 33 | +[Section1] |
| 34 | +Foo=Bar |
| 35 | +Baz = Bat |
| 36 | +
|
| 37 | +[Section2] |
| 38 | +Test=Value |
| 39 | + `); |
| 40 | + |
| 41 | + const ctx = { |
| 42 | + instance: {name: 'test'} |
| 43 | + }; |
| 44 | + |
| 45 | + const expectedPath = '/lib/systemd/system/ghost_test.service'; |
| 46 | + const expectedCtx = { |
| 47 | + unitFilePath: expectedPath, |
| 48 | + unit: { |
| 49 | + Section1: { |
| 50 | + Foo: 'Bar', |
| 51 | + Baz: 'Bat' |
| 52 | + }, |
| 53 | + Section2: { |
| 54 | + Test: 'Value' |
| 55 | + } |
| 56 | + } |
| 57 | + }; |
| 58 | + |
| 59 | + await expect(checkUnitFile(ctx)).to.not.be.rejected; |
| 60 | + expect(readFile.calledOnceWithExactly(expectedPath)).to.be.true; |
| 61 | + expect(ctx.systemd).to.deep.equal(expectedCtx); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + describe('checkNodeVersion', function () { |
| 66 | + it('rejects if ExecStart line not found', async function () { |
| 67 | + const ctx = { |
| 68 | + systemd: { |
| 69 | + unitFilePath: '/tmp/unit-file', |
| 70 | + unit: {} |
| 71 | + } |
| 72 | + }; |
| 73 | + const task = {}; |
| 74 | + |
| 75 | + await expect(checkNodeVersion(ctx, task)).to.be.rejectedWith(errors.SystemError); |
| 76 | + }); |
| 77 | + |
| 78 | + it('rejects if node --version rejects', async function () { |
| 79 | + const stdout = sinon.stub(execa, 'stdout').rejects(new Error('test error')); |
| 80 | + |
| 81 | + const ctx = { |
| 82 | + systemd: { |
| 83 | + unitFilePath: '/tmp/unit-file', |
| 84 | + unit: { |
| 85 | + Service: { |
| 86 | + ExecStart: '/usr/bin/node /usr/bin/ghost' |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + }; |
| 91 | + const task = {}; |
| 92 | + |
| 93 | + await expect(checkNodeVersion(ctx, task)).to.be.rejectedWith(errors.SystemError); |
| 94 | + expect(stdout.calledOnceWithExactly('/usr/bin/node', ['--version'])).to.be.true; |
| 95 | + }); |
| 96 | + |
| 97 | + it('rejects if invalid semver', async function () { |
| 98 | + const stdout = sinon.stub(execa, 'stdout').resolves('not-valid-semver'); |
| 99 | + |
| 100 | + const ctx = { |
| 101 | + systemd: { |
| 102 | + unitFilePath: '/tmp/unit-file', |
| 103 | + unit: { |
| 104 | + Service: { |
| 105 | + ExecStart: '/usr/bin/node /usr/bin/ghost' |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + }; |
| 110 | + const task = {}; |
| 111 | + |
| 112 | + await expect(checkNodeVersion(ctx, task)).to.be.rejectedWith(errors.SystemError); |
| 113 | + expect(stdout.calledOnceWithExactly('/usr/bin/node', ['--version'])).to.be.true; |
| 114 | + }); |
| 115 | + |
| 116 | + it('returns if unable to parse ghost pkg json', async function () { |
| 117 | + const stdout = sinon.stub(execa, 'stdout').resolves('12.0.0'); |
| 118 | + const readJson = sinon.stub(fs, 'readJson').rejects(new Error('test')); |
| 119 | + const log = sinon.stub(); |
| 120 | + |
| 121 | + const ctx = { |
| 122 | + systemd: { |
| 123 | + unitFilePath: '/tmp/unit-file', |
| 124 | + unit: { |
| 125 | + Service: { |
| 126 | + ExecStart: '/usr/bin/node /usr/bin/ghost' |
| 127 | + } |
| 128 | + } |
| 129 | + }, |
| 130 | + ui: {log}, |
| 131 | + instance: {dir: '/var/www/ghost'} |
| 132 | + }; |
| 133 | + const task = {}; |
| 134 | + |
| 135 | + await expect(checkNodeVersion(ctx, task)).to.not.be.rejected; |
| 136 | + expect(stdout.calledOnceWithExactly('/usr/bin/node', ['--version'])).to.be.true; |
| 137 | + expect(task.title).to.equal('Checking systemd node version - found v12.0.0'); |
| 138 | + expect(readJson.calledOnceWithExactly('/var/www/ghost/current/package.json')).to.be.true; |
| 139 | + expect(log.calledOnce).to.be.true; |
| 140 | + }); |
| 141 | + |
| 142 | + it('returns if unable to find node range in ghost pkg json', async function () { |
| 143 | + const stdout = sinon.stub(execa, 'stdout').resolves(process.versions.node); |
| 144 | + const readJson = sinon.stub(fs, 'readJson').resolves({}); |
| 145 | + const log = sinon.stub(); |
| 146 | + |
| 147 | + const ctx = { |
| 148 | + systemd: { |
| 149 | + unitFilePath: '/tmp/unit-file', |
| 150 | + unit: { |
| 151 | + Service: { |
| 152 | + ExecStart: '/usr/bin/node /usr/bin/ghost' |
| 153 | + } |
| 154 | + } |
| 155 | + }, |
| 156 | + ui: {log}, |
| 157 | + instance: {dir: '/var/www/ghost'} |
| 158 | + }; |
| 159 | + const task = {}; |
| 160 | + |
| 161 | + await expect(checkNodeVersion(ctx, task)).to.not.be.rejected; |
| 162 | + expect(stdout.calledOnceWithExactly('/usr/bin/node', ['--version'])).to.be.true; |
| 163 | + expect(task.title).to.equal(`Checking systemd node version - found v${process.versions.node}`); |
| 164 | + expect(readJson.calledOnceWithExactly('/var/www/ghost/current/package.json')).to.be.true; |
| 165 | + expect(log.called).to.be.false; |
| 166 | + }); |
| 167 | + |
| 168 | + it('rejects if node version isn\'t compatible with Ghost' , async function () { |
| 169 | + const stdout = sinon.stub(execa, 'stdout').resolves(process.versions.node); |
| 170 | + const readJson = sinon.stub(fs, 'readJson').resolves({ |
| 171 | + engines: {node: '< 1.0.0'} |
| 172 | + }); |
| 173 | + const log = sinon.stub(); |
| 174 | + |
| 175 | + const ctx = { |
| 176 | + systemd: { |
| 177 | + unitFilePath: '/tmp/unit-file', |
| 178 | + unit: { |
| 179 | + Service: { |
| 180 | + ExecStart: '/usr/bin/node /usr/bin/ghost' |
| 181 | + } |
| 182 | + } |
| 183 | + }, |
| 184 | + ui: {log}, |
| 185 | + instance: {dir: '/var/www/ghost'} |
| 186 | + }; |
| 187 | + const task = {}; |
| 188 | + |
| 189 | + await expect(checkNodeVersion(ctx, task)).to.be.rejectedWith(errors.SystemError); |
| 190 | + expect(stdout.calledOnceWithExactly('/usr/bin/node', ['--version'])).to.be.true; |
| 191 | + expect(task.title).to.equal(`Checking systemd node version - found v${process.versions.node}`); |
| 192 | + expect(readJson.calledOnceWithExactly('/var/www/ghost/current/package.json')).to.be.true; |
| 193 | + expect(log.called).to.be.false; |
| 194 | + }); |
| 195 | + }); |
| 196 | +}); |
0 commit comments