Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
songlinfeng
container-toolkit
Commits
fcdba4f3
Commit
fcdba4f3
authored
Oct 11, 2025
by
songlf
Browse files
add dcu-ctk test
parent
47fd5aa5
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
273 additions
and
0 deletions
+273
-0
cmd/dcu-ctk/main.go
cmd/dcu-ctk/main.go
+70
-0
cmd/dcu-ctk/main_test.go
cmd/dcu-ctk/main_test.go
+203
-0
No files found.
cmd/dcu-ctk/main.go
0 → 100644
View file @
fcdba4f3
/**
# Copyright (c) Advanced Micro Devices, Inc. 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
main
import
(
"fmt"
"os"
"developer.sourcefind.cn/codes/songlinfeng/container-toolkit/cmd/dcu-ctk/runtime"
"developer.sourcefind.cn/codes/songlinfeng/container-toolkit/internal/logger"
"github.com/urfave/cli/v2"
)
var
(
Version
=
"dev"
BuildDate
=
"unknown"
GitCommit
=
"none"
)
func
showVersion
()
*
cli
.
Command
{
showVersionCmd
:=
cli
.
Command
{
Name
:
"version"
,
Usage
:
"Show the version"
,
Action
:
func
(
c
*
cli
.
Context
)
error
{
fmt
.
Printf
(
"Version: %s
\n
Build Date: %s
\n
Git Commit: %s
\n
"
,
Version
,
BuildDate
,
GitCommit
)
return
nil
},
}
return
&
showVersionCmd
}
func
main
()
{
logger
.
Init
(
false
)
//Create the top-level CLI tree
dcuCtkCli
:=
&
cli
.
App
{
Name
:
"DCU Container Toolkit CLI"
,
EnableBashCompletion
:
true
,
Usage
:
"Tool to configure DCU Container Toolkit"
,
}
// Add subcommands
dcuCtkCli
.
Commands
=
[]
*
cli
.
Command
{
showVersion
(),
runtime
.
AddNewCommand
(),
//cdi.AddNewCommand(),
}
err
:=
dcuCtkCli
.
Run
(
os
.
Args
)
if
err
!=
nil
{
fmt
.
Printf
(
"%v
\n
"
,
err
)
os
.
Exit
(
1
)
}
}
\ No newline at end of file
cmd/dcu-ctk/main_test.go
0 → 100644
View file @
fcdba4f3
package
main
import
(
"bytes"
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
"testing"
)
const
(
configFile
=
"/tmp/testConfig.json"
dcuRuntimePath
=
"dcu-container-runtime"
dcuRuntimeName
=
"dcu"
removeAndDefErrMsg
=
"remove flag cannot be used along with set-as-default flag"
setUnsetDefErrMsg
=
"both set and unset as default cannot be used at the same time"
)
var
cliPath
=
os
.
Getenv
(
"DCU_CTK_PATH"
)
//Helper function to run the CLI command and return the output/error
func
runCLI
(
args
...
string
)(
string
,
string
,
error
)
{
cmd
:=
exec
.
Command
(
cliPath
,
args
...
)
var
stdout
,
stderr
bytes
.
Buffer
cmd
.
Stdout
=
&
stdout
cmd
.
Stderr
=
&
stderr
err
:=
cmd
.
Run
()
return
stdout
.
String
(),
stderr
.
String
(),
err
}
func
setup
(
t
*
testing
.
T
){
if
cliPath
==
""
{
t
.
Fatalf
(
"cliPath is not set, usage: export DCU_CTK_PATH=<path to dcu-ctk executable>; go test -v"
)
}
if
_
,
err
:=
os
.
Stat
(
cliPath
);
os
.
IsNotExist
(
err
)
{
t
.
Fatalf
(
"dcu-ctk is not built, please run 'make container-toolkit-ctk'"
)
}
}
func
verifyConfigFile
(
t
*
testing
.
T
,
isDefault
bool
,
isEmpty
bool
)
{
type
features
struct
{
Cdi
bool
`json:"cdi"`
}
type
runtimeConfig
struct
{
Args
[]
string
`json:"args"`
Path
string
`json:"path"`
}
type
runtimes
map
[
string
]
runtimeConfig
type
config
struct
{
DefaultRuntime
string
`json:"defaultRuntime"`
Features
features
`json:"features"`
Runtimes
runtimes
`json:"runtimes"`
}
// read the configFile
_
,
err
:=
os
.
Stat
(
configFile
)
Assert
(
t
,
os
.
IsNotExist
(
err
)
==
false
,
fmt
.
Sprintf
(
"config file: %v doesn't exist"
,
configFile
))
cfg
:=
config
{}
fmt
.
Printf
(
"Loading configuration from: %v
\n
"
,
configFile
)
readB
,
err
:=
os
.
ReadFile
(
configFile
)
Assert
(
t
,
err
==
nil
,
fmt
.
Sprintf
(
"Error reading config file: %v, err: %v"
,
configFile
,
err
))
reader
:=
bytes
.
NewReader
(
readB
)
err
=
json
.
NewDecoder
(
reader
)
.
Decode
(
&
cfg
)
Assert
(
t
,
err
==
nil
,
fmt
.
Sprintf
(
"Error decoding file: %v, err: %v"
,
configFile
,
err
))
fmt
.
Printf
(
"config: %+v
\n
"
,
cfg
)
if
isEmpty
{
// verify the config is removed
//Assert(t, cfg.Features.Cdi == false, "CDI is enabled in the config file")
Assert
(
t
,
len
(
cfg
.
Runtimes
)
==
0
,
"Number of runtimes in config is not 0"
)
Assert
(
t
,
cfg
.
DefaultRuntime
==
""
,
fmt
.
Sprintf
(
"default runtime should not be set to %v"
,
cfg
.
DefaultRuntime
))
}
else
{
//Assert(t, cfg.Features.Cdi == true, "CDI is not enabled in the config file")
Assert
(
t
,
len
(
cfg
.
Runtimes
)
==
1
,
"Number of runtimes in config is not 1"
)
rtime
,
exists
:=
cfg
.
Runtimes
[
"dcu"
]
Assert
(
t
,
exists
==
true
,
"dcu runtime doesn't exist in the config file"
)
Assert
(
t
,
rtime
.
Path
==
dcuRuntimePath
,
fmt
.
Sprintf
(
"dcu runtime path isn't set to %v"
,
dcuRuntimePath
))
if
isDefault
{
Assert
(
t
,
cfg
.
DefaultRuntime
==
dcuRuntimeName
,
fmt
.
Sprintf
(
"default runtime not set to %v"
,
dcuRuntimeName
))
}
else
{
Assert
(
t
,
cfg
.
DefaultRuntime
==
""
,
fmt
.
Sprintf
(
"default runtime should not be set to %v"
,
cfg
.
DefaultRuntime
))
}
}
}
func
Assert
(
t
*
testing
.
T
,
b
bool
,
errString
string
)
{
if
!
b
{
t
.
Errorf
(
errString
)
}
}
func
cleanUp
(
t
*
testing
.
T
)
{
fmt
.
Printf
(
"Deleting file: %v
\n
"
,
configFile
)
os
.
Remove
(
configFile
)
}
func
TestConfigureRunTimeAddRemove
(
t
*
testing
.
T
)
{
fmt
.
Printf
(
"dcu-ctk path: %v
\n
"
,
cliPath
)
setup
(
t
)
cfgPathArg
:=
"--config-path="
+
configFile
// add dcu to runtimes
out
,
outErr
,
err
:=
runCLI
(
"runtime"
,
"configure"
,
"--runtime=docker"
,
cfgPathArg
)
Assert
(
t
,
outErr
==
""
,
fmt
.
Sprintf
(
"dcu-ctk runtime configure returned err: %v"
,
outErr
))
Assert
(
t
,
err
==
nil
,
fmt
.
Sprintf
(
"Error running dcu-ctk err: %v"
,
err
))
fmt
.
Println
(
"output: "
,
out
)
verifyConfigFile
(
t
,
false
,
false
)
// remove dcu from runtimes
out
,
outErr
,
err
=
runCLI
(
"runtime"
,
"configure"
,
"--runtime=docker"
,
cfgPathArg
,
"--remove"
)
Assert
(
t
,
outErr
==
""
,
fmt
.
Sprintf
(
"dcu-ctk runtime configure remove returned err: %v"
,
outErr
))
Assert
(
t
,
err
==
nil
,
fmt
.
Sprintf
(
"Error running dcu-ctk err: %v"
,
err
))
fmt
.
Println
(
"output: "
,
out
)
verifyConfigFile
(
t
,
false
,
true
)
cleanUp
()
}
func
TestConfigureRunTimeDefault
(
t
*
testing
.
T
)
{
fmt
.
Printf
(
"dcu-ctk path: %v
\n
"
,
cliPath
)
setup
(
t
)
cfgPathArg
:=
"--config-path="
+
configFile
// add dcu to runtimes
out
,
outErr
,
err
:=
runCLI
(
"runtime"
,
"configure"
,
"--runtime=docker"
,
cfgPathArg
,
"--set-as-default"
)
Assert
(
t
,
outErr
==
""
,
fmt
.
Sprintf
(
"dcu-ctk runtime configure as default returned err: %v"
,
outErr
))
Assert
(
t
,
err
==
nil
,
fmt
.
Sprintf
(
"Error running dcu-ctk err: %v"
,
err
))
fmt
.
Println
(
"output: "
,
out
)
verifyConfigFile
(
t
,
true
,
false
)
// unset as default
out
,
outErr
,
err
=
runCLI
(
"runtime"
,
"configure"
,
"--runtime=docker"
,
cfgPathArg
,
"--unset-as-default"
)
Assert
(
t
,
outErr
==
""
,
fmt
.
Sprintf
(
"dcu-ctk runtime configure unset default returned err: %v"
,
outErr
))
Assert
(
t
,
err
==
nil
,
fmt
.
Sprintf
(
"Error running dcu-ctk err: %v"
,
err
))
fmt
.
Println
(
"output: "
,
out
)
verifyConfigFile
(
t
,
false
,
false
)
// add it back
out
,
outErr
,
err
=
runCLI
(
"runtime"
,
"configure"
,
"--runtime=docker"
,
cfgPathArg
,
"--set-as-default"
)
Assert
(
t
,
outErr
==
""
,
fmt
.
Sprintf
(
"dcu-ctk runtime configure as default returned err: %v"
,
outErr
))
Assert
(
t
,
err
==
nil
,
fmt
.
Sprintf
(
"Error running dcu-ctk err: %v"
,
err
))
fmt
.
Println
(
"output: "
,
out
)
verifyConfigFile
(
t
,
true
,
false
)
// use remove flag and make sure default gets deleted too
out
,
outErr
,
err
=
runCLI
(
"runtime"
,
"configure"
,
"--runtime=docker"
,
cfgPathArg
,
"--remove"
)
Assert
(
t
,
outErr
==
""
,
fmt
.
Sprintf
(
"dcu-ctk runtime configure unset default returned err: %v"
,
outErr
))
Assert
(
t
,
err
==
nil
,
fmt
.
Sprintf
(
"Error running amd-ctk err: %v"
,
err
))
fmt
.
Println
(
"output: "
,
out
)
verifyConfigFile
(
t
,
false
,
true
)
cleanUp
()
}
func
TestConfigureRuntimeMultiFlags
(
t
*
testing
.
T
)
{
fmt
.
Printf
(
"dcu-ctk path: %v
\n
"
,
cliPath
)
setup
(
t
)
cfgPathArg
:=
"--config-path="
+
configFile
// add amd to runtimes as default along with remove flag
out
,
outErr
,
err
:=
runCLI
(
"runtime"
,
"configure"
,
"--runtime=docker"
,
cfgPathArg
,
"--remove"
,
"--set-as-default"
)
Assert
(
t
,
outErr
==
""
,
fmt
.
Sprintf
(
"dcu-ctk runtime configure as default returned err: %v"
,
outErr
))
// shoudl error
Assert
(
t
,
err
!=
nil
,
"err shouldn't be nil"
)
// match the error message
Assert
(
t
,
strings
.
TrimSpace
(
out
)
==
removeAndDefErrMsg
,
fmt
.
Sprintf
(
"stdout: %v should have been '%v'"
,
out
,
removeAndDefErrMsg
))
// use set default and unset default flags at the same time
out
,
outErr
,
err
=
runCLI
(
"runtime"
,
"configure"
,
"--runtime=docker"
,
cfgPathArg
,
"--unset-as-default"
,
"--set-as-default"
)
Assert
(
t
,
outErr
==
""
,
fmt
.
Sprintf
(
"dcu-ctk runtime configure as default returned err: %v"
,
outErr
))
// shoudl error
Assert
(
t
,
err
!=
nil
,
"err shouldn't be nil"
)
// match the error message
Assert
(
t
,
strings
.
TrimSpace
(
out
)
==
setUnsetDefErrMsg
,
fmt
.
Sprintf
(
"stdout: %v should have been '%v'"
,
out
,
setUnsetDefErrMsg
))
cleanUp
()
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment