Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

strip \r and \n when reading the file /etc/host_hostname #335

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions adapters/syslog/syslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net"
"os"
"strconv"
"strings"
"syscall"
"text/template"
"time"
Expand Down Expand Up @@ -70,9 +71,9 @@ func NewSyslogAdapter(route *router.Route) (router.LogAdapter, error) {
priority := getopt("SYSLOG_PRIORITY", "{{.Priority}}")
pid := getopt("SYSLOG_PID", "{{.Container.State.Pid}}")

content, err := ioutil.ReadFile("/etc/host_hostname") // just pass the file name
if err == nil && len(content) > 0{
hostname = string(content) // convert content to a 'string'
content, err := ioutil.ReadFile("/etc/host_hostname")
if err == nil && len(content) > 0 {
hostname = strings.TrimRight(string(content), "\r\n")
} else {
hostname = getopt("SYSLOG_HOSTNAME", "{{.Container.Config.Hostname}}")
}
Expand Down
41 changes: 41 additions & 0 deletions adapters/syslog/syslog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"bufio"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"os"
"strconv"
"strings"
"sync"
"testing"
"text/template"
Expand Down Expand Up @@ -41,8 +43,46 @@ var (
}
testTmplStr = fmt.Sprintf("<%s>%s %s %s[%s]: %s\n",
testPriority, testTimestamp, testHostname, testTag, testPid, testData)
hostHostnameFilename = "/etc/host_hostname"
hostHostnameContentWithoutLinefeed = "hostname"
hostHostnameContent = "hostname\r\n"
)

func TestHostnameDoesNotHaveLineFeed(t *testing.T) {
err := ioutil.WriteFile(hostHostnameFilename, []byte(hostHostnameContent), 0777)
if err != nil {
t.Fatal(err)
}
done := make(chan string)
addr, sock, srvWG := startServer("tcp", "", done)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like that broke the reconnect test. No need to spin up the server. I'd just move the hostname logic to it's own function and test that specifically.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, I'm sorry, I really don't have time to do this. Maybe someone with more experience in go should do this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries. I'll take it from here. Thanks for the initial contribution!

defer srvWG.Wait()
defer os.Remove(addr)
defer sock.Close()
route := &router.Route{Adapter: "syslog+tcp", Address: addr}
adapter, err := NewSyslogAdapter(route)
if err != nil {
t.Fatal(err)
}

msg := &Message{
Message: &router.Message{
Container: container,
Data: "test",
Time: time.Now(),
Source: "stdout",
},
}
b, _ := msg.Render(adapter.(*Adapter).tmpl)
templateString := string(b)

if strings.Contains(templateString, hostHostnameContent) {
t.Errorf("expected hostname to be %s got %s in tmp %s", hostHostnameContentWithoutLinefeed, hostHostnameContent, templateString)
}
if ! strings.Contains(templateString, hostHostnameContentWithoutLinefeed) {
t.Errorf("hostname in template string does not contain the correct hostname in tpl %s. Should be %s", templateString, hostHostnameContentWithoutLinefeed)
}
}

func TestSyslogRetryCount(t *testing.T) {
newRetryCount := uint(20)
os.Setenv("RETRY_COUNT", strconv.Itoa(int(newRetryCount)))
Expand Down Expand Up @@ -173,3 +213,4 @@ func check(t *testing.T, tmpl *template.Template, in string, out string) {
t.Errorf("expected: %s\ngot: %s\n", in, out)
}
}