Skip to content
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
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ require (
github.com/BurntSushi/toml v1.1.0
github.com/Masterminds/sprig/v3 v3.2.2
github.com/bradleyfalzon/ghinstallation/v2 v2.0.4
github.com/go-playground/assert/v2 v2.0.1
github.com/go-playground/validator/v10 v10.11.0
github.com/google/go-github/v45 v45.1.0
github.com/gorilla/mux v1.8.0
github.com/slack-go/slack v0.11.0
github.com/stretchr/testify v1.7.0
go.uber.org/zap v1.21.0
golang.org/x/oauth2 v0.0.0-20220622183110-fd043fe589d2
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/time v0.0.0-20220609170525-579cf78fd858
gopkg.in/h2non/gock.v1 v1.1.2
k8s.io/api v0.24.2
k8s.io/apimachinery v0.24.2
k8s.io/client-go v0.24.2
Expand All @@ -22,7 +25,9 @@ require (
require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB7
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
Expand Down Expand Up @@ -293,6 +295,8 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8m
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw=
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 h1:W6apQkHrMkS0Muv8G/TipAy/FJl/rCYT0+EuS8+Z0z4=
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
Expand Down Expand Up @@ -738,6 +742,8 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntN
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/h2non/gock.v1 v1.1.2 h1:jBbHXgGBK/AoPVfJh5x4r/WxIrElvbLel8TCZkkZJoY=
gopkg.in/h2non/gock.v1 v1.1.2/go.mod h1:n7UGz/ckNChHiK05rDoiC4MYSunEC/lyaUm2WWaDva0=
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
Expand Down
137 changes: 137 additions & 0 deletions pkg/github/jobs/synchronizer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package jobs
Copy link
Collaborator

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.

Copy link
Contributor Author

@Ngkaokis Ngkaokis Jul 12, 2022

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()


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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be private function.

return &v
}
func TestRun(t *testing.T) {

logger, _ := zap.NewProduction()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can use NewNop to avoid polluting output.

sync_page_size := 5
webhook_server_addr := "127.0.0.1:8001"
config := &Config{
Disabled: true,
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("succeed"),
Conclusion: Ptr("succeed"),
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: testGithubWorkflowRun.GetStatus(),
Conclusion: testGithubWorkflowRun.GetConclusion(),

StartedAt: testGithubWorkflowRun.GetRunStartedAt().Time,
CommitMessageTitle: testCommitMsgTitle,
CommitURL: testCommitURL,
}

testGithubWorkflowJob := &github.WorkflowJob{
ID: Ptr(int64(0)),
HTMLURL: Ptr("testing"),
Status: Ptr("succeed"),
Conclusion: Ptr("succeed"),
}

var startedAt *time.Time
if gt := testGithubWorkflowJob.GetStartedAt(); !gt.IsZero() {
startedAt = &gt.Time
}

var completedAt *time.Time
if gt := testGithubWorkflowJob.GetCompletedAt(); !gt.IsZero() {
completedAt = &gt.Time
}

testWorkflowJob := &WorkflowJob{
Key: Key{ID: int64(0), RepoOwner: "tester", RepoName: "testing"},

Name: testGithubWorkflowJob.GetName(),
URL: testGithubWorkflowJob.GetHTMLURL(),
Status: testGithubWorkflowJob.GetStatus(),
Conclusion: testGithubWorkflowJob.GetConclusion(),

StartedAt: startedAt,
CompletedAt: completedAt,
RunnerID: testGithubWorkflowJob.RunnerID,
RunnerName: testGithubWorkflowJob.RunnerName,
RunnerLabels: testGithubWorkflowJob.Labels,
}

testWorkflowRun.Jobs = append(testWorkflowRun.Jobs, testWorkflowJob)

testWebhookJob := webhookObject[*github.WorkflowJob]{
Key{ID: int64(0), RepoOwner: "tester", RepoName: "testing"},
testGithubWorkflowJob,
}

testWebhookRun := webhookObject[*github.WorkflowRun]{
Key{ID: int64(0), RepoOwner: "tester", RepoName: "testing"},
testGithubWorkflowRun,
}

gock.New("https://api.github.com/repos/(.*)/(.*)/actions/jobs/(.*)").
Persist().
Reply(200).
JSON(testGithubWorkflowJob)

gock.New("https://api.github.com/repos/(.*)/(.*)/actions/runs/(.*)").
Persist().
Reply(200).
JSON(testGithubWorkflowRun)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

_, ctx = errgroup.WithContext(ctx)

runs := make(chan webhookObject[*github.WorkflowRun])
jobs := make(chan webhookObject[*github.WorkflowJob])
s, _ := NewSynchronizer(logger, config, client, kv, registry)
go s.run(ctx, runs, jobs)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be testing the public interface of the unit, so should use Start instead.

runs <- testWebhookRun
jobs <- testWebhookJob
time.Sleep(20 * time.Second)
assert.Equal(t, testWorkflowRun, s.metrics.state.WorkflowRuns[0])

}