public void LinuxServiceControlManagerShouldConfigureCorrectly()
        {
            using (var tc = CreateTestContext())
            {
                var controlManager = new LinuxServiceControlManager();
                controlManager.Initialize(tc);
                tc.EnqueueInstance <IProcessInvoker>(_processInvoker.Object);
                tc.EnqueueInstance <IProcessInvoker>(_processInvoker.Object);
                tc.EnqueueInstance <IProcessInvoker>(_processInvoker.Object);

                var agentSettings = new AgentSettings
                {
                    AgentName = "agent",
                    ServerUrl = "http://server.name"
                };
                string unitFileTemplatePath = Path.Combine(IOUtil.GetBinPath(), "vsts.agent.service.template");
                File.WriteAllText(unitFileTemplatePath, "{User}-{Description}");

                controlManager.ConfigureService(agentSettings, null);


                Assert.Equal(controlManager.ServiceName, LinuxServiceName);
                Assert.Equal(controlManager.ServiceDisplayName, "VSTS Agent (server.agent)");

                _processInvoker.Verify(
                    x => x.ExecuteAsync("/usr/bin", "systemctl", "daemon-reload", It.IsAny <Dictionary <string, string> >(), It.IsAny <CancellationToken>()),
                    Times.Once);

                _processInvoker.Verify(
                    x =>
                    x.ExecuteAsync("/usr/bin", "systemctl", "enable vsts.agent.server.agent.service", It.IsAny <Dictionary <string, string> >(), It.IsAny <CancellationToken>()),
                    Times.Once);
            }
        }
        public void LinuxServiceControlManagerShouldStartServiceCorrectly()
        {
            using (var tc = CreateTestContext())
            {
                var controlManager = new LinuxServiceControlManager();
                controlManager.Initialize(tc);

                tc.EnqueueInstance <IProcessInvoker>(_processInvoker.Object);
                tc.EnqueueInstance <IProcessInvoker>(_processInvoker.Object);
                var agentSettings = new AgentSettings
                {
                    AgentName = "agent",
                    ServerUrl = "http://server.name"
                };

                // Tests dont run with sudo permission
                Environment.SetEnvironmentVariable("SUDO_USER", Environment.GetEnvironmentVariable("USER"));
                controlManager.ServiceName = "testservice";
                controlManager.StartService();

                _processInvoker.Verify(
                    x => x.ExecuteAsync("/usr/bin", "systemctl", "daemon-reload", It.IsAny <Dictionary <string, string> >(), It.IsAny <CancellationToken>()),
                    Times.Once);

                _processInvoker.Verify(
                    x => x.ExecuteAsync("/usr/bin", "systemctl", "start testservice", It.IsAny <Dictionary <string, string> >(), It.IsAny <CancellationToken>()),
                    Times.Once);
            }
        }
        public void VerifyLinuxServiceConfigurationFileContent()
        {
            using (var tc = CreateTestContext())
            {
                var controlManager = new LinuxServiceControlManager();
                controlManager.Initialize(tc);
                tc.EnqueueInstance <IProcessInvoker>(_processInvoker.Object);
                tc.EnqueueInstance <IProcessInvoker>(_processInvoker.Object);
                tc.EnqueueInstance <IProcessInvoker>(_processInvoker.Object);

                var testUser = "******";
                Environment.SetEnvironmentVariable("SUDO_USER", testUser);
                string expectedUnitContent = string.Format(@"[Unit]
Description=VSTS Agent (server.agent)
After=network.target

[Service]
ExecStart={0}/runsvc.sh
User={1}
Environment=NODE_ENV=production
WorkingDirectory={0}
KillMode=process
KillSignal=SIGTERM
TimeoutStopSec=5min

[Install]
WantedBy=multi-user.target
", IOUtil.GetRootPath(), testUser);

                var agentSettings = new AgentSettings
                {
                    AgentName = "agent",
                    ServerUrl = "http://server.name"
                };
                string unitFileTemplatePath = Path.Combine(TestUtil.GetSrcPath(), "Misc", "layoutbin", "vsts.agent.service.template");
                string unitFilePath         = _serviceHelper.Object.GetUnitFile(LinuxServiceName);
                File.Copy(unitFileTemplatePath, IOUtil.GetBinPath(), true);
                _serviceHelper.Setup(x => x.CheckIfSystemdExists()).Returns(true);

                controlManager.ConfigureService(agentSettings, null);

                Assert.Equal(File.ReadAllText(unitFilePath), expectedUnitContent);
            }
        }