public void CalculateServiceNameLimitsServiceNameTo80Chars()
        {
            RunnerSettings settings = new RunnerSettings();

            settings.AgentName = "thisisareallyreallylongbutstillvalidagentname";
            settings.ServerUrl = "https://example.githubusercontent.com/12345678901234567890123456789012345678901234567890";
            settings.GitHubUrl = "https://github.com/myreallylongorganizationexample/myreallylongrepoexample";

            string serviceNamePattern        = "actions.runner.{0}.{1}";
            string serviceDisplayNamePattern = "GitHub Actions Runner ({0}.{1})";

            using (TestHostContext hc = CreateTestContext())
            {
                ServiceControlManager scm = new ServiceControlManager();

                scm.Initialize(hc);
                scm.CalculateServiceName(
                    settings,
                    serviceNamePattern,
                    serviceDisplayNamePattern,
                    out string serviceName,
                    out string serviceDisplayName);

                // Verify name has been shortened to 80 characters
                Assert.Equal(80, serviceName.Length);

                var serviceNameParts = serviceName.Split('.');

                // Verify that each component has been shortened to a sensible length
                Assert.Equal("actions", serviceNameParts[0]);                                       // Never shortened
                Assert.Equal("runner", serviceNameParts[1]);                                        // Never shortened
                Assert.Equal("myreallylongorganizationexample-myreallylongr", serviceNameParts[2]); // First 45 chars, '/' has been replaced with '-'
                Assert.Equal("thisisareallyreally", serviceNameParts[3]);                           // Remainder of unused chars
            }
        }
        public void CalculateServiceNameSanitizeOutOfRangeChars()
        {
            RunnerSettings settings = new RunnerSettings();

            settings.AgentName = "name";
            settings.ServerUrl = "https://example.githubusercontent.com/12345678901234567890123456789012345678901234567890";
            settings.GitHubUrl = "https://github.com/org!@$*+[]()/repo!@$*+[]()";

            string serviceNamePattern        = "actions.runner.{0}.{1}";
            string serviceDisplayNamePattern = "GitHub Actions Runner ({0}.{1})";

            using (TestHostContext hc = CreateTestContext())
            {
                ServiceControlManager scm = new ServiceControlManager();

                scm.Initialize(hc);
                scm.CalculateServiceName(
                    settings,
                    serviceNamePattern,
                    serviceDisplayNamePattern,
                    out string serviceName,
                    out string serviceDisplayName);

                var serviceNameParts = serviceName.Split('.');

                // Verify service name parts are sanitized correctly
                Assert.Equal("actions", serviceNameParts[0]);
                Assert.Equal("runner", serviceNameParts[1]);
                Assert.Equal("org----------repo---------", serviceNameParts[2]); // Chars replaced with '-'
                Assert.Equal("name", serviceNameParts[3]);
            }
        }
        public void CalculateServiceName80Chars()
        {
            RunnerSettings settings = new RunnerSettings();

            settings.AgentName = "thisiskindofalongrunnername12";
            settings.ServerUrl = "https://example.githubusercontent.com/12345678901234567890123456789012345678901234567890";
            settings.GitHubUrl = "https://github.com/myorganizationexample/myrepoexample";

            string serviceNamePattern        = "actions.runner.{0}.{1}";
            string serviceDisplayNamePattern = "GitHub Actions Runner ({0}.{1})";

            using (TestHostContext hc = CreateTestContext())
            {
                ServiceControlManager scm = new ServiceControlManager();

                scm.Initialize(hc);
                scm.CalculateServiceName(
                    settings,
                    serviceNamePattern,
                    serviceDisplayNamePattern,
                    out string serviceName,
                    out string serviceDisplayName);

                // Verify name is still equal to 80 characters
                Assert.Equal(80, serviceName.Length);

                var serviceNameParts = serviceName.Split('.');

                // Verify nothing has been shortened out
                Assert.Equal("actions", serviceNameParts[0]);
                Assert.Equal("runner", serviceNameParts[1]);
                Assert.Equal("myorganizationexample-myrepoexample", serviceNameParts[2]); // '/' has been replaced with '-'
                Assert.Equal("thisiskindofalongrunnername12", serviceNameParts[3]);
            }
        }
Ejemplo n.º 4
0
        public void CalculateServiceNameL0()
        {
            using (TestHostContext tc = CreateTestContext())
            {
                Tracing trace = tc.GetTrace();

                trace.Info("Creating service control manager");
                ServiceControlManager scm = new ServiceControlManager();
                scm.Initialize(tc);
                ServiceNameTest[] tests = new ServiceNameTest[] {
                    new ServiceNameTest {
                        TestName                   = "SystemD Test",
                        ServiceNamePattern         = "vsts.agent.{0}.{1}.{2}.service",
                        ServiceDisplayPattern      = "Azure Pipelines Agent ({0}.{1}.{2})",
                        AgentName                  = "foo",
                        PoolName                   = "pool1",
                        ServerUrl                  = "https://dev.azure.com/bar",
                        ExpectedServiceName        = "vsts.agent.bar.pool1.foo.service",
                        ExpectedServiceDisplayName = "Azure Pipelines Agent (bar.pool1.foo)"
                    },
                    new ServiceNameTest {
                        TestName                   = "Long Agent/Pool Test",
                        ServiceNamePattern         = "vsts.agent.{0}.{1}.{2}.service",
                        ServiceDisplayPattern      = "Azure Pipelines Agent ({0}.{1}.{2})",
                        AgentName                  = new string('X', 40),
                        PoolName                   = new string('Y', 40),
                        ServerUrl                  = "https://dev.azure.com/bar",
                        ExpectedServiceName        = "vsts.agent.bar.YYYYYYYYYYYYYYYYYYYYYYYYY.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.service",
                        ExpectedServiceDisplayName = "Azure Pipelines Agent (bar.YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX)"
                    },
                    new ServiceNameTest {
                        TestName                   = "Pool With Unicode Dash Test",
                        ServiceNamePattern         = "vsts.agent.{0}.{1}.{2}.service",
                        ServiceDisplayPattern      = "Azure Pipelines Agent ({0}.{1}.{2})",
                        AgentName                  = "foo",
                        PoolName                   = "pool" + "\u002D" + "1",
                        ServerUrl                  = "https://dev.azure.com/bar",
                        ExpectedServiceName        = "vsts.agent.bar.pool-1.foo.service",
                        ExpectedServiceDisplayName = "Azure Pipelines Agent (bar.pool-1.foo)"
                    },
                };
                foreach (var test in tests)
                {
                    AgentSettings settings = new AgentSettings();
                    settings.ServerUrl = test.ServerUrl;
                    settings.AgentName = test.AgentName;
                    settings.PoolName  = test.PoolName;

                    string serviceName;
                    string serviceDisplayName;

                    scm.CalculateServiceName(settings, test.ServiceNamePattern, test.ServiceDisplayPattern, out serviceName, out serviceDisplayName);

                    Assert.True(string.Equals(serviceName, test.ExpectedServiceName), $"{test.TestName} Service Name Expected: {test.ExpectedServiceName}, Got: {serviceName}");
                    Assert.True(serviceName.Length <= 80, $"{test.TestName} Service Name is <= 80");
                    Assert.True(string.Equals(serviceDisplayName, test.ExpectedServiceDisplayName), $"{test.TestName} Service Display Name Expected: {test.ExpectedServiceDisplayName}, Got: {serviceDisplayName}");
                }
            }
        }