/// <summary> /// Serializes the object to JSON. /// </summary> /// <param name="writer">The <see cref="T: Newtonsoft.Json.JsonWriter" /> to write to.</param> /// <param name="obj">The object to serialize to JSON.</param> internal static void Serialize(JsonWriter writer, NetworkRef obj) { // Required properties are always serialized, optional properties are serialized when not null. writer.WriteStartObject(); if (obj.Name != null) { writer.WriteProperty(obj.Name, "name", JsonWriterExtensions.WriteStringValue); } writer.WriteEndObject(); }
public void TestValidateNATNetworkPoliciesForManifestGenerationOnOpenForComposeScenario() { ApplicationTypeInformation applicationTypeInfo = new ApplicationTypeInformation("AppType", "1.0"); Application app = new Application(); app.name = "TestApp"; Management.ImageBuilder.SingleInstance.CodePackage cpkg = new Management.ImageBuilder.SingleInstance.CodePackage(); cpkg.image = "image"; cpkg.name = "CodePackage"; var endpoints = new List <Endpoint>(); var endpoint = new Endpoint(); endpoint.name = "publicListener"; endpoint.port = 8080; endpoints.Add(endpoint); cpkg.endpoints = endpoints; var resource = new Resources(); var req = new ResourceDescription(); req.cpu = 0.5; req.memoryInGB = 1; resource.requests = req; cpkg.resources = resource; Service svc = new Service(); svc.properties.codePackages = new List <Management.ImageBuilder.SingleInstance.CodePackage>(); svc.properties.codePackages.Add(cpkg); svc.name = "Svc1"; NetworkRef nref = new NetworkRef(); nref.name = StringConstants.NatNetworkName; EndpointRef endpointRef = new EndpointRef(); endpointRef.name = "publicListener"; nref.endpointRefs = new List <EndpointRef>() { endpointRef }; svc.properties.networkRefs = new List <NetworkRef>(); svc.properties.networkRefs.Add(nref); app.properties.services = new List <Service>(); app.properties.services.Add(svc); string applicationTestDir = Path.Combine(TestUtility.TestDirectory, Guid.NewGuid().ToString()); string currentExecutingDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); this.imageBuilder.BuildSingleInstanceApplication(app, "AppType", "1.0", "TestApp", new Uri("fabric:/TestApp"), false, TimeSpan.MaxValue, applicationTestDir, null, true, false, null, new GenerationConfig()); string expectedAppManifest = Path.Combine(applicationTestDir, "ApplicationManifest.xml"); var validatingXmlReaderSettings = new XmlReaderSettings(); validatingXmlReaderSettings.ValidationType = ValidationType.Schema; validatingXmlReaderSettings.Schemas.Add(StringConstants.FabricNamespace, Path.Combine(currentExecutingDirectory, StringConstants.DefaultSchemaPath)); validatingXmlReaderSettings.XmlResolver = null; var applicationManifestType = ImageBuilderUtility.ReadXml <ApplicationManifestType>(expectedAppManifest, validatingXmlReaderSettings); ApplicationManifestTypeServiceManifestImport[] servicemanifestimports = applicationManifestType.ServiceManifestImport; foreach (var serviceManifestImport in servicemanifestimports) { var policies = serviceManifestImport.Policies; foreach (var policy in policies) { if (policy.GetType() == typeof(ContainerHostPoliciesType)) { var ContainerHostPoliciesTypeItems = ((ContainerHostPoliciesType)policy).Items; foreach (var item in ContainerHostPoliciesTypeItems) { if (item.GetType() == typeof(PortBindingType)) { var portBindingType = (PortBindingType)item; Verify.AreEqual("publicListener", portBindingType.EndpointRef); Verify.AreEqual(8080, portBindingType.ContainerPort); } } } } } }