Skip to content

Commit 14a9ea9

Browse files
committed
Add new unit tests for schemas pkg
1 parent 2ab20d4 commit 14a9ea9

File tree

9 files changed

+1302
-0
lines changed

9 files changed

+1302
-0
lines changed

pkg/schemas/environments_test.go

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package schemas
2+
3+
import (
4+
"hash/crc32"
5+
"strconv"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestEnvironmentKey(t *testing.T) {
12+
e := Environment{
13+
ProjectName: "group/project",
14+
Name: "production",
15+
}
16+
17+
expected := EnvironmentKey(strconv.Itoa(int(crc32.ChecksumIEEE([]byte("group/project" + "production")))))
18+
assert.Equal(t, expected, e.Key())
19+
}
20+
21+
func TestEnvironmentKey_SameInputSameKey(t *testing.T) {
22+
e1 := Environment{
23+
ProjectName: "group/project",
24+
Name: "staging",
25+
ID: 1,
26+
}
27+
e2 := Environment{
28+
ProjectName: "group/project",
29+
Name: "staging",
30+
ID: 999,
31+
}
32+
33+
assert.Equal(t, e1.Key(), e2.Key())
34+
}
35+
36+
func TestEnvironmentKey_DifferentInputDifferentKey(t *testing.T) {
37+
e1 := Environment{
38+
ProjectName: "group/project",
39+
Name: "staging",
40+
}
41+
e2 := Environment{
42+
ProjectName: "group/project",
43+
Name: "production",
44+
}
45+
46+
assert.NotEqual(t, e1.Key(), e2.Key())
47+
}
48+
49+
func TestEnvironmentsCount(t *testing.T) {
50+
envs := Environments{
51+
EnvironmentKey("1"): {ID: 1},
52+
EnvironmentKey("2"): {ID: 2},
53+
EnvironmentKey("3"): {ID: 3},
54+
}
55+
56+
assert.Equal(t, 3, envs.Count())
57+
}
58+
59+
func TestEnvironmentsCount_Empty(t *testing.T) {
60+
envs := Environments{}
61+
assert.Equal(t, 0, envs.Count())
62+
}
63+
64+
func TestEnvironmentDefaultLabelsValues(t *testing.T) {
65+
e := Environment{
66+
ProjectName: "group/project",
67+
Name: "production",
68+
}
69+
70+
got := e.DefaultLabelsValues()
71+
72+
expected := map[string]string{
73+
"project": "group/project",
74+
"environment": "production",
75+
}
76+
77+
assert.Equal(t, expected, got)
78+
}
79+
80+
func TestEnvironmentInformationLabelsValues(t *testing.T) {
81+
e := Environment{
82+
ProjectName: "group/project",
83+
ID: 42,
84+
Name: "production",
85+
ExternalURL: "https://example.com",
86+
Available: true,
87+
LatestDeployment: Deployment{
88+
JobID: 1001,
89+
RefKind: RefKindBranch,
90+
RefName: "main",
91+
Username: "jdoe",
92+
Timestamp: 1710000000,
93+
DurationSeconds: 12.5,
94+
CommitShortID: "abc123",
95+
Status: "success",
96+
},
97+
}
98+
99+
got := e.InformationLabelsValues()
100+
101+
expected := map[string]string{
102+
"project": "group/project",
103+
"environment": "production",
104+
"environment_id": "42",
105+
"external_url": "https://example.com",
106+
"kind": "branch",
107+
"ref": "main",
108+
"current_commit_short_id": "abc123",
109+
"latest_commit_short_id": "",
110+
"available": "true",
111+
"username": "jdoe",
112+
}
113+
114+
assert.Equal(t, expected, got)
115+
}
116+
117+
func TestEnvironmentInformationLabelsValues_WithZeroValues(t *testing.T) {
118+
e := Environment{
119+
ProjectName: "group/project",
120+
ID: 0,
121+
Name: "staging",
122+
ExternalURL: "",
123+
Available: false,
124+
LatestDeployment: Deployment{
125+
JobID: 0,
126+
RefKind: "",
127+
RefName: "",
128+
Username: "",
129+
Timestamp: 0,
130+
DurationSeconds: 0,
131+
CommitShortID: "",
132+
Status: "",
133+
},
134+
}
135+
136+
got := e.InformationLabelsValues()
137+
138+
expected := map[string]string{
139+
"project": "group/project",
140+
"environment": "staging",
141+
"environment_id": "0",
142+
"external_url": "",
143+
"kind": "",
144+
"ref": "",
145+
"current_commit_short_id": "",
146+
"latest_commit_short_id": "",
147+
"available": "false",
148+
"username": "",
149+
}
150+
151+
assert.Equal(t, expected, got)
152+
}

pkg/schemas/jobs_test.go

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
package schemas
2+
3+
import (
4+
"encoding/json"
5+
"testing"
6+
"time"
7+
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
10+
goGitlab "gitlab.com/gitlab-org/api/client-go"
11+
)
12+
13+
func mustJobFromJSON(t *testing.T, raw string) goGitlab.Job {
14+
t.Helper()
15+
16+
var job goGitlab.Job
17+
err := json.Unmarshal([]byte(raw), &job)
18+
require.NoError(t, err)
19+
20+
return job
21+
}
22+
23+
func mustParseTime(t *testing.T, raw string) time.Time {
24+
t.Helper()
25+
26+
ts, err := time.Parse(time.RFC3339, raw)
27+
require.NoError(t, err)
28+
29+
return ts
30+
}
31+
32+
func TestNewJob(t *testing.T) {
33+
gj := mustJobFromJSON(t, `{
34+
"id": 101,
35+
"name": "unit-tests",
36+
"stage": "test",
37+
"created_at": "2024-03-09T16:00:00Z",
38+
"duration": 42.5,
39+
"queued_duration": 3.2,
40+
"status": "success",
41+
"tag_list": ["docker", "linux"],
42+
"failure_reason": "",
43+
"pipeline": {
44+
"id": 999
45+
},
46+
"artifacts": [
47+
{"size": 100},
48+
{"size": 250},
49+
{"size": 50}
50+
],
51+
"runner": {
52+
"description": "shared-runner-1"
53+
}
54+
}`)
55+
56+
expectedCreatedAt := mustParseTime(t, "2024-03-09T16:00:00Z")
57+
58+
got := NewJob(gj)
59+
60+
assert.Equal(t, 101, got.ID)
61+
assert.Equal(t, "unit-tests", got.Name)
62+
assert.Equal(t, "test", got.Stage)
63+
assert.Equal(t, float64(expectedCreatedAt.Unix()), got.Timestamp)
64+
assert.Equal(t, 42.5, got.DurationSeconds)
65+
assert.Equal(t, 3.2, got.QueuedDurationSeconds)
66+
assert.Equal(t, "success", got.Status)
67+
assert.Equal(t, 999, got.PipelineID)
68+
assert.Equal(t, "docker,linux", got.TagList)
69+
assert.Equal(t, 400.0, got.ArtifactSize)
70+
assert.Equal(t, "", got.FailureReason)
71+
assert.Equal(t, "shared-runner-1", got.Runner.Description)
72+
}
73+
74+
func TestNewJob_NoCreatedAt(t *testing.T) {
75+
gj := mustJobFromJSON(t, `{
76+
"id": 102,
77+
"name": "build",
78+
"stage": "build",
79+
"duration": 10,
80+
"queued_duration": 1,
81+
"status": "running",
82+
"tag_list": ["kubernetes"],
83+
"pipeline": {
84+
"id": 1000
85+
},
86+
"artifacts": [
87+
{"size": 500}
88+
],
89+
"runner": {
90+
"description": "runner-k8s"
91+
}
92+
}`)
93+
94+
got := NewJob(gj)
95+
96+
assert.Equal(t, 0.0, got.Timestamp)
97+
assert.Equal(t, 500.0, got.ArtifactSize)
98+
assert.Equal(t, "kubernetes", got.TagList)
99+
assert.Equal(t, "runner-k8s", got.Runner.Description)
100+
}
101+
102+
func TestNewJob_EmptyArtifactsAndTags(t *testing.T) {
103+
gj := mustJobFromJSON(t, `{
104+
"id": 103,
105+
"name": "deploy",
106+
"stage": "deploy",
107+
"created_at": "2024-03-09T16:01:40Z",
108+
"duration": 5,
109+
"queued_duration": 0.5,
110+
"status": "failed",
111+
"tag_list": [],
112+
"failure_reason": "script_failure",
113+
"pipeline": {
114+
"id": 1001
115+
},
116+
"artifacts": [],
117+
"runner": {
118+
"description": ""
119+
}
120+
}`)
121+
122+
expectedCreatedAt := mustParseTime(t, "2024-03-09T16:01:40Z")
123+
124+
got := NewJob(gj)
125+
126+
assert.Equal(t, 103, got.ID)
127+
assert.Equal(t, "deploy", got.Name)
128+
assert.Equal(t, "deploy", got.Stage)
129+
assert.Equal(t, float64(expectedCreatedAt.Unix()), got.Timestamp)
130+
assert.Equal(t, 5.0, got.DurationSeconds)
131+
assert.Equal(t, 0.5, got.QueuedDurationSeconds)
132+
assert.Equal(t, "failed", got.Status)
133+
assert.Equal(t, 1001, got.PipelineID)
134+
assert.Equal(t, "", got.TagList)
135+
assert.Equal(t, 0.0, got.ArtifactSize)
136+
assert.Equal(t, "script_failure", got.FailureReason)
137+
assert.Equal(t, "", got.Runner.Description)
138+
}
139+
140+
func TestNewJob_ArtifactSizeSum(t *testing.T) {
141+
gj := mustJobFromJSON(t, `{
142+
"pipeline": {
143+
"id": 1
144+
},
145+
"artifacts": [
146+
{"size": 1},
147+
{"size": 2},
148+
{"size": 3},
149+
{"size": 4}
150+
]
151+
}`)
152+
153+
got := NewJob(gj)
154+
155+
assert.Equal(t, 10.0, got.ArtifactSize)
156+
}

0 commit comments

Comments
 (0)