Skip to content

Commit b0f259d

Browse files
authored
Merge pull request #1417 from Nordix/lentzi90/e2e-log-fix
🌱 e2e: Change logging to avoid use of By
2 parents 43c27cd + c4229c9 commit b0f259d

File tree

6 files changed

+56
-62
lines changed

6 files changed

+56
-62
lines changed

test/e2e/shared/cluster.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func createClusterctlLocalRepository(config *clusterctl.E2EConfig, repositoryFol
5959

6060
// setupBootstrapCluster installs Cluster API components via clusterctl.
6161
func setupBootstrapCluster(config *clusterctl.E2EConfig, scheme *runtime.Scheme, useExistingCluster bool) (bootstrap.ClusterProvider, framework.ClusterProxy) {
62-
Byf("Running setupBootstrapCluster (useExistingCluster: %t)", useExistingCluster)
62+
Logf("Running setupBootstrapCluster (useExistingCluster: %t)", useExistingCluster)
6363

6464
// We only want to set clusterProvider if we create a new bootstrap cluster in this test.
6565
// If we re-use an existing one, we don't want to delete it afterwards, so we don't set it.
@@ -78,7 +78,7 @@ func setupBootstrapCluster(config *clusterctl.E2EConfig, scheme *runtime.Scheme,
7878
// Only use the kubeconfigPath if the current context is the configured kubeContext
7979
// Otherwise we might deploy to the wrong cluster.
8080
// TODO(sbuerin): this logic could be a lot nicer if we could hand over a kubeContext to NewClusterProxy
81-
Byf("Found currentContext %q in %q (configured kubeContext is %q)", kubecfg.CurrentContext, testKubeconfigPath, kubeContext)
81+
Logf("Found currentContext %q in %q (configured kubeContext is %q)", kubecfg.CurrentContext, testKubeconfigPath, kubeContext)
8282
if kubecfg.CurrentContext == kubeContext {
8383
kubeconfigPath = testKubeconfigPath
8484
}

test/e2e/shared/common.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import (
4040
)
4141

4242
func SetupSpecNamespace(ctx context.Context, specName string, e2eCtx *E2EContext) *corev1.Namespace {
43-
Byf("Creating a namespace for hosting the %q test spec", specName)
43+
Logf("Creating a namespace for hosting the %q test spec", specName)
4444
namespace, cancelWatches := framework.CreateNamespaceAndWatchEvents(ctx, framework.CreateNamespaceAndWatchEventsInput{
4545
Creator: e2eCtx.Environment.BootstrapClusterProxy.GetClient(),
4646
ClientSet: e2eCtx.Environment.BootstrapClusterProxy.GetClientSet(),
@@ -54,14 +54,14 @@ func SetupSpecNamespace(ctx context.Context, specName string, e2eCtx *E2EContext
5454
}
5555

5656
func DumpSpecResourcesAndCleanup(ctx context.Context, specName string, namespace *corev1.Namespace, e2eCtx *E2EContext) {
57-
Byf("Running DumpSpecResourcesAndCleanup for namespace %q", namespace.Name)
57+
Logf("Running DumpSpecResourcesAndCleanup for namespace %q", namespace.Name)
5858
// Dump all Cluster API related resources to artifacts before deleting them.
5959
cancelWatches := e2eCtx.Environment.Namespaces[namespace]
6060
dumpSpecResources(ctx, e2eCtx, namespace)
6161

6262
dumpOpenStack(ctx, e2eCtx, e2eCtx.Environment.BootstrapClusterProxy.GetName())
6363

64-
Byf("Dumping all OpenStack server instances in the %q namespace", namespace.Name)
64+
Logf("Dumping all OpenStack server instances in the %q namespace", namespace.Name)
6565
dumpMachines(ctx, e2eCtx, namespace)
6666

6767
if !e2eCtx.Settings.SkipCleanup {
@@ -70,7 +70,7 @@ func DumpSpecResourcesAndCleanup(ctx context.Context, specName string, namespace
7070
Namespace: namespace.Name,
7171
}, e2eCtx.E2EConfig.GetIntervals(specName, "wait-delete-cluster")...)
7272

73-
Byf("Deleting namespace used for hosting the %q test spec", specName)
73+
Logf("Deleting namespace used for hosting the %q test spec", specName)
7474
framework.DeleteNamespace(ctx, framework.DeleteNamespaceInput{
7575
Deleter: e2eCtx.Environment.BootstrapClusterProxy.GetClient(),
7676
Name: namespace.Name,
@@ -207,13 +207,13 @@ func dumpSpecResources(ctx context.Context, e2eCtx *E2EContext, namespace *corev
207207
})
208208
}
209209

210-
func Byf(format string, a ...interface{}) {
211-
By("[" + time.Now().Format(time.RFC3339) + "] " + fmt.Sprintf(format, a...))
210+
func Logf(format string, a ...interface{}) {
211+
fmt.Fprintf(GinkgoWriter, "["+time.Now().Format(time.RFC3339)+"] "+format+"\n", a...)
212212
}
213213

214214
func Debugf(debug bool, format string, a ...interface{}) {
215215
if debug {
216-
By("[DEBUG] [" + time.Now().Format(time.RFC3339) + "] " + fmt.Sprintf(format, a...))
216+
fmt.Fprintf(GinkgoWriter, "[DEBUG] ["+time.Now().Format(time.RFC3339)+"] "+format+"\n", a...)
217217
}
218218
}
219219

@@ -232,6 +232,6 @@ func SetEnvVar(key, value string, private bool) {
232232
printableValue = value
233233
}
234234

235-
Byf("Setting environment variable: key=%s, value=%s", key, printableValue)
235+
Logf("Setting environment variable: key=%s, value=%s", key, printableValue)
236236
_ = os.Setenv(key, value)
237237
}

test/e2e/shared/openstack.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ type ServerExtWithIP struct {
6363

6464
// ensureSSHKeyPair ensures A SSH key is present under the name.
6565
func ensureSSHKeyPair(e2eCtx *E2EContext) {
66-
Byf("Ensuring presence of SSH key %q in OpenStack", DefaultSSHKeyPairName)
66+
Logf("Ensuring presence of SSH key %q in OpenStack", DefaultSSHKeyPairName)
6767

6868
providerClient, clientOpts, _, err := GetTenantProviderClient(e2eCtx)
6969
Expect(err).NotTo(HaveOccurred())
@@ -83,7 +83,7 @@ func ensureSSHKeyPair(e2eCtx *E2EContext) {
8383
}
8484

8585
sshDir := filepath.Join(e2eCtx.Settings.ArtifactFolder, "ssh")
86-
Byf("Storing keypair in %q", sshDir)
86+
Logf("Storing keypair in %q", sshDir)
8787

8888
err = os.MkdirAll(sshDir, 0o750)
8989
Expect(err).NotTo(HaveOccurred())
@@ -96,7 +96,7 @@ func ensureSSHKeyPair(e2eCtx *E2EContext) {
9696
}
9797

9898
func dumpOpenStack(_ context.Context, e2eCtx *E2EContext, bootstrapClusterProxyName string) {
99-
Byf("Running dumpOpenStack")
99+
Logf("Running dumpOpenStack")
100100
logPath := filepath.Join(e2eCtx.Settings.ArtifactFolder, "clusters", bootstrapClusterProxyName, "openstack-resources")
101101
if err := os.MkdirAll(logPath, os.ModePerm); err != nil {
102102
_, _ = fmt.Fprintf(GinkgoWriter, "error creating directory %s: %s\n", logPath, err)

test/e2e/shared/suite.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,17 @@ type synchronizedBeforeTestSuiteConfig struct {
5252

5353
// Node1BeforeSuite is the common setup down on the first ginkgo node before the test suite runs.
5454
func Node1BeforeSuite(e2eCtx *E2EContext) []byte {
55-
Byf("Running Node1BeforeSuite")
56-
defer Byf("Finished Node1BeforeSuite")
55+
Logf("Running Node1BeforeSuite")
56+
defer Logf("Finished Node1BeforeSuite")
5757

5858
flag.Parse()
5959
Expect(e2eCtx.Settings.ConfigPath).To(BeAnExistingFile(), "Invalid test suite argument. configPath should be an existing file.")
6060
Expect(os.MkdirAll(e2eCtx.Settings.ArtifactFolder, 0o750)).To(Succeed(), "Invalid test suite argument. Can't create artifacts-folder %q", e2eCtx.Settings.ArtifactFolder)
61-
Byf("Loading the e2e test configuration from %q", e2eCtx.Settings.ConfigPath)
61+
Logf("Loading the e2e test configuration from %q", e2eCtx.Settings.ConfigPath)
6262
e2eCtx.E2EConfig = LoadE2EConfig(e2eCtx.Settings.ConfigPath)
6363

6464
Expect(e2eCtx.E2EConfig.GetVariable(OpenStackCloudYAMLFile)).To(BeAnExistingFile(), "Invalid test suite argument. Value of environment variable OPENSTACK_CLOUD_YAML_FILE should be an existing file: %s", e2eCtx.E2EConfig.GetVariable(OpenStackCloudYAMLFile))
65-
Byf("Loading the clouds.yaml from %q", e2eCtx.E2EConfig.GetVariable(OpenStackCloudYAMLFile))
65+
Logf("Loading the clouds.yaml from %q", e2eCtx.E2EConfig.GetVariable(OpenStackCloudYAMLFile))
6666

6767
// TODO(sbuerin): we always need ci artifacts, because we don't have images for every Kubernetes version
6868
err := filepath.WalkDir(path.Join(e2eCtx.Settings.DataFolder, "infrastructure-openstack"), func(f string, d fs.DirEntry, _ error) error {
@@ -114,13 +114,13 @@ func Node1BeforeSuite(e2eCtx *E2EContext) []byte {
114114

115115
ensureSSHKeyPair(e2eCtx)
116116

117-
Byf("Creating a clusterctl local repository into %q", e2eCtx.Settings.ArtifactFolder)
117+
Logf("Creating a clusterctl local repository into %q", e2eCtx.Settings.ArtifactFolder)
118118
e2eCtx.Environment.ClusterctlConfigPath = createClusterctlLocalRepository(e2eCtx.E2EConfig, filepath.Join(e2eCtx.Settings.ArtifactFolder, "repository"))
119119

120-
Byf("Setting up the bootstrap cluster")
120+
Logf("Setting up the bootstrap cluster")
121121
e2eCtx.Environment.BootstrapClusterProvider, e2eCtx.Environment.BootstrapClusterProxy = setupBootstrapCluster(e2eCtx.E2EConfig, e2eCtx.Environment.Scheme, e2eCtx.Settings.UseExistingCluster)
122122

123-
Byf("Initializing the bootstrap cluster")
123+
Logf("Initializing the bootstrap cluster")
124124
initBootstrapCluster(e2eCtx)
125125

126126
conf := synchronizedBeforeTestSuiteConfig{
@@ -142,8 +142,8 @@ func Node1BeforeSuite(e2eCtx *E2EContext) []byte {
142142

143143
// AllNodesBeforeSuite is the common setup down on each ginkgo parallel node before the test suite runs.
144144
func AllNodesBeforeSuite(e2eCtx *E2EContext, data []byte) {
145-
Byf("Running AllNodesBeforeSuite")
146-
defer Byf("Finished AllNodesBeforeSuite")
145+
Logf("Running AllNodesBeforeSuite")
146+
defer Logf("Finished AllNodesBeforeSuite")
147147

148148
conf := &synchronizedBeforeTestSuiteConfig{}
149149
err := yaml.UnmarshalStrict(data, conf)
@@ -222,20 +222,20 @@ func AllNodesBeforeSuite(e2eCtx *E2EContext, data []byte) {
222222

223223
// AllNodesAfterSuite is cleanup that runs on all ginkgo parallel nodes after the test suite finishes.
224224
func AllNodesAfterSuite(e2eCtx *E2EContext) {
225-
Byf("Running AllNodesAfterSuite")
226-
defer Byf("Finished AllNodesAfterSuite")
225+
Logf("Running AllNodesAfterSuite")
226+
defer Logf("Finished AllNodesAfterSuite")
227227

228-
Byf("Stopping ResourceTicker")
228+
Logf("Stopping ResourceTicker")
229229
if e2eCtx.Environment.ResourceTickerDone != nil {
230230
e2eCtx.Environment.ResourceTickerDone <- true
231231
}
232-
Byf("Stopped ResourceTicker")
232+
Logf("Stopped ResourceTicker")
233233

234-
Byf("Stopping MachineTicker")
234+
Logf("Stopping MachineTicker")
235235
if e2eCtx.Environment.MachineTickerDone != nil {
236236
e2eCtx.Environment.MachineTickerDone <- true
237237
}
238-
Byf("Stopped MachineTicker")
238+
Logf("Stopped MachineTicker")
239239

240240
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Minute)
241241
defer cancel()
@@ -252,10 +252,10 @@ func AllNodesAfterSuite(e2eCtx *E2EContext) {
252252

253253
// Node1AfterSuite is cleanup that runs on the first ginkgo node after the test suite finishes.
254254
func Node1AfterSuite(e2eCtx *E2EContext) {
255-
Byf("Running Node1AfterSuite")
256-
defer Byf("Finished Node1AfterSuite")
255+
Logf("Running Node1AfterSuite")
256+
defer Logf("Finished Node1AfterSuite")
257257

258-
Byf("Tearing down the management cluster")
258+
Logf("Tearing down the management cluster")
259259
if !e2eCtx.Settings.SkipCleanup {
260260
tearDown(e2eCtx.Environment.BootstrapClusterProvider, e2eCtx.Environment.BootstrapClusterProxy)
261261
}

test/e2e/suites/e2e/clusterctl_upgrade_test.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ package e2e
2121

2222
import (
2323
"context"
24-
"os"
2524

2625
. "github.com/onsi/ginkgo/v2"
2726
capi_e2e "sigs.k8s.io/cluster-api/test/e2e"
@@ -36,13 +35,8 @@ import (
3635
// if we want to continue testing v0.6.
3736
var _ = Describe("When testing clusterctl upgrades (v0.6=>current) [clusterctl-upgrade]", func() {
3837
ctx := context.TODO()
39-
// Set environment variables for rendering the cluster templates.
40-
// We cannot use shared.SetEnvVar here because it tries to log things
41-
// using `By`, which cannot be done outside of `It` blocks.
42-
// We cannot put this in an `It` block either since `ClusterctlUpgradeSpec`
43-
// contains `BeforeEach` which cannot be inside `It`.
44-
_ = os.Setenv("USE_CI_ARTIFACTS", "true")
45-
_ = os.Setenv("DOWNLOAD_E2E_IMAGE", "true")
38+
shared.SetEnvVar("USE_CI_ARTIFACTS", "true", false)
39+
shared.SetEnvVar("DOWNLOAD_E2E_IMAGE", "true", false)
4640

4741
capi_e2e.ClusterctlUpgradeSpec(ctx, func() capi_e2e.ClusterctlUpgradeSpecInput {
4842
return capi_e2e.ClusterctlUpgradeSpecInput{

0 commit comments

Comments
 (0)