From aeb8025f43253b53668402e28008a58bb96020fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Sat, 27 Jul 2024 10:01:27 +0800 Subject: [PATCH 01/16] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91add=20dt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hook/process/process_test.go | 157 +++++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) diff --git a/hook/process/process_test.go b/hook/process/process_test.go index 2398e54..ac07262 100644 --- a/hook/process/process_test.go +++ b/hook/process/process_test.go @@ -17,10 +17,14 @@ package process import ( "context" + "io/fs" "os" "os/exec" + "reflect" + "strings" "testing" + "github.com/agiledragon/gomonkey/v2" "github.com/prashantv/gostub" "github.com/stretchr/testify/assert" ) @@ -209,3 +213,156 @@ func TestGetContainerConfig(t *testing.T) { getContainerConfig() } + +// FileInfoMock is used to test +type fileInfoMock struct { + os.FileInfo +} + +// TestReadConfigsOfDir tests the function readConfigsOfDir +func TestReadConfigsOfDir(t *testing.T) { + patch := gomonkey.ApplyFunc(os.Stat, func(name string) (os.FileInfo, error) { + return &fileInfoMock{}, nil + }) + defer patch.Reset() + patchSize := gomonkey.ApplyMethod(reflect.TypeOf(&fileInfoMock{}), "Mode", func(f *fileInfoMock) fs.FileMode { + return fs.ModeDir + }) + defer patchSize.Reset() + tests := []struct { + name string + dir string + configs []string + want []string + want1 []string + wantErr bool + }{ + { + name: "readConfigsOfDir success case 1", + want: []string{}, + want1: []string{}, + }, + { + name: "readConfigsOfDir fail case 2", + configs: []string{"base"}, + wantErr: true, + want: nil, + want1: nil, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, got1, err := readConfigsOfDir(tt.dir, tt.configs) + if (err == nil) == tt.wantErr { + t.Errorf("readConfigsOfDir() got = %v, want %v", err, tt.wantErr) + } + assert.Equalf(t, tt.want, got, "readConfigsOfDir(%v, %v)", tt.dir, tt.configs) + assert.Equalf(t, tt.want1, got1, "readConfigsOfDir(%v, %v)", tt.dir, tt.configs) + }) + } +} + +func TestDoPrestartHook(t *testing.T) { + tests := []struct { + name string + wantErr bool + }{ + { + name: "DoPrestartHook success case 1", + wantErr: true, + }, + } + conCfg := containerConfig{ + Pid: pidSample, + Rootfs: ".", + Env: []string{"ASCEND_VISIBLE_DEVICES=0-3,5,7", "ASCEND_RUNTIME_MOUNTS=a"}, + } + stub := gostub.StubFunc(&getContainerConfig, &conCfg, nil) + defer stub.Reset() + patch := gomonkey.ApplyFunc(os.Stat, func(name string) (os.FileInfo, error) { + return &fileInfoMock{}, nil + }) + defer patch.Reset() + patchReadConfigsOfDir := gomonkey.ApplyFunc(readConfigsOfDir, func(dir string, configs []string) ([]string, []string, error) { + return []string{}, []string{}, nil + }) + defer patchReadConfigsOfDir.Reset() + patchSize := gomonkey.ApplyMethod(reflect.TypeOf(&fileInfoMock{}), "Mode", func(f *fileInfoMock) fs.FileMode { + return fs.ModeDir + }) + defer patchSize.Reset() + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := DoPrestartHook() + if (err == nil) == tt.wantErr { + t.Errorf("DoPrestartHook() got = %v, want %v", err, tt.wantErr) + } + }) + } +} + +func Test_parseRuntimeOptions(t *testing.T) { + virtualValue := "VIRTUAL" + tests := []struct { + name string + runtimeOptions string + want []string + wantErr bool + }{ + { + name: "too long case 1", + runtimeOptions: strings.Repeat("a", 129), + wantErr: true, + }, + { + name: "invalid case 2", + runtimeOptions: "a", + wantErr: true, + }, + { + name: "success case 3", + runtimeOptions: virtualValue, + wantErr: false, + want: []string{virtualValue}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := parseRuntimeOptions(tt.runtimeOptions) + if (err == nil) == tt.wantErr { + t.Errorf("DoPrestartHook() got = %v, want %v", err, tt.wantErr) + } + assert.Equalf(t, tt.want, got, "parseRuntimeOptions(%v)", tt.runtimeOptions) + }) + } +} + +func Test_getArgs(t *testing.T) { + tests := []struct { + name string + cliPath string + containerConfig *containerConfig + fileMountList []string + dirMountList []string + allowLink string + want []string + }{ + { + name: "success case 1", + containerConfig: &containerConfig{}, + fileMountList: []string{"test"}, + dirMountList: []string{"test"}, + allowLink: "true", + cliPath: "testcli", + want: []string{"testcli", "--allow-link", "true", "--pid", "0", "--rootfs", "", + "--mount-file", "test", "--mount-dir", "test"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equalf(t, tt.want, getArgs(tt.cliPath, tt.containerConfig, tt.fileMountList, + tt.dirMountList, tt.allowLink), "getArgs(%v, %v, %v, %v, %v)", + tt.cliPath, tt.containerConfig, tt.fileMountList, tt.dirMountList, tt.allowLink) + }) + } +} -- Gitee From 3fef063af31ace4035fb7e6c5e246db924dac47f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Sat, 27 Jul 2024 10:17:07 +0800 Subject: [PATCH 02/16] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91add=20dt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hook/process/process_test.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/hook/process/process_test.go b/hook/process/process_test.go index ac07262..fd21899 100644 --- a/hook/process/process_test.go +++ b/hook/process/process_test.go @@ -214,7 +214,7 @@ func TestGetContainerConfig(t *testing.T) { getContainerConfig() } -// FileInfoMock is used to test +// fileInfoMock is used to test type fileInfoMock struct { os.FileInfo } @@ -262,6 +262,7 @@ func TestReadConfigsOfDir(t *testing.T) { } } +// TestDoPrestartHook tests the function DoPrestartHook func TestDoPrestartHook(t *testing.T) { tests := []struct { name string @@ -301,7 +302,8 @@ func TestDoPrestartHook(t *testing.T) { } } -func Test_parseRuntimeOptions(t *testing.T) { +// TestParseRuntimeOptions tests the function parseRuntimeOptions +func TestParseRuntimeOptions(t *testing.T) { virtualValue := "VIRTUAL" tests := []struct { name string @@ -337,7 +339,8 @@ func Test_parseRuntimeOptions(t *testing.T) { } } -func Test_getArgs(t *testing.T) { +// TestGetArgs tests the function getArgs +func TestGetArgs(t *testing.T) { tests := []struct { name string cliPath string -- Gitee From f004142a087a5c5188b75b13167fd16a9fcf8006 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Mon, 29 Jul 2024 10:54:38 +0800 Subject: [PATCH 03/16] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91clean=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- install/process/constant.go | 19 +++++++++++-------- install/process/containerd_process.go | 2 +- install/process/containerd_process_test.go | 17 ++++++++++++++++- install/process/docker_process.go | 4 ++-- runtime/dcmi/dcmi_api_test.go | 17 +++++++++-------- 5 files changed, 39 insertions(+), 20 deletions(-) diff --git a/install/process/constant.go b/install/process/constant.go index a5a461b..427b8ef 100644 --- a/install/process/constant.go +++ b/install/process/constant.go @@ -14,6 +14,8 @@ package process +import "os" + const commonTemplate = `{ "runtimes": { "ascend": { @@ -34,14 +36,15 @@ const noDefaultTemplate = `{ }` const ( - actionPosition = 0 - srcFilePosition = 1 - destFilePosition = 2 - runtimeFilePosition = 3 - rmCommandLength = 6 - addCommandLength = 7 - maxFileSize = 1024 * 1024 * 10 - cgroupInfoIndexFromEnd = 1 + actionPosition = 0 + srcFilePosition = 1 + destFilePosition = 2 + runtimeFilePosition = 3 + rmCommandLength = 6 + addCommandLength = 7 + maxFileSize = 1024 * 1024 * 10 + cgroupInfoIndexFromEnd = 1 + perm os.FileMode = 0600 ) const ( diff --git a/install/process/containerd_process.go b/install/process/containerd_process.go index c471eb4..6a79745 100644 --- a/install/process/containerd_process.go +++ b/install/process/containerd_process.go @@ -223,7 +223,7 @@ func writeContainerdConfigToFile(cfg config.Config, destFilePath string) error { hwlog.RunLog.Errorf("failed to marshall to toml, error: %v", err) return err } - file, err := os.Create(destFilePath) + file, err := os.OpenFile(destFilePath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, perm) if err != nil { hwlog.RunLog.Errorf("failed to create file, error: %v", err) return err diff --git a/install/process/containerd_process_test.go b/install/process/containerd_process_test.go index 6859e47..64e744b 100644 --- a/install/process/containerd_process_test.go +++ b/install/process/containerd_process_test.go @@ -1,3 +1,17 @@ +/* Copyright(C) 2024. Huawei Technologies Co.,Ltd. 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 process import ( @@ -97,7 +111,8 @@ func TestEditContainerdConfig(t *testing.T) { defer patch.Reset() for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if err := editContainerdConfig(tt.srcFilePath, tt.runtimeFilePath, tt.destFilePath, tt.action, tt.cgroupInfo); (err != nil) != tt.wantErr { + if err := editContainerdConfig(tt.srcFilePath, tt.runtimeFilePath, tt.destFilePath, + tt.action, tt.cgroupInfo); (err != nil) != tt.wantErr { t.Errorf("editContainerdConfig() error = %v, wantErr %v", err, tt.wantErr) } }) diff --git a/install/process/docker_process.go b/install/process/docker_process.go index 084061c..c2aeef7 100644 --- a/install/process/docker_process.go +++ b/install/process/docker_process.go @@ -142,7 +142,7 @@ func modifyDaemon(srcFilePath, runtimeFilePath, action string) (map[string]inter func addDockerDaemon(runtimeConfig, daemon map[string]interface{}, runtimeFilePath string, ) (map[string]interface{}, map[string]interface{}, error) { - if _, ok := runtimeConfig["ascend"]; !ok { + if _, ok := runtimeConfig["ascend"]; !ok && runtimeConfig != nil { runtimeConfig["ascend"] = map[string]interface{}{} } ascendConfig, ok := runtimeConfig["ascend"].(map[string]interface{}) @@ -153,7 +153,7 @@ func addDockerDaemon(runtimeConfig, daemon map[string]interface{}, runtimeFilePa if _, ok := ascendConfig["runtimeArgs"]; !ok { ascendConfig["runtimeArgs"] = []string{} } - if !reserveDefaultRuntime { + if !reserveDefaultRuntime && daemon != nil { daemon[defaultRuntimeKey] = "ascend" } return runtimeConfig, daemon, nil diff --git a/runtime/dcmi/dcmi_api_test.go b/runtime/dcmi/dcmi_api_test.go index 324bf93..0cf987f 100644 --- a/runtime/dcmi/dcmi_api_test.go +++ b/runtime/dcmi/dcmi_api_test.go @@ -70,7 +70,6 @@ func TestCreateVDevice(t *testing.T) { process := specs.Process{} spec := specs.Spec{Process: &process} spec.Process.Env = []string{} - var deviceIdList []int backups := 2 logMaxAge := 365 fileMaxSize := 2 @@ -86,6 +85,7 @@ func TestCreateVDevice(t *testing.T) { } // no split, all ok + var deviceIdList []int vdevice, err := CreateVDevice(&mockWorker{}, &spec, deviceIdList) if err != nil { t.Fatalf("%v %v", vdevice, err) @@ -129,13 +129,14 @@ func TestGetChipName(t *testing.T) { 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 - }) + 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 -- Gitee From d8730cccb56d5cf52935a71de37b9a8ba0746079 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Mon, 29 Jul 2024 14:35:56 +0800 Subject: [PATCH 04/16] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91clean=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cli/test/dt_go/build.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/test/dt_go/build.sh b/cli/test/dt_go/build.sh index 044255b..e66c57e 100644 --- a/cli/test/dt_go/build.sh +++ b/cli/test/dt_go/build.sh @@ -24,7 +24,7 @@ export GONOSUMDB="*" function execute_test() { cd ${TOP_DIR} - if ! (go test -mod=mod -gcflags=all=-l -v -race -coverprofile cov.out ${TOP_DIR}/... >./$file_input); then + if ! (go test -mod=mod -gcflags=all=-l -v -race -coverprofile cov.out ${TOP_DIR}/...); then echo '****** go test cases error! ******' exit 1 else -- Gitee From 2aaa18a05cca7709b5cefb67a07cd47cd18614c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Mon, 29 Jul 2024 14:41:32 +0800 Subject: [PATCH 05/16] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91clean=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hook/process/process_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/hook/process/process_test.go b/hook/process/process_test.go index fd21899..764c75f 100644 --- a/hook/process/process_test.go +++ b/hook/process/process_test.go @@ -186,6 +186,7 @@ func TestParseOciSpecFileCase3(t *testing.T) { defer os.Remove(configFile) _, err := parseOciSpecFile(configFile) if err != nil { + t.Logf("failed to parseOciSpecFile, err: %v", err) t.Fail() } } -- Gitee From 002c08870f5619661d479bca73ee33ff9bde0a5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Mon, 29 Jul 2024 14:55:52 +0800 Subject: [PATCH 06/16] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91clean=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hook/process/process.go | 2 +- hook/process/process_test.go | 25 +++++++++++++++++++++---- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/hook/process/process.go b/hook/process/process.go index 1f8a48c..c40663a 100644 --- a/hook/process/process.go +++ b/hook/process/process.go @@ -157,7 +157,7 @@ func parseSoftLinkMode(allowLink string) (string, error) { func parseOciSpecFile(file string) (*specs.Spec, error) { f, err := os.Open(file) if err != nil { - return nil, fmt.Errorf("failed to open the OCI config file: %s", file) + return nil, err } defer f.Close() diff --git a/hook/process/process_test.go b/hook/process/process_test.go index 764c75f..a0a88fe 100644 --- a/hook/process/process_test.go +++ b/hook/process/process_test.go @@ -17,6 +17,7 @@ package process import ( "context" + "encoding/json" "io/fs" "os" "os/exec" @@ -25,6 +26,7 @@ import ( "testing" "github.com/agiledragon/gomonkey/v2" + "github.com/opencontainers/runtime-spec/specs-go" "github.com/prashantv/gostub" "github.com/stretchr/testify/assert" ) @@ -179,16 +181,31 @@ func TestParseOciSpecFileCase2(t *testing.T) { // TestParseOciSpecFileCase3 test the function parseOciSpecFile func TestParseOciSpecFileCase3(t *testing.T) { - cmd := exec.Command("runc", "spec") - if err := cmd.Run(); err != nil { - t.Log("runc spec failed") + file, err := os.Create(configFile) + defer os.Remove(configFile) + defer file.Close() + if err != nil { + t.Log("create file failed") } + err = file.Chmod(fileMode0600) defer os.Remove(configFile) - _, err := parseOciSpecFile(configFile) + tsetSpec := specs.Spec{} + jsonData, err := json.MarshalIndent(tsetSpec, "", " ") + if err != nil { + t.Logf("failed to MarshalIndent, err: %v", err) + t.Fail() + } + _, err = file.Write(jsonData) + if err != nil { + t.Logf("failed to Write, err: %v", err) + t.Fail() + } + _, err = parseOciSpecFile(configFile) if err != nil { t.Logf("failed to parseOciSpecFile, err: %v", err) t.Fail() } + assert.Nil(t, err) } // TestGetContainerConfig test the function getContainerConfig -- Gitee From 6136da5e7bd980481faba3c66054887a83914a95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Mon, 29 Jul 2024 15:08:51 +0800 Subject: [PATCH 07/16] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91=E4=BF=AE=E5=A4=8Ddt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hook/process/process_test.go | 3 ++- runtime/process/process_test.go | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/hook/process/process_test.go b/hook/process/process_test.go index a0a88fe..e923ff4 100644 --- a/hook/process/process_test.go +++ b/hook/process/process_test.go @@ -18,6 +18,7 @@ package process import ( "context" "encoding/json" + "fmt" "io/fs" "os" "os/exec" @@ -205,7 +206,7 @@ func TestParseOciSpecFileCase3(t *testing.T) { t.Logf("failed to parseOciSpecFile, err: %v", err) t.Fail() } - assert.Nil(t, err) + assert.Equal(t, fmt.Errorf("invalid OCI spec for empty process"), err) } // TestGetContainerConfig test the function getContainerConfig diff --git a/runtime/process/process_test.go b/runtime/process/process_test.go index 3ba9fbd..b94c16f 100644 --- a/runtime/process/process_test.go +++ b/runtime/process/process_test.go @@ -118,7 +118,8 @@ func TestArgsIsCreateCase3(t *testing.T) { t.Log(execStubLog) return nil }) - + err = InitLogModule(context.Background()) + assert.Nil(t, err) err = DoProcess() assert.NotNil(t, err) } -- Gitee From 5c06021f193d67bdb54e72f31d7b3fe544df3012 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Mon, 29 Jul 2024 15:11:29 +0800 Subject: [PATCH 08/16] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91=E4=BF=AE=E5=A4=8Ddt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hook/process/process_test.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/hook/process/process_test.go b/hook/process/process_test.go index e923ff4..c5bce0d 100644 --- a/hook/process/process_test.go +++ b/hook/process/process_test.go @@ -202,10 +202,6 @@ func TestParseOciSpecFileCase3(t *testing.T) { t.Fail() } _, err = parseOciSpecFile(configFile) - if err != nil { - t.Logf("failed to parseOciSpecFile, err: %v", err) - t.Fail() - } assert.Equal(t, fmt.Errorf("invalid OCI spec for empty process"), err) } -- Gitee From 3b221db65f189a41591d915d8120f90e98a5bbef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Mon, 29 Jul 2024 15:32:59 +0800 Subject: [PATCH 09/16] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91=E4=BF=AE=E5=A4=8Ddt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hook/process/process.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hook/process/process.go b/hook/process/process.go index c40663a..5c315f5 100644 --- a/hook/process/process.go +++ b/hook/process/process.go @@ -157,7 +157,7 @@ func parseSoftLinkMode(allowLink string) (string, error) { func parseOciSpecFile(file string) (*specs.Spec, error) { f, err := os.Open(file) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to open the OCI config file: %s, err: %v", file, err) } defer f.Close() -- Gitee From d407bb3f7bb08fed1238e3c8f8581f463558583f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Mon, 29 Jul 2024 16:22:41 +0800 Subject: [PATCH 10/16] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91add=20dt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cli/test/dt_go/build.sh | 2 +- hook/process/process_test.go | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/cli/test/dt_go/build.sh b/cli/test/dt_go/build.sh index e66c57e..044255b 100644 --- a/cli/test/dt_go/build.sh +++ b/cli/test/dt_go/build.sh @@ -24,7 +24,7 @@ export GONOSUMDB="*" function execute_test() { cd ${TOP_DIR} - if ! (go test -mod=mod -gcflags=all=-l -v -race -coverprofile cov.out ${TOP_DIR}/...); then + 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 else diff --git a/hook/process/process_test.go b/hook/process/process_test.go index c5bce0d..6379873 100644 --- a/hook/process/process_test.go +++ b/hook/process/process_test.go @@ -37,6 +37,7 @@ const ( fileMode0600 os.FileMode = 0600 ascendVisibleDeviceTestStr = "ASCEND_VISIBLE_DEVICES=0-3,5,7" configFile = "config.json" + strRepeatTimes = 129 ) // TestDoPrestartHookCase1 test function DoPrestartHook @@ -328,7 +329,7 @@ func TestParseRuntimeOptions(t *testing.T) { }{ { name: "too long case 1", - runtimeOptions: strings.Repeat("a", 129), + runtimeOptions: strings.Repeat("a", strRepeatTimes), wantErr: true, }, { @@ -384,3 +385,33 @@ func TestGetArgs(t *testing.T) { }) } } + +// TestParseMounts tests the function parseMounts +func TestParseMounts(t *testing.T) { + tests := []struct { + name string + mounts string + want []string + }{ + { + name: "base case 1", + mounts: "", + want: []string{baseConfig}, + }, + { + name: "base case 2", + mounts: strings.Repeat("a", strRepeatTimes), + want: []string{baseConfig}, + }, + { + name: "other case 3", + mounts: "testList,testList1", + want: []string{"testlist", "testlist1"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equalf(t, tt.want, parseMounts(tt.mounts), "parseMounts(%v)", tt.mounts) + }) + } +} -- Gitee From e45eeeaea8008c52db0f6a99e47b6fdc1d801559 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Mon, 29 Jul 2024 17:07:05 +0800 Subject: [PATCH 11/16] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91add=20dt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hook/process/process_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hook/process/process_test.go b/hook/process/process_test.go index 6379873..e545064 100644 --- a/hook/process/process_test.go +++ b/hook/process/process_test.go @@ -191,8 +191,8 @@ func TestParseOciSpecFileCase3(t *testing.T) { } err = file.Chmod(fileMode0600) defer os.Remove(configFile) - tsetSpec := specs.Spec{} - jsonData, err := json.MarshalIndent(tsetSpec, "", " ") + testSpec := specs.Spec{} + jsonData, err := json.MarshalIndent(testSpec, "", " ") if err != nil { t.Logf("failed to MarshalIndent, err: %v", err) t.Fail() -- Gitee From a012b0a8f6b9ecfb0466d16fd9f62b5a10dc7ce7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Tue, 30 Jul 2024 12:09:19 +0800 Subject: [PATCH 12/16] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91=E6=9B=B4=E6=96=B0=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index bbc7820..acbe5d3 100644 --- a/README.md +++ b/README.md @@ -65,12 +65,14 @@ root@#:/home/test/ascend-docker-runtime/output# ll # 更新日志 -| 版本 | 发布日期 | 修改说明 | -|:----------:|:----------:|:-----------------:| -| v3.0.0 | 2023-01-18 | 第一次发布 | -| v5.0.0-RC1 | 2023-04-18 | 配套MindX 5.0.RC1版本 | -| v5.0.0-RC2 | 2023-07-18 | 配套MindX 5.0.RC2版本 | -| v5.0.0-RC3 | 2023-10-27 | 配套MindX 5.0.RC3版本 | -| v5.0.0 | 2023-12-29 | 配套MindX 5.0.0版本 | -| v6.0.0-RC1 | 2024-04-22 | 配套MindX 6.0.RC1版本 | - +| 版本 | 发布日期 | 修改说明 | +|:-------------:|:----------:|:-----------------:| +| v3.0.0 | 2023-01-18 | 第一次发布 | +| v5.0.0-RC1 | 2023-04-18 | 配套MindX 5.0.RC1版本 | +| v5.0.0-RC2 | 2023-07-18 | 配套MindX 5.0.RC2版本 | +| v5.0.0-RC3 | 2023-10-27 | 配套MindX 5.0.RC3版本 | +| v5.0.0 | 2023-12-29 | 配套MindX 5.0.0版本 | +| v6.0.0-RC1 | 2024-04-22 | 配套MindX 6.0.RC1版本 | +| v5.0.1 | 2024-05-18 | MindX 5.0.1补丁版本 | +| v5.0.1-Patch1 | 2024-06-26 | MindX 5.0.1.1补丁版本 | +| v6.0.0-RC2 | 2024-07-16 | 配套MindX 6.0.RC2版本 | -- Gitee From a04c69b36220904dc7daa962fb34dd1bdc33a042 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Tue, 30 Jul 2024 14:28:46 +0800 Subject: [PATCH 13/16] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91=E6=9B=B4=E6=96=B0readme?= =?UTF-8?q?=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index acbe5d3..39566e9 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ root@#:/home/test/ascend-docker-runtime/output# ll ``` # 组件安装 -请参考[《MindX DL用户指南》--Ascend Docker Runtime用户指南](https://www.hiascend.com/document/detail/zh/mindx-dl/60rc1/clusterscheduling/dockerruntimeug/dlruntime_ug_005.html)中“安装Ascend Docker Runtime”章节进行。 +请参考[《MindX DL集群调度安装指南》--安装部署](https://www.hiascend.com/document/detail/zh/mindx-dl/60rc2/clusterscheduling/clusterschedulingig/clusterschedulingig/dlug_installation_012.html)中“Ascend Docker Runtime”章节进行。 # 更新日志 -- Gitee From 268c5c2dfe699b7c1800f17fd0ed492406cc40d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Tue, 30 Jul 2024 19:53:31 +0800 Subject: [PATCH 14/16] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91clean=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- install/process/containerd_process.go | 3 +++ install/process/docker_process.go | 4 +++- install/process/docker_process_test.go | 6 ++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/install/process/containerd_process.go b/install/process/containerd_process.go index 6a79745..07e2e6a 100644 --- a/install/process/containerd_process.go +++ b/install/process/containerd_process.go @@ -29,6 +29,9 @@ import ( // ContainerdProcess modifies the containerd configuration file when installing or uninstalling the containerd scenario. func ContainerdProcess(command []string) (string, error) { + if len(command) == 0 { + return "", fmt.Errorf("error param, length of command is 0") + } action := command[actionPosition] correctParam, behavior := checkParamAndGetBehavior(action, command) if !correctParam { diff --git a/install/process/docker_process.go b/install/process/docker_process.go index c2aeef7..c7a6787 100644 --- a/install/process/docker_process.go +++ b/install/process/docker_process.go @@ -29,7 +29,9 @@ var reserveDefaultRuntime = false // DockerProcess modifies the docker configuration file when installing or uninstalling the docker scenario. func DockerProcess(command []string) (string, error) { - + if len(command) == 0 { + return "", fmt.Errorf("error param, length of command is 0") + } action := command[actionPosition] correctParam, behavior := checkParamAndGetBehavior(action, command) if !correctParam { diff --git a/install/process/docker_process_test.go b/install/process/docker_process_test.go index 5ae454e..38532f7 100644 --- a/install/process/docker_process_test.go +++ b/install/process/docker_process_test.go @@ -245,6 +245,12 @@ func getTestDockerProcessCases() []testProcessArg { WantErr: true, WantResult: addBehavior, }, + { + Name: "error param case 5", + Command: []string{}, + WantErr: true, + WantResult: emptyStr, + }, } } -- Gitee From b12c18266b06bb2827720f004218d75ac875d835 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Wed, 31 Jul 2024 09:07:36 +0800 Subject: [PATCH 15/16] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91clean=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hook/process/process_test.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/hook/process/process_test.go b/hook/process/process_test.go index e545064..1cb5ff5 100644 --- a/hook/process/process_test.go +++ b/hook/process/process_test.go @@ -184,23 +184,27 @@ func TestParseOciSpecFileCase2(t *testing.T) { // TestParseOciSpecFileCase3 test the function parseOciSpecFile func TestParseOciSpecFileCase3(t *testing.T) { file, err := os.Create(configFile) - defer os.Remove(configFile) - defer file.Close() if err != nil { t.Log("create file failed") + t.FailNow() } - err = file.Chmod(fileMode0600) defer os.Remove(configFile) + defer file.Close() + err = file.Chmod(fileMode0600) + if err != nil { + t.Log("chmod file failed") + t.FailNow() + } testSpec := specs.Spec{} jsonData, err := json.MarshalIndent(testSpec, "", " ") if err != nil { t.Logf("failed to MarshalIndent, err: %v", err) - t.Fail() + t.FailNow() } _, err = file.Write(jsonData) if err != nil { t.Logf("failed to Write, err: %v", err) - t.Fail() + t.FailNow() } _, err = parseOciSpecFile(configFile) assert.Equal(t, fmt.Errorf("invalid OCI spec for empty process"), err) -- Gitee From cc95c5c866e0ac5dd8ac4499c0c080209206fc75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Wed, 31 Jul 2024 16:56:18 +0800 Subject: [PATCH 16/16] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91clean=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- install/process/docker_process.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/install/process/docker_process.go b/install/process/docker_process.go index c7a6787..67b99c9 100644 --- a/install/process/docker_process.go +++ b/install/process/docker_process.go @@ -144,7 +144,10 @@ func modifyDaemon(srcFilePath, runtimeFilePath, action string) (map[string]inter func addDockerDaemon(runtimeConfig, daemon map[string]interface{}, runtimeFilePath string, ) (map[string]interface{}, map[string]interface{}, error) { - if _, ok := runtimeConfig["ascend"]; !ok && runtimeConfig != nil { + if runtimeConfig == nil { + return nil, daemon, fmt.Errorf("runtime config is nil") + } + if _, ok := runtimeConfig["ascend"]; !ok { runtimeConfig["ascend"] = map[string]interface{}{} } ascendConfig, ok := runtimeConfig["ascend"].(map[string]interface{}) -- Gitee