336 lines
11 KiB
Go
336 lines
11 KiB
Go
package scripts
|
|
|
|
import (
|
|
"encoding/json"
|
|
"go/version"
|
|
"maps"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"reflect"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
|
|
"golang.org/x/mod/modfile"
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
const goStub = `#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
printf '%s\0' "$PWD" "GOWORK=$GOWORK" "GOTOOLCHAIN=$GOTOOLCHAIN" "$@" > "$RELEASEKIT_GO_LOG"
|
|
if [[ ${RELEASEKIT_BOOTSTRAP_STATUS:-0} != 0 ]]; then
|
|
exit "$RELEASEKIT_BOOTSTRAP_STATUS"
|
|
fi
|
|
[[ $1 == tool && $2 == releasekit ]]
|
|
shift 2
|
|
printf '%s\0' "$PWD" "$@" > "$RELEASEKIT_ARGS_LOG"
|
|
exit "${RELEASEKIT_TOOL_STATUS:-0}"
|
|
`
|
|
|
|
func TestReleaseWrapperForwardsNativeArgumentsAndStatus(t *testing.T) {
|
|
for _, toolStatus := range []int{0, 3} {
|
|
t.Run("status "+strconv.Itoa(toolStatus), func(t *testing.T) {
|
|
layout := newReleaseTestLayout(t)
|
|
args := []string{"publish", "v4.2.0", "argument with spaces"}
|
|
cmd := exec.Command("bash", append([]string{layout.wrapper}, args...)...)
|
|
cmd.Dir = layout.caller
|
|
cmd.Env = layout.environment(map[string]string{
|
|
"RELEASEKIT_ARGS_LOG": layout.argsLog,
|
|
"RELEASEKIT_GO_LOG": layout.goLog,
|
|
"RELEASEKIT_TOOL_STATUS": strconv.Itoa(toolStatus),
|
|
})
|
|
output, err := cmd.CombinedOutput()
|
|
if got := commandStatus(err); got != toolStatus {
|
|
t.Fatalf("exit=%d, want %d; output=%q, error=%v", got, toolStatus, output, err)
|
|
}
|
|
if len(output) != 0 {
|
|
t.Fatalf("unexpected output: %q", output)
|
|
}
|
|
wantGo := append([]string{layout.repo, "GOWORK=off", "GOTOOLCHAIN=local", "tool", "releasekit"}, args...)
|
|
if got := readNULArguments(t, layout.goLog); !reflect.DeepEqual(got, wantGo) {
|
|
t.Fatalf("Go invocation=%q, want %q", got, wantGo)
|
|
}
|
|
wantTool := append([]string{layout.repo}, args...)
|
|
if got := readNULArguments(t, layout.argsLog); !reflect.DeepEqual(got, wantTool) {
|
|
t.Fatalf("tool invocation=%q, want %q", got, wantTool)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDeliveryWorkflowUsesModuleSelectedReleaseKit(t *testing.T) {
|
|
data, err := os.ReadFile("../.woodpecker/release.yml")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var workflow struct {
|
|
Matrix struct {
|
|
Include []map[string]string `yaml:"include"`
|
|
} `yaml:"matrix"`
|
|
When []struct {
|
|
Event string `yaml:"event"`
|
|
Ref string `yaml:"ref"`
|
|
} `yaml:"when"`
|
|
Steps []struct {
|
|
Name string `yaml:"name"`
|
|
Image string `yaml:"image"`
|
|
Environment map[string]struct {
|
|
FromSecret string `yaml:"from_secret"`
|
|
} `yaml:"environment"`
|
|
Commands []string `yaml:"commands"`
|
|
} `yaml:"steps"`
|
|
}
|
|
if err := yaml.Unmarshal(data, &workflow); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
goMod, err := modfile.Parse("go.mod", []byte(readText(t, "../go.mod")), nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if goMod.Go == nil {
|
|
t.Fatal("module does not declare its Go minimum")
|
|
}
|
|
if len(workflow.Matrix.Include) != 1 {
|
|
t.Fatalf("workflow Go matrix = %v", workflow.Matrix.Include)
|
|
}
|
|
image := workflow.Matrix.Include[0]["GO_IMAGE"]
|
|
_, tag, _ := strings.Cut(image, ":")
|
|
patch, _, _ := strings.Cut(tag, "-")
|
|
imageVersion := "go" + patch
|
|
if !version.IsValid(imageVersion) || version.Compare(imageVersion, "go"+goMod.Go.Version) < 0 {
|
|
t.Fatalf("workflow Go image %q cannot satisfy module minimum %s", image, goMod.Go.Version)
|
|
}
|
|
if len(workflow.When) != 1 || workflow.When[0].Event != "tag" || workflow.When[0].Ref != "refs/tags/v*" {
|
|
t.Fatalf("workflow trigger = %+v", workflow.When)
|
|
}
|
|
if len(workflow.Steps) != 1 || workflow.Steps[0].Name != "deliver" || workflow.Steps[0].Image != "${GO_IMAGE}" {
|
|
t.Fatalf("workflow delivery step = %+v", workflow.Steps)
|
|
}
|
|
if secret := workflow.Steps[0].Environment["RELEASEKIT_GITEA_TOKEN"].FromSecret; secret != "GITEA_RELEASE_TOKEN" {
|
|
t.Fatalf("delivery secret = %q, want existing GITEA_RELEASE_TOKEN", secret)
|
|
}
|
|
if len(workflow.Steps[0].Commands) != 1 {
|
|
t.Fatalf("workflow commands = %q, want one bootstrap/delivery command", workflow.Steps[0].Commands)
|
|
}
|
|
command := workflow.Steps[0].Commands[0]
|
|
for _, fragment := range []string{
|
|
"git fetch origin", "refs/tags/$${CI_COMMIT_TAG}",
|
|
"git checkout --detach", "git status --porcelain",
|
|
"go list", "-modfile=", "-m", "go build", "$${releasekit_module}/cmd/releasekit",
|
|
"ci-deliver", "--repo \"$${CI_WORKSPACE}\"", "--prepared", "--commit \"$${CI_COMMIT_SHA}\"",
|
|
"\"$${CI_COMMIT_TAG}\"",
|
|
} {
|
|
if !strings.Contains(command, fragment) {
|
|
t.Fatalf("workflow command is missing %q: %s", fragment, command)
|
|
}
|
|
}
|
|
if strings.Index(command, "git checkout --detach") > strings.Index(command, "go list") || strings.Index(command, "git status --porcelain") > strings.Index(command, "go list") {
|
|
t.Fatalf("workflow resolves the tool before verifying the tagged source: %s", command)
|
|
}
|
|
if strings.Contains(command, "RELEASEKIT_VERSION") || strings.Contains(command, "cmd/releasekit@") {
|
|
t.Fatalf("workflow retains a separate tool version: %s", command)
|
|
}
|
|
toolSelected := false
|
|
for _, tool := range goMod.Tool {
|
|
if tool.Path == "gitea.maximumdirect.net/eric/ReleaseKit/cmd/releasekit" {
|
|
toolSelected = true
|
|
}
|
|
}
|
|
if !toolSelected {
|
|
t.Fatal("module does not register the ReleaseKit CLI tool")
|
|
}
|
|
}
|
|
|
|
func TestReleaseWrapperStopsWhenBootstrapFails(t *testing.T) {
|
|
layout := newReleaseTestLayout(t)
|
|
cmd := exec.Command("bash", layout.wrapper, "publish", "v4.2.0")
|
|
cmd.Dir = layout.caller
|
|
cmd.Env = layout.environment(map[string]string{
|
|
"RELEASEKIT_ARGS_LOG": layout.argsLog,
|
|
"RELEASEKIT_GO_LOG": layout.goLog,
|
|
"RELEASEKIT_BOOTSTRAP_STATUS": "42",
|
|
})
|
|
output, err := cmd.CombinedOutput()
|
|
if got := commandStatus(err); got != 42 {
|
|
t.Fatalf("exit=%d, want 42; output=%q, error=%v", got, output, err)
|
|
}
|
|
if _, err := os.Stat(layout.argsLog); !os.IsNotExist(err) {
|
|
t.Fatalf("ReleaseKit executed after failed bootstrap; stat error=%v", err)
|
|
}
|
|
}
|
|
|
|
func TestReleaseKitProfileDeclaresProductAndDeliverySettings(t *testing.T) {
|
|
data, err := os.ReadFile("../releasekit.json")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var raw map[string]json.RawMessage
|
|
if err := json.Unmarshal(data, &raw); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var deliveryFields map[string]json.RawMessage
|
|
if err := json.Unmarshal(raw["delivery"], &deliveryFields); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, exists := deliveryFields["token"]; exists {
|
|
t.Fatal("profile must not contain a credential value")
|
|
}
|
|
var profile struct {
|
|
Product string `json:"product"`
|
|
Remote string `json:"remote"`
|
|
Checks []json.RawMessage `json:"checks"`
|
|
Build struct {
|
|
MainPackage string `json:"main_package"`
|
|
BinaryName string `json:"binary_name"`
|
|
Targets []string `json:"targets"`
|
|
} `json:"build"`
|
|
Delivery struct {
|
|
BaseURL string `json:"base_url"`
|
|
Owner string `json:"owner"`
|
|
Repository string `json:"repository"`
|
|
TokenEnv string `json:"token_env"`
|
|
} `json:"delivery"`
|
|
}
|
|
if err := json.Unmarshal(data, &profile); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(profile.Checks) != 0 {
|
|
t.Fatal("profile must not define additional checks")
|
|
}
|
|
if profile.Product != "Audita" || profile.Remote != "origin" {
|
|
t.Fatalf("product=%q remote=%q", profile.Product, profile.Remote)
|
|
}
|
|
if profile.Build.MainPackage != "./cmd/audita" || profile.Build.BinaryName != "audita" {
|
|
t.Fatalf("build=%+v", profile.Build)
|
|
}
|
|
if !sameMembers(profile.Build.Targets, []string{"linux/amd64", "linux/arm64"}) {
|
|
t.Fatalf("build targets=%q", profile.Build.Targets)
|
|
}
|
|
if profile.Delivery.BaseURL != "https://gitea.maximumdirect.net" || profile.Delivery.Owner != "eric" || profile.Delivery.Repository != "audita" || profile.Delivery.TokenEnv != "RELEASEKIT_GITEA_TOKEN" {
|
|
t.Fatalf("delivery=%+v", profile.Delivery)
|
|
}
|
|
}
|
|
|
|
func TestReleaseKitTargetMembership(t *testing.T) {
|
|
want := []string{"linux/amd64", "linux/arm64"}
|
|
for _, test := range []struct {
|
|
name string
|
|
targets []string
|
|
match bool
|
|
}{
|
|
{"missing amd64", []string{"linux/arm64", "linux/arm64"}, false},
|
|
{"missing arm64", []string{"linux/amd64", "linux/amd64"}, false},
|
|
{"additional target", []string{"linux/amd64", "linux/arm64", "darwin/arm64"}, false},
|
|
{"reordered", []string{"linux/arm64", "linux/amd64"}, true},
|
|
{"repeated", []string{"linux/amd64", "linux/arm64", "linux/amd64"}, true},
|
|
} {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
if got := sameMembers(test.targets, want); got != test.match {
|
|
t.Fatalf("target membership for %q = %t, want %t", test.targets, got, test.match)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
type releaseTestLayout struct {
|
|
repo string
|
|
caller string
|
|
wrapper string
|
|
argsLog string
|
|
goLog string
|
|
path string
|
|
}
|
|
|
|
func newReleaseTestLayout(t *testing.T) releaseTestLayout {
|
|
t.Helper()
|
|
root := t.TempDir()
|
|
layout := releaseTestLayout{
|
|
repo: filepath.Join(root, "repository"),
|
|
caller: filepath.Join(root, "caller"),
|
|
argsLog: filepath.Join(root, "tool-arguments"),
|
|
goLog: filepath.Join(root, "go-arguments"),
|
|
path: filepath.Join(root, "bin"),
|
|
}
|
|
for _, directory := range []string{filepath.Join(layout.repo, "scripts"), layout.caller, layout.path} {
|
|
if err := os.MkdirAll(directory, 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
wrapper, err := os.ReadFile("release.sh")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
layout.wrapper = filepath.Join(layout.repo, "scripts", "release.sh")
|
|
writeReleaseTestFile(t, layout.wrapper, wrapper, 0755)
|
|
writeReleaseTestFile(t, filepath.Join(layout.path, "go"), []byte(goStub), 0755)
|
|
return layout
|
|
}
|
|
|
|
func readText(t *testing.T, path string) string {
|
|
t.Helper()
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return string(data)
|
|
}
|
|
|
|
func (layout releaseTestLayout) environment(values map[string]string) []string {
|
|
filtered := make([]string, 0, len(os.Environ())+len(values)+1)
|
|
for _, value := range os.Environ() {
|
|
key, _, _ := strings.Cut(value, "=")
|
|
if _, overridden := values[key]; key == "PATH" || overridden {
|
|
continue
|
|
}
|
|
filtered = append(filtered, value)
|
|
}
|
|
filtered = append(filtered, "PATH="+layout.path+string(os.PathListSeparator)+os.Getenv("PATH"))
|
|
for key, value := range values {
|
|
filtered = append(filtered, key+"="+value)
|
|
}
|
|
return filtered
|
|
}
|
|
|
|
func writeReleaseTestFile(t *testing.T, path string, data []byte, mode os.FileMode) {
|
|
t.Helper()
|
|
if err := os.WriteFile(path, data, mode); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func readNULArguments(t *testing.T, path string) []string {
|
|
t.Helper()
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
parts := strings.Split(string(data), "\x00")
|
|
if len(parts) > 0 && parts[len(parts)-1] == "" {
|
|
parts = parts[:len(parts)-1]
|
|
}
|
|
return parts
|
|
}
|
|
|
|
func commandStatus(err error) int {
|
|
if err == nil {
|
|
return 0
|
|
}
|
|
if exitErr, ok := err.(*exec.ExitError); ok {
|
|
return exitErr.ExitCode()
|
|
}
|
|
return -1
|
|
}
|
|
|
|
func sameMembers(got, want []string) bool {
|
|
gotSet := make(map[string]struct{}, len(got))
|
|
for _, value := range got {
|
|
gotSet[value] = struct{}{}
|
|
}
|
|
wantSet := make(map[string]struct{}, len(want))
|
|
for _, value := range want {
|
|
wantSet[value] = struct{}{}
|
|
}
|
|
return maps.Equal(gotSet, wantSet)
|
|
}
|