From 41abb89b57868af8c93b7875fd9e701b2766e8e8 Mon Sep 17 00:00:00 2001 From: "James O. D. Hunt" Date: Tue, 12 Sep 2017 14:26:13 +0100 Subject: [PATCH] tests: Simplify makeRuntimeConfig Create a new makeVersionBinary() function to allow makeRuntimeConfig() to be simplified. Signed-off-by: James O. D. Hunt --- cc-env_test.go | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/cc-env_test.go b/cc-env_test.go index a8b50e4b..0b41275b 100644 --- a/cc-env_test.go +++ b/cc-env_test.go @@ -37,6 +37,25 @@ const testProxyURL = "file:///proxyURL" const testProxyVersion = "proxy version 0.1" const testShimVersion = "shim version 0.1" +// makeVersionBinary creates a shell script with the specified file +// name. When run as "file --version", it will display the specified +// version to stdout and exit successfully. +func makeVersionBinary(file, version string) error { + err := createFile(file, + fmt.Sprintf(`#!/bin/sh + [ "$1" = "--version" ] && echo "%s"`, version)) + if err != nil { + return err + } + + err = os.Chmod(file, testExeFileMode) + if err != nil { + return err + } + + return nil +} + func makeRuntimeConfig(prefixDir string) (configFile string, config oci.RuntimeConfig, err error) { const logPath = "/log/path" hypervisorPath := filepath.Join(prefixDir, "hypervisor") @@ -75,26 +94,12 @@ func makeRuntimeConfig(prefixDir string) (configFile string, config oci.RuntimeC } } - err = createFile(shimPath, - fmt.Sprintf(`#!/bin/sh - [ "$1" = "--version" ] && echo "%s"`, testShimVersion)) - if err != nil { - return "", oci.RuntimeConfig{}, err - } - - err = os.Chmod(shimPath, testExeFileMode) - if err != nil { - return "", oci.RuntimeConfig{}, err - } - - err = createFile(proxyPath, - fmt.Sprintf(`#!/bin/sh - [ "$1" = "--version" ] && echo "%s"`, testProxyVersion)) + err = makeVersionBinary(shimPath, testShimVersion) if err != nil { return "", oci.RuntimeConfig{}, err } - err = os.Chmod(proxyPath, testExeFileMode) + err = makeVersionBinary(proxyPath, testProxyVersion) if err != nil { return "", oci.RuntimeConfig{}, err }