From f9bc4f837b3d4f52deb3742a1bee83c79303f9a7 Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Thu, 25 Aug 2022 18:19:16 +0200 Subject: [PATCH] e2e framework: move log size verification into framework/debug This helps getting rid of the ssh dependency. The same init package as for dumping namespaces takes care of adding the functionality back to framework instances. --- test/e2e/framework/debug/init/init.go | 31 ++++++++++++++++++- .../{ => debug}/log_size_monitoring.go | 18 +++++------ test/e2e/framework/framework.go | 23 -------------- 3 files changed, 39 insertions(+), 33 deletions(-) rename test/e2e/framework/{ => debug}/log_size_monitoring.go (94%) diff --git a/test/e2e/framework/debug/init/init.go b/test/e2e/framework/debug/init/init.go index 0d3df3c5e50..a2d1896d9be 100644 --- a/test/e2e/framework/debug/init/init.go +++ b/test/e2e/framework/debug/init/init.go @@ -14,10 +14,14 @@ See the License for the specific language governing permissions and limitations under the License. */ -// Package init sets debug.DumpAllNamespaceInfo as implementation in the framework. +// Package init sets debug.DumpAllNamespaceInfo as implementation in the framework +// and enables log size verification. package init import ( + "sync" + + "github.com/onsi/ginkgo/v2" "k8s.io/kubernetes/test/e2e/framework" "k8s.io/kubernetes/test/e2e/framework/debug" ) @@ -28,6 +32,31 @@ func init() { f.DumpAllNamespaceInfo = func(f *framework.Framework, ns string) { debug.DumpAllNamespaceInfo(f.ClientSet, ns) } + + if framework.TestContext.GatherLogsSizes { + var ( + wg sync.WaitGroup + closeChannel chan bool + verifier *debug.LogsSizeVerifier + ) + + ginkgo.BeforeEach(func() { + wg.Add(1) + closeChannel = make(chan bool) + verifier = debug.NewLogsVerifier(f.ClientSet, closeChannel) + go func() { + defer wg.Done() + verifier.Run() + }() + ginkgo.DeferCleanup(func() { + ginkgo.By("Gathering log sizes data", func() { + close(closeChannel) + wg.Wait() + f.TestSummaries = append(f.TestSummaries, verifier.GetSummary()) + }) + }) + }) + } }, ) } diff --git a/test/e2e/framework/log_size_monitoring.go b/test/e2e/framework/debug/log_size_monitoring.go similarity index 94% rename from test/e2e/framework/log_size_monitoring.go rename to test/e2e/framework/debug/log_size_monitoring.go index 3d397607647..b5f53a208fb 100644 --- a/test/e2e/framework/log_size_monitoring.go +++ b/test/e2e/framework/debug/log_size_monitoring.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package framework +package debug import ( "bytes" @@ -27,7 +27,7 @@ import ( clientset "k8s.io/client-go/kubernetes" - // TODO: Remove the following imports (ref: https://github.com/kubernetes/kubernetes/issues/81245) + "k8s.io/kubernetes/test/e2e/framework" e2essh "k8s.io/kubernetes/test/e2e/framework/ssh" ) @@ -109,7 +109,7 @@ func (s *LogsSizeDataSummary) PrintHumanReadable() string { // PrintJSON returns the summary of log size data with JSON format. func (s *LogsSizeDataSummary) PrintJSON() string { - return PrettyPrintJSON(*s) + return framework.PrettyPrintJSON(*s) } // SummaryKind returns the summary of log size data summary. @@ -158,8 +158,8 @@ func (d *LogsSizeData) addNewData(ip, path string, timestamp time.Time, size int // NewLogsVerifier creates a new LogsSizeVerifier which will stop when stopChannel is closed func NewLogsVerifier(c clientset.Interface, stopChannel chan bool) *LogsSizeVerifier { nodeAddresses, err := e2essh.NodeSSHHosts(c) - ExpectNoError(err) - instanceAddress := APIAddress() + ":22" + framework.ExpectNoError(err) + instanceAddress := framework.APIAddress() + ":22" workChannel := make(chan WorkItem, len(nodeAddresses)+1) workers := make([]*LogSizeGatherer, workersNo) @@ -256,13 +256,13 @@ func (g *LogSizeGatherer) Work() bool { sshResult, err := e2essh.SSH( fmt.Sprintf("ls -l %v | awk '{print $9, $5}' | tr '\n' ' '", strings.Join(workItem.paths, " ")), workItem.ip, - TestContext.Provider, + framework.TestContext.Provider, ) if err != nil { - Logf("Error while trying to SSH to %v, skipping probe. Error: %v", workItem.ip, err) + framework.Logf("Error while trying to SSH to %v, skipping probe. Error: %v", workItem.ip, err) // In case of repeated error give up. if workItem.backoffMultiplier >= 128 { - Logf("Failed to ssh to a node %v multiple times in a row. Giving up.", workItem.ip) + framework.Logf("Failed to ssh to a node %v multiple times in a row. Giving up.", workItem.ip) g.wg.Done() return false } @@ -278,7 +278,7 @@ func (g *LogSizeGatherer) Work() bool { path := results[i] size, err := strconv.Atoi(results[i+1]) if err != nil { - Logf("Error during conversion to int: %v, skipping data. Error: %v", results[i+1], err) + framework.Logf("Error during conversion to int: %v, skipping data. Error: %v", results[i+1], err) continue } g.data.addNewData(workItem.ip, path, now, size) diff --git a/test/e2e/framework/framework.go b/test/e2e/framework/framework.go index 1fc1b329838..c122787b281 100644 --- a/test/e2e/framework/framework.go +++ b/test/e2e/framework/framework.go @@ -28,7 +28,6 @@ import ( "os" "path" "strings" - "sync" "time" "k8s.io/apimachinery/pkg/runtime" @@ -106,10 +105,6 @@ type Framework struct { // as expectations vary greatly. Constraints are grouped by the container names. AddonResourceConstraints map[string]ResourceConstraint - logsSizeWaitGroup sync.WaitGroup - logsSizeCloseChannel chan bool - logsSizeVerifier *LogsSizeVerifier - // Flaky operation failures in an e2e test can be captured through this. flakeReport *FlakeReport @@ -287,17 +282,6 @@ func (f *Framework) BeforeEach() { } } - if TestContext.GatherLogsSizes { - f.logsSizeWaitGroup = sync.WaitGroup{} - f.logsSizeWaitGroup.Add(1) - f.logsSizeCloseChannel = make(chan bool) - f.logsSizeVerifier = NewLogsVerifier(f.ClientSet, f.logsSizeCloseChannel) - go func() { - f.logsSizeVerifier.Run() - f.logsSizeWaitGroup.Done() - }() - } - f.flakeReport = NewFlakeReport() } @@ -416,13 +400,6 @@ func (f *Framework) AfterEach() { f.TestSummaries = append(f.TestSummaries, summary) } - if TestContext.GatherLogsSizes { - ginkgo.By("Gathering log sizes data") - close(f.logsSizeCloseChannel) - f.logsSizeWaitGroup.Wait() - f.TestSummaries = append(f.TestSummaries, f.logsSizeVerifier.GetSummary()) - } - TestContext.CloudConfig.Provider.FrameworkAfterEach(f) // Report any flakes that were observed in the e2e test and reset.