Debian Codegraph @

infra/bots/task_drivers/common/bazel_utils_test.go

raw
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
6 package common
7
8 import (
9 "context"
10 "fmt"
11 "os"
12 "path/filepath"
13 "testing"
14
15 "github.com/stretchr/testify/assert"
16 "github.com/stretchr/testify/require"
17 "go.skia.org/infra/task_driver/go/lib/os_steps"
18 "go.skia.org/infra/task_driver/go/td"
19 "go.skia.org/skia/infra/bots/task_drivers/testutils"
20 )
21
22 func TestValidateLabelAndReturnOutputZipPath_ValidLabel_Success(t *testing.T) {
23 test := func(label, expected string) {
24 t.Run(label, func(t *testing.T) {
25 actual, err := ValidateLabelAndReturnOutputsZipPath("/path/to/skia", label)
26 require.NoError(t, err)
27 assert.Equal(t, expected, actual)
28 })
29 }
30
31 test("//:foo", "/path/to/skia/bazel-testlogs/foo/test.outputs/outputs.zip")
32 test("//foo:bar", "/path/to/skia/bazel-testlogs/foo/bar/test.outputs/outputs.zip")
33 test("//foo/bar:baz", "/path/to/skia/bazel-testlogs/foo/bar/baz/test.outputs/outputs.zip")
34 test("//foo/bar/baz:qux", "/path/to/skia/bazel-testlogs/foo/bar/baz/qux/test.outputs/outputs.zip")
35 }
36
37 func TestValidateLabelAndReturnOutputZipPath_InvalidLabel_Error(t *testing.T) {
38 test := func(label string) {
39 t.Run(label, func(t *testing.T) {
40 _, err := ValidateLabelAndReturnOutputsZipPath("/path/to/skia", label)
41 require.Error(t, err)
42 assert.Contains(t, err.Error(), fmt.Sprintf("invalid label: %q", label))
43 })
44 }
45
46 test("foo")
47 test("/foo")
48 test("//foo")
49 test(":foo")
50 test("/:foo")
51
52 test("foo/bar")
53 test("foo:bar")
54 test("/foo/bar")
55 test("/foo:bar")
56 test(":foo/bar")
57 test(":foo:bar")
58 test("//foo/bar")
59
60 test("foo/bar/baz")
61 test("foo/bar:baz")
62 test("foo:bar/baz")
63 test("foo:bar:baz")
64 test("/foo/bar/baz")
65 test("/foo/bar:baz")
66 test("/foo:bar/baz")
67 test("/foo:bar:baz")
68 test("//foo/bar/baz")
69 test("//foo:bar/baz")
70 test("//foo:bar:baz")
71 }
72
73 func TestExtractOutputsZip_Success(t *testing.T) {
74 zipContents := map[string]string{
75 // File contents are not important for this test; only paths and file extensions are taken into
76 // account.
77 "alfa.png": "fake PNG file",
78 "alfa.json": "fake JSON file",
79 "bravo.PNG": "fake PNG",
80 "bravo.JSON": "fake JSON file",
81 "charlie.txt": "fake TXT file", // Neither PNG nor JSON; should be ignored.
82 // Subdirectories should be ignored.
83 "delta/echo.png": "fake PNG file",
84 "delta/echo.json": "fake JSON file",
85 "delta/echo.txt": "fake JSON file",
86 }
87 zipPath := filepath.Join(t.TempDir(), "outputs.zip")
88 testutils.MakeZIP(t, zipPath, zipContents)
89
90 extractionDir := t.TempDir()
91 res := td.RunTestSteps(t, false, func(ctx context.Context) error {
92 ctx = context.WithValue(ctx, os_steps.TempDirContextKey, testutils.MakeTempDirMockFn(t, extractionDir))
93 var err error
94 extractionDir, err = ExtractOutputsZip(ctx, zipPath)
95 require.NoError(t, err)
96 return nil
97 })
98 require.Empty(t, res.Errors)
99 require.Empty(t, res.Exceptions)
100 testutils.AssertStepNames(t, res,
101 "Creating TempDir",
102 fmt.Sprintf("Extract undeclared outputs archive %s into %s", zipPath, extractionDir),
103 "Extracting file: alfa.json",
104 "Extracting file: alfa.png",
105 "Extracting file: bravo.JSON",
106 "Extracting file: bravo.PNG",
107 "Not extracting non-PNG / non-JSON file: charlie.txt",
108 "Not extracting file within subdirectory: delta/echo.json",
109 "Not extracting file within subdirectory: delta/echo.png",
110 "Not extracting file within subdirectory: delta/echo.txt")
111
112 extractedFiles := map[string]bool{}
113 files, err := os.ReadDir(extractionDir)
114 require.NoError(t, err)
115 for _, file := range files {
116 assert.False(t, file.IsDir())
117 extractedFiles[file.Name()] = true
118 }
119
120 assert.Equal(t, map[string]bool{
121 "alfa.png": true,
122 "alfa.json": true,
123 "bravo.PNG": true,
124 "bravo.JSON": true,
125 }, extractedFiles)
126 }