GitLabgitlab-org
cli
main
1//go:build !integration
3package run_trig
5import (
6 "testing"
8 "github.com/stretchr/testify/assert"
9 "github.com/stretchr/testify/require"
10 "go.uber.org/mock/gomock"
12 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
13 gitlabtesting "gitlab.com/gitlab-org/api/client-go/v3/testing"
15 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
16)
18func TestParseVarArg_EmptyKey(t *testing.T) {
19 t.Parallel()
21 for _, input := range []string{":value", " :value"} {
22 _, _, err := parseVarArg(input)
24 require.EqualError(t, err, "variable key cannot be empty")
25 }
26}
28func TestParseVarArg_TrimsKey(t *testing.T) {
29 t.Parallel()
31 key, value, err := parseVarArg(" FOO :value")
33 require.NoError(t, err)
34 assert.Equal(t, "FOO", key)
35 assert.Equal(t, "value", value)
36}
38func TestCIRunTrig(t *testing.T) {
39 // Cannot use t.Parallel() because subtests use t.Setenv()
41 tests := []struct {
42 name string
43 cli string
44 ciJobToken string
45 expectedRef string
46 expectedOut string
47 }{
48 {
49 name: "when running `ci run-trig` without branch parameter, defaults to current branch",
50 cli: "-t foobar",
51 ciJobToken: "",
52 expectedRef: "custom-branch-123",
53 expectedOut: "Created pipeline (ID: 123), status: created, ref: custom-branch-123, weburl: https://gitlab.com/OWNER/REPO/-/pipelines/123\n",
54 },
55 {
56 name: "when running `ci run-trig` with branch parameter, run CI at branch",
57 cli: "-t foobar -b ci-cd-improvement-399",
58 ciJobToken: "",
59 expectedRef: "ci-cd-improvement-399",
60 expectedOut: "Created pipeline (ID: 123), status: created, ref: ci-cd-improvement-399, weburl: https://gitlab.com/OWNER/REPO/-/pipelines/123\n",
61 },
62 {
63 name: "when running `ci run-trig` without any parameter, takes trigger token from env variable",
64 cli: "",
65 ciJobToken: "foobar",
66 expectedRef: "custom-branch-123",
67 expectedOut: "Created pipeline (ID: 123), status: created, ref: custom-branch-123, weburl: https://gitlab.com/OWNER/REPO/-/pipelines/123\n",
68 },
69 }
71 for _, tc := range tests {
72 t.Run(tc.name, func(t *testing.T) {
73 t.Setenv("CI_JOB_TOKEN", tc.ciJobToken)
75 testClient := gitlabtesting.NewTestClient(t)
77 testClient.MockPipelineTriggers.EXPECT().
78 RunPipelineTrigger("OWNER/REPO", gomock.Any()).
79 DoAndReturn(func(pid any, opts *gitlab.RunPipelineTriggerOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Pipeline, *gitlab.Response, error) {
80 // Verify the token is passed correctly
81 assert.Equal(t, "foobar", *opts.Token)
82 // Verify the ref matches expected
83 assert.Equal(t, tc.expectedRef, *opts.Ref)
85 return &gitlab.Pipeline{
86 ID: 123,
87 IID: 123,
88 Status: "created",
89 Ref: *opts.Ref,
90 WebURL: "https://gitlab.com/OWNER/REPO/-/pipelines/123",
91 }, nil, nil
92 })
94 exec := cmdtest.SetupCmdForTest(t, NewCmdRunTrig, false,
95 cmdtest.WithGitLabClient(testClient.Client),
96 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
97 cmdtest.WithBranch("custom-branch-123"),
98 )
100 output, err := exec(tc.cli)
101 require.NoError(t, err)
103 assert.Equal(t, tc.expectedOut, output.String())
104 assert.Empty(t, output.Stderr())
105 })
106 }
107}
109func TestCIRunTrigWithInputs(t *testing.T) {
110 // NOTE: This test cannot run in parallel because NewCmdRunTrig modifies global pflag state.
112 testClient := gitlabtesting.NewTestClient(t)
114 testClient.MockPipelineTriggers.EXPECT().
115 RunPipelineTrigger("OWNER/REPO", gomock.Any()).
116 DoAndReturn(func(pid any, opts *gitlab.RunPipelineTriggerOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Pipeline, *gitlab.Response, error) {
117 // Verify the token
118 assert.Equal(t, "foobar", *opts.Token)
119 // Verify the ref
120 assert.Equal(t, "custom-branch-123", *opts.Ref)
121 // Verify inputs are passed (check keys and values exist)
122 assert.Len(t, opts.Inputs, 2)
123 assert.Contains(t, opts.Inputs, "key1")
124 assert.Contains(t, opts.Inputs, "key2")
126 return &gitlab.Pipeline{
127 ID: 123,
128 IID: 123,
129 Status: "created",
130 Ref: *opts.Ref,
131 WebURL: "https://gitlab.com/OWNER/REPO/-/pipelines/123",
132 }, nil, nil
133 })
135 exec := cmdtest.SetupCmdForTest(t, NewCmdRunTrig, false,
136 cmdtest.WithGitLabClient(testClient.Client),
137 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
138 cmdtest.WithBranch("custom-branch-123"),
139 )
141 output, err := exec("-t foobar -i key1:val1 --input key2:val2")
142 require.NoError(t, err)
144 assert.Equal(t, "Created pipeline (ID: 123), status: created, ref: custom-branch-123, weburl: https://gitlab.com/OWNER/REPO/-/pipelines/123\n", output.String())
145 assert.Empty(t, output.Stderr())
146}
148func TestCIRunTrigWithVariables(t *testing.T) {
149 // NOTE: This test cannot run in parallel because NewCmdRunTrig modifies global pflag state.
151 testClient := gitlabtesting.NewTestClient(t)
153 testClient.MockPipelineTriggers.EXPECT().
154 RunPipelineTrigger("OWNER/REPO", gomock.Any()).
155 DoAndReturn(func(pid any, opts *gitlab.RunPipelineTriggerOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Pipeline, *gitlab.Response, error) {
156 // Verify the token
157 assert.Equal(t, "foobar", *opts.Token)
158 // Verify variables are passed
159 assert.Equal(t, map[string]string{"VAR1": "value1", "VAR2": "value2"}, opts.Variables)
161 return &gitlab.Pipeline{
162 ID: 123,
163 IID: 123,
164 Status: "created",
165 Ref: *opts.Ref,
166 WebURL: "https://gitlab.com/OWNER/REPO/-/pipelines/123",
167 }, nil, nil
168 })
170 exec := cmdtest.SetupCmdForTest(t, NewCmdRunTrig, false,
171 cmdtest.WithGitLabClient(testClient.Client),
172 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
173 cmdtest.WithBranch("main"),
174 )
176 output, err := exec("-t foobar --variables VAR1:value1,VAR2:value2")
177 require.NoError(t, err)
179 assert.Equal(t, "Created pipeline (ID: 123), status: created, ref: main, weburl: https://gitlab.com/OWNER/REPO/-/pipelines/123\n", output.String())
180 assert.Empty(t, output.Stderr())
181}
183func TestCIRunTrigMissingToken(t *testing.T) {
184 // Cannot use t.Parallel() because test uses t.Setenv()
186 // Ensure CI_JOB_TOKEN is not set
187 t.Setenv("CI_JOB_TOKEN", "")
189 testClient := gitlabtesting.NewTestClient(t)
191 // No mock expectation since the command should fail before making API call
193 exec := cmdtest.SetupCmdForTest(t, NewCmdRunTrig, false,
194 cmdtest.WithGitLabClient(testClient.Client),
195 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
196 cmdtest.WithBranch("main"),
197 )
199 _, err := exec("")
200 require.Error(t, err)
201 assert.Contains(t, err.Error(), "`--token` parameter can be omitted only if `CI_JOB_TOKEN` environment variable is set")
202}
Diagnostics8 git calls · 37 ms
CallStatusSizeTime037 ms
discover200185 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 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=2 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
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=4273 git.response.cache=HIT git.response.duration_ms=3 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:0 · deepen 1020032 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=32926 git.response.cache=HIT git.response.duration_ms=3 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
fetchtree:02001.8 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=1857 git.response.cache=HIT git.response.duration_ms=3 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:0200635 B · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=635 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=517b073a0d5c32bc4e93f86de74b31d60ff8cf03 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=635 git.pkt.payload.max=608 git.sideband.band1.bytes=608 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=608 git.pack.trailer=178abb9456d874b7fd38fa21ad5cf9ca3ebcccaaOpen pack map
fetchtree:0200140 B · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=23 git.response.bytes=140 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=162ed9061bb0c0014a90f249c2efe150cdfa6c89 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=140 git.pkt.payload.max=113 git.sideband.band1.bytes=113 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=113 git.pack.trailer=704a25849d431d46eab038a54255e921b0401b4dOpen pack map
fetch2001.6 KiB · hit23 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=1681 git.response.cache=HIT git.response.duration_ms=23 git.fetch.wants=cf4260ccc8c5d149283bb73c7a6b605f7ee99859 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=1681 git.pkt.payload.max=1654 git.sideband.band1.bytes=1654 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
- a5e3000589a7f3c9
- Request
- f7e4b1f8…
- 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