-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathhostname_test.go
130 lines (105 loc) · 3.35 KB
/
hostname_test.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// 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 2016-present Datadog, Inc.
//go:build !serverless
// +build !serverless
package util
import (
"context"
"io/ioutil"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/DataDog/datadog-agent/pkg/config"
"github.com/DataDog/datadog-agent/pkg/util/cache"
)
func TestGetHostnameFromHostnameConfig(t *testing.T) {
clearCache()
config.Datadog.Set("hostname", "expectedhostname")
config.Datadog.Set("hostname_file", "")
defer cleanUpConfigValues()
hostname, err := GetHostname(context.TODO())
if !assert.Nil(t, err) {
return
}
assert.Equal(t, "expectedhostname", hostname)
}
func TestGetHostnameCaching(t *testing.T) {
clearCache()
config.Datadog.Set("hostname", "expectedhostname")
config.Datadog.Set("hostname_file", "badhostname")
defer cleanUpConfigValues()
hostname, err := GetHostname(context.TODO())
if !assert.Nil(t, err) {
return
}
assert.Equal(t, "expectedhostname", hostname)
config.Datadog.Set("hostname", "newhostname")
hostname, err = GetHostname(context.TODO())
if !assert.Nil(t, err) {
return
}
assert.Equal(t, "expectedhostname", hostname)
}
func TestGetHostnameFromHostnameFileConfig(t *testing.T) {
hostnameFile, err := writeTempHostnameFile("expectedfilehostname")
if !assert.Nil(t, err) {
return
}
defer os.RemoveAll(hostnameFile)
config.Datadog.Set("hostname", "")
config.Datadog.Set("hostname_file", hostnameFile)
defer cleanUpConfigValues()
hostname, err := GetHostname(context.TODO())
if !assert.Nil(t, err) {
return
}
assert.Equal(t, "expectedfilehostname", hostname)
}
func TestNormalizeHost(t *testing.T) {
assert := assert.New(t)
long := strings.Repeat("a", 256)
hostname, err := NormalizeHost(long)
assert.NotNil(err, "long host should return error")
assert.Len(hostname, 0, "long host should be dropped")
hostname, err = NormalizeHost("a<b")
assert.Nil(err, "< should not return error")
assert.Equal("a-b", hostname, "< defanged")
hostname, err = NormalizeHost("a>b")
assert.Nil(err, "> should not return error")
assert.Equal("a-b", hostname, "> defanged")
hostname, err = NormalizeHost("example.com\r\n")
assert.Nil(err, "NL/CR should not return error")
assert.Equal("example.com", hostname, "NL/CR dropped")
// Invalid host name as bytes that would look like this: 9cbef2d1-8c20-4bf2-97a5-7d70��
b := []byte{
57, 99, 98, 101, 102, 50, 100, 49, 45, 56, 99, 50, 48, 45,
52, 98, 102, 50, 45, 57, 55, 97, 53, 45, 55, 100, 55, 48,
0, 0, 0, 0, 239, 191, 189, 239, 191, 189, 1, // these are bad bytes
}
hostname, err = NormalizeHost(string(b))
assert.NotNil(err, "null rune should return error")
assert.Equal("", hostname, "host with null rune should be dropped")
}
func writeTempHostnameFile(content string) (string, error) {
destFile, err := ioutil.TempFile("", "test-hostname-file-config-")
if err != nil {
return "", err
}
err = ioutil.WriteFile(destFile.Name(), []byte(content), os.ModePerm)
if err != nil {
os.RemoveAll(destFile.Name())
return "", err
}
return destFile.Name(), nil
}
func cleanUpConfigValues() {
clearCache()
config.Datadog.Set("hostname", "")
config.Datadog.Set("hostname_file", "")
}
func clearCache() {
cache.Cache.Flush()
}