@@ -16,6 +16,8 @@ package framework
16
16
17
17
import (
18
18
"fmt"
19
+ "io"
20
+ "os"
19
21
"os/exec"
20
22
"path/filepath"
21
23
"strings"
@@ -248,6 +250,35 @@ func (f *Framework) BeforeEach() {
248
250
func (f * Framework ) AfterEach () {
249
251
RemoveCleanupAction (f .cleanupHandle )
250
252
253
+ pods , err := f .ClientSet .CoreV1 ().Pods (f .Namespace .Name ).List (metav1.ListOptions {})
254
+ if err != nil {
255
+ Logf ("Error retrieving pod list in namespace %s: %+v" , f .Namespace .Name , err )
256
+ }
257
+
258
+ var opPod v1.Pod
259
+ var agPods []v1.Pod
260
+ for _ , pod := range pods .Items {
261
+ if strings .Contains (pod .Spec .Containers [0 ].Image , "mysql-operator" ) {
262
+ opPod = pod
263
+ continue
264
+ }
265
+ if strings .Contains (pod .Spec .Containers [0 ].Image , "mysql-agent" ) || strings .Contains (pod .Spec .Containers [0 ].Image , "mysql-server" ) {
266
+ agPods = append (agPods , pod )
267
+ }
268
+ }
269
+
270
+ // Operator Logs
271
+ if opPod .Name != "" {
272
+ printContainerLogs (f , opPod .GetName (), & v1.PodLogOptions {})
273
+ }
274
+
275
+ for _ , agPod := range agPods {
276
+ // Server Logs
277
+ printContainerLogs (f , agPod .GetName (), & v1.PodLogOptions {Container : "mysql" })
278
+ // Agent Logs
279
+ printContainerLogs (f , agPod .GetName (), & v1.PodLogOptions {Container : "mysql-agent" })
280
+ }
281
+
251
282
nsDeletionErrors := map [string ]error {}
252
283
253
284
// Whether to delete namespace is determined by 3 factors: delete-namespace flag, delete-namespace-on-failure flag and the test result
@@ -272,3 +303,33 @@ func (f *Framework) AfterEach() {
272
303
}
273
304
f .OperatorInstalled = false
274
305
}
306
+
307
+ func printLogs (read io.ReadCloser , fileName string ) error {
308
+ defer read .Close ()
309
+ env := os .Getenv ("WERCKER_REPORT_ARTIFACTS_DIR" )
310
+ dst := os .Stdout
311
+ if env != "" {
312
+ filepath := env + "/" + fileName + ".log"
313
+ file , err := os .OpenFile (filepath , os .O_WRONLY | os .O_CREATE , 0666 )
314
+ if err != nil {
315
+ return fmt .Errorf ("Error getting file for logs: %+v" , err )
316
+ }
317
+ dst = file
318
+ defer dst .Close ()
319
+ }
320
+ _ , err := io .Copy (dst , read )
321
+ if err != nil {
322
+ return fmt .Errorf ("Error exporting logs: %+v" , err )
323
+ }
324
+ return nil
325
+ }
326
+
327
+ func printContainerLogs (f * Framework , podName string , options * v1.PodLogOptions ) {
328
+ podLogs := f .ClientSet .CoreV1 ().Pods (f .Namespace .Name ).GetLogs (podName , options )
329
+ if podLogs != nil {
330
+ Logf ("Writing %s container logs to file for %s" , options .Container , podName )
331
+ read , _ := podLogs .Stream ()
332
+ printLogs (read , podName )
333
+ Logf ("Finished writing %s container logs for %s" , options .Container , podName )
334
+ }
335
+ }
0 commit comments