Skip to content

Commit

Permalink
cleanup of logging in helm task test
Browse files Browse the repository at this point in the history
  • Loading branch information
nader-ziada committed Oct 19, 2018
1 parent 5f218cb commit 4adfd60
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 32 deletions.
66 changes: 66 additions & 0 deletions test/build_logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Copyright 2018 The Knative 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 test

import (
"io/ioutil"
"strings"

"github.com/knative/pkg/test/logging"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)

//CollectBuildLogs will get the build logs for a task run
func CollectBuildLogs(c *clients, buildName, namespace string, logger *logging.BaseLogger) {
b, err := c.BuildClient.Get(buildName, metav1.GetOptions{})
if err != nil {
logger.Infof("Expected there to be a Build with the same name as TaskRun %s but got error: %s", buildName, err)
}
cluster := b.Status.Cluster
if cluster == nil || cluster.PodName == "" {
logger.Infof("Expected build status to have a podname but it didn't!")
}
logs, err := getInitContainerLogsFromPod(c.KubeClient.Kube, cluster.PodName, namespace)
if err != nil {
logger.Infof("Expected there to be logs from build helm-deploy-pipeline-run-helm-deploy %s", err)
}
logger.Infof("build logs %s", logs)
}

func getInitContainerLogsFromPod(c kubernetes.Interface, pod, namespace string) (string, error) {
p, err := c.CoreV1().Pods(namespace).Get(pod, metav1.GetOptions{})
if err != nil {
return "", err
}

sb := strings.Builder{}
for _, initContainer := range p.Spec.InitContainers {
req := c.CoreV1().Pods(namespace).GetLogs(pod, &corev1.PodLogOptions{Follow: true, Container: initContainer.Name})
rc, err := req.Stream()
if err != nil {
return "", err
}
bs, err := ioutil.ReadAll(rc)
if err != nil {
return "", err
}
sb.Write(bs)
}
return sb.String(), nil
}
39 changes: 7 additions & 32 deletions test/helm_task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"

"github.com/knative/build-pipeline/pkg/apis/pipeline/v1alpha1"
)
Expand Down Expand Up @@ -96,26 +95,16 @@ func TestHelmDeployPipelineRun(t *testing.T) {
}
return false, nil
}, "PipelineRunCompleted"); err != nil {
taskruns, err := c.TaskRunClient.List(metav1.ListOptions{})
if err != nil {
t.Errorf("Error getting TaskRun list for PipelineRun %s %s", helmDeployPipelineRunName, err)
}
for _, tr := range taskruns.Items {
CollectBuildLogs(c, tr.Name, namespace, logger)
}
t.Errorf("Error waiting for PipelineRun %s to finish: %s", helmDeployPipelineRunName, err)
}

// The Build created by the TaskRun will have the same name
b, err := c.BuildClient.Get("helm-deploy-pipeline-run-helm-deploy", metav1.GetOptions{})
if err != nil {
t.Errorf("Expected there to be a Build with the same name as TaskRun %s but got error: %s", "helm-deploy-pipeline-run-helm-deploy", err)
}
cluster := b.Status.Cluster
if cluster == nil || cluster.PodName == "" {
t.Fatalf("Expected build status to have a podname but it didn't!")
}
logs, err := getInitContainerLogsFromPod(c.KubeClient.Kube, cluster.PodName, namespace)
if err != nil {
t.Errorf("Expected there to be logs from build helm-deploy-pipeline-run-helm-deploy %s", err)
}
logger.Info("=========== build logs ===============")
logger.Info(logs)
logger.Info("=========== build logs ===============")

logger.Info("Waiting for service to get external IP")
var serviceIp string
if err := WaitForServiceExternalIPState(c, namespace, helmDeployServiceName, func(svc *corev1.Service) (bool, error) {
Expand Down Expand Up @@ -149,20 +138,6 @@ func TestHelmDeployPipelineRun(t *testing.T) {
}
}

func getInitContainerLogsFromPod(c kubernetes.Interface, pod, namespace string) (string, error) {
p, err := c.CoreV1().Pods(namespace).Get(pod, metav1.GetOptions{})
if err != nil {
return "", err
}

var containers []string
for _, initContainer := range p.Spec.InitContainers {
containers = append(containers, initContainer.Name)
}

return getContainerLogs(c, pod, namespace, containers...)
}

func getGoHelloworldGitResource(namespace string) *v1alpha1.PipelineResource {
return &v1alpha1.PipelineResource{
ObjectMeta: metav1.ObjectMeta{
Expand Down

0 comments on commit 4adfd60

Please sign in to comment.