This repository has been archived by the owner on Sep 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 654
/
CommandExecutorTests.cs
155 lines (128 loc) · 6.63 KB
/
CommandExecutorTests.cs
1
2
3
4
5
6
7
8
9
10
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System;
using System.Collections.Generic;
using Kudu.Core;
using Kudu.TestHarness;
using Kudu.TestHarness.Xunit;
using Xunit;
namespace Kudu.FunctionalTests
{
[KuduXunitTestClass]
public class CommandExecutorTests
{
[Fact]
public void CommandExecutorTest()
{
// Arrange
string appName = "CommandExecuterEnvironmentSetCorrectly";
ApplicationManager.Run(appName, appManager =>
{
List<CommandTestSettings> tests = new List<CommandTestSettings>();
var commandTestSettings = new CommandTestSettings("set MSBUILD_PATH");
commandTestSettings.ExpectedResult.Output = "msbuild";
tests.Add(commandTestSettings);
commandTestSettings = new CommandTestSettings("set NONEXISTING");
commandTestSettings.ExpectedResult.Error = "NONEXISTING";
commandTestSettings.ExpectedResult.ExitCode = 1;
tests.Add(commandTestSettings);
commandTestSettings = new CommandTestSettings("echo %DEPLOYMENT_SOURCE%");
commandTestSettings.ExpectedResult.Output = "\\";
tests.Add(commandTestSettings);
commandTestSettings = new CommandTestSettings("echo %DEPLOYMENT_TARGET%");
commandTestSettings.ExpectedResult.Output = "\\wwwroot";
tests.Add(commandTestSettings);
commandTestSettings = new CommandTestSettings("echo %KUDU_SYNC_CMD%");
commandTestSettings.ExpectedResult.Output = "kudusync";
tests.Add(commandTestSettings);
commandTestSettings = new CommandTestSettings("echo %KUDU_SELECT_NODE_VERSION_CMD%");
commandTestSettings.ExpectedResult.Output = "\\selectNodeVersion";
tests.Add(commandTestSettings);
commandTestSettings = new CommandTestSettings("echo %NPM_JS_PATH%");
commandTestSettings.ExpectedResult.Output = "\\npm";
tests.Add(commandTestSettings);
commandTestSettings = new CommandTestSettings("echo %PATH%");
commandTestSettings.ExpectedResult.Output = "git";
tests.Add(commandTestSettings);
commandTestSettings = new CommandTestSettings("echo %HOME%");
commandTestSettings.ExpectedResult.Output = appManager.ApplicationName;
tests.Add(commandTestSettings);
commandTestSettings = new CommandTestSettings("dir");
commandTestSettings.WorkingDirectory = ".\\site";
commandTestSettings.ExpectedResult.Output = "wwwroot";
tests.Add(commandTestSettings);
commandTestSettings = new CommandTestSettings("echo %EnableNuGetPackageRestore%");
commandTestSettings.ExpectedResult.Output = "true";
tests.Add(commandTestSettings);
// Make sure that we are able to launch Powershell
commandTestSettings = new CommandTestSettings("powershell get-process");
commandTestSettings.ExpectedResult.Output = "Handles";
tests.Add(commandTestSettings);
// Make sure we can use braces in the command
commandTestSettings = new CommandTestSettings(@"powershell -command "" & { dir }""");
commandTestSettings.ExpectedResult.Output = "LastWriteTime";
tests.Add(commandTestSettings);
// Make sure that we are able to launch certutil
commandTestSettings = new CommandTestSettings("certutil");
commandTestSettings.ExpectedResult.Output = "dump command completed successfully";
tests.Add(commandTestSettings);
// Make sure 'npm -g' installs to AppData (and not Program Files)
commandTestSettings = new CommandTestSettings("npm install -g underscore --quiet");
commandTestSettings.ExpectedResult.Output = "AppData";
tests.Add(commandTestSettings);
// Test mv
var batches = new[]
{
"if exist \"%HOME%\\LogFiles\\xyz.txt\" del \"%HOME%\\LogFiles\\*.txt\" /q",
"touch \"%HOME%\\LogFiles\\abc.txt\"",
"mv \"%HOME%\\LogFiles\\abc.txt\" \"%HOME%\\LogFiles\\xyz.txt\"",
"if exist \"%HOME%\\LogFiles\\xyz.txt\" del \"%HOME%\\LogFiles\\*.txt\" /q"
};
foreach (var cmd in batches)
{
commandTestSettings = new CommandTestSettings(cmd);
tests.Add(commandTestSettings);
}
foreach (CommandTestSettings test in tests)
{
VerifyCommand(test, appManager);
}
});
}
private void VerifyCommand(CommandTestSettings commandTestSettings, ApplicationManager appManager)
{
TestTracer.Trace("Running command - '{0}' on '{1}'", commandTestSettings.Command, commandTestSettings.WorkingDirectory);
CommandResult commandResult = appManager.CommandExecutor.ExecuteCommand(commandTestSettings.Command, commandTestSettings.WorkingDirectory).Result;
TestTracer.Trace("Received result\nOutput\n======\n{0}\nError\n======\n{1}\nExit Code - {2}", commandResult.Output, commandResult.Error, commandResult.ExitCode);
Assert.Equal(commandTestSettings.ExpectedResult.ExitCode, commandResult.ExitCode);
AssertOutput(commandTestSettings.ExpectedResult.Error, commandResult.Error);
AssertOutput(commandTestSettings.ExpectedResult.Output, commandResult.Output);
}
private void AssertOutput(string expected, string actual)
{
if (!String.IsNullOrEmpty(expected))
{
Assert.Contains(expected, actual, StringComparison.InvariantCultureIgnoreCase);
}
else
{
Assert.Equal(expected, actual);
}
}
private class CommandTestSettings
{
public CommandResult ExpectedResult { get; set; }
public string WorkingDirectory { get; set; }
public string Command { get; set; }
public CommandTestSettings(string command)
{
ExpectedResult = new CommandResult()
{
Output = String.Empty,
Error = String.Empty,
ExitCode = 0
};
WorkingDirectory = ".";
Command = command;
}
}
}
}