Esempio n. 1
0
        private IEnumerable <string> ExpectBasicSpanDataExists(IMockSpan span)
        {
            if (string.IsNullOrWhiteSpace(span.Resource))
            {
                yield return("Resource must be set.");
            }

            if (string.IsNullOrWhiteSpace(span.Name))
            {
                yield return("Name must be set.");
            }

            if (string.IsNullOrWhiteSpace(span.Service))
            {
                yield return("Service must be set.");
            }

            if (span.TraceId == default)
            {
                yield return("TraceId must be set.");
            }

            if (span.SpanId == default)
            {
                yield return("SpanId must be set.");
            }
        }
        private bool IsNotServerLifeCheck(IMockSpan span)
        {
            var url = SpanExpectation.GetTag(span, Tags.HttpUrl);

            if (url == null)
            {
                return(true);
            }

            return(!url.Contains("alive-check"));
        }
        public override bool Matches(IMockSpan span)
        {
            var spanUri = GetTag(span, Tags.HttpUrl);

            if (spanUri == null || !spanUri.Contains(OriginalUri))
            {
                return(false);
            }

            return(base.Matches(span));
        }
Esempio n. 4
0
        private IEnumerable <string> ExpectErrorMatch(IMockSpan span)
        {
            var error = GetTag(span, Tags.ErrorMsg);

            if (string.IsNullOrEmpty(error))
            {
                if (IsGraphQLError)
                {
                    yield return($"Expected an error message but {Tags.ErrorMsg} tag is missing or empty.");
                }
            }
            else
            {
                if (!IsGraphQLError)
                {
                    yield return($"Expected no error message but {Tags.ErrorMsg} tag was {error}.");
                }
            }
        }
    public static void AssertExpectationsMet <T>(
        List <T> expectations,
        List <IMockSpan> spans)
        where T : SpanExpectation
    {
        Assert.True(spans.Count >= expectations.Count, $"Expected at least {expectations.Count} spans, received {spans.Count}");

        List <string>    failures       = new List <string>();
        List <IMockSpan> remainingSpans = spans.Select(s => s).ToList();

        foreach (SpanExpectation expectation in expectations)
        {
            List <IMockSpan> possibleSpans =
                remainingSpans
                .Where(s => expectation.Matches(s))
                .ToList();

            if (possibleSpans.Count == 0)
            {
                failures.Add($"No spans for: {expectation}");
                continue;
            }

            IMockSpan resultSpan = possibleSpans.First();

            if (!remainingSpans.Remove(resultSpan))
            {
                throw new Exception("Failed to remove an inspected span, can't trust this test.'");
            }

            if (!expectation.MeetsExpectations(resultSpan, out var failureMessage))
            {
                failures.Add($"{expectation} failed with: {failureMessage}");
            }
        }

        string finalMessage = Environment.NewLine + string.Join(Environment.NewLine, failures.Select(f => " - " + f));

        Assert.True(!failures.Any(), finalMessage);
        Assert.True(remainingSpans.Count == 0, $"There were {remainingSpans.Count} spans unaccounted for.");
    }
Esempio n. 6
0
        /// <summary>
        /// The aggregate assertion which is run for a test.
        /// </summary>
        /// <param name="span">The span being asserted against.</param>
        /// <param name="message">The developer friendly message for the test failure.</param>
        /// <returns>Whether the span meets expectations.</returns>
        public bool MeetsExpectations(IMockSpan span, out string message)
        {
            message = string.Empty;

            var messages = new List <string>();

            foreach (var assertion in Assertions)
            {
                var mismatchMessage = assertion(span);
                if (!string.IsNullOrWhiteSpace(mismatchMessage))
                {
                    messages.Add(mismatchMessage);
                }
            }

            if (messages.Any())
            {
                message = string.Join(",", messages);
                return(false);
            }

            return(true);
        }
Esempio n. 7
0
 /// <summary>
 /// Override for custom filters.
 /// </summary>
 /// <param name="span">The span on which to filter.</param>
 /// <returns>Whether the span qualifies for this expectation.</returns>
 public virtual bool Matches(IMockSpan span)
 {
     return(span.Service == ServiceName &&
            span.Name == OperationName &&
            span.Type == Type);
 }
Esempio n. 8
0
 public static string GetTag(IMockSpan span, string tag)
 {
     span.Tags.TryGetValue(tag, out var value);
     return(value);
 }