|
| 1 | +package cmd_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/google/go-github/v66/github" |
| 10 | + "github.com/input-output-hk/catalyst-forge/tools/github-job-checker/cmd" |
| 11 | +) |
| 12 | + |
| 13 | +// MockGitHubClient mocks the GitHubClient interface for testing. |
| 14 | +type MockGitHubClient struct { |
| 15 | + FetchFunc func(ctx context.Context, owner, repo, ref string) (*github.ListCheckRunsResults, error) |
| 16 | +} |
| 17 | + |
| 18 | +func (m *MockGitHubClient) FetchCheckRunsForRef(ctx context.Context, owner, repo, ref string) (*github.ListCheckRunsResults, error) { |
| 19 | + if m.FetchFunc != nil { |
| 20 | + return m.FetchFunc(ctx, owner, repo, ref) |
| 21 | + } |
| 22 | + return nil, errors.New("FetchFunc not implemented") |
| 23 | +} |
| 24 | + |
| 25 | +func TestChecker_Run_Success(t *testing.T) { |
| 26 | + client := &MockGitHubClient{ |
| 27 | + FetchFunc: func(ctx context.Context, owner, repo, ref string) (*github.ListCheckRunsResults, error) { |
| 28 | + return &github.ListCheckRunsResults{ |
| 29 | + Total: github.Int(1), |
| 30 | + CheckRuns: []*github.CheckRun{ |
| 31 | + { |
| 32 | + Status: github.String("completed"), |
| 33 | + Conclusion: github.String("success"), |
| 34 | + }, |
| 35 | + }, |
| 36 | + }, nil |
| 37 | + }, |
| 38 | + } |
| 39 | + |
| 40 | + checker := &cmd.Checker{ |
| 41 | + Owner: "owner", |
| 42 | + Repo: "repo", |
| 43 | + Ref: "ref", |
| 44 | + CheckInterval: 1 * time.Second, |
| 45 | + Timeout: 5 * time.Second, |
| 46 | + Client: client, |
| 47 | + } |
| 48 | + |
| 49 | + ctx := context.Background() |
| 50 | + err := checker.Run(ctx) |
| 51 | + if err != nil { |
| 52 | + t.Fatalf("expected no error, got %v", err) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func TestChecker_Run_Failure(t *testing.T) { |
| 57 | + client := &MockGitHubClient{ |
| 58 | + FetchFunc: func(ctx context.Context, owner, repo, ref string) (*github.ListCheckRunsResults, error) { |
| 59 | + return &github.ListCheckRunsResults{ |
| 60 | + Total: github.Int(1), |
| 61 | + CheckRuns: []*github.CheckRun{ |
| 62 | + { |
| 63 | + Status: github.String("completed"), |
| 64 | + Conclusion: github.String("failure"), |
| 65 | + }, |
| 66 | + }, |
| 67 | + }, nil |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + checker := &cmd.Checker{ |
| 72 | + Owner: "owner", |
| 73 | + Repo: "repo", |
| 74 | + Ref: "ref", |
| 75 | + CheckInterval: 1 * time.Second, |
| 76 | + Timeout: 5 * time.Second, |
| 77 | + Client: client, |
| 78 | + } |
| 79 | + |
| 80 | + ctx := context.Background() |
| 81 | + err := checker.Run(ctx) |
| 82 | + if err == nil || err.Error() != "one or more GitHub jobs failed" { |
| 83 | + t.Fatalf("expected failure error, got %v", err) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func TestChecker_Run_PendingToSuccess(t *testing.T) { |
| 88 | + callCount := 0 |
| 89 | + client := &MockGitHubClient{ |
| 90 | + FetchFunc: func(ctx context.Context, owner, repo, ref string) (*github.ListCheckRunsResults, error) { |
| 91 | + defer func() { callCount++ }() |
| 92 | + if callCount == 0 { |
| 93 | + // First call: return pending |
| 94 | + return &github.ListCheckRunsResults{ |
| 95 | + Total: github.Int(1), |
| 96 | + CheckRuns: []*github.CheckRun{ |
| 97 | + { |
| 98 | + Status: github.String("queued"), |
| 99 | + Conclusion: nil, |
| 100 | + }, |
| 101 | + }, |
| 102 | + }, nil |
| 103 | + } |
| 104 | + // Subsequent calls: return success |
| 105 | + return &github.ListCheckRunsResults{ |
| 106 | + Total: github.Int(1), |
| 107 | + CheckRuns: []*github.CheckRun{ |
| 108 | + { |
| 109 | + Status: github.String("completed"), |
| 110 | + Conclusion: github.String("success"), |
| 111 | + }, |
| 112 | + }, |
| 113 | + }, nil |
| 114 | + }, |
| 115 | + } |
| 116 | + |
| 117 | + checker := &cmd.Checker{ |
| 118 | + Owner: "owner", |
| 119 | + Repo: "repo", |
| 120 | + Ref: "ref", |
| 121 | + CheckInterval: 500 * time.Millisecond, |
| 122 | + Timeout: 2 * time.Second, |
| 123 | + Client: client, |
| 124 | + } |
| 125 | + |
| 126 | + ctx := context.Background() |
| 127 | + err := checker.Run(ctx) |
| 128 | + if err != nil { |
| 129 | + t.Fatalf("expected no error, got %v", err) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +func TestChecker_Run_Timeout(t *testing.T) { |
| 134 | + client := &MockGitHubClient{ |
| 135 | + FetchFunc: func(ctx context.Context, owner, repo, ref string) (*github.ListCheckRunsResults, error) { |
| 136 | + // Always return in-progress status |
| 137 | + return &github.ListCheckRunsResults{ |
| 138 | + Total: github.Int(1), |
| 139 | + CheckRuns: []*github.CheckRun{ |
| 140 | + { |
| 141 | + Status: github.String("in_progress"), |
| 142 | + Conclusion: nil, |
| 143 | + }, |
| 144 | + }, |
| 145 | + }, nil |
| 146 | + }, |
| 147 | + } |
| 148 | + |
| 149 | + checker := &cmd.Checker{ |
| 150 | + Owner: "owner", |
| 151 | + Repo: "repo", |
| 152 | + Ref: "ref", |
| 153 | + CheckInterval: 500 * time.Millisecond, |
| 154 | + Timeout: 1 * time.Second, |
| 155 | + Client: client, |
| 156 | + } |
| 157 | + |
| 158 | + ctx := context.Background() |
| 159 | + err := checker.Run(ctx) |
| 160 | + if err == nil || !errors.Is(err, context.DeadlineExceeded) && err.Error() != "timeout of 1s reached. GitHub jobs did not finish in time" { |
| 161 | + t.Fatalf("expected timeout error, got %v", err) |
| 162 | + } |
| 163 | +} |
0 commit comments