Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leverage logstream to get better e2e diagnostic logs. #3388

Merged
merged 4 commits into from
Oct 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions test/cancel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
knativetest "knative.dev/pkg/test"
"knative.dev/pkg/test/helpers"
)

// TestTaskRunPipelineRunCancel cancels a PipelineRun and verifies TaskRun statuses and Pod deletions.
Expand All @@ -49,9 +50,8 @@ func TestTaskRunPipelineRunCancel(t *testing.T) {
knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, namespace) }, t.Logf)
defer tearDown(ctx, t, c, namespace)

pipelineRunName := "cancel-me"
pipelineRun := &v1beta1.PipelineRun{
ObjectMeta: metav1.ObjectMeta{Name: pipelineRunName, Namespace: namespace},
ObjectMeta: metav1.ObjectMeta{Name: helpers.ObjectNameForTest(t), Namespace: namespace},
Spec: v1beta1.PipelineRunSpec{
PipelineSpec: &v1beta1.PipelineSpec{
Tasks: []v1beta1.PipelineTask{{
Expand All @@ -72,21 +72,21 @@ func TestTaskRunPipelineRunCancel(t *testing.T) {

t.Logf("Creating PipelineRun in namespace %s", namespace)
if _, err := c.PipelineRunClient.Create(ctx, pipelineRun, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create PipelineRun `%s`: %s", pipelineRunName, err)
t.Fatalf("Failed to create PipelineRun `%s`: %s", pipelineRun.Name, err)
}

t.Logf("Waiting for Pipelinerun %s in namespace %s to be started", pipelineRunName, namespace)
if err := WaitForPipelineRunState(ctx, c, pipelineRunName, pipelineRunTimeout, Running(pipelineRunName), "PipelineRunRunning"); err != nil {
t.Fatalf("Error waiting for PipelineRun %s to be running: %s", pipelineRunName, err)
t.Logf("Waiting for Pipelinerun %s in namespace %s to be started", pipelineRun.Name, namespace)
if err := WaitForPipelineRunState(ctx, c, pipelineRun.Name, pipelineRunTimeout, Running(pipelineRun.Name), "PipelineRunRunning"); err != nil {
t.Fatalf("Error waiting for PipelineRun %s to be running: %s", pipelineRun.Name, err)
}

taskrunList, err := c.TaskRunClient.List(ctx, metav1.ListOptions{LabelSelector: "tekton.dev/pipelineRun=" + pipelineRunName})
taskrunList, err := c.TaskRunClient.List(ctx, metav1.ListOptions{LabelSelector: "tekton.dev/pipelineRun=" + pipelineRun.Name})
if err != nil {
t.Fatalf("Error listing TaskRuns for PipelineRun %s: %s", pipelineRunName, err)
t.Fatalf("Error listing TaskRuns for PipelineRun %s: %s", pipelineRun.Name, err)
}

var wg sync.WaitGroup
t.Logf("Waiting for TaskRuns from PipelineRun %s in namespace %s to be running", pipelineRunName, namespace)
t.Logf("Waiting for TaskRuns from PipelineRun %s in namespace %s to be running", pipelineRun.Name, namespace)
for _, taskrunItem := range taskrunList.Items {
wg.Add(1)
go func(name string) {
Expand All @@ -99,9 +99,9 @@ func TestTaskRunPipelineRunCancel(t *testing.T) {
}
wg.Wait()

pr, err := c.PipelineRunClient.Get(ctx, pipelineRunName, metav1.GetOptions{})
pr, err := c.PipelineRunClient.Get(ctx, pipelineRun.Name, metav1.GetOptions{})
if err != nil {
t.Fatalf("Failed to get PipelineRun `%s`: %s", pipelineRunName, err)
t.Fatalf("Failed to get PipelineRun `%s`: %s", pipelineRun.Name, err)
}

patches := []jsonpatch.JsonPatchOperation{{
Expand All @@ -114,15 +114,15 @@ func TestTaskRunPipelineRunCancel(t *testing.T) {
t.Fatalf("failed to marshal patch bytes in order to cancel")
}
if _, err := c.PipelineRunClient.Patch(ctx, pr.Name, types.JSONPatchType, patchBytes, metav1.PatchOptions{}, ""); err != nil {
t.Fatalf("Failed to patch PipelineRun `%s` with cancellation: %s", pipelineRunName, err)
t.Fatalf("Failed to patch PipelineRun `%s` with cancellation: %s", pipelineRun.Name, err)
}

t.Logf("Waiting for PipelineRun %s in namespace %s to be cancelled", pipelineRunName, namespace)
if err := WaitForPipelineRunState(ctx, c, pipelineRunName, pipelineRunTimeout, FailedWithReason("PipelineRunCancelled", pipelineRunName), "PipelineRunCancelled"); err != nil {
t.Errorf("Error waiting for PipelineRun %q to finished: %s", pipelineRunName, err)
t.Logf("Waiting for PipelineRun %s in namespace %s to be cancelled", pipelineRun.Name, namespace)
if err := WaitForPipelineRunState(ctx, c, pipelineRun.Name, pipelineRunTimeout, FailedWithReason("PipelineRunCancelled", pipelineRun.Name), "PipelineRunCancelled"); err != nil {
t.Errorf("Error waiting for PipelineRun %q to finished: %s", pipelineRun.Name, err)
}

t.Logf("Waiting for TaskRuns in PipelineRun %s in namespace %s to be cancelled", pipelineRunName, namespace)
t.Logf("Waiting for TaskRuns in PipelineRun %s in namespace %s to be cancelled", pipelineRun.Name, namespace)
for _, taskrunItem := range taskrunList.Items {
wg.Add(1)
go func(name string) {
Expand All @@ -136,15 +136,15 @@ func TestTaskRunPipelineRunCancel(t *testing.T) {
wg.Wait()

var trName []string
taskrunList, err = c.TaskRunClient.List(ctx, metav1.ListOptions{LabelSelector: "tekton.dev/pipelineRun=" + pipelineRunName})
taskrunList, err = c.TaskRunClient.List(ctx, metav1.ListOptions{LabelSelector: "tekton.dev/pipelineRun=" + pipelineRun.Name})
if err != nil {
t.Fatalf("Error listing TaskRuns for PipelineRun %s: %s", pipelineRunName, err)
t.Fatalf("Error listing TaskRuns for PipelineRun %s: %s", pipelineRun.Name, err)
}
for _, taskrunItem := range taskrunList.Items {
trName = append(trName, taskrunItem.Name)
}

matchKinds := map[string][]string{"PipelineRun": {pipelineRunName}, "TaskRun": trName}
matchKinds := map[string][]string{"PipelineRun": {pipelineRun.Name}, "TaskRun": trName}
// Expected failure events: 1 for the pipelinerun cancel, 1 for each TaskRun
expectedNumberOfEvents := 1 + len(trName)
t.Logf("Making sure %d events were created from pipelinerun with kinds %v", expectedNumberOfEvents, matchKinds)
Expand Down
2 changes: 2 additions & 0 deletions test/e2e-common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ function install_pipeline_crd() {
| sed -e 's%loglevel.webhook: "info"%loglevel.webhook: "debug"%' \
| kubectl apply -f - || fail_test "Build pipeline installation failed"
verify_pipeline_installation

export SYSTEM_NAMESPACE=tekton-pipelines
}

# Install the Tekton pipeline crd based on the release number
Expand Down
52 changes: 26 additions & 26 deletions test/helm_task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,7 @@ import (
rbacv1 "k8s.io/api/rbac/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
knativetest "knative.dev/pkg/test"
)

const (
sourceResourceName = "go-helloworld-git"
sourceImageName = "go-helloworld-image"
createImageTaskName = "create-image-task"
helmDeployTaskName = "helm-deploy-task"
checkServiceTaskName = "check-service-task"
helmDeployPipelineName = "helm-deploy-pipeline"
helmDeployPipelineRunName = "helm-deploy-pipeline-run"
helmDeployServiceName = "gohelloworld-chart"
"knative.dev/pkg/test/helpers"
)

var (
Expand All @@ -59,41 +49,51 @@ func TestHelmDeployPipelineRun(t *testing.T) {
c, namespace := setup(ctx, t)
setupClusterBindingForHelm(ctx, c, t, namespace)

var (
sourceResourceName = helpers.ObjectNameForTest(t)
sourceImageName = helpers.ObjectNameForTest(t)
createImageTaskName = helpers.ObjectNameForTest(t)
helmDeployTaskName = helpers.ObjectNameForTest(t)
checkServiceTaskName = helpers.ObjectNameForTest(t)
helmDeployPipelineName = helpers.ObjectNameForTest(t)
helmDeployPipelineRunName = helpers.ObjectNameForTest(t)
)

knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, namespace) }, t.Logf)
defer tearDown(ctx, t, c, namespace)

t.Logf("Creating Git PipelineResource %s", sourceResourceName)
if _, err := c.PipelineResourceClient.Create(ctx, getGoHelloworldGitResource(), metav1.CreateOptions{}); err != nil {
if _, err := c.PipelineResourceClient.Create(ctx, getGoHelloworldGitResource(sourceResourceName), metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create Pipeline Resource `%s`: %s", sourceResourceName, err)
}

t.Logf("Creating Image PipelineResource %s", sourceImageName)
if _, err := c.PipelineResourceClient.Create(ctx, getHelmImageResource(repo), metav1.CreateOptions{}); err != nil {
if _, err := c.PipelineResourceClient.Create(ctx, getHelmImageResource(repo, sourceImageName), metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create Pipeline Resource `%s`: %s", sourceImageName, err)
}

t.Logf("Creating Task %s", createImageTaskName)
if _, err := c.TaskClient.Create(ctx, getCreateImageTask(namespace), metav1.CreateOptions{}); err != nil {
if _, err := c.TaskClient.Create(ctx, getCreateImageTask(namespace, createImageTaskName), metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create Task `%s`: %s", createImageTaskName, err)
}

t.Logf("Creating Task %s", helmDeployTaskName)
if _, err := c.TaskClient.Create(ctx, getHelmDeployTask(namespace), metav1.CreateOptions{}); err != nil {
if _, err := c.TaskClient.Create(ctx, getHelmDeployTask(namespace, helmDeployTaskName), metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create Task `%s`: %s", helmDeployTaskName, err)
}

t.Logf("Creating Task %s", checkServiceTaskName)
if _, err := c.TaskClient.Create(ctx, getCheckServiceTask(namespace), metav1.CreateOptions{}); err != nil {
if _, err := c.TaskClient.Create(ctx, getCheckServiceTask(namespace, checkServiceTaskName), metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create Task `%s`: %s", checkServiceTaskName, err)
}

t.Logf("Creating Pipeline %s", helmDeployPipelineName)
if _, err := c.PipelineClient.Create(ctx, getHelmDeployPipeline(namespace), metav1.CreateOptions{}); err != nil {
if _, err := c.PipelineClient.Create(ctx, getHelmDeployPipeline(namespace, createImageTaskName, helmDeployTaskName, checkServiceTaskName, helmDeployPipelineName), metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create Pipeline `%s`: %s", helmDeployPipelineName, err)
}

t.Logf("Creating PipelineRun %s", helmDeployPipelineRunName)
if _, err := c.PipelineRunClient.Create(ctx, getHelmDeployPipelineRun(namespace), metav1.CreateOptions{}); err != nil {
if _, err := c.PipelineRunClient.Create(ctx, getHelmDeployPipelineRun(namespace, sourceResourceName, sourceImageName, helmDeployPipelineRunName, helmDeployPipelineName), metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create Pipeline `%s`: %s", helmDeployPipelineRunName, err)
}

Expand All @@ -108,14 +108,14 @@ func TestHelmDeployPipelineRun(t *testing.T) {
defer helmCleanup(ctx, c, t, namespace)
}

func getGoHelloworldGitResource() *v1alpha1.PipelineResource {
func getGoHelloworldGitResource(sourceResourceName string) *v1alpha1.PipelineResource {
return tb.PipelineResource(sourceResourceName, tb.PipelineResourceSpec(
v1alpha1.PipelineResourceTypeGit,
tb.PipelineResourceSpecParam("url", "https://github.com/tektoncd/pipeline"),
))
}

func getHelmImageResource(dockerRepo string) *v1alpha1.PipelineResource {
func getHelmImageResource(dockerRepo, sourceImageName string) *v1alpha1.PipelineResource {
imageName := fmt.Sprintf("%s/%s", dockerRepo, names.SimpleNameGenerator.RestrictLengthWithRandomSuffix(sourceImageName))

return tb.PipelineResource(sourceImageName, tb.PipelineResourceSpec(
Expand All @@ -124,7 +124,7 @@ func getHelmImageResource(dockerRepo string) *v1alpha1.PipelineResource {
))
}

func getCreateImageTask(namespace string) *v1beta1.Task {
func getCreateImageTask(namespace, createImageTaskName string) *v1beta1.Task {
return &v1beta1.Task{
ObjectMeta: metav1.ObjectMeta{Name: createImageTaskName, Namespace: namespace},
Spec: v1beta1.TaskSpec{
Expand All @@ -149,7 +149,7 @@ func getCreateImageTask(namespace string) *v1beta1.Task {
}
}

func getHelmDeployTask(namespace string) *v1beta1.Task {
func getHelmDeployTask(namespace, helmDeployTaskName string) *v1beta1.Task {
empty := *v1beta1.NewArrayOrString("")
return &v1beta1.Task{
ObjectMeta: metav1.ObjectMeta{Name: helmDeployTaskName, Namespace: namespace},
Expand Down Expand Up @@ -196,7 +196,7 @@ func getHelmDeployTask(namespace string) *v1beta1.Task {
}
}

func getCheckServiceTask(namespace string) *v1beta1.Task {
func getCheckServiceTask(namespace, checkServiceTaskName string) *v1beta1.Task {
return &v1beta1.Task{
ObjectMeta: metav1.ObjectMeta{Name: checkServiceTaskName, Namespace: namespace},
Spec: v1beta1.TaskSpec{
Expand All @@ -216,7 +216,7 @@ func getCheckServiceTask(namespace string) *v1beta1.Task {
}
}

func getHelmDeployPipeline(namespace string) *v1beta1.Pipeline {
func getHelmDeployPipeline(namespace, createImageTaskName, helmDeployTaskName, checkServiceTaskName, helmDeployPipelineName string) *v1beta1.Pipeline {
return &v1beta1.Pipeline{
ObjectMeta: metav1.ObjectMeta{Name: helmDeployPipelineName, Namespace: namespace},
Spec: v1beta1.PipelineSpec{
Expand Down Expand Up @@ -258,15 +258,15 @@ func getHelmDeployPipeline(namespace string) *v1beta1.Pipeline {
Name: "check-service",
TaskRef: &v1beta1.TaskRef{Name: checkServiceTaskName},
Params: []v1beta1.Param{{
Name: "serviceUrl", Value: *v1beta1.NewArrayOrString(fmt.Sprintf("http://%s:8080", helmDeployServiceName)),
Name: "serviceUrl", Value: *v1beta1.NewArrayOrString("http://gohelloworld-chart:8080"),
}},
RunAfter: []string{"helm-deploy"},
}},
},
}
}

func getHelmDeployPipelineRun(namespace string) *v1beta1.PipelineRun {
func getHelmDeployPipelineRun(namespace, sourceResourceName, sourceImageName, helmDeployPipelineRunName, helmDeployPipelineName string) *v1beta1.PipelineRun {
return &v1beta1.PipelineRun{
ObjectMeta: metav1.ObjectMeta{Name: helmDeployPipelineRunName, Namespace: namespace},
Spec: v1beta1.PipelineRunSpec{
Expand Down
5 changes: 5 additions & 0 deletions test/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
knativetest "knative.dev/pkg/test"
"knative.dev/pkg/test/logging" // Mysteriously by k8s libs, or they fail to create `KubeClient`s from config. Apparently just importing it is enough. @_@ side effects @_@. https://github.com/kubernetes/client-go/issues/242
"knative.dev/pkg/test/logstream"
)

var initMetrics sync.Once
Expand All @@ -56,6 +57,10 @@ func setup(ctx context.Context, t *testing.T, fn ...func(context.Context, *testi

initializeLogsAndMetrics(t)

// Inline controller logs from SYSTEM_NAMESPACE into the t.Log output.
cancel := logstream.Start(t)
t.Cleanup(cancel)

c := newClients(t, knativetest.Flags.Kubeconfig, knativetest.Flags.Cluster, namespace)
createNamespace(ctx, t, namespace, c.KubeClient)
verifyServiceAccountExistence(ctx, t, namespace, c.KubeClient)
Expand Down
Loading