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
|
package api
import (
"io"
"net/http"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/tool/testing/mock_gitlab"
"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/tool/testing/testhelpers"
"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/pkg/agentcfg"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/testing/protocmp"
)
func TestPostAgentConfiguration(t *testing.T) {
id := "bla"
config := &agentcfg.ConfigurationFile{
Gitops: &agentcfg.GitopsCF{
ManifestProjects: []*agentcfg.ManifestProjectCF{
{
Id: &id,
},
},
},
// don't need to test all fields, some is good enough
}
ctx, traceId := testhelpers.CtxWithSpanContext(t)
c := mock_gitlab.SetupClient(t, AgentConfigurationApiPath, func(w http.ResponseWriter, r *http.Request) {
testhelpers.AssertJWTSignature(t, r)
testhelpers.AssertRequestMethod(t, r, http.MethodPost)
testhelpers.AssertRequestContentTypeJson(t, r)
testhelpers.AssertCommonRequestParams(t, r, traceId)
data, err := io.ReadAll(r.Body)
if !assert.NoError(t, err) {
return
}
actual := &AgentConfigurationRequest{
AgentConfig: &agentcfg.ConfigurationFile{},
}
err = protojson.Unmarshal(data, actual)
if !assert.NoError(t, err) {
return
}
expected := &AgentConfigurationRequest{
AgentId: testhelpers.AgentId,
AgentConfig: config,
}
assert.Empty(t, cmp.Diff(expected, actual, protocmp.Transform()))
w.WriteHeader(http.StatusNoContent)
})
err := PostAgentConfiguration(ctx, c, testhelpers.AgentId, config)
require.NoError(t, err)
}
|