package ci import ( "os" "os/exec" "path/filepath" "strings" "testing" "time" "gitbay.org/gitbay/internal/config" "gitbay.org/gitbay/internal/store" ) // TestSchedulerRunDue drives one scheduler pass against a real store and // bare repo: a due entry queues a build, sets a pending status, and // advances next_run; entries whose job vanished are dropped. func TestSchedulerRunDue(t *testing.T) { st, err := store.Open(":memory:") if err != nil { t.Fatal(err) } defer st.Close() if err := st.MigrateUp(); err != nil { t.Fatal(err) } uid, err := st.CreateUser("alice", false) if err != nil { t.Fatal(err) } _ = uid repoID, err := st.CreateRepo("user", uid, "app", "public") if err != nil { t.Fatal(err) } // A bare repo whose main holds a ci.yml with one scheduled job. work := t.TempDir() env := append(os.Environ(), "GIT_CONFIG_NOSYSTEM=1", "GIT_CONFIG_GLOBAL=/dev/null", "GIT_AUTHOR_NAME=t", "GIT_AUTHOR_EMAIL=t@example.test", "GIT_COMMITTER_NAME=t", "GIT_COMMITTER_EMAIL=t@example.test") git := func(dir string, args ...string) { t.Helper() cmd := exec.Command("git", args...) cmd.Dir = dir cmd.Env = env if out, err := cmd.CombinedOutput(); err != nil { t.Fatalf("git %v: %v\n%s", args, err, out) } } src := filepath.Join(work, "src") os.MkdirAll(filepath.Join(src, ".gitbay"), 0o755) os.WriteFile(filepath.Join(src, ".gitbay", "ci.yml"), []byte("jobs:\n nightly:\n schedule: \"0 6 * * *\"\n steps: [echo hi]\n"), 0o644) git(work, "init", "-q", "-b", "main", "src") git(src, "add", ".") git(src, "commit", "-q", "-m", "base") bare := filepath.Join(work, "bare.git") git(work, "clone", "-q", "--bare", src, "bare.git") now := time.Now() past := now.Add(-time.Hour).UTC().Format("2006-01-02T15:04:05Z") if err := st.SyncSchedules(repoID, []store.Schedule{ {RepoID: repoID, Job: "nightly", Cron: "0 6 * * *", NextRun: past}, {RepoID: repoID, Job: "gone", Cron: "0 7 * * *", NextRun: past}, }); err != nil { t.Fatal(err) } s := &Scheduler{St: st, SiteURL: "https://x.test", RepoDir: func(owner, name string) string { return bare }} s.RunDue(now) builds, err := st.ListBuilds(repoID, store.BuildFilter{}, 10) if err != nil || len(builds) != 1 { t.Fatalf("builds after run: %v %v", builds, err) } if builds[0].Job != "nightly" || builds[0].Status != "pending" || builds[0].Ref != "main" { t.Fatalf("queued build wrong: %+v", builds[0]) } statuses, _ := st.ListCommitStatuses(repoID, builds[0].SHA) if len(statuses) != 1 || statuses[0].Context != "ci/nightly" || statuses[0].State != "pending" { t.Fatalf("status wrong: %+v", statuses) } // next_run advanced past now; the vanished job's entry is gone. due, _ := st.DueSchedules(now.UTC().Format("2006-01-02T15:04:05Z")) if len(due) != 0 { t.Fatalf("still due after run: %+v", due) } all, _ := st.DueSchedules("9999-01-01T00:00:00Z") if len(all) != 1 || all[0].Job != "nightly" { t.Fatalf("schedule set after run: %+v", all) } // Cron fires in server-local time, stored as UTC. cr, _ := ParseCron("0 6 * * *") if want := cr.Next(now).UTC().Format("2006-01-02T15:04:05Z"); all[0].NextRun != want { t.Fatalf("next_run = %s, want %s", all[0].NextRun, want) } // A second pass fires nothing: next_run is in the future. s.RunDue(now) if builds, _ = st.ListBuilds(repoID, store.BuildFilter{}, 10); len(builds) != 1 { t.Fatalf("second pass queued extra builds: %+v", builds) } // Due again while the first build is still pending: nothing is // queued, so a repository no runner serves holds one row per // scheduled job, not one per tick (#206). Once that build finishes, // the next tick queues again — a schedule re-runs an unchanged // commit on purpose. if err := st.SetScheduleNext(repoID, "nightly", past); err != nil { t.Fatal(err) } s.RunDue(now) if builds, _ = st.ListBuilds(repoID, store.BuildFilter{}, 10); len(builds) != 1 { t.Fatalf("tick with the last build pending queued another: %+v", builds) } b, ok, err := st.ClaimBuild(nil, false) if err != nil || !ok { t.Fatalf("claim: %v ok=%v", err, ok) } if err := st.SetScheduleNext(repoID, "nightly", past); err != nil { t.Fatal(err) } s.RunDue(now) if builds, _ = st.ListBuilds(repoID, store.BuildFilter{}, 10); len(builds) != 1 { t.Fatalf("tick with the last build running queued another: %+v", builds) } if err := st.FinishBuild(b.ID, "success"); err != nil { t.Fatal(err) } if err := st.SetScheduleNext(repoID, "nightly", past); err != nil { t.Fatal(err) } s.RunDue(now) if builds, _ = st.ListBuilds(repoID, store.BuildFilter{}, 10); len(builds) != 2 { t.Fatalf("tick after the last build finished did not queue: %+v", builds) } } // The code scan has its own schedule: a due repository gets one build of // the instance's job, with no commit status, and none while one is pending // or while the instance has no image. func TestSchedulerQueuesSecurityScan(t *testing.T) { st, err := store.Open(":memory:") if err != nil { t.Fatal(err) } defer st.Close() if err := st.MigrateUp(); err != nil { t.Fatal(err) } uid, _ := st.CreateUser("alice", false) repoID, _ := st.CreateRepo("user", uid, "app", "public") work := t.TempDir() bare := filepath.Join(work, "bare.git") for _, args := range [][]string{ {"init", "-q", "-b", "main", "src"}, {"-C", "src", "-c", "user.name=t", "-c", "user.email=t@example.test", "commit", "-q", "--allow-empty", "-m", "base"}, {"clone", "-q", "--bare", "src", "bare.git"}, } { cmd := exec.Command("git", args...) cmd.Dir = work cmd.Env = append(os.Environ(), "GIT_CONFIG_NOSYSTEM=1", "GIT_CONFIG_GLOBAL=/dev/null") if out, err := cmd.CombinedOutput(); err != nil { t.Fatalf("git %v: %v\n%s", args, err, out) } } now := time.Now() if err := st.EnableSecurityScan(repoID, now.Add(-time.Minute).UTC().Format("2006-01-02T15:04:05Z")); err != nil { t.Fatal(err) } s := &Scheduler{St: st, SiteURL: "https://x.test", RepoDir: func(owner, name string) string { return bare }} s.RunDue(now) if builds, _ := st.ListBuilds(repoID, store.BuildFilter{}, 10); len(builds) != 0 { t.Fatalf("queued with no image: %+v", builds) } s.Security = config.Security{Image: "localhost/gitbay-security:1", Hour: 4, MostOften: "daily", LeastOften: "monthly", Default: "daily", Steps: []string{"true"}} st.SetSecurityNext(repoID, now.Add(-time.Minute).UTC().Format("2006-01-02T15:04:05Z")) s.RunDue(now) builds, _ := st.ListBuilds(repoID, store.BuildFilter{}, 10) if len(builds) != 1 || builds[0].Job != SecurityJob || builds[0].Image != "localhost/gitbay-security:1" { t.Fatalf("builds = %+v", builds) } if statuses, _ := st.CommitStatusesFor(repoID, []string{builds[0].SHA}); len(statuses[builds[0].SHA]) != 0 { t.Errorf("a status was set: %+v", statuses) } if sc, _ := st.SecurityScanFor(repoID); sc.NextRun <= now.UTC().Format("2006-01-02T15:04:05Z") { t.Errorf("next run not advanced: %q", sc.NextRun) } // Due again while the first is pending: nothing more. st.SetSecurityNext(repoID, now.Add(-time.Minute).UTC().Format("2006-01-02T15:04:05Z")) s.RunDue(now) if builds, _ := st.ListBuilds(repoID, store.BuildFilter{}, 10); len(builds) != 1 { t.Errorf("queued over a pending scan: %d builds", len(builds)) } } func TestSecurityJobNameIsReserved(t *testing.T) { if _, err := Parse([]byte("jobs:\n gitbay-security:\n steps: [echo]\n")); err == nil || !strings.Contains(err.Error(), "reserved") { t.Fatalf("Parse = %v", err) } } func TestSecurityNextRun(t *testing.T) { sec := config.Default().Security sec.Hour = 6 // Wednesday 2026-11-04 10:00 UTC. after := time.Date(2026, 11, 4, 10, 0, 0, 0, time.UTC).In(time.FixedZone("UTC-6", -6*3600)) for chosen, want := range map[string]string{ "": "2026-11-05T06:00:00Z", "daily": "2026-11-05T06:00:00Z", "weekly": "2026-11-08T06:00:00Z", "monthly": "2026-12-01T06:00:00Z", } { if got := SecurityNextRun(sec, chosen, after); got != want { t.Errorf("%q: %s, want %s", chosen, got, want) } } // A choice the instance no longer allows scans at the nearest allowed. sec.LeastOften, sec.Default = "weekly", "weekly" if got := SecurityNextRun(sec, "monthly", after); got != "2026-11-08T06:00:00Z" { t.Errorf("monthly outside the range: %s", got) } }