Skip to content
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
22 changes: 10 additions & 12 deletions pkg/chart/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -956,22 +956,21 @@ func (t *Testing) PrintEventsPodDetailsAndLogs(namespace string, selector string
return t.kubectl.DescribePod(namespace, pod)
}, pod)

initContainers, err := t.kubectl.GetInitContainers(namespace, pod)
if err != nil {
fmt.Println("Error printing logs:", err)
return
}

if t.config.PrintLogs {
t.printDetails(pod, "Logs of init container", "-",
func(item string) error {
return t.kubectl.Logs(namespace, pod, item)
}, initContainers...)
initContainers, err := t.kubectl.GetInitContainers(namespace, pod)
if err != nil {
fmt.Println("Error printing logs:", err)
} else {
t.printDetails(pod, "Logs of init container", "-",
func(item string) error {
return t.kubectl.Logs(namespace, pod, item)
}, initContainers...)
}

containers, err := t.kubectl.GetContainers(namespace, pod)
if err != nil {
fmt.Printf("failed printing logs: %v\n", err.Error())
return
continue
}

t.printDetails(pod, "Logs of container", "-",
Expand Down Expand Up @@ -999,7 +998,6 @@ func (t *Testing) printDetails(resource string, text string, delimiterChar strin

if err := printFunc(item); err != nil {
fmt.Println("Error printing details:", err)
return
}

if !t.config.GithubGroups {
Expand Down
121 changes: 121 additions & 0 deletions pkg/chart/diagnostics_collection_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Copyright The Helm 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 chart

import (
"errors"
"fmt"
"os"
"strings"
"testing"

"github.com/helm/chart-testing/v3/pkg/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type collectingKubectl struct {
Kubectl
failure string
described []string
logs []string
containerReads int
}

func (k *collectingKubectl) GetEvents(_ string) error {
if k.failure == "events" {
return errors.New("events unavailable")
}
return nil
}

func (k *collectingKubectl) GetPods(_ ...string) ([]string, error) {
return []string{"first", "second"}, nil
}

func (k *collectingKubectl) DescribePod(_, pod string) error {
k.described = append(k.described, pod)
return nil
}

func (k *collectingKubectl) GetInitContainers(_, pod string) ([]string, error) {
k.containerReads++
if pod == "first" && k.failure == "init list" {
return nil, errors.New("init containers unavailable")
}
return []string{"init1", "init2"}, nil
}

func (k *collectingKubectl) GetContainers(_, pod string) ([]string, error) {
k.containerReads++
if pod == "first" && k.failure == "container list" {
return nil, errors.New("containers unavailable")
}
return []string{"main1", "main2"}, nil
}

func (k *collectingKubectl) Logs(_, pod, container string) error {
key := pod + "/" + container
k.logs = append(k.logs, key)
if key == k.failure {
return errors.New("container logs unavailable")
}
return nil
}

func TestDiagnosticsContinueAfterErrors(t *testing.T) {
allLogs := []string{"first/init1", "first/init2", "first/main1", "first/main2", "second/init1", "second/init2", "second/main1", "second/main2"}
for _, tc := range []struct {
name string
failure string
printLogs bool
wantLogs []string
}{
{name: "success", printLogs: true, wantLogs: allLogs},
{name: "events error", failure: "events", printLogs: true, wantLogs: allLogs},
{name: "init list error", failure: "init list", printLogs: true, wantLogs: allLogs[2:]},
{name: "container list error", failure: "container list", printLogs: true, wantLogs: []string{"first/init1", "first/init2", "second/init1", "second/init2", "second/main1", "second/main2"}},
{name: "init log error", failure: "first/init1", printLogs: true, wantLogs: allLogs},
{name: "container log error", failure: "first/main1", printLogs: true, wantLogs: allLogs},
{name: "logs disabled", failure: "init list"},
} {
for _, groups := range []bool{false, true} {
t.Run(fmt.Sprintf("%s/groups=%t", tc.name, groups), func(t *testing.T) {
output, err := os.CreateTemp(t.TempDir(), "diagnostics")
require.NoError(t, err)
originalStdout := os.Stdout
t.Cleanup(func() {
os.Stdout = originalStdout
_ = output.Close()
})
kubectl := &collectingKubectl{failure: tc.failure}
ct := Testing{config: config.Configuration{PrintLogs: tc.printLogs, GithubGroups: groups}, kubectl: kubectl}
os.Stdout = output
ct.PrintEventsPodDetailsAndLogs("testing", "app=test")
os.Stdout = originalStdout
data, err := os.ReadFile(output.Name())
require.NoError(t, err)
assert.Equal(t, []string{"first", "second"}, kubectl.described)
assert.Equal(t, tc.wantLogs, kubectl.logs)
if !tc.printLogs {
assert.Zero(t, kubectl.containerReads)
} else if tc.failure != "" {
assert.Contains(t, string(data), "unavailable")
}
assert.Equal(t, strings.Count(string(data), "::group::"), strings.Count(string(data), "::endgroup::"), "all GitHub output groups must be closed")
})
}
}
}
Loading