func()

in pkg/export/gcm/promtest/promtest.go [242:313]


func (it *ingestionTest) FatalOnUnexpectedPromQLResults(b Backend, metric prometheus.Metric, timeout time.Duration) {
	it.t.Helper()

	m := dto.Metric{}
	//nolint:errcheck
	metric.Write(&m)

	if m.GetSummary() != nil || m.GetHistogram() != nil {
		// TODO(bwplotka): Implement alternative.
		it.t.Fatal("It's not practical to use FatalOnUnexpectedPromQLResults against histograms and summaries.")
	}

	bMeta, ok := it.backends[b.Ref()]
	if !ok {
		it.t.Fatalf("%s backend not seen before? Did you pass it in NewIngestionTest?", b.Ref())
	}

	modelMetric := toModelMetric(metric)
	exp := it.preparedExpectedMatrix(it.expectationsPerBackend[b.Ref()], modelMetric, bMeta.extLset)
	if exp == nil {
		it.t.Fatalf("expected metric %v, not found in expected Matrix. Did you use scrape(...).expect(...) method?", modelMetric.String())
	}

	modelMetric["test"] = model.LabelValue(it.testID)
	query := fmt.Sprintf(`%s[10h]`, modelMetric.String())

	fmt.Printf("%s Checking if PromQL instant query for %v matches expected samples for %v backend\n", it.t.Name(), query, b.Ref())

	ctx, cancel := context.WithTimeout(context.Background(), timeout)
	it.t.Cleanup(cancel)

	var lastDiff string
	var sameDiffTimes int
	if err := runutil.RetryWithLog(log.NewJSONLogger(os.Stderr), 10*time.Second, ctx.Done(), func() error {
		value, warns, err := bMeta.api.Query(ctx, query, it.currTime.Add(1*time.Second))
		if err != nil {
			return fmt.Errorf("instant query %s for %v %w", query, it.currTime.Add(1*time.Second), err)
		}
		if len(warns) > 0 {
			fmt.Println(it.t.Name(), "Warnings:", warns)
		}

		if value.Type() != model.ValMatrix {
			return fmt.Errorf("expected matrix, got %v", value.Type())
		}

		if cmp.Equal(exp, value.(model.Matrix)) {
			return nil
		}

		diff := cmp.Diff(exp, value.(model.Matrix))
		if lastDiff == diff {
			if sameDiffTimes > 3 {
				// Likely nothing will change, abort.
				fmt.Println(it.t.Name(), lastDiff)
				it.t.Error(errors.New("resulted Matrix is different than expected (see printed diff)"))
				return nil
			}
			sameDiffTimes++
		} else {
			lastDiff = diff
			sameDiffTimes = 0
		}

		return errors.New("resulted Matrix is different than expected (diff, if any, will be printed at the end)")
	}); err != nil {
		if lastDiff != "" {
			fmt.Println(it.t.Name(), lastDiff)
		}
		it.t.Error(err)
	}
}