Add azcli extension (#16029)

This commit is contained in:
Charles Gagnon
2021-07-07 13:00:12 -07:00
committed by GitHub
parent 6078e9f459
commit d942799f9d
39 changed files with 4797 additions and 1 deletions

View File

@@ -0,0 +1,68 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as should from 'should';
import * as sudo from 'sudo-prompt';
import * as sinon from 'sinon';
import Logger from '../../common/logger';
import { executeCommand, executeSudoCommand } from '../../common/childProcess';
describe('ChildProcess', function (): void {
afterEach(function(): void {
sinon.restore();
});
describe('executeCommand', function(): void {
[[], ['test']].forEach(args => {
it(`Output channel is used with ${JSON.stringify(args)} args`, async function (): Promise<void> {
const logStub = sinon.stub(Logger, 'log');
await executeCommand('echo', args);
should(logStub.called).be.true('Log should have been called');
});
});
it('Gets expected output', async function (): Promise<void> {
const echoOutput = 'test';
const output = await executeCommand('echo', [echoOutput]);
should(output.stdout).equal(echoOutput);
});
it('Invalid command errors', async function (): Promise<void> {
await should(executeCommand('invalid_command', [])).be.rejected();
});
});
describe('executeSudoCommand', function(): void {
it('Gets expected stdout output', async function (): Promise<void> {
const stdout = 'stdout output';
sinon.stub(sudo, 'exec').callsFake( (_cmd, _options, callback) => {
callback!(undefined, stdout);
});
const result = await executeSudoCommand('echo');
should(result.stdout).equal(stdout);
should(result.stderr).equal('');
});
it('Gets expected stderr output', async function (): Promise<void> {
const stderr = 'stderr output';
sinon.stub(sudo, 'exec').callsFake( (_cmd, _options, callback) => {
callback!(undefined, undefined, stderr);
});
const result = await executeSudoCommand('echo');
should(result.stdout).equal('');
should(result.stderr).equal(stderr);
});
it('Error rejects', async function (): Promise<void> {
sinon.stub(sudo, 'exec').callsFake( (_cmd, _options, callback) => {
callback!(new Error('error'));
});
await should(executeSudoCommand('invalid_command')).be.rejected();
});
});
});

View File

@@ -0,0 +1,100 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as fs from 'fs';
import * as nock from 'nock';
import * as os from 'os';
import * as should from 'should';
import * as sinon from 'sinon';
import { PassThrough } from 'stream';
import { HttpClient } from '../../common/httpClient';
import { Deferred } from '../../common/promise';
describe('HttpClient', function (): void {
afterEach(function (): void {
nock.cleanAll();
nock.enableNetConnect();
sinon.restore();
});
describe('downloadFile', function (): void {
it('downloads file successfully', async function (): Promise<void> {
nock('https://127.0.0.1')
.get('/README.md')
.replyWithFile(200, __filename);
const downloadFolder = os.tmpdir();
const downloadPath = await HttpClient.downloadFile('https://127.0.0.1/README.md', downloadFolder);
// Verify file was downloaded correctly
await fs.promises.stat(downloadPath);
});
it('errors on response stream error', async function (): Promise<void> {
const downloadFolder = os.tmpdir();
nock('https://127.0.0.1')
.get('/')
.replyWithError('Unexpected Error');
const downloadPromise = HttpClient.downloadFile('https://127.0.0.1', downloadFolder);
await should(downloadPromise).be.rejected();
});
it('rejects on non-OK status code', async function (): Promise<void> {
const downloadFolder = os.tmpdir();
nock('https://127.0.0.1')
.get('/')
.reply(404, '');
const downloadPromise = HttpClient.downloadFile('https://127.0.0.1', downloadFolder);
await should(downloadPromise).be.rejected();
});
it('errors on write stream error', async function (): Promise<void> {
const downloadFolder = os.tmpdir();
const mockWriteStream = new PassThrough();
const deferredPromise = new Deferred();
sinon.stub(fs, 'createWriteStream').callsFake(() => {
deferredPromise.resolve();
return <any>mockWriteStream;
});
nock('https://127.0.0.1')
.get('/')
.reply(200, '');
const downloadPromise = HttpClient.downloadFile('https://127.0.0.1', downloadFolder);
// Wait for the stream to be created before throwing the error or HttpClient will miss the event
await deferredPromise;
try {
// Passthrough streams will throw the error we emit so just no-op and
// let the HttpClient handler handle the error
mockWriteStream.emit('error', 'Unexpected write error');
} catch (err) { }
await should(downloadPromise).be.rejected();
});
});
describe('getTextContent', function (): void {
it('Gets file contents correctly', async function (): Promise<void> {
nock('https://127.0.0.1')
.get('/arbitraryFile')
.replyWithFile(200, __filename);
const receivedContents = await HttpClient.getTextContent(`https://127.0.0.1/arbitraryFile`);
should(receivedContents).equal((await fs.promises.readFile(__filename)).toString());
});
it('rejects on response error', async function (): Promise<void> {
nock('https://127.0.0.1')
.get('/')
.replyWithError('Unexpected Error');
const getFileContentsPromise = HttpClient.getTextContent('https://127.0.0.1/', );
await should(getFileContentsPromise).be.rejected();
});
it('rejects on non-OK status code', async function (): Promise<void> {
nock('https://127.0.0.1')
.get('/')
.reply(404, '');
const getFileContentsPromise = HttpClient.getTextContent('https://127.0.0.1/', );
await should(getFileContentsPromise).be.rejected();
});
});
});

View File

@@ -0,0 +1,30 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as should from 'should';
import { Deferred } from '../../common/promise';
describe('DeferredPromise', function (): void {
it('Resolves correctly', async function(): Promise<void> {
const deferred = new Deferred();
deferred.resolve();
await should(deferred.promise).be.resolved();
});
it('Rejects correctly', async function(): Promise<void> {
const deferred = new Deferred();
deferred.reject();
await should(deferred.promise).be.rejected();
});
it('Chains then correctly', function(done): void {
const deferred = new Deferred();
deferred.then( () => {
done();
});
deferred.resolve();
});
});

View File

@@ -0,0 +1,26 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as should from 'should';
import { NoAzdataError, searchForCmd as searchForExe } from '../../common/utils';
describe('utils', function () {
describe('searchForExe', function (): void {
it('finds exe successfully', async function (): Promise<void> {
await searchForExe('node');
});
it('throws for non-existent exe', async function (): Promise<void> {
await should(searchForExe('someFakeExe')).be.rejected();
});
});
describe('NoAzdataError', function (): void {
it('error contains message with and without links', function (): void {
const error = new NoAzdataError();
should(error.message).not.be.empty();
should(error.messageWithLink).not.be.empty();
should(error.message).not.equal(error.messageWithLink, 'Messages should not be equal');
});
});
});