forked from DataDog/datadog-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
origin_detection.go
94 lines (79 loc) · 2.82 KB
/
origin_detection.go
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
// 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 2018 Datadog, Inc.
// +build linux
package dogstatsd
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/DataDog/datadog-agent/pkg/config"
"github.com/DataDog/datadog-agent/pkg/dogstatsd/listeners"
"github.com/DataDog/datadog-agent/test/integration/utils"
)
const (
senderImg string = "datadog/test-origin-detection-sender:master"
)
// testUDSOriginDetection ensures UDS origin detection works, by submitting
// a metric from a container. As we need the origin PID to stay running,
// we can't just `netcat` to the socket, that's why we run a custom python
// script that will stay up after sending packets.
func testUDSOriginDetection(t *testing.T) {
// Detect whether we are containerised and set the socket path accordingly
var socketVolume string
var composeFile string
dir := os.ExpandEnv("$SCRATCH_VOLUME_PATH")
if dir == "" { // Running on the host
var err error
dir, err = ioutil.TempDir("", "dd-test-")
assert.Nil(t, err)
defer os.RemoveAll(dir) // clean up
socketVolume = dir
composeFile = "mount_path.compose"
} else { // Running in container
socketVolume = os.ExpandEnv("$SCRATCH_VOLUME_NAME")
composeFile = "mount_volume.compose"
}
socketPath := filepath.Join(dir, "dsd.socket")
config.Datadog.Set("dogstatsd_socket", socketPath)
config.Datadog.Set("dogstatsd_origin_detection", true)
// Start DSD
packetChannel := make(chan *listeners.Packet)
packetPool := listeners.NewPacketPool(config.Datadog.GetInt("dogstatsd_buffer_size"))
s, err := listeners.NewUDSListener(packetChannel, packetPool)
require.Nil(t, err)
go s.Listen()
defer s.Stop()
compose := &utils.ComposeConf{
ProjectName: "origin-detection-test",
FilePath: fmt.Sprintf("testdata/origin_detection/%s", composeFile),
Variables: map[string]string{"socket_dir_path": socketVolume},
}
output, err := compose.Start()
defer compose.Stop()
require.Nil(t, err, string(output))
containers, err := compose.ListContainers()
require.Nil(t, err)
require.True(t, len(containers) > 0)
containerId := containers[0]
require.Equal(t, 64, len(containerId))
t.Logf("Running sender container: %s", containerId)
stopCmd := exec.Command("docker", "stop", containerId)
defer stopCmd.Run()
select {
case packet := <-packetChannel:
require.NotNil(t, packet)
require.Equal(t, string(packet.Contents), "custom_counter1:1|c")
require.Equal(t, packet.Origin, fmt.Sprintf("docker://%s", containerId))
packetPool.Put(packet)
case <-time.After(2 * time.Second):
assert.FailNow(t, "Timeout on receive channel")
}
}