bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/host/manager_test.go (about) 1 package host 2 3 import ( 4 "strings" 5 "testing" 6 ) 7 8 func validateManager(m Manager, expectedHostname string, t *testing.T) { 9 expectedHostname = strings.ToLower(expectedHostname) 10 11 if m.GetHostName() != expectedHostname { 12 t.Errorf("Expected hostname to be '%s' but it was '%s'", expectedHostname, m.GetHostName()) 13 } else if m.GetHost().GetName() != expectedHostname { 14 t.Errorf("Expected hostname to be '%s' but it was '%s'", expectedHostname, m.GetHostName()) 15 } else if m.GetNameProcessor() == nil { 16 t.Errorf("Expected managed host to have a name processor") 17 } 18 } 19 20 func TestNewManager(t *testing.T) { 21 // we should save fqdn for NewManager since os.Hostname will return /proc/sys/kernel/hostname and it can be fqdn 22 // underhood NewManager will use same call, but if we don't save fqdn we are trying to split that name 23 // In that can this test will fail anyway 24 testHostnames := []struct { 25 hostname string 26 expected string 27 preserveFullHostName bool 28 }{ 29 {"test1", "test1", true}, 30 {"test1", "test1", false}, 31 {"test1.domain.com", "test1.domain.com", true}, 32 {"test1.domain.com", "test1", false}, 33 {"test-01.domain.com", "test-01", false}, 34 {"test-01.domain.com", "test-01.domain.com", true}, 35 {"test-103.subdomain1.subdomain2.domain.com", "test-103", false}, 36 {"test-103.subdomain1.subdomain2.domain.com", "test-103.subdomain1.subdomain2.domain.com", true}, 37 } 38 for _, params := range testHostnames { 39 hostname = func() (string, error) { return params.hostname, nil } 40 m, err := NewManager(params.preserveFullHostName) 41 if err != nil { 42 t.Errorf("Error while create new manager for %s: %v", params.hostname, err) 43 } 44 45 validateManager(m, params.expected, t) 46 } 47 48 } 49 50 func TestNewManagerForHostname(t *testing.T) { 51 expectedHostname := "JiMMYs-PC" 52 m, err := NewManagerForHostname(expectedHostname, false) 53 if err != nil { 54 t.Error(err) 55 } 56 57 validateManager(m, expectedHostname, t) 58 }