From 81562142debbd6535e59094f8764aefa217ecbb9 Mon Sep 17 00:00:00 2001 From: mochizuki875 Date: Fri, 6 Sep 2024 02:31:14 +0000 Subject: [PATCH] Add warning message for attach Kubernetes-commit: 97dd6dc284682d76fe7e0bc51df851473deee24f --- pkg/cmd/attach/attach.go | 1 + pkg/cmd/attach/attach_test.go | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/pkg/cmd/attach/attach.go b/pkg/cmd/attach/attach.go index 73b3cea7d..caf6ec7f1 100644 --- a/pkg/cmd/attach/attach.go +++ b/pkg/cmd/attach/attach.go @@ -307,6 +307,7 @@ func (o *AttachOptions) Run() error { } if !o.Quiet { + _, _ = fmt.Fprintln(o.ErrOut, "All commands and output from this session will be recorded in container logs, including credentials and sensitive information passed through the command prompt.") fmt.Fprintln(o.ErrOut, "If you don't see a command prompt, try pressing enter.") } if err := t.Safe(o.AttachFunc(o, containerToAttach, t.Raw, sizeQueue)); err != nil { diff --git a/pkg/cmd/attach/attach_test.go b/pkg/cmd/attach/attach_test.go index ac217e08c..6a599656e 100644 --- a/pkg/cmd/attach/attach_test.go +++ b/pkg/cmd/attach/attach_test.go @@ -242,6 +242,7 @@ func TestAttach(t *testing.T) { pod *corev1.Pod remoteAttachErr bool expectedErr string + expectedErrOut []string }{ { name: "pod attach", @@ -251,6 +252,10 @@ func TestAttach(t *testing.T) { attachPath: "/api/" + version + "/namespaces/test/pods/foo/attach", pod: attachPod(), container: "bar", + expectedErrOut: []string{ + "All commands and output from this session will be recorded in container logs, including credentials and sensitive information passed through the command prompt.", + "If you don't see a command prompt, try pressing enter.", + }, }, { name: "pod attach error", @@ -305,10 +310,11 @@ func TestAttach(t *testing.T) { if test.remoteAttachErr { remoteAttach.err = fmt.Errorf("attach error") } + streams, _, _, errOut := genericiooptions.NewTestIOStreams() options := &AttachOptions{ StreamOptions: exec.StreamOptions{ ContainerName: test.container, - IOStreams: genericiooptions.NewTestIOStreamsDiscard(), + IOStreams: streams, }, Attach: remoteAttach, GetPodTimeout: 1000, @@ -349,6 +355,14 @@ func TestAttach(t *testing.T) { if remoteAttach.url.Query().Get("container") != "bar" { t.Errorf("%s: Did not have query parameters: %s", test.name, remoteAttach.url.Query()) } + if test.expectedErrOut != nil { + for _, expect := range test.expectedErrOut { + if !strings.Contains(errOut.String(), expect) { + t.Errorf("%s: expected message %s not found, got: %s", test.name, expect, strings.ReplaceAll(errOut.String(), "\n", "")) + return + } + } + } }) } }