linuxCommands.test.ts 3.63 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 { LinuxCommands } from '../../../training_service/remote_machine/extends/linuxCommands';
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


describe('Unit Test for linuxCommands', () => {

    let linuxCommands: LinuxCommands

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

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

    beforeEach(() => {
        linuxCommands = component.get(LinuxCommands);
    });

    afterEach(() => {
    });

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

    it('createFolder', async () => {
        chai.expect(linuxCommands.createFolder("test")).to.equal("mkdir -p 'test'");
        chai.expect(linuxCommands.createFolder("test", true)).to.equal("umask 0; mkdir -p 'test'");
    })

    it('allowPermission', async () => {
        chai.expect(linuxCommands.allowPermission(true, "test", "test1")).to.equal("chmod 777 -R 'test' 'test1'");
        chai.expect(linuxCommands.allowPermission(false, "test")).to.equal("chmod 777 'test'");
    })

    it('removeFolder', async () => {
        chai.expect(linuxCommands.removeFolder("test")).to.equal("rm -df 'test'");
        chai.expect(linuxCommands.removeFolder("test", true)).to.equal("rm -rf 'test'");
        chai.expect(linuxCommands.removeFolder("test", true, false)).to.equal("rm -r 'test'");
        chai.expect(linuxCommands.removeFolder("test", false, false)).to.equal("rm 'test'");
    })

    it('removeFiles', async () => {
        chai.expect(linuxCommands.removeFiles("test", "*.sh")).to.equal("rm 'test/*.sh'");
        chai.expect(linuxCommands.removeFiles("test", "")).to.equal("rm 'test'");
    })

    it('readLastLines', async () => {
        chai.expect(linuxCommands.readLastLines("test", 3)).to.equal("tail -n 3 'test'");
    })

    it('isProcessAlive', async () => {
        chai.expect(linuxCommands.isProcessAliveCommand("test")).to.equal("kill -0 `cat 'test'`");
        chai.expect(linuxCommands.isProcessAliveProcessOutput(
            {
                exitCode: 0,
                stdout: "",
                stderr: ""
            }
        )).to.equal(true);
        chai.expect(linuxCommands.isProcessAliveProcessOutput(
            {
                exitCode: 10,
                stdout: "",
                stderr: ""
            }
        )).to.equal(false);
    })

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

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