/src/nni_manager/training_service/remote_machine/test/windowsCommands.test.ts

https://github.com/microsoft/nni · TypeScript · 102 lines · 82 code · 18 blank · 2 comment · 1 complexity · 90fd910e072aabf14eb96ac65649348b MD5 · raw file

  1. // Copyright (c) Microsoft Corporation.
  2. // Licensed under the MIT license.
  3. 'use strict';
  4. import * as chai from 'chai';
  5. import * as chaiAsPromised from 'chai-as-promised';
  6. import * as component from '../../../common/component';
  7. import { cleanupUnitTest, prepareUnitTest } from '../../../common/utils';
  8. import { WindowsCommands } from '../extends/windowsCommands';
  9. describe('Unit Test for Windows Commands', () => {
  10. let windowsCommands: WindowsCommands
  11. before(() => {
  12. chai.should();
  13. chai.use(chaiAsPromised);
  14. prepareUnitTest();
  15. });
  16. after(() => {
  17. cleanupUnitTest();
  18. });
  19. beforeEach(() => {
  20. windowsCommands = component.get(WindowsCommands);
  21. });
  22. afterEach(() => {
  23. });
  24. it('joinPath', async () => {
  25. chai.expect(windowsCommands.joinPath("/root/", "\\first")).to.equal("\\root\\first");
  26. chai.expect(windowsCommands.joinPath("root/", "first")).to.equal("root\\first");
  27. chai.expect(windowsCommands.joinPath("\\root/", "\\first")).to.equal("\\root\\first");
  28. chai.expect(windowsCommands.joinPath("\\root\\", "\\first")).to.equal("\\root\\first");
  29. chai.expect(windowsCommands.joinPath("\\root", "first")).to.equal("\\root\\first");
  30. chai.expect(windowsCommands.joinPath("\\root\\", "first")).to.equal("\\root\\first");
  31. chai.expect(windowsCommands.joinPath("root\\", "first")).to.equal("root\\first");
  32. chai.expect(windowsCommands.joinPath("root\\")).to.equal("root\\");
  33. chai.expect(windowsCommands.joinPath("root")).to.equal("root");
  34. chai.expect(windowsCommands.joinPath(".\\root")).to.equal(".\\root");
  35. chai.expect(windowsCommands.joinPath("")).to.equal(".");
  36. chai.expect(windowsCommands.joinPath("..")).to.equal("..");
  37. })
  38. it('createFolder', async () => {
  39. chai.expect(windowsCommands.createFolder("test")).to.equal("mkdir \"test\"");
  40. chai.expect(windowsCommands.createFolder("test", true)).to.equal("mkdir \"test\"\r\nICACLS \"test\" /grant \"Users\":F");
  41. })
  42. it('allowPermission', async () => {
  43. chai.expect(windowsCommands.allowPermission(true, "test", "test1")).to.equal("ICACLS \"test\" /grant \"Users\":F /T\r\nICACLS \"test1\" /grant \"Users\":F /T\r\n");
  44. chai.expect(windowsCommands.allowPermission(false, "test")).to.equal("ICACLS \"test\" /grant \"Users\":F\r\n");
  45. })
  46. it('removeFolder', async () => {
  47. chai.expect(windowsCommands.removeFolder("test")).to.equal("rmdir /q \"test\"");
  48. chai.expect(windowsCommands.removeFolder("test", true)).to.equal("rmdir /s /q \"test\"");
  49. chai.expect(windowsCommands.removeFolder("test", true, false)).to.equal("rmdir /s \"test\"");
  50. chai.expect(windowsCommands.removeFolder("test", false, false)).to.equal("rmdir \"test\"");
  51. chai.expect(windowsCommands.removeFolder("test", true, true)).to.equal("rmdir /s /q \"test\"");
  52. })
  53. it('removeFiles', async () => {
  54. chai.expect(windowsCommands.removeFiles("test", "*.sh")).to.equal("del \"test\\*.sh\"");
  55. chai.expect(windowsCommands.removeFiles("test", "")).to.equal("del \"test\"");
  56. })
  57. it('readLastLines', async () => {
  58. chai.expect(windowsCommands.readLastLines("test", 3)).to.equal("powershell.exe Get-Content \"test\" -Tail 3");
  59. })
  60. it('isProcessAlive', async () => {
  61. chai.expect(windowsCommands.isProcessAliveCommand("test")).to.equal("powershell.exe Get-Process -Id (get-content \"test\") -ErrorAction SilentlyContinue");
  62. chai.expect(windowsCommands.isProcessAliveProcessOutput(
  63. {
  64. exitCode: 0,
  65. stdout: "",
  66. stderr: ""
  67. }
  68. )).to.equal(true);
  69. chai.expect(windowsCommands.isProcessAliveProcessOutput(
  70. {
  71. exitCode: 10,
  72. stdout: "",
  73. stderr: ""
  74. }
  75. )).to.equal(false);
  76. })
  77. it('extractFile', async () => {
  78. chai.expect(windowsCommands.extractFile("test.tar", "testfolder")).to.equal("tar -xf \"test.tar\" -C \"testfolder\"");
  79. })
  80. it('executeScript', async () => {
  81. chai.expect(windowsCommands.executeScript("test.sh", true)).to.equal("test.sh");
  82. chai.expect(windowsCommands.executeScript("test script'\"", false)).to.equal("test script'\"");
  83. })
  84. });