Skip to content
Merged
13 changes: 4 additions & 9 deletions comp/core/telemetry/telemetryimpl/prom_counter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestPromCounterInitializer(t *testing.T) {
Expand Down Expand Up @@ -41,18 +42,12 @@ func TestPromCounterInitializer(t *testing.T) {
counter.InitializeToZero("mycheck", "mystate")

endMetrics, err := promTelemetry.Gather()
if !assert.NoError(t, err) {
return
}
require.NoError(t, err)

if !assert.Equal(t, len(endMetrics), 1) {
return
}
require.Len(t, endMetrics, 1)

metricFamily := endMetrics[0]
if !assert.Equal(t, len(metricFamily.GetMetric()), 1) {
return
}
require.Len(t, metricFamily.GetMetric(), 1)

assert.Equal(t, metricFamily.GetName(), "subsystem_test")

Expand Down
8 changes: 2 additions & 6 deletions comp/core/telemetry/telemetryimpl/telemetry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,9 @@ func TestCounterInitializer(t *testing.T) {
counter.InitializeToZero("mycheck", "mystate")

startMetrics, err := telemetry.GetRegistry().Gather()
if !assert.NoError(t, err) {
return
}
require.NoError(t, err)

if !assert.Equal(t, len(startMetrics), 1) {
return
}
require.Equal(t, len(startMetrics), 1)

metrics, err := telemetry.GetCountMetric("subsystem", "test")
assert.NoError(t, err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"go.uber.org/fx"

"github.com/DataDog/datadog-agent/comp/core/config"
Expand Down Expand Up @@ -692,9 +693,8 @@ func TestProcessDifferentCmdline(t *testing.T) {
// Wait for first collection to complete
assert.EventuallyWithT(t, func(cT *assert.CollectT) {
actualProc, err := c.mockStore.GetProcess(pid)
if !assert.NoError(cT, err) || !assert.NotNil(cT, actualProc) {
return
}
require.NoError(cT, err)
require.NotNil(cT, actualProc)
assert.Equal(cT, []string{"bash"}, actualProc.Cmdline)
}, time.Second, time.Millisecond*100)

Expand All @@ -704,9 +704,8 @@ func TestProcessDifferentCmdline(t *testing.T) {
// After exec, the store should have htop, not bash
assert.EventuallyWithT(t, func(cT *assert.CollectT) {
actualProc, err := c.mockStore.GetProcess(pid)
if !assert.NoError(cT, err) || !assert.NotNil(cT, actualProc) {
return
}
require.NoError(cT, err)
require.NotNil(cT, actualProc)
// Critical assertion: cmdline should be updated to htop after exec
assert.Equal(cT, []string{"htop"}, actualProc.Cmdline, "Process cmdline should be updated after exec")
assert.Equal(cT, "htop", actualProc.Name, "Process name should be updated after exec")
Expand Down
5 changes: 2 additions & 3 deletions pkg/collector/check/stats/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/DataDog/datadog-agent/comp/core/telemetry/telemetryimpl"
healthplatformmock "github.com/DataDog/datadog-agent/comp/healthplatform/mock"
Expand Down Expand Up @@ -95,9 +96,7 @@ func TestNewStatsStateTelemetryInitialized(t *testing.T) {
NewStats(newMockCheck(), healthplatformmock.Mock(t))

tlmData, err := getTelemetryData()
if !assert.NoError(t, err) {
return
}
require.NoError(t, err)

assert.Contains(
t,
Expand Down
5 changes: 2 additions & 3 deletions pkg/collector/corechecks/gpu/integrationtests/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,8 @@ func assertMetricCase(t *testing.T, metricsByName map[string][]mock.Call, tc met
t.Helper()

calls, ok := metricsByName[tc.name]
if !assert.True(t, ok, "%s metric should be present", tc.name) || !assert.NotEmpty(t, calls, "No calls found for metric %s", tc.name) {
return
}
require.True(t, ok, "%s metric should be present", tc.name)
require.NotEmpty(t, calls, "No calls found for metric %s", tc.name)

for _, call := range calls {
value := call.Arguments[1].(float64)
Expand Down
13 changes: 4 additions & 9 deletions pkg/compliance/dbconfig/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/DataDog/datadog-agent/pkg/compliance/types"
"github.com/shirou/gopsutil/v4/process"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestDBConfLoader(t *testing.T) {
Expand Down Expand Up @@ -599,9 +600,7 @@ include_dir 'conf.d'
defer stop()

c, ok := LoadPostgreSQLConfig(context.Background(), hostroot, proc)
if !assert.True(t, ok) {
return
}
require.True(t, ok)

configData := c.ConfigData.(map[string]string)
assert.Equal(t, "100", configData["max_connections"])
Expand Down Expand Up @@ -665,9 +664,7 @@ work_mem = 16MB

c, ok := LoadPostgreSQLConfig(context.Background(), hostroot, proc)
assert.True(t, ok)
if !assert.NotNil(t, c) {
return
}
require.NotNil(t, c)

configData := c.ConfigData.(map[string]string)
assert.Equal(t, "200", configData["max_connections"])
Expand Down Expand Up @@ -698,9 +695,7 @@ include 'circular.conf'

c, ok := LoadPostgreSQLConfig(context.Background(), hostroot, proc)
assert.True(t, ok)
if !assert.NotNil(t, c) {
return
}
require.NotNil(t, c)
// Should not crash, circular protection should kick in
configData := c.ConfigData.(map[string]string)
assert.Equal(t, "100", configData["max_connections"])
Expand Down
6 changes: 2 additions & 4 deletions pkg/dyninst/end_to_end_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,14 +306,12 @@ func runE2ETest(t *testing.T, cfg e2eTestConfig) {
makeTargetStatus(uploader.StatusEmitting, expectedProbeIDs...),
)

assertModuleStats := func(t assert.TestingT, expected actuator.Metrics) {
assertModuleStats := func(t require.TestingT, expected actuator.Metrics) {
stats := ts.module.GetStats()["actuator"].(map[string]any)
exp := expected.AsStats()
gotKeys := slices.Sorted(maps.Keys(stats))
expectedKeys := slices.Sorted(maps.Keys(exp))
if !assert.Equal(t, gotKeys, expectedKeys) {
return
}
require.Equal(t, gotKeys, expectedKeys)
for _, key := range gotKeys {
assert.Equal(t, exp[key], stats[key], "key %s", key)
}
Expand Down
4 changes: 1 addition & 3 deletions pkg/dyninst/missing_type_recompilation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,7 @@ func TestMissingTypeRecompilation(t *testing.T) {
require.EventuallyWithT(t, func(c *assert.CollectT) {
stats := m.GetStats()
actuatorStats, ok := stats["actuator"].(map[string]any)
if !assert.True(c, ok, "actuator stats missing") {
return
}
require.True(c, ok, "actuator stats missing")
assert.Equal(c, uint64(0), actuatorStats["numPrograms"],
"expected numPrograms == 0 after cleanup")
assert.Equal(c, uint64(0), actuatorStats["numProcesses"],
Expand Down
23 changes: 6 additions & 17 deletions pkg/dyninst/procsubscribe/procscan/proc_stat_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -95,28 +94,18 @@ func testCases(t testing.TB) iter.Seq[testCase] {
filename := filepath.Base(entry)
name := strings.TrimSuffix(filename, ".txt")
expected, ok := expectedValues[name]
if !assert.True(t, ok,
"test file %s found but no expected values defined", name,
) {
return
}
require.True(t, ok,
"test file %s found but no expected values defined", name)
delete(expectedValues, name)
data, err := testdataFS.ReadFile(entry)
if !assert.NoError(t, err, "failed to read test data") {
return
}
require.NoError(t, err, "failed to read test data")
procRoot := filepath.Join(tempDir, name, "proc")
pidDir := filepath.Join(procRoot, strconv.FormatInt(int64(expected.pid), 10))
if !assert.NoError(t, os.MkdirAll(pidDir, 0o755)) {
return
}
require.NoError(t, os.MkdirAll(pidDir, 0o755))
statPath := filepath.Join(pidDir, "stat")
if !assert.NoError(
require.NoError(
t,
os.WriteFile(statPath, data, 0o644),
) {
return
}
os.WriteFile(statPath, data, 0o644))
if !yield(testCase{
name: name,
expected: expected,
Expand Down
13 changes: 4 additions & 9 deletions pkg/fleet/installer/setup/config/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/unicode"
)
Expand Down Expand Up @@ -362,9 +363,7 @@ api_key: newkey
// The config file may be UTF-16 on Windows
t.Run(tc.name+" (UTF-16)", func(t *testing.T) {
encoded, err := unicode.UTF16(unicode.LittleEndian, unicode.ExpectBOM).NewEncoder().String(tc.initialYAML)
if !assert.NoError(t, err) {
return
}
require.NoError(t, err)
tc.initialYAML = encoded
runWriteConfigTestCase(t, tc)
})
Expand Down Expand Up @@ -425,14 +424,10 @@ func TestEnsureUTF8(t *testing.T) {
for _, e := range encodings {
// encode input to new encoding
encoded, err := e.NewEncoder().Bytes(input)
if !assert.NoError(t, err) {
return
}
require.NoError(t, err)
// convert it back to UTF-8
output, err := ensureUTF8(encoded)
if !assert.NoError(t, err) {
return
}
require.NoError(t, err)
assert.Equal(t, input, output)
// assert output does not contain BOM
assert.False(t, bytes.HasPrefix(output, []byte{0xFF, 0xFE}), "output should not have UTF-16LE BOM")
Expand Down
34 changes: 9 additions & 25 deletions pkg/network/tracer/tracer_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,7 @@ func (s *TracerSuite) TestTCPRemoveEntries() {
conns, cleanup := getConnections(ct, tr)
defer cleanup()
conn, ok := findConnection(c2.LocalAddr(), c2.RemoteAddr(), conns)
if !assert.True(ct, ok) {
return
}
require.True(ct, ok)
assert.Equal(ct, clientMessageSize, int(conn.Monotonic.SentBytes))
assert.Equal(ct, 0, int(conn.Monotonic.RecvBytes))
assert.Equal(ct, 0, int(conn.Monotonic.Retransmits))
Expand Down Expand Up @@ -324,9 +322,7 @@ func (s *TracerSuite) TestTCPRTT() {
allConnections, cleanup := getConnections(ct, tr)
defer cleanup()
conn, ok := findConnection(c.LocalAddr(), c.RemoteAddr(), allConnections)
if !assert.True(ct, ok) {
return
}
require.True(ct, ok)

if cfg.EnableEbpfless {
timeoutUs := uint32((10 * time.Second).Microseconds())
Expand Down Expand Up @@ -493,18 +489,14 @@ func (s *TracerSuite) TestConntrackExpiration() {
var conn *network.ConnectionStats
require.EventuallyWithT(t, func(collect *assert.CollectT) {
_, err = c.Write([]byte("ping\n"))
if !assert.NoError(collect, err, "error sending data to server") {
return
}
require.NoError(collect, err, "error sending data to server")

connections, cleanup := getConnections(collect, tr)
defer cleanup()
t.Log(connections) // for debugging failures
var ok bool
conn, ok = findConnection(c.LocalAddr(), c.RemoteAddr(), connections)
if !assert.True(collect, ok, "connection not found") {
return
}
require.True(collect, ok, "connection not found")
assert.NotNil(collect, tr.conntracker.GetTranslationForConn(&conn.ConnectionTuple), "connection does not have NAT translation")
}, 3*time.Second, 100*time.Millisecond, "failed to find connection translation")

Expand Down Expand Up @@ -662,9 +654,7 @@ func (s *TracerSuite) TestUnconnectedUDPSendIPv6() {
}
return cs.DPort == uint16(remoteAddr.Port)
})
if !assert.Len(ct, outgoing, 1) {
return
}
require.Len(ct, outgoing, 1)
assert.Equal(ct, remoteAddr.IP.String(), outgoing[0].Dest.String())
assert.Equal(ct, bytesSent, int(outgoing[0].Monotonic.SentBytes))
}, 3*time.Second, 100*time.Millisecond)
Expand Down Expand Up @@ -2079,10 +2069,8 @@ func (s *TracerSuite) TestBlockingReadCounts() {
return true
})

if !assert.NoError(collect, err, "error reading from connection") ||
!assert.NoError(collect, readErr, "error from raw conn") {
return
}
require.NoError(collect, err, "error reading from connection")
require.NoError(collect, readErr, "error from raw conn")

read += n
t.Logf("read %d", read)
Expand Down Expand Up @@ -2150,9 +2138,7 @@ func (s *TracerSuite) TestPreexistingConnectionDirection() {

require.NotNil(collect, outgoing)
require.NotNil(collect, incoming)
if !assert.True(collect, incoming != nil && outgoing != nil) {
return
}
require.True(collect, incoming != nil && outgoing != nil)

m := outgoing.Monotonic
// skip byte counts in ebpfless: for ebpfless pre-existing connections,
Expand Down Expand Up @@ -3328,9 +3314,7 @@ func (s *TracerSuite) TestTCPRetransmitSyncOnClose() {
defer cleanup()

conn, ok := findConnection(c.LocalAddr(), c.RemoteAddr(), conns)
if !assert.True(ct, ok, "connection not found") {
return
}
require.True(ct, ok, "connection not found")

// We expect retransmits > 0
assert.Greater(ct, int(conn.Monotonic.Retransmits), 0, "should have retransmits")
Expand Down
Loading