windowsCommands.test.ts 4.29 KB
Newer Older
1
2
3
4
5
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

'use strict';

6
7
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
8
9
import * as component from '../../../common/component';
import { cleanupUnitTest, prepareUnitTest } from '../../../common/utils';
liuzhe-lz's avatar
liuzhe-lz committed
10
import { WindowsCommands } from '../../../training_service/remote_machine/extends/windowsCommands';
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102


describe('Unit Test for Windows Commands', () => {

    let windowsCommands: WindowsCommands

    before(() => {
        chai.should();
        chai.use(chaiAsPromised);
        prepareUnitTest();
    });

    after(() => {
        cleanupUnitTest();
    });

    beforeEach(() => {
        windowsCommands = component.get(WindowsCommands);
    });

    afterEach(() => {
    });

    it('joinPath', async () => {
        chai.expect(windowsCommands.joinPath("/root/", "\\first")).to.equal("\\root\\first");
        chai.expect(windowsCommands.joinPath("root/", "first")).to.equal("root\\first");
        chai.expect(windowsCommands.joinPath("\\root/", "\\first")).to.equal("\\root\\first");
        chai.expect(windowsCommands.joinPath("\\root\\", "\\first")).to.equal("\\root\\first");
        chai.expect(windowsCommands.joinPath("\\root", "first")).to.equal("\\root\\first");
        chai.expect(windowsCommands.joinPath("\\root\\", "first")).to.equal("\\root\\first");
        chai.expect(windowsCommands.joinPath("root\\", "first")).to.equal("root\\first");
        chai.expect(windowsCommands.joinPath("root\\")).to.equal("root\\");
        chai.expect(windowsCommands.joinPath("root")).to.equal("root");
        chai.expect(windowsCommands.joinPath(".\\root")).to.equal(".\\root");
        chai.expect(windowsCommands.joinPath("")).to.equal(".");
        chai.expect(windowsCommands.joinPath("..")).to.equal("..");
    })

    it('createFolder', async () => {
        chai.expect(windowsCommands.createFolder("test")).to.equal("mkdir \"test\"");
        chai.expect(windowsCommands.createFolder("test", true)).to.equal("mkdir \"test\"\r\nICACLS \"test\" /grant \"Users\":F");
    })

    it('allowPermission', async () => {
        chai.expect(windowsCommands.allowPermission(true, "test", "test1")).to.equal("ICACLS \"test\" /grant \"Users\":F /T\r\nICACLS \"test1\" /grant \"Users\":F /T\r\n");
        chai.expect(windowsCommands.allowPermission(false, "test")).to.equal("ICACLS \"test\" /grant \"Users\":F\r\n");
    })

    it('removeFolder', async () => {
        chai.expect(windowsCommands.removeFolder("test")).to.equal("rmdir /q \"test\"");
        chai.expect(windowsCommands.removeFolder("test", true)).to.equal("rmdir /s /q \"test\"");
        chai.expect(windowsCommands.removeFolder("test", true, false)).to.equal("rmdir /s \"test\"");
        chai.expect(windowsCommands.removeFolder("test", false, false)).to.equal("rmdir \"test\"");
        chai.expect(windowsCommands.removeFolder("test", true, true)).to.equal("rmdir /s /q \"test\"");
    })

    it('removeFiles', async () => {
        chai.expect(windowsCommands.removeFiles("test", "*.sh")).to.equal("del \"test\\*.sh\"");
        chai.expect(windowsCommands.removeFiles("test", "")).to.equal("del \"test\"");
    })

    it('readLastLines', async () => {
        chai.expect(windowsCommands.readLastLines("test", 3)).to.equal("powershell.exe Get-Content \"test\" -Tail 3");
    })

    it('isProcessAlive', async () => {
        chai.expect(windowsCommands.isProcessAliveCommand("test")).to.equal("powershell.exe Get-Process -Id (get-content \"test\") -ErrorAction SilentlyContinue");
        chai.expect(windowsCommands.isProcessAliveProcessOutput(
            {
                exitCode: 0,
                stdout: "",
                stderr: ""
            }
        )).to.equal(true);
        chai.expect(windowsCommands.isProcessAliveProcessOutput(
            {
                exitCode: 10,
                stdout: "",
                stderr: ""
            }
        )).to.equal(false);
    })

    it('extractFile', async () => {
        chai.expect(windowsCommands.extractFile("test.tar", "testfolder")).to.equal("tar -xf \"test.tar\" -C \"testfolder\"");
    })

    it('executeScript', async () => {
        chai.expect(windowsCommands.executeScript("test.sh", true)).to.equal("test.sh");
        chai.expect(windowsCommands.executeScript("test script'\"", false)).to.equal("test script'\"");
    })
});