Пример #1
0
        public void ShouldDeployAndInstallWithCustomUserName()
        {
            if (!CalamariEnvironment.IsRunningOnWindows)
            {
                Assert.Inconclusive("Services are only supported on windows");
            }

#if WINDOWS_USER_ACCOUNT_SUPPORT
            TestUserPrincipal userPrincipal = null;
            try
            {
                userPrincipal = new TestUserPrincipal("calamari-svc-test")
                                .EnsureIsMemberOfGroup("Administrators")
                                .GrantLogonAsAServiceRight();
                Variables[SpecialVariables.Action.WindowsService.CustomAccountName]     = userPrincipal.NTAccountName;
                Variables[SpecialVariables.Action.WindowsService.CustomAccountPassword] = userPrincipal.Password;

                RunDeployment();
            }
            finally
            {
                userPrincipal?.Delete();
            }
#else
            Assert.Inconclusive("Not yet able to configure user accounts under netcore to test service accounts");
#endif
        }
        public void RunAsDifferentUser_ShouldWork(string command, string arguments)
        {
            var user = new TestUserPrincipal(Username);

            using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));

            // Target the CommonApplicationData folder since this is a place the particular user can get to
            var workingDirectory           = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
            var networkCredential          = user.GetCredential();
            var customEnvironmentVariables = new Dictionary <string, string>();

            var exitCode = Execute(command,
                                   arguments,
                                   workingDirectory,
                                   out var debugMessages,
                                   out var infoMessages,
                                   out var errorMessages,
                                   networkCredential,
                                   customEnvironmentVariables,
                                   cts.Token);

            exitCode.Should().Be(0, "the process should have run to completion");
            errorMessages.ToString().Should().BeEmpty("no messages should be written to stderr");
            infoMessages.ToString().Should().ContainEquivalentOf($@"{user.DomainName}\{user.UserName}");
        }
        public void RunningAsDifferentUser_ShouldWorkLotsOfTimes()
        {
            var user = new TestUserPrincipal(Username);

            using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(120));

            for (var i = 0; i < 20; i++)
            {
                var arguments = $"{CommandParam} \"echo {EchoEnvironmentVariable("customenvironmentvariable")}%\"";
                // Target the CommonApplicationData folder since this is a place the particular user can get to
                var workingDirectory           = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
                var networkCredential          = user.GetCredential();
                var customEnvironmentVariables = new Dictionary <string, string>
                {
                    { "customenvironmentvariable", $"customvalue-{i}" }
                };

                var exitCode = Execute(Command,
                                       arguments,
                                       workingDirectory,
                                       out var debugMessages,
                                       out var infoMessages,
                                       out var errorMessages,
                                       networkCredential,
                                       customEnvironmentVariables,
                                       cts.Token);

                exitCode.Should().Be(0, "the process should have run to completion");
                infoMessages.ToString().Should().ContainEquivalentOf($"customvalue-{i}", "the environment variable should have been copied to the child process");
                errorMessages.ToString().Should().BeEmpty("no messages should be written to stderr");
            }
        }
        public void RunningAsDifferentUser_CanWriteToItsOwnTempPath()
        {
            var user = new TestUserPrincipal(Username);

            using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
            {
                var arguments = $"{CommandParam} \"echo hello > %temp%hello.txt\"";
                // Target the CommonApplicationData folder since this is a place the particular user can get to
                var workingDirectory           = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
                var networkCredential          = user.GetCredential();
                var customEnvironmentVariables = new Dictionary <string, string>();

                var exitCode = Execute(Command,
                                       arguments,
                                       workingDirectory,
                                       out var debugMessages,
                                       out var infoMessages,
                                       out var errorMessages,
                                       networkCredential,
                                       customEnvironmentVariables,
                                       cts.Token);

                exitCode.Should().Be(0, "the process should have run to completion after writing to the temp folder for the other user");
                errorMessages.ToString().Should().BeEmpty("no messages should be written to stderr");
            }
        }
        public void DebugLogging_ShouldContainDiagnosticsInfo_DifferentUser()
        {
            var user = new TestUserPrincipal(Username);

            using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));

            var arguments = $"{CommandParam} \"echo %userdomain%\\%username%\"";
            // Target the CommonApplicationData folder since this is a place the particular user can get to
            var workingDirectory           = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
            var networkCredential          = user.GetCredential();
            var customEnvironmentVariables = new Dictionary <string, string>();

            var exitCode = Execute(Command,
                                   arguments,
                                   workingDirectory,
                                   out var debugMessages,
                                   out var infoMessages,
                                   out var errorMessages,
                                   networkCredential,
                                   customEnvironmentVariables,
                                   cts.Token);

            exitCode.Should().Be(0, "the process should have run to completion");
            debugMessages.ToString()
            .Should()
            .ContainEquivalentOf(Command, "the command should be logged")
            .And.ContainEquivalentOf($@"{user.DomainName}\{user.UserName}", "the custom user details should be logged")
            .And.ContainEquivalentOf(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "the working directory should be logged");
            infoMessages.ToString().Should().ContainEquivalentOf($@"{user.DomainName}\{user.UserName}");
            errorMessages.ToString().Should().BeEmpty("no messages should be written to stderr");
        }