'use strict';

var _regenerator = require('babel-runtime/regenerator');

var _regenerator2 = _interopRequireDefault(_regenerator);

var _promise = require('babel-runtime/core-js/promise');

var _promise2 = _interopRequireDefault(_promise);

var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');

var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);

require('source-map-support/register');

var _sinon = require('sinon');

var _sinon2 = _interopRequireDefault(_sinon);

var _mocha = require('mocha');

var _assert = require('assert');

var _assert2 = _interopRequireDefault(_assert);

var _Executor = require('../../src/agent/Executor');

var _ExecutorDocker = require('../../src/executors/ExecutorDocker');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

(0, _mocha.describe)('ExecutorDocker', function () {
    var executorDocker = void 0;
    var eventData = void 0;

    (0, _mocha.beforeEach)(function () {
        eventData = {
            executor: 'Docker',
            context: 'run ls in latest alpine',
            options: {
                image: 'alpine:latest',
                command: ['/bin/ls', '-ltr', '/bin'],
                validator: {
                    type: 'outputRegex',
                    regex: '.*HOSTNAME.*'
                }
            }
        };
        executorDocker = new _ExecutorDocker.ExecutorDocker(eventData, console);
    });

    (0, _mocha.describe)('constructor', function () {
        (0, _mocha.it)('should instantiate as Executor', function () {
            _assert2.default.equal(executorDocker instanceof _Executor.Executor, true);
        });
    });

    (0, _mocha.describe)('validateImage', function () {
        process.env.GTM_DOCKER_IMAGE_WHITELIST = 'alpine:*,bash:latest';
        process.env.GTM_DOCKER_IMAGE_WHITELIST_FILE = '.dockerImageWhitelistExample';

        (0, _mocha.it)('should validate the given image', function () {
            var result = executorDocker.validateImage('nginx:*');
            _assert2.default.equal(result, true);
        });
    });

    (0, _mocha.describe)('pullImage', function () {
        (0, _mocha.it)('should return a promise', (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee() {
            var result;
            return _regenerator2.default.wrap(function _callee$(_context) {
                while (1) {
                    switch (_context.prev = _context.next) {
                        case 0:
                            result = executorDocker.pullImage();

                            _assert2.default.equal(result instanceof _promise2.default, true);

                        case 2:
                        case 'end':
                            return _context.stop();
                    }
                }
            }, _callee, undefined);
        })));
    });

    (0, _mocha.describe)('containerLogs', function () {
        (0, _mocha.it)('should return a promise', (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2() {
            var result;
            return _regenerator2.default.wrap(function _callee2$(_context2) {
                while (1) {
                    switch (_context2.prev = _context2.next) {
                        case 0:
                            result = executorDocker.containerLogs({ logs: function logs() {} });

                            _assert2.default.equal(result instanceof _promise2.default, true);

                        case 2:
                        case 'end':
                            return _context2.stop();
                    }
                }
            }, _callee2, undefined);
        })));
    });

    (0, _mocha.describe)('executeTask', function () {
        var stubCall = void 0;
        var customResult = void 0;
        (0, _mocha.before)(function () {
            customResult = {
                passed: true,
                url: 'https://docker.com'
            };
            stubCall = _sinon2.default.stub(executorDocker, 'executeTask').returns(_promise2.default.resolve(customResult));
        });

        (0, _mocha.it)('should invoke mocked ExecuteDocker.executeTask', (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee3() {
            var result;
            return _regenerator2.default.wrap(function _callee3$(_context3) {
                while (1) {
                    switch (_context3.prev = _context3.next) {
                        case 0:
                            _context3.next = 2;
                            return stubCall(eventData).then(function (data) {
                                return data;
                            });

                        case 2:
                            result = _context3.sent;

                            _assert2.default.equal(result, customResult);

                        case 4:
                        case 'end':
                            return _context3.stop();
                    }
                }
            }, _callee3, undefined);
        })));

        (0, _mocha.after)(function () {
            stubCall.restore();
        });

        /*TO WORK AROUND - the uncaughtException and then uncomment this test
        it('should throw exception due to invalid environment', async done => {
            var error = new Error('Exception while executing the function executorDocker.executeTask');
            var recordedError;
            var originalException = process.listeners('uncaughtException').pop();
            //Needed in node 0.10.5+
            process.removeListener('uncaughtException', originalException);
            process.once('uncaughtException', function(error) {
                recordedError = error;
            });
             let result = await TestUtils.assertThrowsAsynchronously(
                async () => await executorDocker.executeTask(eventData),
                'executorDocker.executeTask'
            ).then(data => {
                done();
                return data;
            });
            console.log(result);
            process.nextTick(function() {
                process.listeners('uncaughtException').push(originalException);
                assert.equal(recordedError, error);
                done();
                //next();
            });
        });
        */
    });
    (0, _mocha.describe)('validate', function () {
        (0, _mocha.it)('should validate output', function () {
            var actual = executorDocker.validate(eventData, 'HOSTNAME=docker.com');
            var expected = true;
            _assert2.default.equal(actual, expected);
        });
    });
});