Debian Codegraph @

debian/go/src/golang.org/x/telemetry/internal/configgen/validate_test.go

raw symbols SCIP
1 // Copyright 2023 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build go1.22
6
7 package main
8
9 import (
10 "strings"
11 "testing"
12
13 "golang.org/x/telemetry/internal/chartconfig"
14 )
15
16 func TestLoadedChartsAreValid(t *testing.T) {
17 // Test that we can actually load the chart config.
18 charts, err := chartconfig.Load()
19 if err != nil {
20 t.Errorf("Load() failed: %v", err)
21 }
22 for i, chart := range charts {
23 if err := ValidateChartConfig(chart); err != nil {
24 t.Errorf("Chart %d is invalid: %v", i, err)
25 }
26 }
27 }
28
29 func TestValidateOK(t *testing.T) {
30 // A minimally valid chart config.
31 const input = `
32 title: Editor Distribution
33 counter: gopls/editor:{emacs,vim,vscode,other}
34 type: partition
35 issue: https://go.dev/issue/12345
36 program: golang.org/x/tools/gopls
37 `
38 records, err := chartconfig.Parse([]byte(input))
39 if err != nil {
40 t.Fatal(err)
41 }
42 if len(records) != 1 {
43 t.Fatalf("Parse(%q) returned %d records, want exactly 1", input, len(records))
44 }
45 if err := ValidateChartConfig(records[0]); err != nil {
46 t.Errorf("Validate(%q) = %v, want nil", input, err)
47 }
48 }
49
50 func TestValidate(t *testing.T) {
51 tests := map[string][]string{ // input -> want errors
52 // validation of mandatory fields
53 "description:bar": {"title", "program", "issue", "counter", "type"},
54
55 // validation of semver intervals
56 "version:1.2.3.4": {"semver"},
57
58 // valid of stack configuration
59 "depth:-1": {"non-negative", "stack"},
60 }
61
62 for input, wantErrs := range tests {
63 records, err := chartconfig.Parse([]byte(input))
64 if err != nil {
65 t.Fatal(err)
66 }
67 if len(records) != 1 {
68 t.Fatalf("Parse(%q) returned %d records, want exactly 1", input, len(records))
69 }
70 err = ValidateChartConfig(records[0])
71 if err == nil {
72 t.Fatalf("Validate(%q) succeeded unexpectedly", input)
73 }
74 errs := err.Error()
75 for _, want := range wantErrs {
76 if !strings.Contains(errs, want) {
77 t.Errorf("Validate(%q) = %v, want containing %q", input, err, want)
78 }
79 }
80 }
81 }