golang.org/x/build@v0.0.0-20240506185731-218518f32b70/internal/task/tagtelemetry_test.go (about) 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 task 6 7 import ( 8 "context" 9 "fmt" 10 "testing" 11 12 "golang.org/x/build/internal/workflow" 13 ) 14 15 func TestTagTelemetry(t *testing.T) { 16 mustHaveShell(t) 17 18 tests := []struct { 19 label string 20 tags []string 21 initialConfig string 22 masterConfig string 23 generatedConfig string 24 wantCommit bool 25 wantTag string 26 }{ 27 { 28 label: "no existing tag", 29 tags: []string{"v0.1.0"}, // note: not a tag of the config submodule 30 initialConfig: "{}", 31 masterConfig: "{}", 32 generatedConfig: "{ }", 33 wantCommit: true, // we should commit, even if we don't tag 34 wantTag: "", // only start tagging once the config module has been tagged at least once 35 }, 36 { 37 label: "generated tag", 38 tags: []string{"v0.6.0", "config/v0.0.1"}, 39 initialConfig: "{}", 40 masterConfig: "{}", 41 generatedConfig: "{ }", 42 wantCommit: true, 43 wantTag: "config/v0.1.0", 44 }, 45 { 46 label: "master tag", 47 tags: []string{"config/v0.1.0", "config/v0.2.0", "config/v0.2.1"}, 48 initialConfig: "{}", 49 masterConfig: `{ }`, 50 generatedConfig: `{ }`, 51 wantCommit: false, // no change since master 52 wantTag: "config/v0.3.0", 53 }, 54 } 55 56 for _, test := range tests { 57 t.Run(test.label, func(t *testing.T) { 58 // Gerrit setup: create an initial commit with the initialConfig 59 // contents, all tags at that initial commit, and then a master commit 60 // with the masterConfig contents. 61 telemetry := NewFakeRepo(t, "telemetry") 62 t1 := telemetry.Commit(map[string]string{ 63 "go.mod": "module golang.org/x/telemetry\n", 64 "go.sum": "\n", 65 "config/go.mod": "module golang.org/x/telemetry/config\n", 66 "config/go.sum": "\n", 67 "config/config.json": test.initialConfig, 68 }) 69 for _, tag := range test.tags { 70 telemetry.Tag(tag, t1) 71 } 72 t2 := telemetry.Commit(map[string]string{ 73 "a/a.go": "package a", // an arbitrary change to ensure the commit is nonempty 74 "config/config.json": test.masterConfig, 75 }) 76 gerrit := NewFakeGerrit(t, telemetry) 77 78 // Go setup: since the task only ever invokes `go run`, just write the 79 // resulting generated config to config.json. 80 var fakeGo = fmt.Sprintf(`#!/bin/bash -eu 81 82 echo -n %q > config/config.json 83 `, test.generatedConfig) 84 85 tasks := &TagTelemetryTasks{ 86 Gerrit: gerrit, 87 CloudBuild: NewFakeCloudBuild(t, gerrit, "", nil, fakeGo), 88 } 89 90 wd := tasks.NewDefinition() 91 w, err := workflow.Start(wd, map[string]interface{}{ 92 reviewersParam.Name: []string(nil), 93 }) 94 if err != nil { 95 t.Fatal(err) 96 } 97 98 ctx, cancel := context.WithCancel(context.Background()) 99 defer cancel() 100 101 outputs, err := w.Run(ctx, &verboseListener{t: t}) 102 if err != nil { 103 t.Fatal(err) 104 } 105 106 // Verify that the master branch was updated as expected. 107 gotMaster, err := gerrit.ReadBranchHead(ctx, "telemetry", "master") 108 if err != nil { 109 t.Fatal(err) 110 } 111 if test.wantCommit != (gotMaster != t2) { 112 t.Errorf("telemetry@master = %s (from %s), but want commit: %t", gotMaster, t2, test.wantCommit) 113 } 114 115 // Verify that we created the expected tag. 116 if got := outputs["tag"]; got != test.wantTag { 117 t.Errorf("Output: got \"tag\" %q, want %q", got, test.wantTag) 118 } 119 finalConfig, err := gerrit.ReadFile(ctx, "telemetry", gotMaster, "config/config.json") 120 if err != nil { 121 t.Fatal(err) 122 } 123 124 // Finally, check that the resulting config state in master is correct. 125 // No matter what, the final state of master should match the generated 126 // state. 127 if got, want := string(finalConfig), test.generatedConfig; got != want { 128 t.Errorf("Final config = %q, want %q", got, want) 129 } 130 }) 131 } 132 }