Debian Codegraph @

debian/go/src/golang.org/x/telemetry/internal/configstore/download_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 package configstore_test
6
7 import (
8 "encoding/json"
9 "fmt"
10 "reflect"
11 "strings"
12 "testing"
13
14 "golang.org/x/telemetry/internal/configstore"
15 "golang.org/x/telemetry/internal/configtest"
16 "golang.org/x/telemetry/internal/telemetry"
17 "golang.org/x/telemetry/internal/testenv"
18 )
19
20 func TestDownload(t *testing.T) {
21 testenv.NeedsGo(t)
22
23 configVersion := "v0.1.0"
24 in := &telemetry.UploadConfig{
25 GOOS: []string{"darwin"},
26 GOARCH: []string{"amd64", "arm64"},
27 GoVersion: []string{"1.20.3", "1.20.4"},
28 Programs: []*telemetry.ProgramConfig{{
29 Name: "gopls",
30 Versions: []string{"v0.11.0"},
31 Counters: []telemetry.CounterConfig{{
32 Name: "foobar",
33 Rate: 2,
34 }},
35 }},
36 }
37
38 env := configtest.LocalProxyEnv(t, in, configVersion)
39 testCases := []struct {
40 version string
41 want telemetry.UploadConfig
42 }{
43 {version: configVersion, want: *in},
44 {version: "latest", want: *in},
45 }
46 for _, tc := range testCases {
47 t.Run(tc.version, func(t *testing.T) {
48 got, _, err := configstore.Download(tc.version, env)
49 if err != nil {
50 t.Fatal("failed to download:", err)
51 }
52
53 want := tc.want
54 if !reflect.DeepEqual(*got, want) {
55 t.Errorf("Download(latest, _) = %v\nwant %v", stringify(got), stringify(want))
56 }
57 })
58 }
59
60 t.Run("invalidversion", func(t *testing.T) {
61 got, ver, err := configstore.Download("nonexisting", env)
62 if err == nil {
63 t.Fatalf("download succeeded unexpectedly: %v %+v", ver, got)
64 }
65 if !strings.Contains(err.Error(), "invalid version") {
66 t.Errorf("unexpected error message: %v", err)
67 }
68 })
69 }
70
71 func stringify(x any) string {
72 ret, err := json.MarshalIndent(x, "", " ")
73 if err != nil {
74 return fmt.Sprintf("json.Marshal failed - %v", err)
75 }
76 return string(ret)
77 }