diff --git a/cli/test/dt_go/build.sh b/cli/test/dt_go/build.sh index a61cd5ab62586134dc61b87bdbcaa1d64d3cfb55..044255be974e4a60c93fac78fea69d361b5d4a9d 100644 --- a/cli/test/dt_go/build.sh +++ b/cli/test/dt_go/build.sh @@ -24,10 +24,6 @@ export GONOSUMDB="*" function execute_test() { cd ${TOP_DIR} - go mod tidy - go install github.com/axw/gocov/gocov@v1.0.0 - go install github.com/matm/gocov-html@latest - go install gotest.tools/gotestsum@latest if ! (go test -mod=mod -gcflags=all=-l -v -race -coverprofile cov.out ${TOP_DIR}/... >./$file_input); then echo '****** go test cases error! ******' exit 1 diff --git a/runtime/dcmi/dcmi_api_test.go b/runtime/dcmi/dcmi_api_test.go index 1a9ef5576d75944a9a396d377fb870c7a038c144..324bf93db29e9c9ae5e76c24ad5259ee1ad21785 100644 --- a/runtime/dcmi/dcmi_api_test.go +++ b/runtime/dcmi/dcmi_api_test.go @@ -17,8 +17,10 @@ package dcmi import ( "context" + "reflect" "testing" + "github.com/agiledragon/gomonkey/v2" "github.com/opencontainers/runtime-spec/specs-go" "huawei.com/npu-exporter/v5/common-utils/hwlog" ) @@ -108,3 +110,91 @@ func TestCreateVDevice(t *testing.T) { } } + +// TestGetChipName tests the function GetChipName +func TestGetChipName(t *testing.T) { + patchInitialize := gomonkey.ApplyMethod(reflect.TypeOf(&NpuWorker{}), "Initialize", func(f *NpuWorker) error { + return nil + }) + defer patchInitialize.Reset() + patchShutDown := gomonkey.ApplyMethod(reflect.TypeOf(&NpuWorker{}), "ShutDown", func(f *NpuWorker) { + return + }) + defer patchShutDown.Reset() + patch := gomonkey.ApplyFunc(GetCardList, func() (int32, []int32, error) { + return 1, []int32{0}, nil + }) + defer patch.Reset() + patchGetDeviceNumInCard := gomonkey.ApplyFunc(GetDeviceNumInCard, func(cardID int32) (int32, error) { + return 1, nil + }) + defer patchGetDeviceNumInCard.Reset() + patchGetChipInfo := gomonkey.ApplyMethod(reflect.TypeOf(&NpuWorker{}), "GetChipInfo", func(f *NpuWorker, cardID int32, deviceID int32) (*ChipInfo, error) { + return &ChipInfo{ + Name: "a", + Type: "b", + Version: "1", + }, nil + }) + defer patchGetChipInfo.Reset() + tests := []struct { + name string + want string + wantErr bool + }{ + { + name: "GetChipName success 1", + want: "a", + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := GetChipName() + if (err != nil) != tt.wantErr { + t.Errorf("GetChipName() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("GetChipName() got = %v, want %v", got, tt.want) + } + }) + } +} + +// TestGetProductType tests the function GetProductType +func TestGetProductType(t *testing.T) { + patch := gomonkey.ApplyFunc(GetCardList, func() (int32, []int32, error) { + return 1, []int32{0}, nil + }) + defer patch.Reset() + patchGetDeviceNumInCard := gomonkey.ApplyFunc(GetDeviceNumInCard, func(cardID int32) (int32, error) { + return 1, nil + }) + defer patchGetDeviceNumInCard.Reset() + tests := []struct { + name string + w WorkerInterface + want string + wantErr bool + }{ + { + name: "GetProductType success case 1", + w: &mockWorker{}, + want: "", + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := GetProductType(tt.w) + if (err != nil) != tt.wantErr { + t.Errorf("GetProductType() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("GetProductType() got = %v, want %v", got, tt.want) + } + }) + } +}