main_test.go (3141B)
1 package main 2 3 import ( 4 "bytes" 5 "fmt" 6 "io" 7 "log" 8 "os" 9 "strings" 10 "sync" 11 "testing" 12 ) 13 14 // Borrowed from - https://medium.com/@hau12a1/golang-capturing-log-println-and-fmt-println-output-770209c791b4 15 // NOTE: NOT threadsafe due to swapping global os.Stdout/Stderr values 16 func captureOutput(f func(), captureStderr bool) string { 17 reader, writer, err := os.Pipe() 18 if err != nil { 19 panic(err) 20 } 21 22 // Save the current stdout and stderr, restore them on return 23 stdout := os.Stdout 24 stderr := os.Stderr 25 defer func() { 26 os.Stdout = stdout 27 os.Stderr = stderr 28 log.SetOutput(os.Stderr) 29 }() 30 31 // Capture the stdout and optionally stderr 32 os.Stdout = writer 33 if captureStderr { 34 os.Stderr = writer 35 } 36 37 // Switch the logger to use the writer 38 log.SetOutput(writer) 39 out := make(chan string) 40 wg := new(sync.WaitGroup) 41 wg.Add(1) 42 go func() { 43 var buf bytes.Buffer 44 wg.Done() 45 if _, err := io.Copy(&buf, reader); err != nil { 46 panic(err) 47 } 48 out <- buf.String() 49 }() 50 wg.Wait() 51 f() 52 writer.Close() 53 return <-out 54 } 55 56 func TestCaptureOutput(t *testing.T) { 57 // Capture just stdout 58 out := captureOutput(func() { 59 fmt.Println("capture stdout") 60 os.Stderr.WriteString("capture stderr\n") 61 }, false) 62 63 if strings.Contains(out, "capture stderr") { 64 t.Fatal("Should not contain stderr") 65 } 66 if !strings.Contains(out, "capture stdout") { 67 t.Fatal("Missing stdout only") 68 } 69 70 // Capture both stdout and stderr 71 out = captureOutput(func() { 72 fmt.Println("capture stdout") 73 os.Stderr.WriteString("capture stderr\n") 74 }, true) 75 76 if !strings.Contains(out, "capture stderr") { 77 t.Fatal("Missing stderr with stdout") 78 } 79 if !strings.Contains(out, "capture stdout") { 80 t.Fatal("Missing stdout with stderr") 81 } 82 } 83 84 func TestLogDebug(t *testing.T) { 85 // test with default config, no output 86 out := captureOutput(func() { 87 logDebugf("logging debug info") 88 }, false) 89 90 if strings.Contains(out, "logging debug info") { 91 t.Fatal("unexpected debug logging") 92 } 93 94 // set global debug to true 95 cmdline.Debug = true 96 out = captureOutput(func() { 97 logDebugf("logging debug info") 98 }, false) 99 cmdline.Debug = false 100 101 if !strings.Contains(out, "logging debug info") { 102 t.Fatal("Missing debug string") 103 } 104 } 105 106 func TestReadConfig(t *testing.T) { 107 // Empty config 108 r := bytes.NewReader([]byte("")) 109 cfg, err := readConfig(r) 110 if err != nil { 111 t.Fatalf("Error reading empty config: %s", err) 112 } 113 // Should be empty 114 if len(cfg.Hosts) > 0 || len(cfg.Emails) > 0 { 115 t.Fatalf("Config not empty: %#v", cfg) 116 } 117 118 // Config with hosts and emails 119 r = bytes.NewReader([]byte(` 120 hosts = ["192.168.101.0/24", "127.0.0.1"] 121 emails = ["root@frobozz.org", "admin@guetech.org"]`)) 122 cfg, err = readConfig(r) 123 if err != nil { 124 t.Fatalf("Error reading full config: %s", err) 125 } 126 if len(cfg.Hosts) != 2 || len(cfg.Emails) != 2 { 127 t.Fatalf("Wrong number of values in config: %#v", cfg) 128 } 129 if cfg.Hosts[0] != "192.168.101.0/24" || 130 cfg.Hosts[1] != "127.0.0.1" { 131 t.Fatalf("Hosts list is incorrect: %#v", cfg.Hosts) 132 } 133 if cfg.Emails[0] != "root@frobozz.org" || 134 cfg.Emails[1] != "admin@guetech.org" { 135 t.Fatalf("Emails list is incorrect: %#v", cfg.Emails) 136 } 137 138 }