-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add jobs synchronizer test #10
Open
Ngkaokis
wants to merge
6
commits into
oursky:master
Choose a base branch
from
Ngkaokis:test
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8911306
Add jobs synchronizer test
Ngkaokis 8aced03
Refactor Synchronizer to facilitate testing
Ngkaokis 759728b
Add function to create WebhookObject
Ngkaokis 3ea249c
Make Ptr fucntion private
Ngkaokis e7e935a
Add Clock interface to make time-based tests faster
Ngkaokis 6775875
Update jobs synchronizer test to update job status
Ngkaokis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package jobs | ||
|
||
import "time" | ||
|
||
type Clock interface { | ||
Now() time.Time | ||
After(d time.Duration) <-chan time.Time | ||
} | ||
|
||
type RealClock struct{} | ||
|
||
func (RealClock) Now() time.Time { return time.Now() } | ||
func (RealClock) After(d time.Duration) <-chan time.Time { return time.After(d) } | ||
|
||
type TestClock struct{} | ||
|
||
func (TestClock) Now() time.Time { return time.Now() } | ||
func (TestClock) After(d time.Duration) <-chan time.Time { return time.After(0) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
package jobs | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/go-playground/assert/v2" | ||
"github.com/google/go-github/v45/github" | ||
"github.com/oursky/github-actions-manager/pkg/kv" | ||
"github.com/oursky/github-actions-manager/pkg/utils/tomltypes" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"go.uber.org/zap" | ||
"golang.org/x/sync/errgroup" | ||
"gopkg.in/h2non/gock.v1" | ||
) | ||
|
||
func ptr[T any](v T) *T { | ||
return &v | ||
} | ||
func TestRun(t *testing.T) { | ||
|
||
logger, _ := zap.NewProduction() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can use |
||
sync_page_size := 5 | ||
webhook_server_addr := "127.0.0.1:8001" | ||
config := &Config{ | ||
Disabled: false, | ||
ReplayEnabled: true, | ||
RetentionPeriod: &tomltypes.Duration{1 * time.Hour}, | ||
SyncInterval: &tomltypes.Duration{5 * time.Second}, | ||
SyncPageSize: &sync_page_size, | ||
WebhookServerAddr: &webhook_server_addr, | ||
WebhookSecret: "testing", | ||
} | ||
|
||
kv := kv.NewInMemoryStore() | ||
registry := prometheus.NewRegistry() | ||
client := &http.Client{Transport: &http.Transport{}} | ||
gock.InterceptClient(client) | ||
defer gock.Off() | ||
|
||
testGithubWorkflowRun := &github.WorkflowRun{ | ||
ID: ptr(int64(0)), | ||
Status: ptr("in_progress"), | ||
Conclusion: ptr(""), | ||
WorkflowID: ptr(int64(0)), | ||
HeadCommit: &github.HeadCommit{}, | ||
HeadRepository: &github.Repository{}, | ||
} | ||
|
||
testCommitMsg := testGithubWorkflowRun.GetHeadCommit().GetMessage() | ||
testCommitMsgTitle, _, _ := strings.Cut(testCommitMsg, "\n") | ||
testCommitURL := testGithubWorkflowRun.GetHeadRepository().GetHTMLURL() + "/commit/" + testGithubWorkflowRun.GetHeadCommit().GetID() | ||
|
||
testWorkflowRun := &WorkflowRun{ | ||
Key: Key{ID: int64(0), RepoOwner: "tester", RepoName: "testing"}, | ||
|
||
Name: testGithubWorkflowRun.GetName(), | ||
URL: testGithubWorkflowRun.GetHTMLURL(), | ||
Status: "completed", | ||
Conclusion: "success", | ||
|
||
StartedAt: testGithubWorkflowRun.GetRunStartedAt().Time, | ||
CommitMessageTitle: testCommitMsgTitle, | ||
CommitURL: testCommitURL, | ||
} | ||
|
||
testGithubWorkflowJob := &github.WorkflowJob{ | ||
ID: ptr(int64(0)), | ||
HTMLURL: ptr("testing"), | ||
Status: ptr("in_progress"), | ||
Conclusion: ptr(""), | ||
} | ||
|
||
var startedAt *time.Time | ||
if gt := testGithubWorkflowJob.GetStartedAt(); !gt.IsZero() { | ||
startedAt = >.Time | ||
} | ||
|
||
var completedAt *time.Time | ||
if gt := testGithubWorkflowJob.GetCompletedAt(); !gt.IsZero() { | ||
completedAt = >.Time | ||
} | ||
|
||
testWorkflowJob := &WorkflowJob{ | ||
Key: Key{ID: int64(0), RepoOwner: "tester", RepoName: "testing"}, | ||
|
||
Name: testGithubWorkflowJob.GetName(), | ||
URL: testGithubWorkflowJob.GetHTMLURL(), | ||
Status: "completed", | ||
Conclusion: "success", | ||
|
||
StartedAt: startedAt, | ||
CompletedAt: completedAt, | ||
RunnerID: testGithubWorkflowJob.RunnerID, | ||
RunnerName: testGithubWorkflowJob.RunnerName, | ||
RunnerLabels: testGithubWorkflowJob.Labels, | ||
} | ||
|
||
testWorkflowRun.Jobs = append(testWorkflowRun.Jobs, testWorkflowJob) | ||
|
||
testWebhookJob := NewWebhookObject( | ||
Key{ID: int64(0), RepoOwner: "tester", RepoName: "testing"}, | ||
testGithubWorkflowJob, | ||
) | ||
|
||
testWebhookRun := NewWebhookObject( | ||
Key{ID: int64(0), RepoOwner: "tester", RepoName: "testing"}, | ||
testGithubWorkflowRun, | ||
) | ||
|
||
testUpdatedGithubWorkflowJob := &github.WorkflowJob{ | ||
ID: ptr(int64(0)), | ||
HTMLURL: ptr("testing"), | ||
Status: ptr("completed"), | ||
Conclusion: ptr("success"), | ||
} | ||
|
||
testUpdatedGithubWorkflowRun := &github.WorkflowRun{ | ||
ID: ptr(int64(0)), | ||
Status: ptr("completed"), | ||
Conclusion: ptr("success"), | ||
WorkflowID: ptr(int64(0)), | ||
HeadCommit: &github.HeadCommit{}, | ||
HeadRepository: &github.Repository{}, | ||
} | ||
|
||
gock.New("https://api.github.com/repos/(.*)/(.*)/actions/jobs/(.*)"). | ||
Persist(). | ||
Reply(200). | ||
JSON(testUpdatedGithubWorkflowJob) | ||
|
||
gock.New("https://api.github.com/repos/(.*)/(.*)/actions/runs/(.*)"). | ||
Persist(). | ||
Reply(200). | ||
JSON(testUpdatedGithubWorkflowRun) | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
g, ctx := errgroup.WithContext(ctx) | ||
s, _ := NewSynchronizer(logger, config, client, kv, registry, TestClock{}) | ||
s.Start(ctx, g) | ||
s.webhookRuns <- testWebhookRun | ||
s.webhookJobs <- testWebhookJob | ||
time.Sleep(1 * time.Second) | ||
assert.Equal(t, testWorkflowRun, s.metrics.state.WorkflowRuns[0]) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
jobs_test
to avoid using package internal functions.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if I alter it, I can't create a webhookObject for testing. Should I create another public function sth like NewWebhookObject()