GitLabgitlab-org
cli
main
1//go:build !integration
3package bootstrap
5import (
6 "errors"
7 "fmt"
8 "strings"
9 "testing"
11 "github.com/stretchr/testify/assert"
12 "github.com/stretchr/testify/require"
13 "go.uber.org/mock/gomock"
14)
16func TestFlux_createHelmRepositoryManifest(t *testing.T) {
17 // GIVEN
18 mockCmd, f := setupFlux(t)
20 mockCmd.EXPECT().RunWithOutput(
21 "flux", "create", "source", "helm", "helm-repository-name", "--export",
22 "-n=helm-repository-namespace", "--url=https://charts.gitlab.io").
23 Return([]byte("content"), nil)
25 actualFile, err := f.createHelmRepositoryManifest()
27 // THEN
28 require.NoError(t, err)
29 assert.Equal(t, "manifest-path/helm-repository-filepath", actualFile.path)
30 assert.Equal(t, actualFile.content, []byte("content"))
31}
33func TestFlux_createHelmRepositoryManifest_Failure(t *testing.T) {
34 // GIVEN
35 mockCmd, f := setupFlux(t)
37 mockCmd.EXPECT().RunWithOutput(
38 "flux", "create", "source", "helm", "helm-repository-name", "--export",
39 "-n=helm-repository-namespace", "--url=https://charts.gitlab.io").
40 Return(nil, errors.New("test"))
42 actualFile, err := f.createHelmRepositoryManifest()
44 // THEN
45 require.Error(t, err)
46 assert.Equal(t, file{}, actualFile)
47}
49func TestFlux_createHelmReReleaseManifest(t *testing.T) {
50 // GIVEN
51 mockCmd, f := setupFlux(t)
53 mockCmd.EXPECT().RunWithOutput(
54 "flux", "create", "helmrelease", "helm-release-name", "--export",
55 "-n=helm-release-namespace", "--target-namespace=helm-release-target-namespace",
56 "--create-target-namespace=true", "--source=HelmRepository/helm-repository-name.helm-repository-namespace",
57 "--chart=gitlab-agent", "--release-name=helm-release-name", StartsWith("--values="), "--values=helm-release-values-1", "--values=helm-release-values-2", "--values-from=helm-release-values-from-1").
58 Return([]byte("content"), nil)
60 actualFile, err := f.createHelmReleaseManifest("wss://kas.gitlab.example.com")
62 // THEN
63 require.NoError(t, err)
64 assert.Equal(t, "manifest-path/helm-release-filepath", actualFile.path)
65 assert.Equal(t, actualFile.content, []byte("content"))
66}
68func TestFlux_createHelmReReleaseManifest_Failure(t *testing.T) {
69 // GIVEN
70 mockCmd, f := setupFlux(t)
72 mockCmd.EXPECT().RunWithOutput(
73 "flux", "create", "helmrelease", "helm-release-name", "--export",
74 "-n=helm-release-namespace", "--target-namespace=helm-release-target-namespace",
75 "--create-target-namespace=true", "--source=HelmRepository/helm-repository-name.helm-repository-namespace",
76 "--chart=gitlab-agent", "--release-name=helm-release-name", StartsWith("--values="), "--values=helm-release-values-1", "--values=helm-release-values-2", "--values-from=helm-release-values-from-1").
77 Return([]byte(""), errors.New("test"))
79 actualFile, err := f.createHelmReleaseManifest("wss://kas.gitlab.example.com")
81 // THEN
82 require.Error(t, err)
83 assert.Equal(t, file{}, actualFile)
84}
86func TestFlux_reconcile(t *testing.T) {
87 // GIVEN
88 mockCmd, f := setupFlux(t)
90 gomock.InOrder(
91 mockCmd.EXPECT().Run("flux", "reconcile", "source", "flux-source-type", "flux-source-name", "-n=flux-source-namespace"),
92 mockCmd.EXPECT().RunWithOutput("flux", "get", "helmreleases", "helm-release-name", "-n=helm-release-namespace"),
93 mockCmd.EXPECT().Run("flux", "reconcile", "helmrelease", "helm-release-name", "-n=helm-release-namespace", "--with-source"),
94 )
96 // WHEN
97 _ = f.reconcile()
98}
100func TestFlux_reconcile_retries(t *testing.T) {
101 // GIVEN
102 mockCmd, f := setupFlux(t)
104 gomock.InOrder(
105 mockCmd.EXPECT().Run("flux", "reconcile", "source", "flux-source-type", "flux-source-name", "-n=flux-source-namespace"),
106 mockCmd.EXPECT().RunWithOutput("flux", "get", "helmreleases", "helm-release-name", "-n=helm-release-namespace").Return([]byte(`HelmRelease object 'helm-release-name' not found in "helm-release-namespace" namespace`), nil),
107 mockCmd.EXPECT().RunWithOutput("flux", "get", "helmreleases", "helm-release-name", "-n=helm-release-namespace"),
108 mockCmd.EXPECT().Run("flux", "reconcile", "helmrelease", "helm-release-name", "-n=helm-release-namespace", "--with-source"),
109 )
111 // WHEN
112 _ = f.reconcile()
113}
115func TestFlux_reconcile_abort_retry_max(t *testing.T) {
116 // GIVEN
117 mockCmd, f := setupFlux(t)
119 gomock.InOrder(
120 mockCmd.EXPECT().Run("flux", "reconcile", "source", "flux-source-type", "flux-source-name", "-n=flux-source-namespace"),
121 mockCmd.EXPECT().RunWithOutput("flux", "get", "helmreleases", "helm-release-name", "-n=helm-release-namespace").Return([]byte(`HelmRelease object 'helm-release-name' not found in "helm-release-namespace" namespace`), nil),
122 mockCmd.EXPECT().RunWithOutput("flux", "get", "helmreleases", "helm-release-name", "-n=helm-release-namespace").Return([]byte(`HelmRelease object 'helm-release-name' not found in "helm-release-namespace" namespace`), nil),
123 mockCmd.EXPECT().RunWithOutput("flux", "get", "helmreleases", "helm-release-name", "-n=helm-release-namespace").Return([]byte(`HelmRelease object 'helm-release-name' not found in "helm-release-namespace" namespace`), nil),
124 mockCmd.EXPECT().RunWithOutput("flux", "get", "helmreleases", "helm-release-name", "-n=helm-release-namespace").Return([]byte(`HelmRelease object 'helm-release-name' not found in "helm-release-namespace" namespace`), nil),
125 mockCmd.EXPECT().RunWithOutput("flux", "get", "helmreleases", "helm-release-name", "-n=helm-release-namespace").Return([]byte(`HelmRelease object 'helm-release-name' not found in "helm-release-namespace" namespace`), nil),
126 mockCmd.EXPECT().RunWithOutput("flux", "get", "helmreleases", "helm-release-name", "-n=helm-release-namespace").Return([]byte(`HelmRelease object 'helm-release-name' not found in "helm-release-namespace" namespace`), nil),
127 )
129 // WHEN
130 err := f.reconcile()
132 assert.Error(t, err)
133}
135func setupFlux(t *testing.T) (*MockCmd, FluxWrapper) {
136 t.Helper()
138 ctrl := gomock.NewController(t)
139 mockCmd := NewMockCmd(ctrl)
140 f := NewLocalFluxWrapper(
141 mockCmd,
142 "flux", "manifest-path",
143 "https://charts.gitlab.io",
144 "helm-repository-name", "helm-repository-namespace", "helm-repository-filepath",
145 "helm-release-name", "helm-release-namespace", "helm-release-filepath", "helm-release-target-namespace",
146 []string{"helm-release-values-1", "helm-release-values-2"}, []string{"helm-release-values-from-1"},
147 "flux-source-type", "flux-source-namespace", "flux-source-name",
148 )
149 fHack, ok := f.(*localFluxWrapper)
150 require.True(t, ok, "newFlux should return a *localFluxWrapper in tests")
151 fHack.reconcileRetryDelay = 0
153 return mockCmd, f
154}
156func StartsWith(prefix string) gomock.Matcher {
157 return &startsWithMatcher{prefix: prefix}
158}
160type startsWithMatcher struct {
161 prefix string
162 actualS string
163}
165func (m startsWithMatcher) Matches(arg any) bool {
166 s, ok := arg.(string)
167 if !ok {
168 return false
169 }
170 m.actualS = s
171 return strings.HasPrefix(m.actualS, m.prefix)
172}
174func (m startsWithMatcher) String() string {
175 return fmt.Sprintf("does not start with: %q, got %q", m.prefix, m.actualS)
176}
Diagnostics9 git calls · 218 ms
CallStatusSizeTime0218 ms
discover200185 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=1 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
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:0200200 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=200 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=9988d47506dcbe3004b8b72cbdb9d66d6d75cda4 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=200 git.pkt.payload.max=173 git.sideband.band1.bytes=173 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=173 git.pack.trailer=948ee807a1ee817af67f2df94421a202e82325f9Open pack map
fetchtree:0200405 B · hit2 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=405 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=d271680e941992259de0ff84d4a04bcbba55f531 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=405 git.pkt.payload.max=378 git.sideband.band1.bytes=378 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=378 git.pack.trailer=adfacba5ff1f4fcf28dcaac12e2874f6d48a7934Open pack map
fetchtree:0200414 B · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=204 git.response.bytes=414 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=3c487cc16dcfeca6d024165580948fbb3a622e23 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=414 git.pkt.payload.max=387 git.sideband.band1.bytes=387 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=387 git.pack.trailer=a621ec095c3ac7a33563831f77c04827c9c343d3Open pack map
fetch2001.2 KiB204 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=1279 git.response.cache=MISS git.response.duration_ms=204 git.fetch.wants=e6e2adcfe88b9e65e2035f7a3e4a006e86fd78e4 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=1279 git.pkt.payload.max=1252 git.sideband.band1.bytes=1252 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
- a5eb08c2c93c21ca
- Request
- 1547ae49…
- 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