github.com/openshift/dpu-operator@v0.0.0-20240502153209-3af840d137c2/dpu-cni/pkgs/cnilogging/cnilogging_test.go (about) 1 package cnilogging 2 3 import ( 4 "fmt" 5 "io" 6 "os" 7 8 g "github.com/onsi/ginkgo/v2" 9 o "github.com/onsi/gomega" 10 ) 11 12 var _ = g.Describe("CNIlogging", func() { 13 var origStderr *os.File 14 var stderrFile *os.File 15 16 g.BeforeEach(func() { 17 var err error 18 stderrFile, err = os.CreateTemp("", "") 19 o.Expect(err).NotTo(o.HaveOccurred()) 20 origStderr = os.Stderr 21 os.Stderr = stderrFile 22 }) 23 24 g.AfterEach(func() { 25 os.Stderr = origStderr 26 o.Expect(stderrFile.Close()).To(o.Succeed()) 27 o.Expect(os.RemoveAll(stderrFile.Name())).To(o.Succeed()) 28 }) 29 30 g.Context("log argument prepender", func() { 31 g.When("none of netns, containerID, ifName are specified", func() { 32 g.BeforeEach(func() { 33 Init("", "", "", "", "") 34 }) 35 36 g.It("should only prepend the cniName", func() { 37 Panic("test message", "a", "b") 38 _, _ = stderrFile.Seek(0, 0) 39 out, err := io.ReadAll(stderrFile) 40 o.Expect(err).NotTo(o.HaveOccurred()) 41 o.Expect(out).Should(o.ContainSubstring(fmt.Sprintf(`%s="%s"`, labelCNIName, cniName))) 42 o.Expect(out).ShouldNot(o.ContainSubstring(labelContainerID)) 43 o.Expect(out).ShouldNot(o.ContainSubstring(labelNetNS)) 44 o.Expect(out).ShouldNot(o.ContainSubstring(labelIFName)) 45 }) 46 }) 47 48 g.When("netns, containerID and ifName are specified", func() { 49 const ( 50 testContainerID = "test-containerid" 51 testNetNS = "test-netns" 52 testIFName = "test-ifname" 53 ) 54 55 g.BeforeEach(func() { 56 Init("", "", testContainerID, testNetNS, testIFName) 57 }) 58 59 g.It("should log cniName, netns, containerID and ifName", func() { 60 Panic("test message", "a", "b") 61 _, _ = stderrFile.Seek(0, 0) 62 out, err := io.ReadAll(stderrFile) 63 o.Expect(err).NotTo(o.HaveOccurred()) 64 o.Expect(out).Should(o.ContainSubstring(fmt.Sprintf(`%s="%s"`, labelCNIName, cniName))) 65 o.Expect(out).Should(o.ContainSubstring(fmt.Sprintf(`%s="%s"`, labelContainerID, testContainerID))) 66 o.Expect(out).Should(o.ContainSubstring(fmt.Sprintf(`%s="%s"`, labelNetNS, testNetNS))) 67 o.Expect(out).Should(o.ContainSubstring(fmt.Sprintf(`%s="%s"`, labelIFName, testIFName))) 68 }) 69 }) 70 }) 71 72 g.Context("log levels", func() { 73 g.When("the defaults are used", func() { 74 g.BeforeEach(func() { 75 Init("", "", "", "", "") 76 }) 77 78 g.It("panic messages are logged to stderr", func() { 79 Panic("test message", "a", "b") 80 _, _ = stderrFile.Seek(0, 0) 81 out, err := io.ReadAll(stderrFile) 82 o.Expect(err).NotTo(o.HaveOccurred()) 83 o.Expect(out).Should(o.ContainSubstring("test message")) 84 }) 85 86 g.It("info messages are logged to stderr and look as expected", func() { 87 Info("test message", "a", "b") 88 _, _ = stderrFile.Seek(0, 0) 89 out, err := io.ReadAll(stderrFile) 90 o.Expect(err).NotTo(o.HaveOccurred()) 91 o.Expect(out).Should(o.ContainSubstring(`msg="test message"`)) 92 o.Expect(out).Should(o.ContainSubstring(`a="b"`)) 93 o.Expect(out).Should(o.ContainSubstring(`level="info"`)) 94 }) 95 96 g.It("debug messages are not logged to stderr", func() { 97 Debug("test message", "a", "b") 98 _, _ = stderrFile.Seek(0, 0) 99 out, err := io.ReadAll(stderrFile) 100 o.Expect(err).NotTo(o.HaveOccurred()) 101 o.Expect(out).ShouldNot(o.ContainSubstring("test message")) 102 }) 103 }) 104 105 g.When("the log level is raised to warning", func() { 106 g.BeforeEach(func() { 107 Init("warning", "", "", "", "") 108 }) 109 110 g.It("panic messages are logged to stderr", func() { 111 Panic("test message", "a", "b") 112 _, _ = stderrFile.Seek(0, 0) 113 out, err := io.ReadAll(stderrFile) 114 o.Expect(err).NotTo(o.HaveOccurred()) 115 o.Expect(out).Should(o.ContainSubstring("test message")) 116 }) 117 118 g.It("error messages are logged to stderr", func() { 119 Error("test message", "a", "b") 120 _, _ = stderrFile.Seek(0, 0) 121 out, err := io.ReadAll(stderrFile) 122 o.Expect(err).NotTo(o.HaveOccurred()) 123 o.Expect(out).Should(o.ContainSubstring("test message")) 124 }) 125 126 g.It("warning messages are logged to stderr", func() { 127 Warning("test message", "a", "b") 128 _, _ = stderrFile.Seek(0, 0) 129 out, err := io.ReadAll(stderrFile) 130 o.Expect(err).NotTo(o.HaveOccurred()) 131 o.Expect(out).Should(o.ContainSubstring("test message")) 132 }) 133 134 g.It("info messages are not logged to stderr", func() { 135 Info("test message", "a", "b") 136 _, _ = stderrFile.Seek(0, 0) 137 out, err := io.ReadAll(stderrFile) 138 o.Expect(err).NotTo(o.HaveOccurred()) 139 o.Expect(out).ShouldNot(o.ContainSubstring("test message")) 140 }) 141 }) 142 143 g.When("the log level is set to an invalid value", func() { 144 g.BeforeEach(func() { 145 Init("I'm invalid", "", "", "", "") 146 }) 147 148 g.It("panic messages are logged to stderr", func() { 149 Panic("test message", "a", "b") 150 _, _ = stderrFile.Seek(0, 0) 151 out, err := io.ReadAll(stderrFile) 152 o.Expect(err).NotTo(o.HaveOccurred()) 153 o.Expect(out).Should(o.ContainSubstring("test message")) 154 }) 155 156 g.It("info messages are logged to stderr", func() { 157 Info("test message", "a", "b") 158 _, _ = stderrFile.Seek(0, 0) 159 out, err := io.ReadAll(stderrFile) 160 o.Expect(err).NotTo(o.HaveOccurred()) 161 o.Expect(out).Should(o.ContainSubstring("test message")) 162 }) 163 164 g.It("debug messages are not logged to stderr", func() { 165 Debug("test message", "a", "b") 166 _, _ = stderrFile.Seek(0, 0) 167 out, err := io.ReadAll(stderrFile) 168 o.Expect(err).NotTo(o.HaveOccurred()) 169 o.Expect(out).ShouldNot(o.ContainSubstring("test message")) 170 }) 171 }) 172 }) 173 174 g.Context("log files", func() { 175 var logFile *os.File 176 177 g.BeforeEach(func() { 178 var err error 179 logFile, err = os.CreateTemp("", "") 180 o.Expect(err).NotTo(o.HaveOccurred()) 181 }) 182 183 g.AfterEach(func() { 184 o.Expect(logFile.Close()).To(o.Succeed()) 185 o.Expect(os.RemoveAll(logFile.Name())).To(o.Succeed()) 186 }) 187 188 g.When("the log file is set", func() { 189 g.BeforeEach(func() { 190 Init("", logFile.Name(), "", "", "") 191 }) 192 193 g.It("error messages are logged to log file but not to stderr", func() { 194 Error("test message", "a", "b") 195 _, _ = stderrFile.Seek(0, 0) 196 out, err := io.ReadAll(logFile) 197 o.Expect(err).NotTo(o.HaveOccurred()) 198 o.Expect(out).Should(o.ContainSubstring("test message")) 199 200 _, _ = stderrFile.Seek(0, 0) 201 out, err = io.ReadAll(stderrFile) 202 o.Expect(err).NotTo(o.HaveOccurred()) 203 o.Expect(out).ShouldNot(o.ContainSubstring("test message")) 204 }) 205 }) 206 207 g.When("the log file is set and then unset", func() { 208 g.BeforeEach(func() { 209 // TODO: This triggers a data race in github.com/k8snetworkplumbingwg/cni-log; fix the datarace in the 210 // logging component and then remove the skip. 211 g.Skip("https://github.com/k8snetworkplumbingwg/cni-log/issues/15") 212 Init("", logFile.Name(), "", "", "") 213 setLogFile("") 214 }) 215 216 g.It("logs to stderr but not to file", func() { 217 Error("test message", "a", "b") 218 _, _ = stderrFile.Seek(0, 0) 219 out, err := io.ReadAll(logFile) 220 o.Expect(err).NotTo(o.HaveOccurred()) 221 o.Expect(out).ShouldNot(o.ContainSubstring("test message")) 222 223 _, _ = stderrFile.Seek(0, 0) 224 out, err = io.ReadAll(stderrFile) 225 o.Expect(err).NotTo(o.HaveOccurred()) 226 o.Expect(out).Should(o.ContainSubstring("test message")) 227 }) 228 }) 229 }) 230 })