-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitlab_test.go
More file actions
131 lines (125 loc) · 5.26 KB
/
Copy pathgitlab_test.go
File metadata and controls
131 lines (125 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func fakeGitLab(t *testing.T) *httptest.Server {
// now-relative stamps: fixed dates rotted out of the activity window
// (?hours=168) the week after they were written
ts := func(agoSec int) string {
return time.Now().UTC().Add(-time.Duration(agoSec) * time.Second).Format(time.RFC3339)
}
_ = ts
t.Helper()
mux := http.NewServeMux()
auth := func(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("PRIVATE-TOKEN") != "tok" {
w.WriteHeader(401)
return
}
w.Header().Set("Content-Type", "application/json")
h(w, r)
}
}
mux.HandleFunc("/api/v4/projects", auth(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Query().Get("page") {
case "", "1":
w.Header().Set("x-next-page", "2")
fmt.Fprint(w, `[{"id":1,"name":"alpha","path_with_namespace":"platform/alpha","web_url":"http://gl/platform/alpha","default_branch":"main","namespace":{"full_path":"platform"}}]`)
case "2":
w.Header().Set("x-next-page", "")
fmt.Fprint(w, `[{"id":2,"name":"beta","path_with_namespace":"themes/beta","web_url":"http://gl/themes/beta","default_branch":"main","namespace":{"full_path":"themes"}}]`)
}
}))
mux.HandleFunc("/api/v4/projects/1/pipelines", auth(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `[{"id":11,"status":"success","ref":"main","sha":"abc123","web_url":"http://gl/platform/alpha/-/pipelines/11","created_at":"`+ts(86400)+`","updated_at":"`+ts(86200)+`"}]`)
}))
mux.HandleFunc("/api/v4/projects/1/pipelines/11", auth(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"id":11,"status":"success","ref":"main","sha":"abc123","web_url":"http://gl/platform/alpha/-/pipelines/11","created_at":"`+ts(86400)+`","updated_at":"`+ts(86200)+`","started_at":"`+ts(86400)+`","finished_at":"`+ts(86200)+`","duration":190}`)
}))
mux.HandleFunc("/api/v4/projects/1/pipelines/11/jobs", auth(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `[{"name":"build","stage":"build","status":"success","started_at":"`+ts(86400)+`","finished_at":"`+ts(86280)+`","duration":110,"web_url":"http://gl/platform/alpha/-/jobs/101"}]`)
}))
mux.HandleFunc("/api/v4/projects/1/events", auth(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `[{"action_name":"pushed to","created_at":"`+ts(86460)+`","author":{"username":"john"},"push_data":{"ref":"main","commit_title":"feat: x","commit_count":1}}]`)
}))
mux.HandleFunc("/api/v4/projects/2/pipelines", auth(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `[{"id":21,"status":"failed","ref":"main","sha":"def456","web_url":"http://gl/themes/beta/-/pipelines/21","created_at":"`+ts(82800)+`","updated_at":"`+ts(82740)+`"}]`)
}))
mux.HandleFunc("/api/v4/projects/2/events", auth(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `[]`)
}))
return httptest.NewServer(mux)
}
func TestClientPaginatesProjects(t *testing.T) {
gl := fakeGitLab(t)
defer gl.Close()
c := newGLClient(gl.URL, "tok")
ps, err := c.projects(context.Background())
if err != nil {
t.Fatal(err)
}
if len(ps) != 2 || ps[0].Name != "alpha" || ps[1].Namespace.FullPath != "themes" {
t.Fatalf("bad projects: %+v", ps)
}
}
func TestClientPipelinesJobsEvents(t *testing.T) {
gl := fakeGitLab(t)
defer gl.Close()
c := newGLClient(gl.URL, "tok")
pl, err := c.pipelines(context.Background(), 1, 20)
if err != nil || len(pl) != 1 || pl[0].Status != "success" {
t.Fatalf("pipelines: %v %+v", err, pl)
}
d, err := c.pipeline(context.Background(), 1, 11)
if err != nil || d.Duration != 190 {
t.Fatalf("pipeline detail: %v %+v", err, d)
}
js, err := c.jobs(context.Background(), 1, 11)
if err != nil || len(js) != 1 || js[0].Stage != "build" {
t.Fatalf("jobs: %v %+v", err, js)
}
ev, err := c.events(context.Background(), 1, 50)
if err != nil || len(ev) != 1 || ev[0].PushData == nil {
t.Fatalf("events: %v %+v", err, ev)
}
}
func TestClientBadToken(t *testing.T) {
gl := fakeGitLab(t)
defer gl.Close()
c := newGLClient(gl.URL, "wrong")
if _, err := c.projects(context.Background()); err == nil {
t.Fatal("expected error on 401")
}
}
// TestTagsSearchAnchoredQuery: the ^prefix anchor must survive URL encoding and
// reach GitLab intact, and results stay newest-first — this is what isolates an
// umbrella's own line out of a shared-prefix charts monorepo.
func TestTagsSearchAnchoredQuery(t *testing.T) {
var gotSearch, gotOrder string
gl := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotSearch = r.URL.Query().Get("search")
gotOrder = r.URL.Query().Get("order_by")
_, _ = w.Write([]byte(`[{"name":"konstruct-v0.7.0-rc.443f3828"},{"name":"konstruct-v0.7.0-rc.184f6054"}]`))
}))
defer gl.Close()
c := newGLClient(gl.URL, "tok")
out, err := c.tagsSearch(context.Background(), "civo/konstruct/charts", "^konstruct-v0.7.0", 50)
if err != nil {
t.Fatalf("tagsSearch: %v", err)
}
if gotSearch != "^konstruct-v0.7.0" {
t.Errorf("search param = %q, want the anchored umbrella line", gotSearch)
}
if gotOrder != "updated" {
t.Errorf("order_by = %q, want updated (newest-first)", gotOrder)
}
if len(out) != 2 || out[0].Name != "konstruct-v0.7.0-rc.443f3828" {
t.Errorf("decoded tags = %+v", out)
}
}