| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/sha256" |
| 5 |
"encoding/hex" |
| 6 |
"fmt" |
| 7 |
"os" |
| 8 |
"path/filepath" |
| 9 |
"sort" |
| 10 |
"strconv" |
| 11 |
"strings" |
| 12 |
|
| 13 |
"github.com/antithesishq/antithesis-sdk-go/internal" |
| 14 |
"github.com/antithesishq/antithesis-sdk-go/tools/antithesis-go-instrumentor/common" |
| 15 |
"github.com/antithesishq/antithesis-sdk-go/tools/antithesis-go-instrumentor/config" |
| 16 |
) |
| 17 |
|
| 18 |
// sdkInstrumentationPackage is the package supplying RegisterModule. Instrumented |
| 19 |
// packages get an import of it and a package-level variable holding the handle. |
| 20 |
const sdkInstrumentationPackage = "github.com/antithesishq/antithesis-sdk-go/instrumentation" |
| 21 |
|
| 22 |
// symbolsDirDefault is where the container image build looks for symbol tables; |
| 23 |
// see environment/container_module/symbols.nix. |
| 24 |
const symbolsDirDefault = "/symbols" |
| 25 |
|
| 26 |
// Config is everything the wrapper needs, gathered from the environment. The go |
| 27 |
// command owns our argument list, so there are no flags of our own. |
| 28 |
type Config struct { |
| 29 |
// Instrument lists import path prefixes to instrument. Empty means "the main |
| 30 |
// module of this build", resolved lazily from the build itself. |
| 31 |
Instrument []string |
| 32 |
|
| 33 |
// SymbolsDir is where symbol tables are collected for the image. |
| 34 |
SymbolsDir string |
| 35 |
|
| 36 |
// SDKModuleDir optionally points at an antithesis-sdk-go checkout, used to |
| 37 |
// supply the SDK to modules that do not depend on it. |
| 38 |
SDKModuleDir string |
| 39 |
|
| 40 |
// SymbolPrefix is prepended to symbol table file names. |
| 41 |
SymbolPrefix string |
| 42 |
|
| 43 |
// Exclusions are paths the instrumentor must leave alone. |
| 44 |
Exclusions map[string]bool |
| 45 |
|
| 46 |
// Coverage and Catalog select the features to apply. They are independent: |
| 47 |
// turning coverage off while leaving cataloguing on is the equivalent of |
| 48 |
// antithesis-go-instrumentor's -assert_only. |
| 49 |
Coverage bool |
| 50 |
Catalog bool |
| 51 |
|
| 52 |
SkipTestFiles bool |
| 53 |
Verbosity common.Verbosity |
| 54 |
} |
| 55 |
|
| 56 |
func loadConfig() (*Config, error) { |
| 57 |
cfg := &Config{ |
| 58 |
Instrument: splitList(os.Getenv("ANTITHESIS_INSTRUMENT")), |
| 59 |
SymbolsDir: envOr("ANTITHESIS_SYMBOLS_DIR", symbolsDirDefault), |
| 60 |
SDKModuleDir: strings.TrimSpace(os.Getenv("ANTITHESIS_SDK_MODULE_DIR")), |
| 61 |
SymbolPrefix: strings.TrimSpace(os.Getenv("ANTITHESIS_SYMBOL_PREFIX")), |
| 62 |
Coverage: !isTrue(os.Getenv("ANTITHESIS_SKIP_COVERAGE")), |
| 63 |
Catalog: !isTrue(os.Getenv("ANTITHESIS_SKIP_CATALOG")), |
| 64 |
SkipTestFiles: isTrue(os.Getenv("ANTITHESIS_SKIP_TEST_FILES")), |
| 65 |
} |
| 66 |
|
| 67 |
if v := strings.TrimSpace(os.Getenv("ANTITHESIS_VERBOSE")); v != "" { |
| 68 |
level, err := strconv.Atoi(v) |
| 69 |
if err != nil { |
| 70 |
return nil, fmt.Errorf("ANTITHESIS_VERBOSE=%q is not a number", v) |
| 71 |
} |
| 72 |
cfg.Verbosity = common.Verbosity(level) |
| 73 |
} |
| 74 |
|
| 75 |
if excludeFile := strings.TrimSpace(os.Getenv("ANTITHESIS_EXCLUDE")); excludeFile != "" { |
| 76 |
// Exclusions are recorded relative to the directory holding the file, the |
| 77 |
// same convention antithesis-go-instrumentor -exclude uses. |
| 78 |
base := filepath.Dir(excludeFile) |
| 79 |
exclusions, err := config.ParseExclusionsFile(excludeFile, base) |
| 80 |
if err != nil { |
| 81 |
return nil, fmt.Errorf("reading ANTITHESIS_EXCLUDE=%q: %w", excludeFile, err) |
| 82 |
} |
| 83 |
cfg.Exclusions = exclusions |
| 84 |
} |
| 85 |
|
| 86 |
if cfg.SDKModuleDir != "" { |
| 87 |
abs, err := filepath.Abs(cfg.SDKModuleDir) |
| 88 |
if err != nil { |
| 89 |
return nil, fmt.Errorf("resolving ANTITHESIS_SDK_MODULE_DIR: %w", err) |
| 90 |
} |
| 91 |
cfg.SDKModuleDir = abs |
| 92 |
} |
| 93 |
|
| 94 |
return cfg, nil |
| 95 |
} |
| 96 |
|
| 97 |
// ToolIDSuffix is appended to each tool's -V=full output, becoming part of the |
| 98 |
// go command's action cache key. It must change whenever anything that affects |
| 99 |
// the bytes we emit changes, or the build will serve stale objects. |
| 100 |
// |
| 101 |
// The format is deliberately close to the toolchain's own " X:experiment" suffix. |
| 102 |
// cmd/go requires the line to keep the shape "<tool> version <version> ..." and |
| 103 |
// only ever appends to it, so extending it here is safe. |
| 104 |
func (c *Config) ToolIDSuffix() (string, error) { |
| 105 |
identity, err := instrumentorIdentity() |
| 106 |
if err != nil { |
| 107 |
return "", err |
| 108 |
} |
| 109 |
inputs := []string{ |
| 110 |
"sdk=" + internal.SDK_Version, |
| 111 |
"instrumentor=" + identity, |
| 112 |
"prefix=" + c.SymbolPrefix, |
| 113 |
"instrument=" + strings.Join(c.Instrument, ","), |
| 114 |
"skiptests=" + strconv.FormatBool(c.SkipTestFiles), |
| 115 |
"coverage=" + strconv.FormatBool(c.Coverage), |
| 116 |
"catalog=" + strconv.FormatBool(c.Catalog), |
| 117 |
} |
| 118 |
// Exclusions change which files get edges, so they belong in the key. |
| 119 |
excluded := make([]string, 0, len(c.Exclusions)) |
| 120 |
for path := range c.Exclusions { |
| 121 |
excluded = append(excluded, path) |
| 122 |
} |
| 123 |
sort.Strings(excluded) |
| 124 |
inputs = append(inputs, "exclude="+strings.Join(excluded, ",")) |
| 125 |
|
| 126 |
sum := sha256.Sum256([]byte(strings.Join(inputs, "\x00"))) |
| 127 |
return "antithesis:" + internal.SDK_Version + "+" + hex.EncodeToString(sum[:])[:16], nil |
| 128 |
} |
| 129 |
|
| 130 |
func envOr(name, fallback string) string { |
| 131 |
if v := strings.TrimSpace(os.Getenv(name)); v != "" { |
| 132 |
return v |
| 133 |
} |
| 134 |
return fallback |
| 135 |
} |
| 136 |
|
| 137 |
func splitList(s string) []string { |
| 138 |
var out []string |
| 139 |
for _, part := range strings.Split(s, ",") { |
| 140 |
if part = strings.TrimSpace(part); part != "" { |
| 141 |
out = append(out, part) |
| 142 |
} |
| 143 |
} |
| 144 |
return out |
| 145 |
} |
| 146 |
|
| 147 |
func isTrue(s string) bool { |
| 148 |
switch strings.ToLower(strings.TrimSpace(s)) { |
| 149 |
case "1", "t", "true", "y", "yes": |
| 150 |
return true |
| 151 |
} |
| 152 |
return false |
| 153 |
} |