GitLabgitlab-org
cli
main
1//go:build !integration
3package send
5import (
6 "strings"
7 "testing"
9 "github.com/google/uuid"
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
12 autoflowrpcv1 "gitlab.com/gitlab-org/protos/gen/go/gitlab/autoflow/rpc/v1"
13 autoflowv1 "gitlab.com/gitlab-org/protos/gen/go/gitlab/autoflow/v1"
14 "go.uber.org/mock/gomock"
15 "google.golang.org/grpc/codes"
16 "google.golang.org/grpc/status"
17 "google.golang.org/protobuf/proto"
19 "gitlab.com/gitlab-org/cli/internal/testing/autoflowtest"
20 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
21)
23type sendCall = autoflowtest.Call[*autoflowrpcv1.SendToWorkflowChannelRequest]
25func expectSend(relay *autoflowtest.Relay, err error) <-chan sendCall {
26 calls := make(chan sendCall, 1)
27 var resp *autoflowrpcv1.SendToWorkflowChannelResponse
28 if err == nil {
29 resp = &autoflowrpcv1.SendToWorkflowChannelResponse{}
30 }
31 relay.Mock.EXPECT().SendToWorkflowChannel(gomock.Any(), gomock.Any()).DoAndReturn(autoflowtest.Answer(calls, resp, err))
32 return calls
33}
35func tokenArgs(t *testing.T) string {
36 t.Helper()
37 return " --workflow-token-file " + autoflowtest.WriteFile(t, "wf-token\n") + " --channel-token-file " + autoflowtest.WriteFile(t, "ch-token\n")
38}
40func TestSend(t *testing.T) {
41 t.Parallel()
43 relay := autoflowtest.NewRelay(t)
44 calls := expectSend(relay, nil)
45 exec, token := autoflowtest.NewTestExec(t, NewCmd)
47 out, err := exec(relay.Args() + tokenArgs(t) + ` --value '["a", 1]'`)
48 require.NoError(t, err)
50 call := autoflowtest.Received(t, calls)
51 assert.Equal(t, []string{"Bearer " + token}, call.Auth)
52 assert.Equal(t, "wf-token", call.Req.GetWorkflowToken())
53 assert.Equal(t, "ch-token", call.Req.GetChannelToken())
54 wantNamespace := uuid.MustParse(autoflowtest.NamespaceID)
55 assert.Equal(t, wantNamespace[:], call.Req.GetNamespaceId())
56 want := &autoflowv1.Value{Kind: &autoflowv1.Value_ListValue{ListValue: &autoflowv1.ListValue{Values: []*autoflowv1.Value{
57 {Kind: &autoflowv1.Value_StringValue{StringValue: "a"}},
58 {Kind: &autoflowv1.Value_IntegerValue{IntegerValue: 1}},
59 }}}}
60 assert.True(t, proto.Equal(want, call.Req.GetValue()), "got %v", call.Req.GetValue())
61 key := call.Req.GetIdempotencyKey()
62 assert.True(t, strings.HasPrefix(key, "glab-"), key)
63 assert.Equal(t, "Sent the value with idempotency key "+key+".\n", out.OutBuf.String())
64}
66func TestSend_ChannelTokenFromStdin(t *testing.T) {
67 t.Parallel()
69 relay := autoflowtest.NewRelay(t)
70 calls := expectSend(relay, nil)
71 exec, _ := autoflowtest.NewTestExec(t, NewCmd, cmdtest.WithStdin("stdin-token\n"))
73 _, err := exec(relay.Args() + " --workflow-token-file " + autoflowtest.WriteFile(t, "wf-token") + " --channel-token-file - --value 1 --idempotency-key my-key")
74 require.NoError(t, err)
76 call := autoflowtest.Received(t, calls)
77 assert.Equal(t, "stdin-token", call.Req.GetChannelToken())
78 assert.Equal(t, "my-key", call.Req.GetIdempotencyKey())
79}
81func TestSend_RelayError(t *testing.T) {
82 t.Parallel()
84 relay := autoflowtest.NewRelay(t)
85 expectSend(relay, status.Error(codes.Unavailable, "connection reset"))
86 exec, _ := autoflowtest.NewTestExec(t, NewCmd)
88 _, err := exec(relay.Args() + tokenArgs(t) + " --value 1 --idempotency-key my-key")
89 require.Error(t, err)
90 assert.Equal(t, codes.Unavailable, status.Code(err))
91 assert.Contains(t, err.Error(), "failed to send the value (to retry without delivering it twice, pass --idempotency-key my-key)")
92}
94func TestSend_FlagErrors(t *testing.T) {
95 t.Parallel()
97 relay := autoflowtest.NewRelay(t)
98 workflowTokenFile := autoflowtest.WriteFile(t, "wf-token")
99 tests := []struct {
100 name string
101 args string
102 wantErr string
103 }{
104 {name: "no value", args: tokenArgs(t), wantErr: `required flag(s) "value" not set`},
105 {name: "invalid value", args: tokenArgs(t) + " --value nope", wantErr: "error parsing --value: invalid character"},
106 {name: "no channel token", args: " --workflow-token-file " + workflowTokenFile + " --value 1", wantErr: "pass --channel-token-file or set GLAB_AUTOFLOW_CHANNEL_TOKEN"},
107 {name: "both tokens from stdin", args: " --workflow-token-file - --channel-token-file - --value 1", wantErr: "only one of --workflow-token-file and --channel-token-file can read from standard input"},
108 }
109 for _, tc := range tests {
110 t.Run(tc.name, func(t *testing.T) {
111 t.Parallel()
113 exec, _ := autoflowtest.NewTestExec(t, NewCmd)
114 _, err := exec(relay.Args() + tc.args)
115 require.Error(t, err)
116 assert.Contains(t, err.Error(), tc.wantErr)
117 })
118 }
119}
Diagnostics9 git calls · 391 ms
CallStatusSizeTime0391 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 KiB2 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=UPDATING 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 · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 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
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit4 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=4 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 · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 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:0200173 B · hit4 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=173 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=9842465ba0bef0fe35148002069a98d0492349fa 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=173 git.pkt.payload.max=146 git.sideband.band1.bytes=146 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=146 git.pack.trailer=257bc2fe070f60e712099d7a0548db758c6c9692Open pack map
fetchtree:0200227 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=227 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=d9a7c66bd00e9fc74c41d266132b2171a7a82cdd 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=227 git.pkt.payload.max=200 git.sideband.band1.bytes=200 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=200 git.pack.trailer=19b86a461d3d30a55c16a56ca10faa7dc7d8b30dOpen pack map
fetchtree:0200136 B · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=372 git.response.bytes=136 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=47c4058afc901fedecae27c99fedc3c3d778ea59 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=136 git.pkt.payload.max=109 git.sideband.band1.bytes=109 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=109 git.pack.trailer=2db11d9facdd0c0469edfbd7d98d4852bf607e09Open pack map
fetch2001.5 KiB372 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=1519 git.response.cache=MISS git.response.duration_ms=372 git.fetch.wants=0f0efb6d3cce57196f39f03cb69033fb0a398e20 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=1519 git.pkt.payload.max=1492 git.sideband.band1.bytes=1492 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
- a5e2dbd53f08c125
- Request
- 7be734cd…
- 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