mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2025-02-21 02:01:35 +00:00
Merge pull request #1402 from balena-io/log-is-vpn-active
log detection of changes to VPN status
This commit is contained in:
commit
d7658e7128
@ -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> {
|
||||
|
@ -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();
|
||||
});
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user