• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2023 Google LLC
2//
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5
6package main
7
8import (
9	"context"
10	"os"
11	"path/filepath"
12	"testing"
13
14	"github.com/stretchr/testify/assert"
15	"github.com/stretchr/testify/require"
16	"go.skia.org/infra/go/exec"
17	exec_testutils "go.skia.org/infra/go/exec/testutils"
18	"go.skia.org/infra/task_driver/go/lib/os_steps"
19	"go.skia.org/infra/task_driver/go/td"
20	"go.skia.org/skia/infra/bots/task_drivers/common"
21	"go.skia.org/skia/infra/bots/task_drivers/testutils"
22)
23
24func TestRun_Success(t *testing.T) {
25	// Given that we have tests for common.UploadToGold(), it suffices to test a couple of
26	// "happy" cases here.
27
28	test := func(name string, tdArgs taskDriverArgs, goldctlWorkDir, goldctlImgtestInitStepName string, goldctlImgtestInitArgs []string) {
29		t.Run(name, func(t *testing.T) {
30			// Create fake archive with undeclared test outputs.
31			outputsZIP := filepath.Join(tdArgs.checkoutDir, "bazel-testlogs", "some", "test", "target", "test.outputs", "outputs.zip")
32			require.NoError(t, os.MkdirAll(filepath.Dir(outputsZIP), 0700))
33			testutils.MakeZIP(t, outputsZIP, map[string]string{
34				// The contents of PNG files does not matter for this test.
35				"alfa.png": "fake PNG",
36				"alfa.json": `{
37					"md5": "a01a01a01a01a01a01a01a01a01a01a0",
38					"keys": {
39						"build_system": "bazel",
40						"name": "alfa",
41						"source_type": "gm"
42					}
43				}`,
44				"beta.png": "fake PNG",
45				"beta.json": `{
46					"md5": "b02b02b02b02b02b02b02b02b02b02b0",
47					"keys": {
48						"build_system": "bazel",
49						"name": "beta",
50						"source_type": "gm"
51					}
52				}`,
53			})
54
55			// Will be returned by the mocked os_steps.TempDir() when the task driver tries to create a
56			// directory in which to extract the undeclared outputs ZIP archive.
57			outputsZIPExtractionDir := t.TempDir()
58
59			commandCollector := exec.CommandCollector{}
60			res := td.RunTestSteps(t, false, func(ctx context.Context) error {
61				ctx = td.WithExecRunFn(ctx, commandCollector.Run)
62				var bazelCacheDirPath string
63				ctx, bazelCacheDirPath = common.WithEnoughSpaceOnBazelCachePartitionTestOnlyContext(ctx)
64
65				// We don't need to assert the exact number of times that os_steps.TempDir() is called
66				// because said function produces a "Creating TempDir" task driver step, and we check the
67				// exact set of steps produced.
68				ctx = context.WithValue(ctx, os_steps.TempDirContextKey, testutils.MakeTempDirMockFn(t, outputsZIPExtractionDir, goldctlWorkDir))
69
70				err := run(ctx, bazelCacheDirPath, tdArgs)
71
72				assert.NoError(t, err)
73				return err
74			})
75
76			require.Empty(t, res.Errors)
77			require.Empty(t, res.Exceptions)
78
79			testutils.AssertStepNames(t, res,
80				"Test //some/test:target with config linux_rbe",
81				"bazelisk test //some/test:target --config=linux_rbe --config=GCE_Debian10_AVX512 --test_output=errors --jobs=100",
82				"Creating TempDir",
83				"Extract undeclared outputs archive "+outputsZIP+" into "+outputsZIPExtractionDir,
84				"Extracting file: alfa.json",
85				"Extracting file: alfa.png",
86				"Extracting file: beta.json",
87				"Extracting file: beta.png",
88				"Gather JSON and PNG files produced by GMs",
89				"Gather \"alfa.png\"",
90				"Gather \"beta.png\"",
91				"Upload GM outputs to Gold",
92				"Creating TempDir",
93				"/path/to/goldctl auth --work-dir "+goldctlWorkDir+" --luci",
94				goldctlImgtestInitStepName,
95				"/path/to/goldctl imgtest add --work-dir "+goldctlWorkDir+" --test-name alfa --png-file "+outputsZIPExtractionDir+"/alfa.png --png-digest a01a01a01a01a01a01a01a01a01a01a0 --add-test-key build_system:bazel --add-test-key name:alfa --add-test-key source_type:gm",
96				"/path/to/goldctl imgtest add --work-dir "+goldctlWorkDir+" --test-name beta --png-file "+outputsZIPExtractionDir+"/beta.png --png-digest b02b02b02b02b02b02b02b02b02b02b0 --add-test-key build_system:bazel --add-test-key name:beta --add-test-key source_type:gm",
97				"/path/to/goldctl imgtest finalize --work-dir "+goldctlWorkDir,
98				"Clean Bazel cache if disk space is too low",
99				"No need to clear the Bazel cache: free space on partition /mnt/pd0 is 20000000000 bytes, which is above the threshold of 15000000000 bytes",
100			)
101
102			// Command "bazelisk test ..." should be called from the checkout directory.
103			assert.Equal(t, tdArgs.checkoutDir, commandCollector.Commands()[0].Dir)
104
105			exec_testutils.AssertCommandsMatch(t,
106				[][]string{
107					{
108						"bazelisk",
109						"test",
110						"//some/test:target",
111						"--config=linux_rbe",
112						"--config=GCE_Debian10_AVX512",
113						"--test_output=errors",
114						"--jobs=100",
115					},
116					{
117
118						"/path/to/goldctl",
119						"auth",
120						"--work-dir", goldctlWorkDir,
121						"--luci",
122					},
123					append([]string{
124						"/path/to/goldctl",
125					}, goldctlImgtestInitArgs...),
126					{
127						"/path/to/goldctl",
128						"imgtest",
129						"add",
130						"--work-dir", goldctlWorkDir,
131						"--test-name", "alfa",
132						"--png-file", outputsZIPExtractionDir + "/alfa.png",
133						"--png-digest", "a01a01a01a01a01a01a01a01a01a01a0",
134						"--add-test-key", "build_system:bazel",
135						"--add-test-key", "name:alfa",
136						"--add-test-key", "source_type:gm",
137					},
138					{
139						"/path/to/goldctl",
140						"imgtest",
141						"add",
142						"--work-dir", goldctlWorkDir,
143						"--test-name", "beta",
144						"--png-file", outputsZIPExtractionDir + "/beta.png",
145						"--png-digest", "b02b02b02b02b02b02b02b02b02b02b0",
146						"--add-test-key", "build_system:bazel",
147						"--add-test-key", "name:beta",
148						"--add-test-key", "source_type:gm",
149					},
150					{
151						"/path/to/goldctl",
152						"imgtest",
153						"finalize",
154						"--work-dir", goldctlWorkDir,
155					},
156				},
157				commandCollector.Commands(),
158			)
159		})
160	}
161
162	checkoutDir := t.TempDir()
163
164	goldctlWorkDir := t.TempDir()
165	test(
166		"post-submit task",
167		taskDriverArgs{
168			UploadToGoldArgs: common.UploadToGoldArgs{
169				TestOnlyAllowAnyBazelLabel: true,
170				BazelLabel:                 "//some/test:target",
171				DeviceSpecificBazelConfig:  "GCE_Debian10_AVX512",
172				GoldctlPath:                "/path/to/goldctl",
173				GitCommit:                  "ff99ff99ff99ff99ff99ff99ff99ff99ff99ff99",
174			},
175			checkoutDir: checkoutDir,
176			bazelConfig: "linux_rbe",
177		},
178		goldctlWorkDir,
179		"/path/to/goldctl imgtest init --work-dir "+goldctlWorkDir+" --instance skia --url https://gold.skia.org --bucket skia-infra-gm --git_hash ff99ff99ff99ff99ff99ff99ff99ff99ff99ff99 --key arch:x86_64 --key model:GCE --key os:Debian10",
180		[]string{
181			"imgtest",
182			"init",
183			"--work-dir", goldctlWorkDir,
184			"--instance", "skia",
185			"--url", "https://gold.skia.org",
186			"--bucket", "skia-infra-gm",
187			"--git_hash", "ff99ff99ff99ff99ff99ff99ff99ff99ff99ff99",
188			"--key", "arch:x86_64",
189			"--key", "model:GCE",
190			"--key", "os:Debian10",
191		},
192	)
193
194	goldctlWorkDir = t.TempDir()
195	test(
196		"CL task",
197		taskDriverArgs{
198			UploadToGoldArgs: common.UploadToGoldArgs{
199				TestOnlyAllowAnyBazelLabel: true,
200				BazelLabel:                 "//some/test:target",
201				DeviceSpecificBazelConfig:  "GCE_Debian10_AVX512",
202				GoldctlPath:                "/path/to/goldctl",
203				GitCommit:                  "ff99ff99ff99ff99ff99ff99ff99ff99ff99ff99",
204				ChangelistID:               "changelist-id",
205				PatchsetOrder:              "1",
206				TryjobID:                   "tryjob-id",
207			},
208			checkoutDir: checkoutDir,
209			bazelConfig: "linux_rbe",
210		},
211		goldctlWorkDir,
212		"/path/to/goldctl imgtest init --work-dir "+goldctlWorkDir+" --instance skia --url https://gold.skia.org --bucket skia-infra-gm --git_hash ff99ff99ff99ff99ff99ff99ff99ff99ff99ff99 --crs gerrit --cis buildbucket --changelist changelist-id --patchset 1 --jobid tryjob-id --key arch:x86_64 --key model:GCE --key os:Debian10",
213		[]string{
214			"imgtest",
215			"init",
216			"--work-dir", goldctlWorkDir,
217			"--instance", "skia",
218			"--url", "https://gold.skia.org",
219			"--bucket", "skia-infra-gm",
220			"--git_hash", "ff99ff99ff99ff99ff99ff99ff99ff99ff99ff99",
221			"--crs", "gerrit",
222			"--cis", "buildbucket",
223			"--changelist", "changelist-id",
224			"--patchset", "1",
225			"--jobid", "tryjob-id",
226			"--key", "arch:x86_64",
227			"--key", "model:GCE",
228			"--key", "os:Debian10",
229		},
230	)
231}
232