Skip to content

Commit

Permalink
bazel: generate contents of pkg/BUILD.bazel in make bazel-generate
Browse files Browse the repository at this point in the history
Previously, the `ALL_TESTS` list was hardcoded, leaving a possibility
that it would fall out of sync with the rest of tree. Now we use `bazel
query` to enumerate all the tests in the repo generate the contents of
`pkg/BUILD.bazel` accordingly. Since this is part of `make
bazel-generate`, CI should ensure that this never falls out of sync
again.

A couple tweaks to the build scripts were necessary to make this work:

- To avoid duplication, I moved the `make bazel-generate` logic to
  its own script at `build/bazelutil/bazel-generate.sh`.
- Refactored the Bazel image tag to one script in
  `build/teamcity-bazel-support.sh` so now there's only one place to
  update when a new builder image is created.
- In `build/teamcity-check.sh`, we were using `builder.sh` to run
  `make bazel-generate` in CI, but `builder.sh` [is known to work poorly
  w/ Bazel](#59224).
  Instead I directly use an appropriate `docker run`. This fixes some
  build errors that would occur otherwise.

Release note: None
  • Loading branch information
rickystewart committed Feb 16, 2021
1 parent 14ac1cf commit 67200f6
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitattributes
@@ -1,3 +1,4 @@
*.pb.* -diff
*.eg.go -diff
DEPS.bzl -diff
pkg/BUILD.bazel -diff
3 changes: 1 addition & 2 deletions Makefile
Expand Up @@ -1772,8 +1772,7 @@ fuzz: bin/fuzz
# Short hand to re-generate all bazel BUILD files.
bazel-generate: ## Generate all bazel BUILD files.
@echo 'Generating DEPS.bzl and BUILD files using gazelle'
@bazel run //:gazelle -- update-repos -from_file=go.mod -build_file_proto_mode=disable_global -to_macro=DEPS.bzl%go_deps
@bazel run //:gazelle
./build/bazelutil/bazel-generate.sh

# No need to include all the dependency files if the user is just
# requesting help or cleanup.
Expand Down
2 changes: 1 addition & 1 deletion build/README.md
Expand Up @@ -97,7 +97,7 @@ The `bazelbuilder` image is used exclusively for performing builds using Bazel.
docker image tag $IMAGE_HASH cockroachdb/bazel:$TAG
docker image push cockroachdb/bazel:$TAG
```
- Then, update `build/teamcity-bazel.sh` with the new tag and commit all your changes.
- Then, update `build/teamcity-bazel-support.sh` with the new tag and commit all your changes.
- Ensure the "Github CI (Optional)" job passes on your PR before merging.

# Dependencies
Expand Down
7 changes: 7 additions & 0 deletions build/bazelutil/bazel-generate.sh
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

set -exuo pipefail

bazel run //:gazelle -- update-repos -from_file=go.mod -build_file_proto_mode=disable_global -to_macro=DEPS.bzl%go_deps
bazel run //:gazelle
bazel run //pkg/cmd/generate-test-suites --run_under="cd $PWD && " > pkg/BUILD.bazel
1 change: 1 addition & 0 deletions build/teamcity-bazel-support.sh
@@ -0,0 +1 @@
BAZEL_IMAGE=cockroachdb/bazel:20210201-174432
3 changes: 2 additions & 1 deletion build/teamcity-bazel.sh
Expand Up @@ -3,6 +3,7 @@
set -euo pipefail

source "$(dirname "${0}")/teamcity-support.sh"
source "$(dirname "${0}")/teamcity-bazel-support.sh" # For BAZEL_IMAGE

tc_prepare

Expand All @@ -18,5 +19,5 @@ docker run -i ${tty-} --rm --init \
--workdir="/go/src/github.com/cockroachdb/cockroach" \
-v "$root:/go/src/github.com/cockroachdb/cockroach:ro" \
-v "$TMPDIR:/artifacts" \
cockroachdb/bazel:20210201-174432 bazelbuild.sh
$BAZEL_IMAGE bazelbuild.sh
tc_end_block "Run Bazel build"
7 changes: 6 additions & 1 deletion build/teamcity-check.sh
Expand Up @@ -7,6 +7,7 @@ require_justification=0
set -euo pipefail

source "$(dirname "${0}")/teamcity-support.sh"
source "$(dirname "${0}")/teamcity-bazel-support.sh" # For BAZEL_IMAGE

function check_clean() {
# The workspace is clean iff `git status --porcelain` produces no output. Any
Expand Down Expand Up @@ -43,7 +44,11 @@ check_clean "Run \`make generate\` to automatically regenerate these."
run build/builder.sh make buildshort &> artifacts/buildshort.log || (cat artifacts/buildshort.log && false)
rm artifacts/buildshort.log
check_clean "Run \`make buildshort\` to automatically regenerate these."
run build/builder.sh make bazel-generate &> artifacts/buildshort.log || (cat artifacts/buildshort.log && false)
# NB: $root is set by teamcity-support.sh.
run docker run -i ${tty-} --rm --init \
--workdir="/go/src/github.com/cockroachdb/cockroach" \
-v "$root:/go/src/github.com/cockroachdb/cockroach" \
$BAZEL_IMAGE build/bazelutil/bazel-generate.sh &> artifacts/buildshort.log || (cat artifacts/buildshort.log && false)
rm artifacts/buildshort.log
check_clean "Run \`make bazel-generate\` to automatically regenerate these."
tc_end_block "Ensure generated code is up-to-date"
Expand Down
11 changes: 7 additions & 4 deletions pkg/BUILD.bazel
@@ -1,6 +1,6 @@
# Code generated by generate-test-suites, DO NOT EDIT.
# gazelle:proto_strip_import_prefix /pkg

# TODO(ricky): Add a lint to make sure this list stays up-to-date.
ALL_TESTS = [
"//pkg/acceptance:acceptance_test",
"//pkg/base:base_test",
Expand All @@ -27,6 +27,7 @@ ALL_TESTS = [
"//pkg/ccl/storageccl:storageccl_test",
"//pkg/ccl/streamingccl/streamclient:streamclient_test",
"//pkg/ccl/streamingccl/streamingest:streamingest_test",
"//pkg/ccl/streamingccl/streamingutils:streamingutils_test",
"//pkg/ccl/utilccl/sampledataccl:sampledataccl_test",
"//pkg/ccl/utilccl:utilccl_test",
"//pkg/ccl/workloadccl/allccl:allccl_test",
Expand Down Expand Up @@ -153,6 +154,7 @@ ALL_TESTS = [
"//pkg/sql/colexec/colbuilder:colbuilder_test",
"//pkg/sql/colexec/execgen:execgen_test",
"//pkg/sql/colexec:colexec_test",
"//pkg/sql/colexecbase/colexecerror:colexecerror_test",
"//pkg/sql/colexecbase:colexecbase_test",
"//pkg/sql/colflow/colrpc:colrpc_test",
"//pkg/sql/colflow:colflow_test",
Expand Down Expand Up @@ -238,9 +240,10 @@ ALL_TESTS = [
"//pkg/storage/metamorphic:metamorphic_test",
"//pkg/storage:storage_test",
"//pkg/testutils/keysutils:keysutils_test",
"//pkg/testutils/lint/passes/descriptormarshal:descriptormarshal_test",
"//pkg/testutils/lint/passes/fmtsafe/testdata/src/a:a_test",
"//pkg/testutils/lint/passes/fmtsafe:fmtsafe_test",
"//pkg/testutils/lint/passes/forbiddenmethod:descriptormarshal_test",
"//pkg/testutils/lint/passes/forbiddenmethod:forbiddenmethod_test",
"//pkg/testutils/lint/passes/hash:hash_test",
"//pkg/testutils/lint/passes/nocopy:nocopy_test",
"//pkg/testutils/lint/passes/passesutil:passesutil_test",
Expand Down Expand Up @@ -327,8 +330,8 @@ ALL_TESTS = [
"//pkg/workload:workload_test",
]

# These suites run only the tests with the appropriate `size` (excepting those
# tagged `broken_in_bazel`) [1]. Note that tests have a default timeout
# These suites run only the tests with the appropriate "size" (excepting those
# tagged "broken_in_bazel") [1]. Note that tests have a default timeout
# depending on the size [2].

# [1] https://docs.bazel.build/versions/master/be/general.html#test_suite
Expand Down
14 changes: 14 additions & 0 deletions pkg/cmd/generate-test-suites/BUILD.bazel
@@ -0,0 +1,14 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")

go_library(
name = "generate-test-suites_lib",
srcs = ["main.go"],
importpath = "github.com/cockroachdb/cockroach/pkg/cmd/generate-test-suites",
visibility = ["//visibility:private"],
)

go_binary(
name = "generate-test-suites",
embed = [":generate-test-suites_lib"],
visibility = ["//visibility:public"],
)
63 changes: 63 additions & 0 deletions pkg/cmd/generate-test-suites/main.go
@@ -0,0 +1,63 @@
// Copyright 2021 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package main

import (
"fmt"
"log"
"os"
"os/exec"
"sort"
"strings"
)

func main() {
// First list all tests.
buf, err := exec.Command("bazel", "query", "kind(go_test, //pkg/...)", "--output=label").Output()
if err != nil {
log.Printf("Could not query Bazel tests: got error %v", err)
os.Exit(1)
}
labels := strings.Split(string(buf[:]), "\n")
sort.Slice(labels, func(i, j int) bool { return labels[i] < labels[j] })

// Write the output to stdout
fmt.Println(`# Code generated by generate-test-suites, DO NOT EDIT.
# gazelle:proto_strip_import_prefix /pkg
ALL_TESTS = [`)
for _, label := range labels {
if len(label) > 0 {
fmt.Printf(" %q,\n", label)
}
}
fmt.Println(`]
# These suites run only the tests with the appropriate "size" (excepting those
# tagged "broken_in_bazel") [1]. Note that tests have a default timeout
# depending on the size [2].
# [1] https://docs.bazel.build/versions/master/be/general.html#test_suite
# [2] https://docs.bazel.build/versions/master/be/common-definitions.html#common-attributes-tests`)

for _, size := range []string{"small", "medium", "large", "enormous"} {
fmt.Printf(`
test_suite(
name = "%[1]s_tests",
tags = [
"-broken_in_bazel",
"%[1]s",
],
tests = ALL_TESTS,
)
`, size)
}
}

0 comments on commit 67200f6

Please sign in to comment.