@@ -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,8 @@ func (f *Framework) BeforeEach() {
248
250
func (f * Framework ) AfterEach () {
249
251
RemoveCleanupAction (f .cleanupHandle )
250
252
253
+ printMySQLPodContainerLogs (f )
254
+
251
255
nsDeletionErrors := map [string ]error {}
252
256
253
257
// Whether to delete namespace is determined by 3 factors: delete-namespace flag, delete-namespace-on-failure flag and the test result
@@ -272,3 +276,64 @@ func (f *Framework) AfterEach() {
272
276
}
273
277
f .OperatorInstalled = false
274
278
}
279
+
280
+ func printMySQLPodContainerLogs (f * Framework ) {
281
+ pods , err := f .ClientSet .CoreV1 ().Pods (f .Namespace .Name ).List (metav1.ListOptions {})
282
+ if err != nil {
283
+ Logf ("Error retrieving pod list in namespace %s: %+v" , f .Namespace .Name , err )
284
+ }
285
+
286
+ var opPod v1.Pod
287
+ var agPods []v1.Pod
288
+ for _ , pod := range pods .Items {
289
+ if strings .Contains (pod .Spec .Containers [0 ].Image , "mysql-operator" ) {
290
+ opPod = pod
291
+ continue
292
+ }
293
+ if strings .Contains (pod .Spec .Containers [0 ].Image , "mysql-agent" ) || strings .Contains (pod .Spec .Containers [0 ].Image , "mysql-server" ) {
294
+ agPods = append (agPods , pod )
295
+ }
296
+ }
297
+
298
+ // Operator Logs
299
+ if opPod .Name != "" {
300
+ printContainerLogs (f , opPod .GetName (), & v1.PodLogOptions {})
301
+ }
302
+
303
+ for _ , agPod := range agPods {
304
+ // Server Logs
305
+ printContainerLogs (f , agPod .GetName (), & v1.PodLogOptions {Container : "mysql" })
306
+ // Agent Logs
307
+ printContainerLogs (f , agPod .GetName (), & v1.PodLogOptions {Container : "mysql-agent" })
308
+ }
309
+ }
310
+
311
+ func printLogs (read io.ReadCloser , fileName string ) error {
312
+ defer read .Close ()
313
+ env := os .Getenv ("WERCKER_REPORT_ARTIFACTS_DIR" )
314
+ dst := os .Stdout
315
+ if env != "" {
316
+ filepath := env + "/" + fileName + ".log"
317
+ file , err := os .OpenFile (filepath , os .O_WRONLY | os .O_CREATE , 0666 )
318
+ if err != nil {
319
+ return fmt .Errorf ("Error getting file for logs: %+v" , err )
320
+ }
321
+ dst = file
322
+ defer dst .Close ()
323
+ }
324
+ _ , err := io .Copy (dst , read )
325
+ if err != nil {
326
+ return fmt .Errorf ("Error exporting logs: %+v" , err )
327
+ }
328
+ return nil
329
+ }
330
+
331
+ func printContainerLogs (f * Framework , podName string , options * v1.PodLogOptions ) {
332
+ podLogs := f .ClientSet .CoreV1 ().Pods (f .Namespace .Name ).GetLogs (podName , options )
333
+ if podLogs != nil {
334
+ Logf ("Writing %s container logs to file for %s" , options .Container , podName )
335
+ read , _ := podLogs .Stream ()
336
+ printLogs (read , podName )
337
+ Logf ("Finished writing %s container logs for %s" , options .Container , podName )
338
+ }
339
+ }
0 commit comments