Skip to content

Commit

Permalink
e2e: replace fmt.Println with writes to ginko's writer
Browse files Browse the repository at this point in the history
  • Loading branch information
cpanato committed May 21, 2020
1 parent 91638c4 commit 2e1f7b9
Show file tree
Hide file tree
Showing 16 changed files with 134 additions and 77 deletions.
27 changes: 27 additions & 0 deletions test/e2e/internal/log/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package log

import (
"fmt"

. "github.com/onsi/ginkgo"
)

func Logf(format string, a ...interface{}) {
fmt.Fprintf(GinkgoWriter, "INFO: "+format+"\n", a...)
}
5 changes: 3 additions & 2 deletions test/e2e/self_hosted.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/utils/pointer"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
"sigs.k8s.io/cluster-api/test/e2e/internal/log"
"sigs.k8s.io/cluster-api/test/framework"
"sigs.k8s.io/cluster-api/test/framework/bootstrap"
"sigs.k8s.io/cluster-api/test/framework/clusterctl"
Expand Down Expand Up @@ -138,7 +139,7 @@ func SelfHostedSpec(ctx context.Context, inputGetter func() SelfHostedSpecInput)
Namespace: namespace.Name,
})

fmt.Fprintf(GinkgoWriter, "Waiting for the cluster infrastructure to be provisioned\n")
log.Logf("Waiting for the cluster infrastructure to be provisioned")
selfHostedCluster = framework.DiscoveryAndWaitForCluster(ctx, framework.DiscoveryAndWaitForClusterInput{
Getter: selfHostedClusterProxy.GetClient(),
Namespace: selfHostedNamespace.Name,
Expand Down Expand Up @@ -175,7 +176,7 @@ func SelfHostedSpec(ctx context.Context, inputGetter func() SelfHostedSpecInput)
Namespace: selfHostedNamespace.Name,
})

fmt.Fprintf(GinkgoWriter, "Waiting for the cluster infrastructure to be provisioned\n")
log.Logf("Waiting for the cluster infrastructure to be provisioned")
cluster = framework.DiscoveryAndWaitForCluster(ctx, framework.DiscoveryAndWaitForClusterInput{
Getter: input.BootstrapClusterProxy.GetClient(),
Namespace: namespace.Name,
Expand Down
7 changes: 3 additions & 4 deletions test/framework/bootstrap/kind_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ package bootstrap

import (
"context"
"fmt"
"io/ioutil"
"os"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"sigs.k8s.io/cluster-api/test/framework/internal/log"
kindv1 "sigs.k8s.io/kind/pkg/apis/config/v1alpha4"
kind "sigs.k8s.io/kind/pkg/cluster"
)
Expand Down Expand Up @@ -130,9 +129,9 @@ func (k *kindClusterProvider) Dispose(ctx context.Context) {
Expect(ctx).NotTo(BeNil(), "ctx is required for Dispose")

if err := kind.NewProvider().Delete(k.name, k.kubeconfigPath); err != nil {
fmt.Fprintf(GinkgoWriter, "Deleting the kind cluster %q failed. You may need to remove this by hand.\n", k.name)
log.Logf("Deleting the kind cluster %q failed. You may need to remove this by hand.", k.name)
}
if err := os.Remove(k.kubeconfigPath); err != nil {
fmt.Fprintf(GinkgoWriter, "Deleting the kubeconfig file %q file. You may need to remove this by hand.\n", k.kubeconfigPath)
log.Logf("Deleting the kubeconfig file %q file. You may need to remove this by hand.", k.kubeconfigPath)
}
}
9 changes: 4 additions & 5 deletions test/framework/bootstrap/kind_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,16 @@ package bootstrap

import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/pkg/errors"

"sigs.k8s.io/cluster-api/test/framework"
"sigs.k8s.io/cluster-api/test/framework/exec"
"sigs.k8s.io/cluster-api/test/framework/internal/log"
kind "sigs.k8s.io/kind/pkg/cluster"
kindnodes "sigs.k8s.io/kind/pkg/cluster/nodes"
kindnodesutils "sigs.k8s.io/kind/pkg/cluster/nodeutils"
Expand All @@ -51,7 +50,7 @@ func CreateKindBootstrapClusterAndLoadImages(ctx context.Context, input CreateKi
Expect(ctx).NotTo(BeNil(), "ctx is required for CreateKindBootstrapClusterAndLoadImages")
Expect(input.Name).ToNot(BeEmpty(), "Invalid argument. Name can't be empty when calling CreateKindBootstrapClusterAndLoadImages")

fmt.Fprintf(GinkgoWriter, "Creating a kind cluster with name %q\n", input.Name)
log.Logf("Creating a kind cluster with name %q", input.Name)

options := []KindClusterOption{}
if input.RequiresDockerSock {
Expand Down Expand Up @@ -86,14 +85,14 @@ func LoadImagesToKindCluster(ctx context.Context, input LoadImagesToKindClusterI
Expect(input.Name).ToNot(BeEmpty(), "Invalid argument. Name can't be empty when calling LoadImagesToKindCluster")

for _, image := range input.Images {
fmt.Fprintf(GinkgoWriter, "Loading image: %q\n", image.Name)
log.Logf("Loading image: %q", image.Name)
err := loadImage(ctx, input.Name, image.Name)
switch image.LoadBehavior {
case framework.MustLoadImage:
Expect(err).ToNot(HaveOccurred(), "Failed to load image %q into the kind cluster %q", image.Name, input.Name)
case framework.TryLoadImage:
if err != nil {
fmt.Fprintf(GinkgoWriter, "[WARNING] Unable to load image %q into the kind cluster %q: %v \n", image.Name, input.Name, err)
log.Logf("[WARNING] Unable to load image %q into the kind cluster %q: %v", image.Name, input.Name, err)
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions test/framework/cluster_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"sigs.k8s.io/cluster-api/test/framework/internal/log"
"sigs.k8s.io/cluster-api/test/framework/options"
"sigs.k8s.io/controller-runtime/pkg/client"

Expand All @@ -48,7 +49,7 @@ func CreateCluster(ctx context.Context, input CreateClusterInput, intervals ...i
By("creating a Cluster resource linked to the InfrastructureCluster resource")
Eventually(func() error {
if err := input.Creator.Create(ctx, input.Cluster); err != nil {
fmt.Printf("%+v\n", err)
log.Logf("Failed to create a cluster: %+v", err)
return err
}
return nil
Expand All @@ -61,7 +62,7 @@ type GetAllClustersByNamespaceInput struct {
Namespace string
}

// GetClustersByNamespace returns the list of Cluster object in a namespace
// GetAllClustersByNamespace returns the list of Cluster object in a namespace
func GetAllClustersByNamespace(ctx context.Context, input GetAllClustersByNamespaceInput) []*clusterv1.Cluster {
clusterList := &clusterv1.ClusterList{}
Expect(input.Lister.List(ctx, clusterList, client.InNamespace(input.Namespace))).To(Succeed(), "Failed to list clusters in namespace %s", input.Namespace)
Expand Down Expand Up @@ -196,14 +197,14 @@ func DeleteClusterAndWait(ctx context.Context, input DeleteClusterAndWaitInput,
Cluster: input.Cluster,
})

fmt.Fprintf(GinkgoWriter, "Waiting for the Cluster object to be deleted\n")
log.Logf("Waiting for the Cluster object to be deleted")
WaitForClusterDeleted(ctx, WaitForClusterDeletedInput{
Getter: input.Client,
Cluster: input.Cluster,
}, intervals...)

//TODO: consider if to move in another func (what if there are more than one cluster?)
fmt.Fprintf(GinkgoWriter, "Check for all the Cluster API resources being deleted\n")
log.Logf("Check for all the Cluster API resources being deleted")
resources := GetCAPIResources(ctx, GetCAPIResourcesInput{
Lister: input.Client,
Namespace: input.Cluster.Namespace,
Expand Down Expand Up @@ -236,7 +237,7 @@ func DeleteAllClustersAndWait(ctx context.Context, input DeleteAllClustersAndWai
}

for _, c := range clusters {
fmt.Fprintf(GinkgoWriter, "Waiting for the Cluster %s/%s to be deleted\n", c.Namespace, c.Name)
log.Logf("Waiting for the Cluster %s/%s to be deleted", c.Namespace, c.Name)
WaitForClusterDeleted(ctx, WaitForClusterDeletedInput{
Getter: input.Client,
Cluster: c,
Expand Down
4 changes: 2 additions & 2 deletions test/framework/cluster_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
goruntime "runtime"
"strings"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

corev1 "k8s.io/api/core/v1"
Expand All @@ -36,6 +35,7 @@ import (
"k8s.io/client-go/tools/clientcmd/api"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
"sigs.k8s.io/cluster-api/test/framework/exec"
"sigs.k8s.io/cluster-api/test/framework/internal/log"
"sigs.k8s.io/controller-runtime/pkg/client"
)

Expand Down Expand Up @@ -246,7 +246,7 @@ func (p *clusterProxy) Dispose(ctx context.Context) {

if p.shouldCleanupKubeconfig {
if err := os.Remove(p.kubeconfigPath); err != nil {
fmt.Fprintf(GinkgoWriter, "Deleting the kubeconfig file %q file. You may need to remove this by hand.\n", p.kubeconfigPath)
log.Logf("Deleting the kubeconfig file %q file. You may need to remove this by hand.", p.kubeconfigPath)
}
}
}
5 changes: 3 additions & 2 deletions test/framework/clusterctl/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
clusterctlclient "sigs.k8s.io/cluster-api/cmd/clusterctl/client"
clusterctllog "sigs.k8s.io/cluster-api/cmd/clusterctl/log"
"sigs.k8s.io/cluster-api/test/framework/clusterctl/logger"
"sigs.k8s.io/cluster-api/test/framework/internal/log"
)

// Provide E2E friendly wrappers for the clusterctl client library.
Expand Down Expand Up @@ -55,7 +56,7 @@ type InitInput struct {

// Init calls clusterctl init with the list of providers defined in the local repository
func Init(ctx context.Context, input InitInput) {
fmt.Fprintf(GinkgoWriter, "clusterctl init --core %s --bootstrap %s --control-plane %s --infrastructure %s\n",
log.Logf("clusterctl init --core %s --bootstrap %s --control-plane %s --infrastructure %s",
input.CoreProvider,
strings.Join(input.BootstrapProviders, ", "),
strings.Join(input.ControlPlaneProviders, ", "),
Expand Down Expand Up @@ -97,7 +98,7 @@ type ConfigClusterInput struct {

// ConfigCluster gets a workload cluster based on a template.
func ConfigCluster(ctx context.Context, input ConfigClusterInput) []byte {
fmt.Fprintf(GinkgoWriter, "clusterctl config cluster %s --infrastructure %s --kubernetes-version %s --control-plane-machine-count %d --worker-machine-count %d --flavor %s\n",
log.Logf("clusterctl config cluster %s --infrastructure %s --kubernetes-version %s --control-plane-machine-count %d --worker-machine-count %d --flavor %s",
input.ClusterName,
valueOrDefault(input.InfrastructureProvider),
input.KubernetesVersion,
Expand Down
21 changes: 10 additions & 11 deletions test/framework/clusterctl/clusterctl_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,15 @@ package clusterctl

import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1alpha3"
"sigs.k8s.io/cluster-api/test/framework"
"sigs.k8s.io/cluster-api/test/framework/internal/log"

"sigs.k8s.io/cluster-api/cmd/clusterctl/client/config"
)
Expand Down Expand Up @@ -70,7 +69,7 @@ func InitManagementClusterAndWatchControllerLogs(ctx context.Context, input Init
})
}

fmt.Fprintf(GinkgoWriter, "Waiting for provider controllers to be running\n")
log.Logf("Waiting for provider controllers to be running")
controllersDeployments = framework.GetControllerDeployments(context.TODO(), framework.GetControllerDeploymentsInput{
Lister: client,
})
Expand Down Expand Up @@ -108,10 +107,10 @@ func ApplyClusterTemplateAndWait(ctx context.Context, input ApplyClusterTemplate

Expect(input.ClusterProxy).ToNot(BeNil(), "Invalid argument. input.ClusterProxy can't be nil when calling ApplyClusterTemplateAndWait")

fmt.Fprintf(GinkgoWriter, "Creating the workload cluster with name %q using the %q template (Kubernetes %s, %d control-plane machines, %d worker machines)\n",
log.Logf("Creating the workload cluster with name %q using the %q template (Kubernetes %s, %d control-plane machines, %d worker machines)",
input.ConfigCluster.ClusterName, valueOrDefault(input.ConfigCluster.Flavor), input.ConfigCluster.KubernetesVersion, input.ConfigCluster.ControlPlaneMachineCount, input.ConfigCluster.WorkerMachineCount)

fmt.Fprintf(GinkgoWriter, "Getting the cluster template yaml\n")
log.Logf("Getting the cluster template yaml")
workloadClusterTemplate := ConfigCluster(ctx, ConfigClusterInput{
// pass reference to the management cluster hosting this test
KubeconfigPath: input.ConfigCluster.KubeconfigPath,
Expand All @@ -131,38 +130,38 @@ func ApplyClusterTemplateAndWait(ctx context.Context, input ApplyClusterTemplate
})
Expect(workloadClusterTemplate).ToNot(BeNil(), "Failed to get the cluster template")

fmt.Fprintf(GinkgoWriter, "Applying the cluster template yaml to the cluster\n")
log.Logf("Applying the cluster template yaml to the cluster")
Expect(input.ClusterProxy.Apply(ctx, workloadClusterTemplate)).ShouldNot(HaveOccurred())

fmt.Fprintf(GinkgoWriter, "Waiting for the cluster infrastructure to be provisioned\n")
log.Logf("Waiting for the cluster infrastructure to be provisioned")
cluster := framework.DiscoveryAndWaitForCluster(ctx, framework.DiscoveryAndWaitForClusterInput{
Getter: input.ClusterProxy.GetClient(),
Namespace: input.ConfigCluster.Namespace,
Name: input.ConfigCluster.ClusterName,
}, input.WaitForClusterIntervals...)

fmt.Fprintf(GinkgoWriter, "Waiting for control plane to be initialized\n")
log.Logf("Waiting for control plane to be initialized")
controlPlane := framework.DiscoveryAndWaitForControlPlaneInitialized(ctx, framework.DiscoveryAndWaitForControlPlaneInitializedInput{
Lister: input.ClusterProxy.GetClient(),
Cluster: cluster,
}, input.WaitForControlPlaneIntervals...)

fmt.Fprintf(GinkgoWriter, "Installing a CNI plugin to the workload cluster\n")
log.Logf("Installing a CNI plugin to the workload cluster")
workloadCluster := input.ClusterProxy.GetWorkloadCluster(context.TODO(), cluster.Namespace, cluster.Name)

cniYaml, err := ioutil.ReadFile(input.CNIManifestPath)
Expect(err).ShouldNot(HaveOccurred())

Expect(workloadCluster.Apply(context.TODO(), cniYaml)).ShouldNot(HaveOccurred())

fmt.Fprintf(GinkgoWriter, "Waiting for control plane to be ready\n")
log.Logf("Waiting for control plane to be ready")
framework.WaitForControlPlaneAndMachinesReady(ctx, framework.WaitForControlPlaneAndMachinesReadyInput{
GetLister: input.ClusterProxy.GetClient(),
Cluster: cluster,
ControlPlane: controlPlane,
}, input.WaitForControlPlaneIntervals...)

fmt.Fprintf(GinkgoWriter, "Waiting for the worker machines to be provisioned\n")
log.Logf("Waiting for the worker machines to be provisioned")
machineDeployments := framework.DiscoveryAndWaitForMachineDeployments(ctx, framework.DiscoveryAndWaitForMachineDeploymentsInput{
Lister: input.ClusterProxy.GetClient(),
Cluster: cluster,
Expand Down
Loading

0 comments on commit 2e1f7b9

Please sign in to comment.