Exemple #1
0
        public void InstallThing()
        {
            //---------------Set up test pack-------------------
            var localPath = TestServicePath;

            //---------------Assert Precondition----------------
            Expect(localPath)
            .To.Exist($"Expected to find test service at {localPath}");

            //---------------Execute Test ----------------------
            Run(localPath, "-i");
            var util = new WindowsServiceUtil("test service");

            Expect(util.IsInstalled).To.Be.True();

            // check that we can uninstall
            util.Uninstall(true);
            Expect(util.IsInstalled)
            .To.Be.False();
            // check that attempting again throws
            Expect(() => util.Uninstall(true))
            .To.Throw <ServiceNotInstalledException>();

            //---------------Test Result -----------------------
        }
        public void ReinstallThing()
        {
            //---------------Set up test pack-------------------
            var util = new WindowsServiceUtil("test-service");

            //---------------Assert Precondition----------------
            Assert.IsTrue(util.IsInstalled);

            //---------------Execute Test ----------------------
            util.Uninstall(true);

            //---------------Test Result -----------------------
        }
Exemple #3
0
        private int UninstallMe()
        {
            var svcUtil = new WindowsServiceUtil(ServiceName);

            if (!svcUtil.IsInstalled)
            {
                Console.WriteLine("Not installed!");
                return((int)CommandlineOptions.ExitCodes.UninstallFailed);
            }
            try
            {
                svcUtil.Uninstall();
                Console.WriteLine("Uninstalled!");
                return((int)CommandlineOptions.ExitCodes.Success);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Unable to install: " + ex.Message);
                return((int)CommandlineOptions.ExitCodes.UninstallFailed);
            }
        }
Exemple #4
0
        private int InstallMe(CommandlineOptions cli)
        {
            var myExePath       = new FileInfo(Environment.GetCommandLineArgs()[0]).FullName;
            var existingSvcUtil = new WindowsServiceUtil(ServiceName);

            if (existingSvcUtil.Commandline == myExePath)
            {
                Console.WriteLine("Service already installed correctly");
                return((int)CommandlineOptions.ExitCodes.Success);
            }

            try
            {
                if (existingSvcUtil.IsInstalled)
                {
                    existingSvcUtil.Uninstall(true);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Service already installed at: " + existingSvcUtil.Commandline +
                                  " and I can't uninstall it: " + ex.Message);
                return((int)CommandlineOptions.ExitCodes.InstallFailed);
            }

            var svcUtil = new WindowsServiceUtil(ServiceName, DisplayName, myExePath);

            try
            {
                var bootFlag = ResolveBootFlagFor(cli);
                svcUtil.Install(bootFlag);
                Console.WriteLine("Installed!");
                return((int)CommandlineOptions.ExitCodes.Success);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Unable to install: " + ex.Message);
                return((int)CommandlineOptions.ExitCodes.InstallFailed);
            }
        }
Exemple #5
0
        public void ServiceWithArgs()
        {
            // Arrange
            var myPath = new Uri(GetType().Assembly.Location).LocalPath;
            var myDir = Path.GetDirectoryName(myPath);
            var serviceExe = Path.Combine(myDir, "ServiceWithArgs.exe");
            var logFile = Path.Combine(myDir, "service.log");
            var arg1 = GetRandomString(3);
            var arg2 = GetRandomString(3);
            var args = new[] { logFile, arg1, arg2 }.Select(p => p.QuoteIfSpaced());
            var serviceName = "test-service-foo-bar";
            var displayName = "Test Service - Foo,Bar";
            var commandline = string.Join(
                " ",
                new[] { serviceExe }
                .Concat(args)
                );
            var util = new WindowsServiceUtil(
                serviceName,
                displayName,
                commandline
                );

            if (util.IsInstalled)
            {
                util.Uninstall();
            }

            if (File.Exists(logFile))
            {
                File.Delete(logFile);
            }

            // Act
            using var resetter = new AutoResetter(
                      () => util.Install(),
                      () => util.Uninstall(true));
            util.Install();
            // Assert
            Expect(util.IsInstalled)
            .To.Be.True();
            util.Start();
            Expect(util.State)
            .To.Equal(ServiceState.Running);

            Expect(logFile)
            .To.Exist();
            var logData = TryReadAllLinesFrom(logFile);

            Expect(logData)
            .Not.To.Be.Empty();
            Expect(logData[0])
            .To.Contain(arg1)
            .Then(arg2);

            util.Stop();

            Expect(util.State)
            .To.Equal(ServiceState.Stopped);

            var anotherUtil = Create(serviceName);

            Expect(anotherUtil.Commandline)
            .To.Equal(commandline);
            Expect(anotherUtil.ServiceExe)
            .To.Equal(serviceExe);
            Expect(anotherUtil.DisplayName)
            .To.Equal(displayName);
        }
Exemple #6
0
        public void BigHairyIntegrationTest()
        {
            if (Platform.Is32Bit)
            {
                Assert.Ignore(
                    "Running 32-bit: test will fail: 32-bit process cannot access 64-bit process info"
                    );
            }

            // Arrange
            var serviceExe = TestServicePath;

            Expect(serviceExe).To.Exist($"Expected to find test service at {serviceExe}");
            EnsureTestServiceIsNotInstalled();
            // Act
            Do("Install via cli", () => Run(serviceExe, "-i", "-n", TestServiceName));
            var util = new WindowsServiceUtil(TestServiceName);

            Do("Test is installed",
               () => Expect(util.IsInstalled)
               .To.Be.True()
               );
            Do("Test service commandline",
               () => Expect(util.Commandline)
               .To.Equal(serviceExe)
               );
            Do("Test default startup type",
               () => Expect(util.StartupType)
               .To.Equal(ServiceStartupTypes.Automatic)
               );

            Do("Disabled service",
               () =>
            {
                util.Disable();
                Expect(util.StartupType)
                .To.Equal(ServiceStartupTypes.Disabled);
            });
            Do("Re-enable service",
               () =>
            {
                util.SetAutomaticStart();
                Expect(util.StartupType)
                .To.Equal(ServiceStartupTypes.Automatic);
            });

            Do("Start service",
               () =>
            {
                util.Start();
                Expect(util.State)
                .To.Equal(ServiceState.Running);
                var processes = Process.GetProcesses();
                var process   = processes.FirstOrDefault(p =>
                {
                    try
                    {
                        return(p?.MainModule?.FileName.Equals(serviceExe, StringComparison.OrdinalIgnoreCase) ??
                               false);
                    }
                    catch
                    {
                        return(false);
                    }
                });
                Expect(process).Not.To.Be.Null(
                    () =>
                    $"@Service should be running: {serviceExe}\nrunning:\n{DumpProcesses()}"
                    );
                Expect(process.Id)
                .To.Equal(util.ServicePID);

                string DumpProcesses()
                {
                    return(processes.Select(p =>
                    {
                        try
                        {
                            return $"{p.MainModule.FileName}";
                        }
                        catch (Exception e)
                        {
                            return $"(unknown) ({e.Message})";
                        }
                    }).OrderBy(s => s).JoinWith("\n"));

                    ;
                }
            });


            var byPath = WindowsServiceUtil.GetServiceByPath(serviceExe);

            Expect(byPath)
            .Not.To.Be.Null($"Should be able to query service by path {serviceExe}");

            Do("Pause service",
               () =>
            {
                byPath.Pause();
                Expect(byPath.State)
                .To.Equal(ServiceState.Paused);
            });

            Do("Resume service",
               () =>
            {
                byPath.Continue();
                Expect(byPath.State)
                .To.Equal(ServiceState.Running);
            });

            Do("Stop service",
               () =>
            {
                util.Stop();
                Expect(util.State)
                .To.Equal(ServiceState.Stopped);
            });

            Do("Uninstall via cli",
               () =>
            {
                Run(serviceExe, "-u", "-n", TestServiceName);

                Expect(util.IsInstalled)
                .To.Be.False();
            });

            Do("Install and start (api)",
               () =>
            {
                util = new WindowsServiceUtil(
                    TestServiceName,
                    TestServiceName,
                    serviceExe);
                util.InstallAndStart();
                Expect(util.IsInstalled)
                .To.Be.True();
                Expect(util.State)
                .To.Equal(ServiceState.Running);
            });
            Do("Re-install and start (api)",
               () =>
            {
                util = new WindowsServiceUtil(
                    TestServiceName,
                    TestServiceName,
                    serviceExe);
                util.Uninstall();
                util.InstallAndStart();
                Expect(util.IsInstalled)
                .To.Be.True();
                Expect(util.State)
                .To.Equal(ServiceState.Running);
            });
            Do("Uninstall (api)", () => util.Uninstall());
            // Assert
        }