suites.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. Copyright 2019 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package e2e
  14. import (
  15. "fmt"
  16. "io/ioutil"
  17. "path"
  18. "time"
  19. "k8s.io/kubernetes/test/e2e/framework"
  20. e2emetrics "k8s.io/kubernetes/test/e2e/framework/metrics"
  21. )
  22. // CleanupSuite is the boilerplate that can be used after tests on ginkgo were run, on the SynchronizedAfterSuite step.
  23. // Similar to SynchronizedBeforeSuite, we want to run some operations only once (such as collecting cluster logs).
  24. // Here, the order of functions is reversed; first, the function which runs everywhere,
  25. // and then the function that only runs on the first Ginkgo node.
  26. func CleanupSuite() {
  27. // Run on all Ginkgo nodes
  28. framework.Logf("Running AfterSuite actions on all nodes")
  29. framework.RunCleanupActions()
  30. }
  31. // AfterSuiteActions are actions that are run on ginkgo's SynchronizedAfterSuite
  32. func AfterSuiteActions() {
  33. // Run only Ginkgo on node 1
  34. framework.Logf("Running AfterSuite actions on node 1")
  35. if framework.TestContext.ReportDir != "" {
  36. framework.CoreDump(framework.TestContext.ReportDir)
  37. }
  38. if framework.TestContext.GatherSuiteMetricsAfterTest {
  39. if err := gatherTestSuiteMetrics(); err != nil {
  40. framework.Logf("Error gathering metrics: %v", err)
  41. }
  42. }
  43. if framework.TestContext.NodeKiller.Enabled {
  44. close(framework.TestContext.NodeKiller.NodeKillerStopCh)
  45. }
  46. }
  47. func gatherTestSuiteMetrics() error {
  48. framework.Logf("Gathering metrics")
  49. c, err := framework.LoadClientset()
  50. if err != nil {
  51. return fmt.Errorf("error loading client: %v", err)
  52. }
  53. // Grab metrics for apiserver, scheduler, controller-manager, kubelet (for non-kubemark case) and cluster autoscaler (optionally).
  54. grabber, err := e2emetrics.NewMetricsGrabber(c, nil, !framework.ProviderIs("kubemark"), true, true, true, framework.TestContext.IncludeClusterAutoscalerMetrics)
  55. if err != nil {
  56. return fmt.Errorf("failed to create MetricsGrabber: %v", err)
  57. }
  58. received, err := grabber.Grab()
  59. if err != nil {
  60. return fmt.Errorf("failed to grab metrics: %v", err)
  61. }
  62. metricsForE2E := (*e2emetrics.ComponentCollection)(&received)
  63. metricsJSON := metricsForE2E.PrintJSON()
  64. if framework.TestContext.ReportDir != "" {
  65. filePath := path.Join(framework.TestContext.ReportDir, "MetricsForE2ESuite_"+time.Now().Format(time.RFC3339)+".json")
  66. if err := ioutil.WriteFile(filePath, []byte(metricsJSON), 0644); err != nil {
  67. return fmt.Errorf("error writing to %q: %v", filePath, err)
  68. }
  69. } else {
  70. framework.Logf("\n\nTest Suite Metrics:\n%s\n", metricsJSON)
  71. }
  72. return nil
  73. }