GitLabgitlab-org
cli
main
1//go:build !integration
3package catalog
5import (
6 "encoding/json"
7 "net/http"
8 "net/http/httptest"
9 "path/filepath"
10 "testing"
12 "github.com/stretchr/testify/assert"
13 "github.com/stretchr/testify/require"
15 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
17 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
18)
20func TestPublishCatalog(t *testing.T) {
21 // This test uses httptest.NewServer because the catalog publish API uses raw HTTP
22 // via client.Do() rather than a GitLab client-go service interface.
24 tests := []struct {
25 name string
26 tagName string
27 isValidTagName bool
29 wantOutput string
30 wantBody string
31 wantErr bool
32 errMsg string
33 }{
34 {
35 name: "valid tag",
36 tagName: "0.0.1",
37 isValidTagName: true,
38 wantBody: `{
39 "version": "0.0.1",
40 "metadata": {
41 "components": [
42 {
43 "component_type": "template",
44 "name": "component-1",
45 "spec": {
46 "inputs": {
47 "compiler": {
48 "default": "gcc"
49 }
50 }
51 }
52 },
53 {
54 "component_type": "template",
55 "name": "component-2",
56 "spec": null
57 },
58 {
59 "component_type": "template",
60 "name": "component-3",
61 "spec": {
62 "inputs": {
63 "test_framework": {
64 "default": "unittest"
65 }
66 }
67 }
68 }
69 ]
70 }
71 }`,
72 wantOutput: `• Publishing release tag=0.0.1 to the GitLab CI/CD catalog for repo=OWNER/REPO...
73✓ Release published: url=https://gitlab.example.com/explore/catalog/my-namespace/my-component-project`,
74 },
75 {
76 name: "missing tag",
77 wantErr: true,
78 errMsg: "accepts 1 arg(s), received 0",
79 },
80 {
81 name: "invalid tag",
82 tagName: "6.6.6",
83 isValidTagName: false,
84 wantErr: true,
85 errMsg: "invalid tag 6.6.6",
86 },
87 }
89 t.Chdir(filepath.Join("testdata", "test-repo"))
91 for _, tc := range tests {
92 t.Run(tc.name, func(t *testing.T) {
93 // Create a test server that handles both tag validation and catalog publish
94 testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
95 switch {
96 case r.Method == http.MethodGet && r.URL.Path == "/api/v4/projects/OWNER/REPO/repository/tags/"+tc.tagName:
97 if tc.isValidTagName {
98 w.WriteHeader(http.StatusOK)
99 _, _ = w.Write([]byte(`{"name": "` + tc.tagName + `"}`))
100 } else {
101 w.WriteHeader(http.StatusNotFound)
102 _, _ = w.Write([]byte(`{"message": "404 Tag Not Found"}`))
103 }
105 case r.Method == http.MethodPost && r.URL.Path == "/api/v4/projects/OWNER/REPO/catalog/publish":
106 if tc.wantBody != "" {
107 var reqBody, expectedBody map[string]any
108 err := json.NewDecoder(r.Body).Decode(&reqBody)
109 assert.NoError(t, err)
110 err = json.Unmarshal([]byte(tc.wantBody), &expectedBody)
111 assert.NoError(t, err)
113 reqBodyJSON, _ := json.Marshal(reqBody)
114 expectedBodyJSON, _ := json.Marshal(expectedBody)
115 assert.JSONEq(t, string(expectedBodyJSON), string(reqBodyJSON))
116 }
118 w.WriteHeader(http.StatusOK)
119 _, _ = w.Write([]byte(`{
120 "catalog_url": "https://gitlab.example.com/explore/catalog/my-namespace/my-component-project"
121 }`))
123 default:
124 // For missing tag test case, we don't expect any HTTP calls
125 if tc.tagName != "" {
126 t.Errorf("unexpected request: %s %s", r.Method, r.URL.Path)
127 }
128 w.WriteHeader(http.StatusNotFound)
129 }
130 }))
131 defer testServer.Close()
133 // Create a GitLab client with the test server's URL
134 gitlabClient, err := gitlab.NewClient("test-token", gitlab.WithBaseURL(testServer.URL+"/api/v4"))
135 require.NoError(t, err)
137 exec := cmdtest.SetupCmdForTest(t, NewCmdPublishCatalog, false,
138 cmdtest.WithGitLabClient(gitlabClient),
139 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
140 )
142 output, err := exec(tc.tagName)
144 if tc.wantErr {
145 require.Error(t, err)
146 assert.Equal(t, tc.errMsg, err.Error())
147 } else {
148 require.NoError(t, err)
149 assert.Contains(t, output.String(), tc.wantOutput)
150 }
151 })
152 }
153}
155func Test_fetchTemplates(t *testing.T) {
156 wd := filepath.Join("testdata", "test-repo")
158 want := map[string]string{
159 "component-1": filepath.Join(wd, "templates", "component-1.yml"),
160 "component-2": filepath.Join(wd, "templates", "component-2.yml"),
161 "component-3": filepath.Join(wd, "templates", "component-3", "template.yml"),
162 }
163 got, err := fetchTemplates(wd)
164 require.NoError(t, err)
166 for k, v := range want {
167 require.Equal(t, got[k], v)
168 }
169}
171func Test_extractComponentName(t *testing.T) {
172 wd := filepath.Join("testdata", "test-repo")
174 tests := []struct {
175 name string
176 path string
177 expected string
178 }{
179 {
180 name: "valid component path",
181 path: filepath.Join(wd, "templates", "component-1.yml"),
182 expected: "component-1",
183 },
184 {
185 name: "valid component path in sub directory",
186 path: filepath.Join(wd, "templates", "component-2", "template.yml"),
187 expected: "component-2",
188 },
189 {
190 name: "invalid component path",
191 path: filepath.Join(wd, "abc_templates", "component-3.yml"),
192 expected: "",
193 },
194 }
196 for _, tt := range tests {
197 t.Run(tt.name, func(t *testing.T) {
198 got, err := extractComponentName(wd, tt.path)
199 require.NoError(t, err)
200 require.Equal(t, tt.expected, got)
201 })
202 }
203}
Diagnostics9 git calls · 231 ms
CallStatusSizeTime0231 ms
discover200185 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=185 git.server.agent=git/2.56.g1891de4-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=185 git.pkt.payload.max=35
ls-refs20058 KiB · hit2 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=1 git.response.bytes=59849 git.response.cache=HIT git.response.duration_ms=2 git.protocol.version=2 git.pkt.frames=731 git.pkt.data=730 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=59849 git.pkt.payload.max=143 git.ls_refs.count=730 git.ls_refs.head=refs/heads/main
fetchtree:0 · deepen 1020032 KiB · hit1 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=1 git.response.bytes=32926 git.response.cache=HIT git.response.duration_ms=1 git.fetch.filter=tree:0 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=22 git.pkt.data=20 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=32926 git.pkt.payload.max=32046 git.sideband.band1.bytes=32046 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=80 git.pack.bytes=32046 git.pack.ofs_delta=36 git.pack.ref_delta=0 git.pack.chain.max=2 git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91 git.pack.full=44Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=4273 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=combine:blob:none+tree:2 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=1 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=4273 git.pkt.payload.max=4173 git.sideband.band1.bytes=4173 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=11 git.pack.bytes=4173 git.fetch.wide.objects=11 git.fetch.wide.extra_bytes=3930 git.fetch.wide.blobs=0
fetchtree:02001.8 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=1857 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=dce174ff8c632a19d17eea99c8a9b7c37fb2b592 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1857 git.pkt.payload.max=1830 git.sideband.band1.bytes=1830 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=1830 git.pack.trailer=fb03183b7909876778f740d196d7516811003517Open pack map
fetchtree:0200614 B · hit1 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=1 git.response.bytes=614 git.response.cache=HIT git.response.duration_ms=1 git.fetch.filter=tree:0 git.fetch.wants=3a89d0cfc8e81880c982180388e42e561a0b7eee git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=614 git.pkt.payload.max=587 git.sideband.band1.bytes=587 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=587 git.pack.trailer=eb8250f8e8340ff2fafc22c5aba90db34fd906a9Open pack map
fetchtree:0200142 B · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=142 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=af7f92870be97ec283a67c3dc90186c455c369ae git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=142 git.pkt.payload.max=115 git.sideband.band1.bytes=115 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=115 git.pack.trailer=93031c1d64f40c6c49227c17486b6689427c106dOpen pack map
fetchtree:0200172 B · hit1 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=1 git.response.bytes=172 git.response.cache=HIT git.response.duration_ms=1 git.fetch.filter=tree:0 git.fetch.wants=4dea86ad6397a41b9cbead34fc69a4c4244f25e7 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=172 git.pkt.payload.max=145 git.sideband.band1.bytes=145 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=145 git.pack.trailer=2bd83ce989fb929895280a53fa461cd216c8ad4dOpen pack map
fetch2001.8 KiB221 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=221 git.response.bytes=1873 git.response.cache=MISS git.response.duration_ms=221 git.fetch.wants=158ca98919a75acf94c911d844c41d6213519ea6 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1873 git.pkt.payload.max=1846 git.sideband.band1.bytes=1846 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://gitlab.com/gitlab-org/cli
- Ref
- refs/heads/main
- Commit
- 0dd7574dce9e385c5c30540e6c15657adc6bdce9
- cf-ray
- a5ea625e4fd7ecd4
- Request
- 227fe002…
- Protocol
- v2 · sha1
- Agent
- git/2.56.g1891de4-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format