diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 00c926e..a7b4e90 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,7 +23,10 @@ jobs: with: go-version: '1.26.8' cache: true - - run: go vet ./... + - if: runner.os == 'Linux' + run: make lint + - if: runner.os != 'Linux' + run: go vet ./... - name: Verify wildcard and loopback port conflicts run: go test -v ./internal/deploy -run '^TestProbeNativePortRejectsListeningSockets$' - run: go test -race ./... @@ -134,7 +137,7 @@ jobs: with: go-version: '1.26.8' cache: true - - run: make check cross-build + - run: make cross-build - uses: actions/upload-artifact@v4 with: name: lbctl-binaries diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 0000000..f26f186 --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,7 @@ +# Working on lbctl + +- Discover the public contract with `lbctl --help` and `lbctl schema --command ''`. +- During edits, run the affected Go package or named test. For CLI flags, requests, output, or exit codes, use the existing tests under `internal/command` and `internal/integration`. Run `make lint` for formatting and vet checks, then `make check` for the full Go suite with race detection. +- For install, upgrade, rollback, or Core HTTP changes, build the candidate with `make build`. Reuse the relevant script in `scripts/` or run the affected command against the target Core. Check the exit code, structured output, and actual post-operation state. Use isolated test instances for writes and lifecycle changes. +- For endpoint normalization, run `go test ./internal/endpoint -run '^FuzzNormalizeIdempotent$'` to replay seeds. Use `go test ./internal/endpoint -fuzz '^FuzzNormalizeIdempotent$' -fuzztime 10s` to explore new inputs and retain any reproducing inputs. +- When an assertion may miss a specific regression, change one behavior in an isolated copy and run the same test against the original and changed code. Report the command, expected behavior, observed result, and verification still pending. diff --git a/Makefile b/Makefile index ae9e681..86689b2 100644 --- a/Makefile +++ b/Makefile @@ -4,17 +4,19 @@ COMMIT ?= $(shell git describe --always --dirty) BUILD_DATE ?= $(shell date -u +%Y-%m-%dT%H:%M:%SZ) LDFLAGS = -s -w -X main.version=$(VERSION) -X main.commit=$(COMMIT) -X main.buildDate=$(BUILD_DATE) -.PHONY: build test check cross-build +.PHONY: build lint test check cross-build build: $(GO) build -trimpath -ldflags '$(LDFLAGS)' -o bin/lbctl ./cmd/lbctl test: $(GO) test -race ./... -check: - test -z "$$(gofmt -l cmd internal)" +lint: + @unformatted=$$(gofmt -l cmd internal) && \ + { test -z "$$unformatted" || { printf 'Run gofmt -w on:\n%s\n' "$$unformatted"; exit 1; }; } $(GO) vet ./... - $(GO) test -race ./... + +check: lint test cross-build: GO='$(GO)' VERSION='$(VERSION)' COMMIT='$(COMMIT)' BUILD_DATE='$(BUILD_DATE)' sh scripts/build.sh diff --git a/internal/deploy/native_test.go b/internal/deploy/native_test.go index 861c29d..2cab054 100644 --- a/internal/deploy/native_test.go +++ b/internal/deploy/native_test.go @@ -5,6 +5,7 @@ import ( "bytes" "compress/gzip" "context" + "errors" "fmt" "io" "net" @@ -126,8 +127,10 @@ func nativeFixture(t *testing.T) (string, Record) { } func TestNativeUpgradeRestoresVersionAndData(t *testing.T) { - for _, failure := range []string{"", "prepare", "health"} { + for _, failure := range []string{"", "prepare", "health", "canceled"} { t.Run(failure, func(t *testing.T) { + ctx, cancel := context.WithCancel(t.Context()) + defer cancel() dir, record := nativeFixture(t) record.Autostart = new(false) if err := (Store{Dir: dir}).Write(context.Background(), record); err != nil { @@ -149,9 +152,9 @@ func TestNativeUpgradeRestoresVersionAndData(t *testing.T) { } return nil }, - stop: func(_ context.Context, _ string, record Record) error { + stop: func(ctx context.Context, _ string, record Record) error { calls = append(calls, "stop:"+record.CoreVersion) - return nil + return ctx.Err() }, start: func(_ context.Context, _ string, record Record) error { if record.Autostart == nil || *record.Autostart { @@ -163,14 +166,18 @@ func TestNativeUpgradeRestoresVersionAndData(t *testing.T) { } return nil }, - health: func(_ context.Context, record Record, _ *http.Client) error { + health: func(ctx context.Context, record Record, _ *http.Client) error { + if failure == "canceled" && record.CoreVersion == "v4.11.8" { + cancel() + return ctx.Err() + } if failure == "health" && record.CoreVersion == "v4.11.8" { return fmt.Errorf("healthz 503") } return nil }, } - backup, err := upgradeNative(context.Background(), dir, record, "v4.11.8", io.Discard, nil, operations) + backup, err := upgradeNative(ctx, dir, record, "v4.11.8", io.Discard, nil, operations) current, readErr := (Store{Dir: dir}).Read() if readErr != nil { t.Fatal(readErr) @@ -195,6 +202,9 @@ func TestNativeUpgradeRestoresVersionAndData(t *testing.T) { if failure == "health" && !strings.Contains(err.Error(), "previous version and data restored") { t.Fatal(err) } + if failure == "canceled" && !errors.Is(err, context.Canceled) { + t.Fatalf("upgrade error = %v, want context cancellation", err) + } }) } } diff --git a/internal/endpoint/endpoint_test.go b/internal/endpoint/endpoint_test.go index b1594c4..9bbbeef 100644 --- a/internal/endpoint/endpoint_test.go +++ b/internal/endpoint/endpoint_test.go @@ -34,3 +34,21 @@ func TestRejectAmbiguousTargets(t *testing.T) { }) } } + +func FuzzNormalizeIdempotent(f *testing.F) { + f.Add("http://LOCALHOST:05300/") + f.Add("https://example.com:443/proxy/") + f.Add("http://[::1]:80") + f.Add("https://example.com/%E5%BC%80%E5%8F%91/") + f.Add("https://example.com/a%20b") + f.Fuzz(func(t *testing.T, input string) { + normalized, err := Normalize(input) + if err != nil { + return + } + again, err := Normalize(normalized) + if err != nil || again != normalized { + t.Fatalf("Normalize(%q) = %q, then %q, %v", input, normalized, again, err) + } + }) +}