Skip to content
Open
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
2 changes: 1 addition & 1 deletion cmd/ocm-backplane/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ func runLogin(cmd *cobra.Command, argv []string) (err error) {
if !isHostedControlPlane {
return fmt.Errorf("manifestworks are only available for hosted control plane clusters")
}
listManifestWork := fmt.Sprintf("ocm backplane elevate -- oc get manifestworks -n %s -l api.openshift.com/id=%s", managingClusterName, targetClusterID)
listManifestWork := fmt.Sprintf("ocm-backplane elevate -n -- oc get manifestworks -n %s -l api.openshift.com/id=%s", managingClusterName, targetClusterID)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[high] correctness

The printed elevate command includes 'oc' after '--', but with the '-n' flag, 'oc' is no longer consumed as the reason argument. The elevate command internally prepends 'oc' via exec.Command("oc", argv[1:]...), so the resulting execution would be 'oc oc get manifestworks...' which fails. All documented examples (CONTRIBUTING.md, README.md) confirm that 'oc' should NOT appear after '--'.

Suggested fix: Remove 'oc' from the format string: fmt.Sprintf("ocm-backplane elevate -n -- get manifestworks -n %s -l api.openshift.com/id=%s", managingClusterName, targetClusterID)


fmt.Println("A list of associated manifestwork for your given cluster can be found using:")
fmt.Println("\t", listManifestWork)
Expand Down
30 changes: 30 additions & 0 deletions cmd/ocm-backplane/login/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,36 @@ var _ = Describe("Login command", func() {
Expect(err).To(BeNil())
})

It("should print correct elevate command with -n flag and ocm-backplane binary name for service login", func() {
globalOpts.Service = true
err := utils.CreateTempKubeConfig(nil)
Expect(err).To(BeNil())
mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes()
mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil)
mockOcmInterface.EXPECT().GetManagingCluster(trueClusterID).Return(managingClusterID, managingClusterID, true, nil).AnyTimes()
mockOcmInterface.EXPECT().GetServiceCluster(trueClusterID).Return(serviceClusterID, serviceClusterName, nil)
mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(serviceClusterID)).Return(false, nil).AnyTimes()
mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil)
mockClientUtil.EXPECT().MakeRawBackplaneAPIClientWithAccessToken(backplaneAPIURI, testToken).Return(mockClient, nil)
mockClient.EXPECT().LoginCluster(gomock.Any(), gomock.Eq(serviceClusterID)).Return(fakeResp, nil)

// Capture stdout to verify the elevate command output
old := os.Stdout

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[low] test-quality

os.Stdout is reassigned without a deferred restore. If runLogin or io.ReadAll panics, os.Stdout remains pointed at a closed pipe, corrupting subsequent test output. The os.Pipe() error is also silently discarded.

Suggested fix: Use 'defer func() { os.Stdout = old }()' immediately after saving the old value, and check the error from os.Pipe().

r, w, _ := os.Pipe()
os.Stdout = w

err = runLogin(nil, []string{testClusterID})

w.Close()
out, _ := io.ReadAll(r)
os.Stdout = old

Expect(err).To(BeNil())
output := string(out)
Expect(output).To(ContainSubstring("ocm-backplane elevate -n --"))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[low] test-quality

The test assertions only check for substring presence of 'ocm-backplane elevate -n --' and absence of 'ocm backplane elevate --', but don't detect the double-'oc' bug. The assertions should verify the full command pattern including what follows '--'.

Suggested fix: Add assertion: Expect(output).To(ContainSubstring("-- get manifestworks")) and Expect(output).NotTo(ContainSubstring("-- oc "))

Expect(output).NotTo(ContainSubstring("ocm backplane elevate --"))
})

It("should login to current cluster if cluster id not provided", func() {
loginType = LoginTypeExistingKubeConfig
err := utils.CreateTempKubeConfig(nil)
Expand Down