Skip to content

Commit

Permalink
test(service): merge functions for test latest version github|url
Browse files Browse the repository at this point in the history
* build-binary: node-version=20.x rather than just 20
  • Loading branch information
JosephKav committed Apr 23, 2024
1 parent 39657f3 commit 04a1901
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 80 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-binary.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
- name: Use Node.js LTS
uses: actions/setup-node@v4
with:
node-version: 20
node-version: 20.x

- name: Build web components
run: make web
Expand Down
16 changes: 8 additions & 8 deletions service/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestService_UpdateLatestApproved(t *testing.T) {
}

for name, tc := range tests {
svc := testServiceURL(name)
svc := testService(name, "url")

t.Run(name, func(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -190,7 +190,7 @@ func TestService_UpdatedVersion(t *testing.T) {
t.Run(name, func(t *testing.T) {
t.Parallel()

svc := testServiceURL(name)
svc := testService(name, "url")
svc.Command = tc.commands
svc.WebHook = tc.webhooks
svc.Status.Init(
Expand Down Expand Up @@ -289,7 +289,7 @@ func TestService_HandleUpdateActions(t *testing.T) {
}

for name, tc := range tests {
svc := testServiceURL(name)
svc := testService(name, "url")
svc.Command = tc.commands
svc.WebHook = tc.webhooks
svc.Status.Init(
Expand Down Expand Up @@ -552,7 +552,7 @@ func TestService_HandleFailedActions(t *testing.T) {
}

for name, tc := range tests {
svc := testServiceURL(name)
svc := testService(name, "url")

t.Run(name, func(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -735,7 +735,7 @@ func TestService_HandleCommand(t *testing.T) {
}

for name, tc := range tests {
svc := testServiceURL(name)
svc := testService(name, "url")

t.Run(name, func(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -896,7 +896,7 @@ func TestService_HandleWebHook(t *testing.T) {
}

for name, tc := range tests {
svc := testServiceURL(name)
svc := testService(name, "url")
svc.Status.Init(
len(svc.Notify), len(tc.webhooks), 0,
&svc.ID,
Expand Down Expand Up @@ -1010,7 +1010,7 @@ func TestService_HandleSkip(t *testing.T) {
}

for name, tc := range tests {
svc := testServiceURL(name)
svc := testService(name, "url")

t.Run(name, func(t *testing.T) {
// t.Parallel() - cannot run in parallel as it uses the same channel
Expand Down Expand Up @@ -1144,7 +1144,7 @@ func TestService_ShouldRetryAll(t *testing.T) {
}

for name, tc := range tests {
svc := testServiceURL(name)
svc := testService(name, "url")

t.Run(name, func(t *testing.T) {
t.Parallel()
Expand Down
94 changes: 35 additions & 59 deletions service/help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,65 +43,15 @@ func TestMain(m *testing.M) {
os.Exit(exitCode)
}

func testServiceGitHub(id string) *Service {
var (
announceChannel chan []byte = make(chan []byte, 2)
saveChannel chan bool = make(chan bool, 5)
databaseChannel chan dbtype.Message = make(chan dbtype.Message, 5)
)
svc := &Service{
ID: id,
LatestVersion: *latestver.New(
test.StringPtr(os.Getenv("GITHUB_TOKEN")),
nil, nil, nil,
&filter.Require{
RegexContent: "content",
RegexVersion: "version"},
nil,
"github",
"release-argus/Argus",
nil,
test.BoolPtr(false),
&latestver.LookupDefaults{},
&latestver.LookupDefaults{}),
Dashboard: *NewDashboardOptions(
test.BoolPtr(false), "test", "https://example.com", "https://release-argus.io",
nil, nil),
Options: *opt.New(
test.BoolPtr(true), "5s", test.BoolPtr(true),
&opt.OptionsDefaults{}, &opt.OptionsDefaults{}),
Defaults: &Defaults{},
HardDefaults: &Defaults{},
}
// Status
svc.Status = *svcstatus.New(
&announceChannel, &databaseChannel, &saveChannel,
"", "", "", "", "", "")
svc.Status.ServiceID = &svc.ID
svc.Status.SetApprovedVersion("1.1.1", false)
svc.Status.SetLatestVersion("2.2.2", false)
svc.Status.SetLatestVersionTimestamp("2002-02-02T02:02:02Z")
svc.Status.SetDeployedVersion("0.0.0", false)
svc.Status.SetDeployedVersionTimestamp("2001-01-01T01:01:01Z")

svc.Init(
&Defaults{}, &Defaults{},
nil, nil, nil,
nil, nil, nil)
svc.Status.ServiceID = &svc.ID
svc.Status.WebURL = &svc.Dashboard.WebURL
return svc
}

func testServiceURL(id string) *Service {
func testService(id string, sType string) *Service {
var (
announceChannel = make(chan []byte, 5)
saveChannel = make(chan bool, 5)
databaseChannel = make(chan dbtype.Message, 5)
)
svc := &Service{
ID: id,
LatestVersion: *testLatestVersionLookupURL(false),
LatestVersion: *testLatestVersion(sType, false),
DeployedVersionLookup: testDeployedVersionLookup(false),
Dashboard: *NewDashboardOptions(
test.BoolPtr(false), "test", "https://release-argus.io", "https://release-argus.io",
Expand Down Expand Up @@ -131,20 +81,36 @@ func testServiceURL(id string) *Service {
svc.Status.SetDeployedVersion("0.0.0", false)
svc.Status.SetDeployedVersionTimestamp("2001-01-01T01:01:01Z")

svc.LatestVersion.Init(
&latestver.LookupDefaults{}, &latestver.LookupDefaults{},
&svc.Status,
&svc.Options)
svc.DeployedVersionLookup.Init(
&deployedver.LookupDefaults{}, &deployedver.LookupDefaults{},
&svc.Status,
&svc.Options)
return svc
}

func testLatestVersionLookupURL(fail bool) *latestver.Lookup {
lv := latestver.New(
func testLatestVersionGitHub(fail bool) (lv *latestver.Lookup) {
lv = latestver.New(
test.StringPtr(os.Getenv("GITHUB_TOKEN")),
nil, nil, nil,
&filter.Require{
RegexContent: "content",
RegexVersion: "version"},
nil,
"github",
"release-argus/Argus",
nil,
test.BoolPtr(false),
&latestver.LookupDefaults{},
&latestver.LookupDefaults{})

if fail {
lv.AccessToken = test.StringPtr("invalid")
}
return
}
func testLatestVersionURL(fail bool) (lv *latestver.Lookup) {
lv = latestver.New(
nil,
test.BoolPtr(!fail),
nil,
opt.New(
Expand All @@ -162,7 +128,17 @@ func testLatestVersionLookupURL(fail bool) *latestver.Lookup {
test.BoolPtr(false),
&latestver.LookupDefaults{},
&latestver.LookupDefaults{})
lv.Status.ServiceID = test.StringPtr("foo")

return
}

func testLatestVersion(lvType string, fail bool) (lv *latestver.Lookup) {
if lvType == "url" {
lv = testLatestVersionURL(fail)
} else {
lv = testLatestVersionGitHub(fail)
}
lv.Status.ServiceID = test.StringPtr("TEST_LV")
return lv
}

Expand Down
4 changes: 2 additions & 2 deletions service/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (

func TestService_ServiceInfo(t *testing.T) {
// GIVEN a Service
svc := testServiceURL("TestServiceInfo")
svc := testService("TestServiceInfo", "url")
id := "test_id"
svc.ID = id
url := "https://test_url.com"
Expand Down Expand Up @@ -124,7 +124,7 @@ func TestService_IconURL(t *testing.T) {
}

for name, tc := range tests {
svc := testServiceGitHub(name)
svc := testService(name, "github")

t.Run(name, func(t *testing.T) {
t.Parallel()
Expand Down
14 changes: 7 additions & 7 deletions service/new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3139,7 +3139,7 @@ func TestFromPayload(t *testing.T) {

func TestService_CheckFetches(t *testing.T) {
// GIVEN a Service
testLV := testLatestVersionLookupURL(false)
testLV := testLatestVersion("url", false)
testLV.Query(false, &util.LogFrom{})
testDVL := testDeployedVersionLookup(false)
v, _ := testDVL.Query(false, &util.LogFrom{})
Expand All @@ -3154,7 +3154,7 @@ func TestService_CheckFetches(t *testing.T) {
}{
"Already have LatestVersion, nil DeployedVersionLookup": {
svc: &Service{
LatestVersion: *testLatestVersionLookupURL(false),
LatestVersion: *testLatestVersion("url", false),
DeployedVersionLookup: nil},
startLatestVersion: "foo",
wantLatestVersion: testLV.Status.LatestVersion(),
Expand All @@ -3164,7 +3164,7 @@ func TestService_CheckFetches(t *testing.T) {
},
"Already have LatestVersion and DeployedVersionLookup": {
svc: &Service{
LatestVersion: *testLatestVersionLookupURL(false),
LatestVersion: *testLatestVersion("url", false),
DeployedVersionLookup: testDeployedVersionLookup(false)},
startLatestVersion: "foo",
wantLatestVersion: testLV.Status.LatestVersion(),
Expand All @@ -3174,20 +3174,20 @@ func TestService_CheckFetches(t *testing.T) {
},
"latest_version query fails": {
svc: &Service{
LatestVersion: *testLatestVersionLookupURL(true),
LatestVersion: *testLatestVersion("url", true),
DeployedVersionLookup: testDeployedVersionLookup(false)},
errRegex: `latest_version - x509 \(certificate invalid\)`,
},
"deployed_version query fails": {
svc: &Service{
LatestVersion: *testLatestVersionLookupURL(false),
LatestVersion: *testLatestVersion("url", false),
DeployedVersionLookup: testDeployedVersionLookup(true)},
wantLatestVersion: "1.2.2",
errRegex: `deployed_version - x509 \(certificate invalid\)`,
},
"both queried": {
svc: &Service{
LatestVersion: *testLatestVersionLookupURL(false),
LatestVersion: *testLatestVersion("url", false),
DeployedVersionLookup: testDeployedVersionLookup(false)},
wantLatestVersion: "1.2.2",
wantDeployedVersion: "1.2.3",
Expand All @@ -3199,7 +3199,7 @@ func TestService_CheckFetches(t *testing.T) {
test.BoolPtr(false), // active
"", nil,
nil, nil),
LatestVersion: *testLatestVersionLookupURL(false),
LatestVersion: *testLatestVersion("url", false),
DeployedVersionLookup: testDeployedVersionLookup(false)},
errRegex: "^$",
},
Expand Down
6 changes: 3 additions & 3 deletions service/track_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ func TestSlice_Track(t *testing.T) {
for _, j := range tc.slice {
switch j {
case "github":
(*slice)[j] = testServiceGitHub(name)
(*slice)[j] = testService(name, "github")
case "url":
(*slice)[j] = testServiceURL(name)
(*slice)[j] = testService(name, "url")
}
if len(tc.active) != 0 {
(*slice)[j].Options.Active = test.BoolPtr(tc.active[i])
Expand Down Expand Up @@ -264,7 +264,7 @@ func TestService_Track(t *testing.T) {
t.Run(name, func(t *testing.T) {
t.Parallel()

svc := testServiceURL(name)
svc := testService(name, "url")
if tc.deleting {
svc.Status.SetDeleting()
}
Expand Down

0 comments on commit 04a1901

Please sign in to comment.