Exemplo n.º 1
0
        public void UpdateWebRoleServiceDefinitionTest()
        {
            var doc = new XmlDocument();

            doc.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8""?>
<ServiceDefinition name=""Azure1"" xmlns=""http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition"" schemaVersion=""2014-01.2.3"">
  <WorkerRole name=""PythonApplication1"" vmsize=""Small"" />
  <WebRole name=""PythonApplication2"" />
</ServiceDefinition>");

            PythonProjectNode.UpdateServiceDefinition(doc, "Web", "PythonApplication2");

            AssertUtil.AreEqual(@"<?xml version=""1.0"" encoding=""utf-8""?>
<ServiceDefinition name=""Azure1"" xmlns=""http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition"" schemaVersion=""2014-01.2.3"">
  <WorkerRole name=""PythonApplication1"" vmsize=""Small"" />
  <WebRole name=""PythonApplication2"">
    <Startup>
      <Task commandLine=""ps.cmd ConfigureCloudService.ps1"" executionContext=""elevated"" taskType=""simple"">
        <Environment>
          <Variable name=""EMULATED"">
            <RoleInstanceValue xpath=""/RoleEnvironment/Deployment/@emulated"" />
          </Variable>
        </Environment>
      </Task>
    </Startup>
  </WebRole>
</ServiceDefinition>", doc);
        }
Exemplo n.º 2
0
        private static void CloudProjectTest(string roleType, bool openServiceDefinition)
        {
            Assert.IsTrue(roleType == "Web" || roleType == "Worker", "Invalid roleType: " + roleType);

            var asm = Assembly.Load("Microsoft.VisualStudio.CloudService.Wizard,Version=1.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a");

            if (asm != null && asm.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false)
                .OfType <AssemblyFileVersionAttribute>()
                .Any(a => {
                Version ver;
                return(Version.TryParse(a.Version, out ver) && ver < new Version(2, 5));
            })
                )
            {
                Assert.Inconclusive("Test requires Microsoft Azure Tools 2.5 or later");
            }

            using (var app = new PythonVisualStudioApp())
                using (FileUtils.Backup(TestData.GetPath(@"TestData\CloudProject\ServiceDefinition.csdef"))) {
                    app.OpenProject("TestData\\CloudProject.sln", expectedProjects: 3);

                    var ccproj = app.Dte.Solution.Projects.Cast <Project>().FirstOrDefault(p => p.Name == "CloudProject");
                    Assert.IsNotNull(ccproj);

                    if (openServiceDefinition)
                    {
                        var wnd = ccproj.ProjectItems.Item("ServiceDefinition.csdef").Open();
                        wnd.Activate();
                        app.OnDispose(() => wnd.Close());
                    }

                    IVsHierarchy hier;
                    var          sln = app.GetService <IVsSolution>(typeof(SVsSolution));
                    ErrorHandler.ThrowOnFailure(sln.GetProjectOfUniqueName(ccproj.FullName, out hier));

                    app.ServiceProvider.GetUIThread().InvokeAsync(() =>
                                                                  PythonProjectNode.UpdateServiceDefinition(hier, roleType, roleType + "Role1", app.ServiceProvider)
                                                                  ).GetAwaiter().GetResult();

                    var doc = new XmlDocument();
                    for (int retries = 5; retries > 0; --retries)
                    {
                        try {
                            doc.Load(TestData.GetPath(@"TestData\CloudProject\ServiceDefinition.csdef"));
                            break;
                        } catch (IOException ex) {
                            Console.WriteLine("Exception while reading ServiceDefinition.csdef.{0}{1}", Environment.NewLine, ex);
                        } catch (XmlException) {
                            var copyTo = TestData.GetPath(@"TestData\CloudProject\" + Path.GetRandomFileName());
                            File.Copy(TestData.GetPath(@"TestData\CloudProject\ServiceDefinition.csdef"), copyTo);
                            Console.WriteLine("Copied file to " + copyTo);
                            throw;
                        }
                        Thread.Sleep(100);
                    }
                    var ns = new XmlNamespaceManager(doc.NameTable);
                    ns.AddNamespace("sd", "http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition");
                    doc.Save(Console.Out);

                    var nav = doc.CreateNavigator();
                    if (roleType == "Web")
                    {
                        Assert.IsNotNull(nav.SelectSingleNode(
                                             "/sd:ServiceDefinition/sd:WebRole[@name='WebRole1']/sd:Startup/sd:Task[@commandLine='ps.cmd ConfigureCloudService.ps1']",
                                             ns
                                             ));
                    }
                    else if (roleType == "Worker")
                    {
                        Assert.IsNotNull(nav.SelectSingleNode(
                                             "/sd:ServiceDefinition/sd:WorkerRole[@name='WorkerRole1']/sd:Startup/sd:Task[@commandLine='bin\\ps.cmd ConfigureCloudService.ps1']",
                                             ns
                                             ));
                        Assert.IsNotNull(nav.SelectSingleNode(
                                             "/sd:ServiceDefinition/sd:WorkerRole[@name='WorkerRole1']/sd:Runtime/sd:EntryPoint/sd:ProgramEntryPoint[@commandLine='bin\\ps.cmd LaunchWorker.ps1']",
                                             ns
                                             ));
                    }
                }
        }