Merge pull request #1402 from balena-io/log-is-vpn-active

log detection of changes to VPN status
This commit is contained in:
bulldozer-balena[bot] 2020-07-24 15:32:53 +00:00 committed by GitHub
commit d7658e7128
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 5 deletions

View File

@ -43,12 +43,14 @@ export function enableCheck(enable: boolean) {
}
export async function isVPNActive(): Promise<boolean> {
let active: boolean = true;
try {
await fs.lstat(`${constants.vpnStatusPath}/active`);
} catch {
return false;
} catch (e) {
active = false;
}
return true;
log.info(`VPN connection is ${active ? 'active' : 'not active'}.`);
return active;
}
async function vpnStatusInotifyCallback(): Promise<void> {

View File

@ -1,8 +1,9 @@
import { fs } from 'mz';
import * as os from 'os';
import { stub } from 'sinon';
import { stub, spy } from 'sinon';
import { expect } from './lib/chai-config';
import Log from '../src/lib/supervisor-console';
import * as network from '../src/network';
describe('network', () => {
@ -78,4 +79,23 @@ describe('network', () => {
it('returns only the relevant IP addresses', () =>
expect(network.getIPAddresses()).to.deep.equal(['192.168.1.137']));
});
it('checks VPN connection status', async () => {
const statStub = stub(fs, 'lstat');
const logStub = spy(Log, 'info');
// Test when VPN is inactive
statStub.rejects(); // Reject so we can't stat the vpn active file
await expect(network.isVPNActive()).to.eventually.equal(false);
expect(logStub.lastCall?.lastArg).to.equal(`VPN connection is not active.`);
// Test when VPN is active
statStub.resolves(); // Resolve so we can stat the vpn active file
await expect(network.isVPNActive()).to.eventually.equal(true);
expect(logStub.lastCall?.lastArg).to.equal(`VPN connection is active.`);
// Restore stubs
statStub.restore();
logStub.restore();
});
});