github.com/observiq/carbon@v0.9.11-0.20200820160507-1b872e368a5e/operator/helper/host_identifier_test.go (about) 1 package helper 2 3 import ( 4 "testing" 5 6 "github.com/observiq/carbon/entry" 7 "github.com/stretchr/testify/require" 8 ) 9 10 func MockHostIdentifierConfig(includeIP, includeHostname bool, ip, hostname string) HostIdentifierConfig { 11 return HostIdentifierConfig{ 12 IncludeIP: includeIP, 13 IncludeHostname: includeHostname, 14 getIP: func() (string, error) { return ip, nil }, 15 getHostname: func() (string, error) { return hostname, nil }, 16 } 17 } 18 19 func TestHostLabeler(t *testing.T) { 20 cases := []struct { 21 name string 22 config HostIdentifierConfig 23 expectedResource map[string]string 24 }{ 25 { 26 "HostnameAndIP", 27 MockHostIdentifierConfig(true, true, "ip", "hostname"), 28 map[string]string{ 29 "hostname": "hostname", 30 "ip": "ip", 31 }, 32 }, 33 { 34 "HostnameNoIP", 35 MockHostIdentifierConfig(false, true, "ip", "hostname"), 36 map[string]string{ 37 "hostname": "hostname", 38 }, 39 }, 40 { 41 "IPNoHostname", 42 MockHostIdentifierConfig(true, false, "ip", "hostname"), 43 map[string]string{ 44 "ip": "ip", 45 }, 46 }, 47 { 48 "NoHostnameNoIP", 49 MockHostIdentifierConfig(false, false, "", "test"), 50 nil, 51 }, 52 } 53 54 for _, tc := range cases { 55 t.Run(tc.name, func(t *testing.T) { 56 identifier, err := tc.config.Build() 57 require.NoError(t, err) 58 59 e := entry.New() 60 identifier.Identify(e) 61 require.Equal(t, tc.expectedResource, e.Resource) 62 }) 63 } 64 }