//go:build crossrepo package runner_test import ( "context" "os" "os/exec" "path/filepath" "strings" "testing" "time" "github.com/barerepo/server/testserver" ) // The two halves are separate programs now, so only a test that runs both catches them drifting. func TestTheRunnerAttachesToARealServer(t *testing.T) { for _, name := range []string{"git", "ssh-keygen", "go"} { if _, err := exec.LookPath(name); err != nil { t.Skipf("%s is not installed", name) } } in := testserver.New(t) john := in.Account(t, "john") in.Repo(t, john, "john", "johnbot") tok := in.RunnerToken(t, "john", "john", "johnbot") bin := filepath.Join(t.TempDir(), "barerepo-runner") if out, err := exec.Command("go", "build", "-o", bin, "./cmd/barerepo-runner").CombinedOutput(); err != nil { t.Fatalf("building the runner: %v\n%s", err, out) } ctx, stop := context.WithTimeout(context.Background(), 30*time.Second) defer stop() cmd := exec.CommandContext(ctx, bin, tok, "--server", in.URL, "--labels", "build,test") cmd.Env = append(os.Environ(), "HOME="+t.TempDir()) out, err := cmd.StdoutPipe() if err != nil { t.Fatal(err) } if err := cmd.Start(); err != nil { t.Fatal(err) } t.Cleanup(func() { cmd.Process.Kill() cmd.Wait() }) said := make(chan string, 1) go func() { buf := make([]byte, 256) n, _ := out.Read(buf) said <- string(buf[:n]) }() select { case line := <-said: if !strings.Contains(line, "attached as") { t.Fatalf("the runner said %q, want it to report attaching", line) } case <-ctx.Done(): t.Fatal("the runner never reported attaching") } runners, err := in.DB.RunnersOf(context.Background(), "john/johnbot") if err != nil { t.Fatal(err) } if len(runners) != 1 { t.Fatalf("the server holds %d runners, want the one that just attached", len(runners)) } if got := runners[0].Labels; !strings.Contains(strings.Join(got, ","), "build") { t.Errorf("the server recorded labels %v, want the ones the runner sent", got) } }