|
| 1 | +// Copyright 2016-2024, Pulumi Corporation |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.IO; |
| 6 | +using System.Linq; |
| 7 | +using System.Runtime.InteropServices; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Pulumi.Automation.Commands; |
| 10 | +using Semver; |
| 11 | +using Xunit; |
| 12 | + |
| 13 | +namespace Pulumi.Automation.Tests |
| 14 | +{ |
| 15 | + public class LocalPulumiCommandTests |
| 16 | + { |
| 17 | + [Fact] |
| 18 | + public async Task CheckVersionCommand() |
| 19 | + { |
| 20 | + var localCmd = await LocalPulumiCommand.CreateAsync(); |
| 21 | + var extraEnv = new Dictionary<string, string?>(); |
| 22 | + var args = new[] { "version" }; |
| 23 | + |
| 24 | + var stdoutLines = new List<string>(); |
| 25 | + var stderrLines = new List<string>(); |
| 26 | + |
| 27 | + // NOTE: not testing onEngineEvent arg as that is not |
| 28 | + // supported for "version"; to test it one needs |
| 29 | + // workspace-aware commands such as up or preview; |
| 30 | + // currently this is covered by |
| 31 | + // LocalWorkspaceTests.HandlesEvents. |
| 32 | + |
| 33 | + var result = await localCmd.RunAsync( |
| 34 | + args, ".", extraEnv, |
| 35 | + onStandardOutput: line => stdoutLines.Add(line), |
| 36 | + onStandardError: line => stderrLines.Add(line)); |
| 37 | + |
| 38 | + Assert.Equal(0, result.Code); |
| 39 | + |
| 40 | + Assert.Matches(@"^v?\d+\.\d+\.\d+", result.StandardOutput); |
| 41 | + // stderr must strictly begin with the version warning message or be an empty string: |
| 42 | + if (result.StandardError.Length > 0) |
| 43 | + { |
| 44 | + Assert.StartsWith("warning: A new version of Pulumi", result.StandardError); |
| 45 | + } |
| 46 | + |
| 47 | + // If these tests begin failing, it may be because the automation output now emits CRLF |
| 48 | + // (\r\n) on Windows. |
| 49 | + // |
| 50 | + // If so, update the Lines method to split on Environment.NewLine instead of "\n". |
| 51 | + Assert.Equal(Lines(result.StandardOutput), stdoutLines.Select(x => x.Trim()).ToList()); |
| 52 | + Assert.Equal(Lines(result.StandardError), stderrLines.Select(x => x.Trim()).ToList()); |
| 53 | + } |
| 54 | + |
| 55 | + private List<string> Lines(string s) |
| 56 | + { |
| 57 | + return s.Split("\n", |
| 58 | + StringSplitOptions.RemoveEmptyEntries) |
| 59 | + .Select(x => x.Trim()) |
| 60 | + .ToList(); |
| 61 | + } |
| 62 | + |
| 63 | + [Fact] |
| 64 | + public async Task InstallDefaultRoot() |
| 65 | + { |
| 66 | + var requestedVersion = new SemVersion(3, 102, 0); |
| 67 | + await LocalPulumiCommand.Install(new LocalPulumiCommandOptions { Version = requestedVersion }); |
| 68 | + var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); |
| 69 | + var pulumiBin = Path.Combine(home, ".pulumi", "versions", requestedVersion.ToString(), "bin", "pulumi"); |
| 70 | + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
| 71 | + { |
| 72 | + pulumiBin += ".exe"; |
| 73 | + } |
| 74 | + Assert.True(File.Exists(pulumiBin)); |
| 75 | + } |
| 76 | + |
| 77 | + [Fact] |
| 78 | + public async Task InstallTwice() |
| 79 | + { |
| 80 | + var tempDir = Path.Combine(Path.GetTempPath(), "automation-test-" + Guid.NewGuid().ToString()); |
| 81 | + Directory.CreateDirectory(tempDir); |
| 82 | + try |
| 83 | + { |
| 84 | + var requestedVersion = new SemVersion(3, 102, 0); |
| 85 | + await LocalPulumiCommand.Install(new LocalPulumiCommandOptions { Version = requestedVersion, Root = tempDir }); |
| 86 | + var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); |
| 87 | + var pulumiBin = Path.Combine(home, ".pulumi", "versions", requestedVersion.ToString(), "bin", "pulumi"); |
| 88 | + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
| 89 | + { |
| 90 | + pulumiBin += ".exe"; |
| 91 | + } |
| 92 | + var t1 = File.GetCreationTime(pulumiBin); |
| 93 | + // Install again with the same options |
| 94 | + await LocalPulumiCommand.Install(new LocalPulumiCommandOptions { Version = requestedVersion, Root = tempDir }); |
| 95 | + var t2 = File.GetCreationTime(pulumiBin); |
| 96 | + Assert.Equal(t1, t2); |
| 97 | + } |
| 98 | + finally |
| 99 | + { |
| 100 | + Directory.Delete(tempDir, true); |
| 101 | + } |
| 102 | + |
| 103 | + } |
| 104 | + |
| 105 | + [Fact] |
| 106 | + public async Task VersionCheck() |
| 107 | + { |
| 108 | + var dirPath = Path.Combine(Path.GetTempPath(), "automation-test-" + Guid.NewGuid().ToString()); |
| 109 | + var dir = Directory.CreateDirectory(dirPath); |
| 110 | + try |
| 111 | + { |
| 112 | + // Install an old version |
| 113 | + var installed_version = new SemVersion(3, 99, 0); |
| 114 | + await LocalPulumiCommand.Install(new LocalPulumiCommandOptions { Version = installed_version, Root = dirPath }); |
| 115 | + |
| 116 | + // Try to create a command with a more recent version |
| 117 | + var requested_version = new SemVersion(3, 102, 0); |
| 118 | + await Assert.ThrowsAsync<InvalidOperationException>(() => LocalPulumiCommand.CreateAsync(new LocalPulumiCommandOptions |
| 119 | + { |
| 120 | + Version = requested_version, |
| 121 | + Root = dirPath |
| 122 | + })); |
| 123 | + |
| 124 | + // Opting out of the version check works |
| 125 | + await LocalPulumiCommand.CreateAsync(new LocalPulumiCommandOptions |
| 126 | + { |
| 127 | + Version = requested_version, |
| 128 | + Root = dirPath, |
| 129 | + SkipVersionCheck = true |
| 130 | + }); |
| 131 | + } |
| 132 | + finally |
| 133 | + { |
| 134 | + dir.Delete(true); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + [Fact] |
| 139 | + public void PulumiEnvironment() |
| 140 | + { |
| 141 | + // Plain "pulumi" command |
| 142 | + var env = new Dictionary<string, string?> { { "PATH", "/usr/bin" } }; |
| 143 | + var newEnv = LocalPulumiCommand.PulumiEnvironment(env, "pulumi", false); |
| 144 | + Assert.Equal("/usr/bin", newEnv["PATH"]); |
| 145 | + |
| 146 | + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
| 147 | + { |
| 148 | + env = new Dictionary<string, string?> { { "PATH", "%SystemRoot%\\system32" } }; |
| 149 | + newEnv = LocalPulumiCommand.PulumiEnvironment(env, "C:\\some\\install\\root\\bin\\pulumi", false); |
| 150 | + Assert.Equal("C:\\some\\install\\root\\bin;%SystemRoot%\\system32", newEnv["PATH"]); |
| 151 | + } |
| 152 | + else |
| 153 | + { |
| 154 | + env = new Dictionary<string, string?> { { "PATH", "/usr/bin" } }; |
| 155 | + newEnv = LocalPulumiCommand.PulumiEnvironment(env, "/some/install/root/bin/pulumi", false); |
| 156 | + Assert.Equal("/some/install/root/bin:/usr/bin", newEnv["PATH"]); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + [Theory] |
| 161 | + [InlineData("100.0.0", true, false)] |
| 162 | + [InlineData("1.0.0", true, false)] |
| 163 | + [InlineData("2.22.0", false, false)] |
| 164 | + [InlineData("2.1.0", true, false)] |
| 165 | + [InlineData("2.21.2", false, false)] |
| 166 | + [InlineData("2.21.1", false, false)] |
| 167 | + [InlineData("2.21.0", true, false)] |
| 168 | + // Note that prerelease < release so this case should error |
| 169 | + [InlineData("2.21.1-alpha.1234", true, false)] |
| 170 | + [InlineData("2.20.0", false, true)] |
| 171 | + [InlineData("2.22.0", false, true)] |
| 172 | + // Invalid version check |
| 173 | + [InlineData("invalid", false, true)] |
| 174 | + [InlineData("invalid", true, false)] |
| 175 | + public void ValidVersionTheory(string currentVersion, bool errorExpected, bool optOut) |
| 176 | + { |
| 177 | + var testMinVersion = new SemVersion(2, 21, 1); |
| 178 | + |
| 179 | + if (errorExpected) |
| 180 | + { |
| 181 | + void ValidatePulumiVersion() => LocalPulumiCommand.ParseAndValidatePulumiVersion(testMinVersion, currentVersion, optOut); |
| 182 | + Assert.Throws<InvalidOperationException>(ValidatePulumiVersion); |
| 183 | + } |
| 184 | + else |
| 185 | + { |
| 186 | + LocalPulumiCommand.ParseAndValidatePulumiVersion(testMinVersion, currentVersion, optOut); |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + } |
| 191 | + |
| 192 | +} |
0 commit comments