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
|
package agent
import (
"context"
"net/http"
"net/url"
"testing"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/tool/testing/matcher"
"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/tool/testing/mock_stdlib"
)
func TestReconcileTrigger_WithKubeProxyApiUrl(t *testing.T) {
// GIVEN
ctrl := gomock.NewController(t)
dummyKubeApiUrl := &url.URL{Scheme: "http", Host: "localhost", Path: "kubeapi"}
mockRoundTripper := mock_stdlib.NewMockRoundTripper(ctrl)
cfgUrl := "/api/v1/namespaces/flux-system/services/http:webhook-receiver:80/proxy"
expectedUrl := &url.URL{Scheme: "http", Host: "localhost", Path: "/kubeapi/api/v1/namespaces/flux-system/services/http:webhook-receiver:80/proxy/some/webhook/path"}
// setup mock expectations
mockRoundTripper.EXPECT().RoundTrip(matcher.DoMatch(func(r *http.Request) bool {
return r.URL.String() == expectedUrl.String()
})).Return(&http.Response{StatusCode: http.StatusOK, Body: http.NoBody}, nil).Times(1)
// WHEN
rt, err := newGitRepositoryReconcileTrigger(cfgUrl, dummyKubeApiUrl, mockRoundTripper, nil)
require.NoError(t, err)
err = rt.reconcile(context.Background(), "/some/webhook/path")
// THEN
assert.NoError(t, err)
}
func TestReconcileTrigger_WithDefaultServiceUrl(t *testing.T) {
// GIVEN
ctrl := gomock.NewController(t)
mockRoundTripper := mock_stdlib.NewMockRoundTripper(ctrl)
expectedUrl := &url.URL{Scheme: "http", Host: "webhook-receiver.flux-system.svc.cluster.local", Path: "/some/webhook/path"}
// setup mock expectations
mockRoundTripper.EXPECT().RoundTrip(matcher.DoMatch(func(r *http.Request) bool {
return r.URL.String() == expectedUrl.String()
})).Return(&http.Response{StatusCode: http.StatusOK, Body: http.NoBody}, nil).Times(1)
// WHEN
rt, err := newGitRepositoryReconcileTrigger(defaultServiceApiBaseUrl, nil, nil, mockRoundTripper)
require.NoError(t, err)
err = rt.reconcile(context.Background(), "/some/webhook/path")
// THEN
assert.NoError(t, err)
}
func TestReconcileTrigger_WithCustomServiceUrl(t *testing.T) {
// GIVEN
ctrl := gomock.NewController(t)
mockRoundTripper := mock_stdlib.NewMockRoundTripper(ctrl)
expectedUrl := &url.URL{Scheme: "https", Host: "localhost", Path: "/some/webhook/path"}
// setup mock expectations
mockRoundTripper.EXPECT().RoundTrip(matcher.DoMatch(func(r *http.Request) bool {
return r.URL.String() == expectedUrl.String()
})).Return(&http.Response{StatusCode: http.StatusOK, Body: http.NoBody}, nil).Times(1)
// WHEN
rt, err := newGitRepositoryReconcileTrigger("https://localhost", nil, nil, mockRoundTripper)
require.NoError(t, err)
err = rt.reconcile(context.Background(), "/some/webhook/path")
// THEN
assert.NoError(t, err)
}
func TestReconcileTrigger_Failure(t *testing.T) {
// GIVEN
ctrl := gomock.NewController(t)
mockRoundTripper := mock_stdlib.NewMockRoundTripper(ctrl)
// setup mock expectations
mockRoundTripper.EXPECT().RoundTrip(gomock.Any()).Return(&http.Response{StatusCode: http.StatusUnauthorized, Status: "401 Unauthorized", Body: http.NoBody}, nil).Times(1)
// WHEN
rt, err := newGitRepositoryReconcileTrigger("https://localhost", nil, nil, mockRoundTripper)
require.NoError(t, err)
err = rt.reconcile(context.Background(), "/some/webhook/path")
// THEN
assert.ErrorContains(t, err, "trigger to \"https://localhost/some/webhook/path\" returned status \"401 Unauthorized\"")
}
|