Example #1
0
        public void TrafficViewEnableDisableQuerySizeScope()
        {
            using (MockContext context = MockContext.Start(this.GetType().FullName))
            {
                TrafficManagerManagementClient trafficManagerClient = this.GetTrafficManagerManagementClient(context);

                string        resourceGroupName = TrafficManagerHelper.GetPersistentResourceGroupName();
                string        profileName       = TrafficManagerHelper.GetPersistentTrafficViewProfile();
                ResourceGroup resourceGroup     = this.CreateResourceGroup(context, resourceGroupName);

                Profile profile = TrafficManagerHelper.CreateOrUpdateDefaultProfileWithExternalEndpoint(
                    trafficManagerClient,
                    resourceGroup.Name,
                    profileName);

                bool authorized = true;
                bool found      = true;
                try
                {
                    trafficManagerClient.HeatMap.Get(resourceGroupName, profileName);
                }
                catch (Microsoft.Rest.Azure.CloudException e)
                {
                    authorized = !e.Body.Code.Contains("NotAuthorized");

                    // 'NotFound' can happen if there were no queries to the endpoint since it was provisioned.
                    found = !(authorized && e.Body.Code.Contains("NotFound")); // Let's hope you were paying attention in that math logic class.
                }

                if (!found)
                {
                    // Pause, then retry once.
                    System.Threading.Thread.Sleep(TimeSpan.FromSeconds(120));
                    try
                    {
                        trafficManagerClient.HeatMap.Get(resourceGroupName, profileName);
                    }
                    catch (Microsoft.Rest.Azure.CloudException e)
                    {
                        authorized = !e.Body.Code.Contains("NotAuthorized");
                    }
                }

                Assert.False(authorized);

                // Change the enrollment status and update the profile.
                // Clear the endpoints first; those are not serializable as child objects because they have their own resource info.
                profile.TrafficViewEnrollmentStatus = "Enabled";
                profile.Endpoints = null;
                trafficManagerClient.Profiles.CreateOrUpdate(
                    resourceGroupName,
                    profileName,
                    profile);

                HeatMapModel heatMapModel = trafficManagerClient.HeatMap.Get(resourceGroupName, profileName);

                Assert.True(heatMapModel.StartTime.Value.CompareTo(System.DateTime.MinValue) > 0, "Invalid start time");
                Assert.True(heatMapModel.StartTime.Value.CompareTo(heatMapModel.EndTime.Value) <= 0, "Start time smaller than end time");
                Assert.True(heatMapModel.Endpoints.Count > 0, "Endpoint list empty, Not really an error but can not run test with no heatmap data.");
                foreach (HeatMapEndpoint ep in heatMapModel.Endpoints)
                {
                    Assert.True((ep.EndpointId ?? -1) >= 0, "Endpoint id null or out of range");
                    Assert.False(string.IsNullOrWhiteSpace(ep.ResourceId), "Resource Id undefined");
                }
                foreach (TrafficFlow tf in heatMapModel.TrafficFlows)
                {
                    Assert.False(string.IsNullOrWhiteSpace(tf.SourceIp), "SourceIp is undefined");
                    foreach (QueryExperience qe in tf.QueryExperiences)
                    {
                        Assert.True(heatMapModel.Endpoints.Where(ep => ep.EndpointId == qe.EndpointId).Count() > 0, "Query Experience does not match an existing endpoint");
                    }
                }

                IList <TrafficFlow> trafficFlowList = heatMapModel.TrafficFlows;


                foreach (TrafficFlow tf in trafficFlowList)
                {
                    if ((tf.Latitude - .1 >= -90.0) && (tf.Latitude + .1 <= 90.0) && (tf.Longitude - .1 >= -180.0) && (tf.Longitude + .1 <= 180.0))
                    {
                        heatMapModel = trafficManagerClient.HeatMap.Get(resourceGroupName, profileName, new List <double?>()
                        {
                            (tf.Latitude + .10), (tf.Longitude - .10)
                        }, new List <double?>()
                        {
                            (tf.Latitude - 0.10), (tf.Longitude + 0.10)
                        });
                        Assert.True(heatMapModel.TrafficFlows.Where(currentTF => (currentTF.Latitude == tf.Latitude && currentTF.Longitude == tf.Longitude)).Count() > 0, "Subset of coordinates not found");

                        heatMapModel = trafficManagerClient.HeatMap.Get(resourceGroupName, profileName, new List <double?>()
                        {
                            (tf.Latitude + .10), (tf.Longitude - .10)
                        }, new List <double?>()
                        {
                            (tf.Latitude + 0.05), (tf.Longitude - 0.05)
                        });
                        Assert.True(heatMapModel.TrafficFlows.Where(currentTF => (currentTF.Latitude == tf.Latitude && currentTF.Longitude == tf.Longitude)).Count() == 0, "Subset of coordinates not expected");
                    }
                }
            }
        }