Skip to content

Commit

Permalink
test: add containerID checks to test
Browse files Browse the repository at this point in the history
  • Loading branch information
Yumasi committed Oct 9, 2024
1 parent 1f3f296 commit 57ebe91
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
40 changes: 40 additions & 0 deletions pkg/collector/corechecks/servicediscovery/cp_stub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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-present Datadog, Inc.

//go:build linux

package servicediscovery

import (
"time"

model "github.com/DataDog/agent-payload/v5/process"

proccontainers "github.com/DataDog/datadog-agent/pkg/process/util/containers"
)

type containerProviderStub struct {
pidToCid map[int]string
}

func newContainerProviderStub(targetPIDs []int) proccontainers.ContainerProvider {
pidToCid := make(map[int]string)

for _, pid := range targetPIDs {
pidToCid[pid] = "abcd"
}

return &containerProviderStub{
pidToCid: pidToCid,
}
}

func (*containerProviderStub) GetContainers(_ time.Duration, _ map[string]*proccontainers.ContainerRateMetrics) ([]*model.Container, map[string]*proccontainers.ContainerRateMetrics, map[int]string, error) {
return nil, nil, nil, nil
}

func (s *containerProviderStub) GetPidToCid(_ time.Duration) map[int]string {
return s.pidToCid
}
28 changes: 26 additions & 2 deletions pkg/collector/corechecks/servicediscovery/impl_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type testProc struct {
}

var (
bootTimeSeconds = uint64(time.Date(2000, 01, 01, 0, 0, 0, 0, time.UTC).Unix())
bootTimeSeconds = uint64(time.Date(2000, 0o1, 0o1, 0, 0, 0, 0, time.UTC).Unix())
// procLaunched is number of clicks (100 per second) since bootTime when the process started
// assume it's 12 hours later
procLaunchedSeconds = bootTimeSeconds + uint64((12 * time.Hour).Seconds())
Expand Down Expand Up @@ -172,6 +172,16 @@ func Test_linuxImpl(t *testing.T) {
time time.Time
}

collectTargetPIDs := func(checkRuns []*checkRun) []int {
targetPIDs := make([]int, 0)
for _, cr := range checkRuns {
for _, service := range cr.servicesResp.Services {
targetPIDs = append(targetPIDs, service.PID)
}
}
return targetPIDs
}

tests := []struct {
name string
checkRun []*checkRun
Expand Down Expand Up @@ -232,6 +242,7 @@ func Test_linuxImpl(t *testing.T) {
APMInstrumentation: "none",
RSSMemory: 100 * 1024 * 1024,
CPUCores: 1.5,
ContainerID: "abcd",
},
},
{
Expand All @@ -254,6 +265,7 @@ func Test_linuxImpl(t *testing.T) {
APMInstrumentation: "none",
RSSMemory: 200 * 1024 * 1024,
CPUCores: 1.5,
ContainerID: "abcd",
},
},
{
Expand All @@ -276,6 +288,7 @@ func Test_linuxImpl(t *testing.T) {
APMInstrumentation: "none",
RSSMemory: 200 * 1024 * 1024,
CPUCores: 1.5,
ContainerID: "abcd",
},
},
{
Expand All @@ -294,6 +307,7 @@ func Test_linuxImpl(t *testing.T) {
PID: 500,
ServiceLanguage: "python",
CommandLine: pythonCommandLine,
ContainerID: "abcd",
},
},
{
Expand All @@ -312,6 +326,7 @@ func Test_linuxImpl(t *testing.T) {
PID: 500,
ServiceLanguage: "python",
CommandLine: pythonCommandLine,
ContainerID: "abcd",
},
},
},
Expand Down Expand Up @@ -366,6 +381,7 @@ func Test_linuxImpl(t *testing.T) {
Ports: []uint16{5432},
PID: 101,
CommandLine: []string{"test-service-1"},
ContainerID: "abcd",
},
},
{
Expand All @@ -388,6 +404,7 @@ func Test_linuxImpl(t *testing.T) {
APMInstrumentation: "none",
RSSMemory: 100 * 1024 * 1024,
CPUCores: 1.5,
ContainerID: "abcd",
},
},
{
Expand All @@ -405,6 +422,7 @@ func Test_linuxImpl(t *testing.T) {
Ports: []uint16{5432},
PID: 101,
CommandLine: []string{"test-service-1"},
ContainerID: "abcd",
},
},
{
Expand All @@ -422,6 +440,7 @@ func Test_linuxImpl(t *testing.T) {
Ports: []uint16{5432},
PID: 101,
CommandLine: []string{"test-service-1"},
ContainerID: "abcd",
},
},
{
Expand All @@ -444,6 +463,7 @@ func Test_linuxImpl(t *testing.T) {
APMInstrumentation: "none",
RSSMemory: 100 * 1024 * 1024,
CPUCores: 1.5,
ContainerID: "abcd",
},
},
},
Expand Down Expand Up @@ -501,6 +521,7 @@ func Test_linuxImpl(t *testing.T) {
APMInstrumentation: "none",
RSSMemory: 100 * 1024 * 1024,
CPUCores: 1.5,
ContainerID: "abcd",
},
},
{
Expand All @@ -521,6 +542,7 @@ func Test_linuxImpl(t *testing.T) {
PID: 102,
CommandLine: []string{"test-service-1"},
APMInstrumentation: "injected",
ContainerID: "abcd",
},
},
},
Expand All @@ -533,7 +555,9 @@ func Test_linuxImpl(t *testing.T) {
defer ctrl.Finish()

// check and mocks setup
check := newCheck()
targetPIDs := collectTargetPIDs(tc.checkRun)
cpStub := newContainerProviderStub(targetPIDs)
check := newCheck(cpStub)

mSender := mocksender.NewMockSender(check.ID())
mSender.SetupAcceptAll()
Expand Down

0 comments on commit 57ebe91

Please sign in to comment.