From c4683b9cd8c525b1b06ad9326c04c903712c4516 Mon Sep 17 00:00:00 2001 From: Boshi Lian Date: Sat, 24 Feb 2018 18:41:34 +0800 Subject: [PATCH] add test for log --- sshpiperd/log_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 sshpiperd/log_test.go diff --git a/sshpiperd/log_test.go b/sshpiperd/log_test.go new file mode 100644 index 00000000..1ae928db --- /dev/null +++ b/sshpiperd/log_test.go @@ -0,0 +1,40 @@ +package main + +import ( + "io/ioutil" + "os" + "strings" + "testing" +) + +func TestCreateLogger(t *testing.T) { + + // flags + { + logger := loggerConfig{LogFlags: 2}.createLogger() + + if logger.Flags() != 2 { + t.Errorf("flags not set") + } + } + + // file + { + tmpfile, err := ioutil.TempFile("", "sshpiperlog") + if err != nil { + t.Errorf("failed to create tmp file %v", err) + } + defer os.Remove(tmpfile.Name()) // clean up + + logger := loggerConfig{LogFile: tmpfile.Name()}.createLogger() + + logger.Print("test123") + + s, _ := ioutil.ReadFile(tmpfile.Name()) + + if !strings.Contains(string(s), "test123") { + + t.Errorf("log failed") + } + } +}