* Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
*
* 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 debug
import (
"bytes"
"fmt"
"io"
"testing"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"cli/constant"
)
type MockCmdIO struct {
mock.Mock
}
func (m *MockCmdIO) In() io.Reader {
args := m.Called()
return args.Get(0).(io.Reader)
}
func (m *MockCmdIO) Out() io.Writer {
args := m.Called()
return args.Get(0).(io.Writer)
}
func (m *MockCmdIO) ErrOut() io.Writer {
args := m.Called()
return args.Get(0).(io.Writer)
}
func TestInitYrDebug(t *testing.T) {
mockCmdIO := &MockCmdIO{}
mockCmdIO.On("In").Return(bytes.NewBufferString(""))
mockCmdIO.On("Out").Return(bytes.NewBuffer(nil))
mockCmdIO.On("ErrOut").Return(bytes.NewBuffer(nil))
cmd := InitYrDebug(mockCmdIO)
assert.NotNil(t, cmd)
assert.Equal(t, "debug", cmd.Use)
assert.Equal(t, fmt.Sprintf("Enter the %s debug shell", constant.PlatformName), cmd.Short)
}
func TestYrDebug(t *testing.T) {
mockCmdIO := &MockCmdIO{}
mockCmdIO.On("In").Return(bytes.NewBufferString(""))
mockCmdIO.On("Out").Return(bytes.NewBuffer(nil))
mockCmdIO.On("ErrOut").Return(bytes.NewBuffer(nil))
opts.cmdIO = mockCmdIO
err := yrDebug(&cobra.Command{}, []string{})
assert.NoError(t, err)
}
func TestStartDebugShell(t *testing.T) {
mockCmdIO := &MockCmdIO{}
mockCmdIO.On("In").Return(bytes.NewBufferString("quit\n"))
mockCmdIO.On("Out").Return(bytes.NewBuffer(nil))
mockCmdIO.On("ErrOut").Return(bytes.NewBuffer(nil))
opts.cmdIO = mockCmdIO
startDebugShell()
output := mockCmdIO.Out().(*bytes.Buffer).String()
expectedOutput := "Type 'help' or 'h' for help, 'quit' or 'q' to exit.\n\n"
assert.Contains(t, output, expectedOutput, "startDebugShell output should contain expected output")
}
func TestHandleHelp(t *testing.T) {
mockCmdIO := &MockCmdIO{}
mockCmdIO.On("In").Return(bytes.NewBufferString(""))
mockCmdIO.On("Out").Return(bytes.NewBuffer(nil))
mockCmdIO.On("ErrOut").Return(bytes.NewBuffer(nil))
err := handleHelp(mockCmdIO, []string{"help"})
assert.NoError(t, err)
}
func TestHandleQuit(t *testing.T) {
mockCmdIO := &MockCmdIO{}
mockCmdIO.On("In").Return(bytes.NewBufferString(""))
mockCmdIO.On("Out").Return(bytes.NewBuffer(nil))
mockCmdIO.On("ErrOut").Return(bytes.NewBuffer(nil))
err := handleQuit(mockCmdIO, []string{"quit"})
assert.NoError(t, err)
}
func TestHandleInstance(t *testing.T) {
mockCmdIO := &MockCmdIO{}
mockCmdIO.On("In").Return(bytes.NewBufferString(""))
mockCmdIO.On("Out").Return(bytes.NewBuffer(nil))
mockCmdIO.On("ErrOut").Return(bytes.NewBuffer(nil))
opts.cmdIO = mockCmdIO
err := handleInstance([]string{"instance", "12345"})
assert.NoError(t, err)
}
func TestHandleInfo(t *testing.T) {
mockCmdIO := &MockCmdIO{}
mockCmdIO.On("In").Return(bytes.NewBufferString(""))
mockCmdIO.On("Out").Return(bytes.NewBuffer(nil))
mockCmdIO.On("ErrOut").Return(bytes.NewBuffer(nil))
err := handleInfo(mockCmdIO, []string{"info", "instance"})
assert.NoError(t, err)
}