|
| 1 | +// Copyright 2026 The Carvel Authors. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package app |
| 5 | + |
| 6 | +import ( |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "carvel.dev/kapp-controller/pkg/apis/kappctrl/v1alpha1" |
| 11 | + "carvel.dev/kapp-controller/pkg/deploy" |
| 12 | + "carvel.dev/kapp-controller/pkg/exec" |
| 13 | + "carvel.dev/kapp-controller/pkg/fetch" |
| 14 | + "carvel.dev/kapp-controller/pkg/metrics" |
| 15 | + "carvel.dev/kapp-controller/pkg/template" |
| 16 | + "github.com/stretchr/testify/assert" |
| 17 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 18 | + logf "sigs.k8s.io/controller-runtime/pkg/log" |
| 19 | +) |
| 20 | + |
| 21 | +// newAppForTest returns a minimal *App wired with no-op hooks. |
| 22 | +func newAppForTest(t *testing.T, appModel v1alpha1.App, opts Opts) *App { |
| 23 | + t.Helper() |
| 24 | + return NewApp(appModel, Hooks{ |
| 25 | + BlockDeletion: func() error { return nil }, |
| 26 | + UnblockDeletion: func() error { return nil }, |
| 27 | + UpdateStatus: func(string) error { return nil }, |
| 28 | + }, fetch.Factory{}, template.Factory{}, deploy.Factory{}, |
| 29 | + logf.Log.WithName("test"), |
| 30 | + opts, |
| 31 | + metrics.NewMetrics(), |
| 32 | + FakeComponentInfo{}, |
| 33 | + ) |
| 34 | +} |
| 35 | + |
| 36 | +func Test_updateLastDeploy_TruncatesLargeStdout(t *testing.T) { |
| 37 | + const limit = 50 |
| 38 | + a := newAppForTest(t, v1alpha1.App{ |
| 39 | + ObjectMeta: metav1.ObjectMeta{Name: "test-app", Namespace: "default"}, |
| 40 | + }, Opts{MaxStatusOutputBytes: limit}) |
| 41 | + a.app.Status.Deploy = &v1alpha1.AppStatusDeploy{} |
| 42 | + |
| 43 | + largeStdout := strings.Repeat("resource change line\n", 1000) |
| 44 | + a.updateLastDeploy(exec.CmdRunResult{Stdout: largeStdout, Finished: true, ExitCode: 0}) |
| 45 | + |
| 46 | + got := a.app.Status.Deploy.Stdout |
| 47 | + assert.True(t, strings.HasPrefix(got, exec.TruncationMarker), |
| 48 | + "deploy stdout should start with truncation marker when over limit") |
| 49 | + assert.LessOrEqual(t, len(got), len(exec.TruncationMarker)+limit, |
| 50 | + "deploy stdout should not exceed marker + limit bytes") |
| 51 | +} |
| 52 | + |
| 53 | +func Test_updateLastDeploy_TruncatesLargeStderr(t *testing.T) { |
| 54 | + const limit = 50 |
| 55 | + a := newAppForTest(t, v1alpha1.App{ |
| 56 | + ObjectMeta: metav1.ObjectMeta{Name: "test-app", Namespace: "default"}, |
| 57 | + }, Opts{MaxStatusOutputBytes: limit}) |
| 58 | + a.app.Status.Deploy = &v1alpha1.AppStatusDeploy{} |
| 59 | + |
| 60 | + largeStderr := strings.Repeat("error line\n", 1000) |
| 61 | + a.updateLastDeploy(exec.CmdRunResult{Stderr: largeStderr, Finished: true, ExitCode: 1}) |
| 62 | + |
| 63 | + got := a.app.Status.Deploy.Stderr |
| 64 | + assert.True(t, strings.HasPrefix(got, exec.TruncationMarker)) |
| 65 | + assert.LessOrEqual(t, len(got), len(exec.TruncationMarker)+limit) |
| 66 | +} |
| 67 | + |
| 68 | +func Test_updateLastDeploy_NoTruncation_WhenUnderLimit(t *testing.T) { |
| 69 | + a := newAppForTest(t, v1alpha1.App{ |
| 70 | + ObjectMeta: metav1.ObjectMeta{Name: "test-app", Namespace: "default"}, |
| 71 | + }, Opts{MaxStatusOutputBytes: 10000}) |
| 72 | + a.app.Status.Deploy = &v1alpha1.AppStatusDeploy{} |
| 73 | + |
| 74 | + smallStdout := "Changes\n\nOp: 1 add\n" |
| 75 | + a.updateLastDeploy(exec.CmdRunResult{Stdout: smallStdout, Finished: true, ExitCode: 0}) |
| 76 | + |
| 77 | + assert.NotEmpty(t, a.app.Status.Deploy.Stdout) |
| 78 | + assert.False(t, strings.HasPrefix(a.app.Status.Deploy.Stdout, exec.TruncationMarker), |
| 79 | + "output under the limit must not be prefixed with the truncation marker") |
| 80 | +} |
| 81 | + |
| 82 | +func Test_updateLastDeploy_TailIsPreserved(t *testing.T) { |
| 83 | + const limit = 40 |
| 84 | + a := newAppForTest(t, v1alpha1.App{ |
| 85 | + ObjectMeta: metav1.ObjectMeta{Name: "test-app", Namespace: "default"}, |
| 86 | + }, Opts{MaxStatusOutputBytes: limit}) |
| 87 | + a.app.Status.Deploy = &v1alpha1.AppStatusDeploy{} |
| 88 | + |
| 89 | + // WithFriendlyYAMLStrings (called inside updateLastDeploy) strips trailing |
| 90 | + // whitespace, so do not include a trailing newline in the expected assertion. |
| 91 | + tail := "IMPORTANT: deploy succeeded" |
| 92 | + a.updateLastDeploy(exec.CmdRunResult{ |
| 93 | + Stdout: strings.Repeat("x", 500) + tail + "\n", |
| 94 | + Finished: true, |
| 95 | + ExitCode: 0, |
| 96 | + }) |
| 97 | + |
| 98 | + assert.Contains(t, a.app.Status.Deploy.Stdout, tail, |
| 99 | + "the tail (most recent output) must survive truncation") |
| 100 | +} |
| 101 | + |
| 102 | +func Test_setUsefulErrorMessage_TruncatesLargeStderr(t *testing.T) { |
| 103 | + const limit = 50 |
| 104 | + a := newAppForTest(t, v1alpha1.App{ |
| 105 | + ObjectMeta: metav1.ObjectMeta{Name: "test-app", Namespace: "default"}, |
| 106 | + }, Opts{MaxStatusOutputBytes: limit}) |
| 107 | + |
| 108 | + largeStderr := strings.Repeat("kapp: Error: resource conflict\n", 1000) |
| 109 | + a.setUsefulErrorMessage(exec.CmdRunResult{Stderr: largeStderr, ExitCode: 1}) |
| 110 | + |
| 111 | + got := a.app.Status.UsefulErrorMessage |
| 112 | + assert.True(t, strings.HasPrefix(got, exec.TruncationMarker)) |
| 113 | + assert.LessOrEqual(t, len(got), len(exec.TruncationMarker)+limit) |
| 114 | +} |
| 115 | + |
| 116 | +func Test_setUsefulErrorMessage_NoTruncation_WhenUnderLimit(t *testing.T) { |
| 117 | + a := newAppForTest(t, v1alpha1.App{ |
| 118 | + ObjectMeta: metav1.ObjectMeta{Name: "test-app", Namespace: "default"}, |
| 119 | + }, Opts{MaxStatusOutputBytes: 10000}) |
| 120 | + |
| 121 | + stderr := "kapp: Error: resource configmap/foo not found" |
| 122 | + a.setUsefulErrorMessage(exec.CmdRunResult{Stderr: stderr, ExitCode: 1}) |
| 123 | + |
| 124 | + assert.Equal(t, stderr, a.app.Status.UsefulErrorMessage) |
| 125 | +} |
0 commit comments