From a9aba613518983f3d9c9fa45c1ae8e8740756953 Mon Sep 17 00:00:00 2001 From: Tony Redondo Date: Thu, 24 Oct 2024 15:39:36 +0200 Subject: [PATCH] internal/civisibility: more tests --- .../coverage/coverage_payload_test.go | 99 +++++++++++++++++++ .../gotesting/coverage/reflections_test.go | 5 + 2 files changed, 104 insertions(+) create mode 100644 internal/civisibility/integrations/gotesting/coverage/coverage_payload_test.go diff --git a/internal/civisibility/integrations/gotesting/coverage/coverage_payload_test.go b/internal/civisibility/integrations/gotesting/coverage/coverage_payload_test.go new file mode 100644 index 0000000000..93cda93510 --- /dev/null +++ b/internal/civisibility/integrations/gotesting/coverage/coverage_payload_test.go @@ -0,0 +1,99 @@ +// Unless explicitly stated otherwise all files in this repository are licensed +// under the Apache License Version 2.0. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright 2024 Datadog, Inc. + +package coverage + +import ( + "bytes" + "encoding/json" + "github.com/stretchr/testify/assert" + "github.com/tinylib/msgp/msgp" + "io" + "strconv" + "testing" +) + +func newCoverageData(n int) []*ciTestCoverageData { + list := make([]*ciTestCoverageData, n) + for i := 0; i < n; i++ { + cov := newCiTestCoverageData(NewTestCoverage(uint64(i), uint64(i), uint64(i), uint64(i), "").(*testCoverage)) + list[i] = cov + } + + return list +} + +// TestCoveragePayloadIntegrity tests that whatever we push into the payload +// allows us to read the same content as would have been encoded by +// the codec. +func TestCoveragePayloadIntegrity(t *testing.T) { + want := new(bytes.Buffer) + for _, n := range []int{10, 1 << 10, 1 << 17} { + t.Run(strconv.Itoa(n), func(t *testing.T) { + assert := assert.New(t) + p := newCoveragePayload() + var allEvents ciTestCoverages + + for i := 0; i < n; i++ { + list := newCoverageData(i%5 + 1) + allEvents = append(allEvents, list...) + for _, event := range list { + p.push(event) + } + } + + want.Reset() + err := msgp.Encode(want, allEvents) + assert.NoError(err) + assert.Equal(want.Len(), p.size()) + assert.Equal(p.itemCount(), len(allEvents)) + + got, err := io.ReadAll(p) + assert.NoError(err) + assert.Equal(want.Bytes(), got) + }) + } +} + +// TestCoveragePayloadDecode ensures that whatever we push into the payload can +// be decoded by the codec. +func TestCoveragePayloadDecode(t *testing.T) { + assert := assert.New(t) + for _, n := range []int{10, 1 << 10} { + t.Run(strconv.Itoa(n), func(t *testing.T) { + p := newCoveragePayload() + for i := 0; i < n; i++ { + list := newCoverageData(i%5 + 1) + for _, event := range list { + p.push(event) + } + } + var got ciTestCoverages + err := msgp.Decode(p, &got) + assert.NoError(err) + }) + } +} + +func TestCoveragePayloadEnvelope(t *testing.T) { + assert := assert.New(t) + p := newCoveragePayload() + encodedBuf, err := p.getBuffer() + assert.NoError(err) + + // Convert the message pack to json + jsonBuf := new(bytes.Buffer) + _, err = msgp.CopyToJSON(jsonBuf, encodedBuf) + assert.NoError(err) + + // Decode the json payload + var testCyclePayload ciTestCovPayload + err = json.Unmarshal(jsonBuf.Bytes(), &testCyclePayload) + assert.NoError(err) + + // Now let's assert the decoded envelope metadata + assert.Equal(testCyclePayload.Version, int32(2)) + assert.Empty(testCyclePayload.Coverages) +} diff --git a/internal/civisibility/integrations/gotesting/coverage/reflections_test.go b/internal/civisibility/integrations/gotesting/coverage/reflections_test.go index 7edf4764d5..5e50d6e0c7 100644 --- a/internal/civisibility/integrations/gotesting/coverage/reflections_test.go +++ b/internal/civisibility/integrations/gotesting/coverage/reflections_test.go @@ -1,3 +1,8 @@ +// Unless explicitly stated otherwise all files in this repository are licensed +// under the Apache License Version 2.0. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright 2024 Datadog, Inc. + package coverage import (