提交 9a53484c 编写于 作者: E Evan Lezar

Add getConfigFilePath as method on args struct

This change moves the specifics for determining the config (OCI spec)
file path to a method. This allows the default behaviour to be tested
and reused.
Signed-off-by: NEvan Lezar <elezar@nvidia.com>
上级 eedcddd3
......@@ -7,6 +7,7 @@ import (
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"syscall"
......@@ -157,14 +158,12 @@ func main() {
execRuncAndExit()
}
if args.bundleDirPath == "" {
args.bundleDirPath, err = os.Getwd()
exitOnError(err, "get working directory")
logger.Printf("Bundle dirrectory path is empty, using working directory: %s\n", args.bundleDirPath)
}
configFilePath, err := args.getConfigFilePath()
exitOnError(err, "error getting config file path")
logger.Printf("Using OCI specification file path: %v", configFilePath)
logger.Printf("Using bundle file: %s\n", args.bundleDirPath+"/config.json")
jsonFile, err := os.OpenFile(args.bundleDirPath+"/config.json", os.O_RDWR, 0644)
jsonFile, err := os.OpenFile(configFilePath, os.O_RDWR, 0644)
exitOnError(err, "open OCI spec file")
defer jsonFile.Close()
......@@ -188,3 +187,21 @@ func main() {
logger.Print("Prestart hook added, executing runc")
execRuncAndExit()
}
func (a args) getConfigFilePath() (string, error) {
configRoot := a.bundleDirPath
if configRoot == "" {
logger.Printf("Bundle directory path is empty, using working directory.")
workingDirectory, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("error getting working directory: %v", err)
}
configRoot = workingDirectory
}
logger.Printf("Using bundle directory: %v", configRoot)
configFilePath := filepath.Join(configRoot, "config.json")
return configFilePath, nil
}
......@@ -3,6 +3,7 @@ package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
......@@ -223,3 +224,34 @@ func TestGetConfigWithCustomConfig(t *testing.T) {
require.NoError(t, err)
require.Equal(t, cfg.debugFilePath, "/nvidia-container-toolkit.log")
}
func TestArgs(t *testing.T) {
wd, err := os.Getwd()
require.NoError(t, err)
testCases := []struct {
args args
configPath string
}{
{
args: args{},
configPath: fmt.Sprintf("%v/config.json", wd),
},
{
args: args{bundleDirPath: "/foo/bar"},
configPath: "/foo/bar/config.json",
},
{
args: args{bundleDirPath: "/foo/bar/"},
configPath: "/foo/bar/config.json",
},
}
for i, tc := range testCases {
cp, err := tc.args.getConfigFilePath()
require.NoErrorf(t, err, "%d: %v", i, tc)
require.Equalf(t, tc.configPath, cp, "%d: %v", i, tc)
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册