golib/net/gitshallow/gitshallow_integration_test.go
AJ ONeal 1dacbdd8f7
feat(net/gitshallow): incremental shallow git-repo mirroring
Periodically-updated shallow clone for repos used as a data source.
One Fetch verb (clone-or-pull-or-skip) drives both the Fetcher
interface and per-File handles. MaxAge gates on FETCH_HEAD mtime so
short-lived CLI invocations don't hammer the remote across process
restarts; an in-memory 1s debounce coalesces concurrent Fetches.
Hardens git invocations against argument injection: validates
branch/ref shape, rejects URLs starting with '-', and uses '--'
separators before positional args. Userinfo in URL is redacted from
error output (both args echo and git's own messages).
2026-05-02 16:15:54 -06:00

102 lines
2.4 KiB
Go

//go:build integration
package gitshallow_test
import (
"os"
"path/filepath"
"testing"
"github.com/therootcompany/golib/net/gitshallow"
)
const testRepoURL = "https://github.com/bitwire-it/ipblocklist"
func testdataDir(t *testing.T) string {
t.Helper()
dir, _ := filepath.Abs(".")
for {
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
return filepath.Join(dir, "testdata")
}
parent := filepath.Dir(dir)
if parent == dir {
t.Fatal("could not find module root (go.mod)")
}
dir = parent
}
}
func repoDir(t *testing.T) string {
return filepath.Join(testdataDir(t), "gitshallow_ipblocklist")
}
func TestRepo_Clone(t *testing.T) {
dir := repoDir(t)
os.RemoveAll(dir)
repo := gitshallow.New(testRepoURL, dir, 1, "")
updated, err := repo.Fetch(t.Context())
if err != nil {
t.Fatalf("Fetch (clone): %v", err)
}
if !updated {
t.Error("fresh clone: expected updated=true")
}
for _, rel := range []string{
"tables/inbound/single_ips.txt",
"tables/inbound/networks.txt",
"tables/outbound/single_ips.txt",
"tables/outbound/networks.txt",
} {
info, err := os.Stat(filepath.Join(dir, rel))
if err != nil {
t.Errorf("expected file missing: %s", rel)
} else {
t.Logf("%s: %d bytes", rel, info.Size())
}
}
}
func TestRepo_Pull_SameInstance(t *testing.T) {
dir := repoDir(t)
repo := gitshallow.New(testRepoURL, dir, 1, "")
// Ensure cloned.
if _, err := repo.Fetch(t.Context()); err != nil {
t.Fatalf("initial Fetch: %v", err)
}
// Second pull on the same instance — already at HEAD, should not advance.
updated, err := repo.Fetch(t.Context())
if err != nil {
t.Fatalf("second Fetch: %v", err)
}
if updated {
t.Error("same-instance second pull: expected updated=false (already at HEAD)")
}
t.Log("same-instance pull correctly reported no update")
}
func TestRepo_Pull_FreshInstance(t *testing.T) {
dir := repoDir(t)
// Ensure cloned via a first instance.
first := gitshallow.New(testRepoURL, dir, 1, "")
if _, err := first.Fetch(t.Context()); err != nil {
t.Fatalf("initial Fetch: %v", err)
}
// Fresh instance with no in-memory state — git HEAD on disk drives the check.
fresh := gitshallow.New(testRepoURL, dir, 1, "")
updated, err := fresh.Fetch(t.Context())
if err != nil {
t.Fatalf("fresh-instance Fetch: %v", err)
}
if updated {
t.Error("fresh-instance pull: expected updated=false (HEAD unchanged on disk)")
}
t.Log("fresh-instance pull correctly reported no update")
}