|
| 1 | +// Licensed to Elasticsearch B.V. under one or more contributor |
| 2 | +// license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright |
| 4 | +// ownership. Elasticsearch B.V. licenses this file to you under |
| 5 | +// the Apache License, Version 2.0 (the "License"); you may |
| 6 | +// not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +// Package testbin builds Go test binaries via "go test -c". It is the single |
| 19 | +// source of truth for how beat test binaries are compiled, used by both the |
| 20 | +// mage build system and the Go integration test framework. |
| 21 | +// |
| 22 | +// Environment variables respected: |
| 23 | +// - DEV=true: disables optimizations for debugging (-gcflags=all=-N -l) |
| 24 | +// - TEST_COVERAGE=true: enables coverage instrumentation (-coverpkg ./...) |
| 25 | +// |
| 26 | +// Platform-specific flags (e.g. stripping DWARF on Windows 386) should be |
| 27 | +// passed by the caller via BuildOptions.ExtraFlags. |
| 28 | +package testbin |
| 29 | + |
| 30 | +import ( |
| 31 | + "fmt" |
| 32 | + "log" |
| 33 | + "os" |
| 34 | + "path/filepath" |
| 35 | + "strconv" |
| 36 | + "strings" |
| 37 | + "time" |
| 38 | + |
| 39 | + "golang.org/x/sys/execabs" |
| 40 | +) |
| 41 | + |
| 42 | +// buildOptions holds the resolved build configuration. |
| 43 | +type buildOptions struct { |
| 44 | + extraFlags []string |
| 45 | + inputFiles []string |
| 46 | +} |
| 47 | + |
| 48 | +// Option configures a Build invocation. Options are applied in order, |
| 49 | +// so later options override earlier ones. |
| 50 | +type Option func(*buildOptions) |
| 51 | + |
| 52 | +// WithExtraFlags appends additional flags passed to 'go test'. |
| 53 | +func WithExtraFlags(flags ...string) Option { |
| 54 | + return func(o *buildOptions) { |
| 55 | + o.extraFlags = append(o.extraFlags, flags...) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// WithInputFiles appends specific files/packages after all flags. |
| 60 | +func WithInputFiles(files ...string) Option { |
| 61 | + return func(o *buildOptions) { |
| 62 | + o.inputFiles = append(o.inputFiles, files...) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +// Build compiles a test binary for the given beat using "go test -c". |
| 67 | +// dir is the beat root directory where "go test -c" runs and where the |
| 68 | +// resulting binary is written. It returns the absolute path of the built |
| 69 | +// binary. |
| 70 | +func Build(beatName, dir string, opts ...Option) (string, error) { |
| 71 | + if !strings.HasSuffix(beatName, ".test") { |
| 72 | + beatName += ".test" |
| 73 | + } |
| 74 | + outputPath, err := filepath.Abs(filepath.Join(dir, beatName)) |
| 75 | + if err != nil { |
| 76 | + return "", fmt.Errorf("failed to resolve output path: %w", err) |
| 77 | + } |
| 78 | + |
| 79 | + args := []string{"test", "-c", "-o", outputPath} |
| 80 | + |
| 81 | + if devBuild, _ := strconv.ParseBool(os.Getenv("DEV")); devBuild { |
| 82 | + args = append(args, `-gcflags=all=-N -l`) |
| 83 | + } |
| 84 | + |
| 85 | + if testCoverage, _ := strconv.ParseBool(os.Getenv("TEST_COVERAGE")); testCoverage { |
| 86 | + args = append(args, "-coverpkg", "./...") |
| 87 | + } |
| 88 | + |
| 89 | + var o buildOptions |
| 90 | + for _, fn := range opts { |
| 91 | + fn(&o) |
| 92 | + } |
| 93 | + args = append(args, o.extraFlags...) |
| 94 | + args = append(args, o.inputFiles...) |
| 95 | + |
| 96 | + cmd := execabs.Command("go", args...) |
| 97 | + cmd.Dir = dir |
| 98 | + |
| 99 | + start := time.Now() |
| 100 | + defer func() { |
| 101 | + log.Printf("testbin.Build (go %s) took %v.", |
| 102 | + strings.Join(args, " "), time.Since(start)) |
| 103 | + }() |
| 104 | + |
| 105 | + output, err := cmd.CombinedOutput() |
| 106 | + if err != nil { |
| 107 | + log.Printf("testbin.Build failed:\n%s", output) |
| 108 | + return "", fmt.Errorf("failed to build test binary %q: %w (see log output for details)", beatName, err) |
| 109 | + } |
| 110 | + |
| 111 | + return outputPath, nil |
| 112 | +} |
0 commit comments