Initial commit
This commit is contained in:
46
vendor/github.com/hyperhq/hypercli/pkg/integration/checker/checker.go
generated
vendored
Normal file
46
vendor/github.com/hyperhq/hypercli/pkg/integration/checker/checker.go
generated
vendored
Normal file
@@ -0,0 +1,46 @@
|
||||
// Package checker provide Docker specific implementations of the go-check.Checker interface.
|
||||
package checker
|
||||
|
||||
import (
|
||||
"github.com/go-check/check"
|
||||
"github.com/vdemeester/shakers"
|
||||
)
|
||||
|
||||
// As a commodity, we bring all check.Checker variables into the current namespace to avoid having
|
||||
// to think about check.X versus checker.X.
|
||||
var (
|
||||
DeepEquals = check.DeepEquals
|
||||
ErrorMatches = check.ErrorMatches
|
||||
FitsTypeOf = check.FitsTypeOf
|
||||
HasLen = check.HasLen
|
||||
Implements = check.Implements
|
||||
IsNil = check.IsNil
|
||||
Matches = check.Matches
|
||||
Not = check.Not
|
||||
NotNil = check.NotNil
|
||||
PanicMatches = check.PanicMatches
|
||||
Panics = check.Panics
|
||||
|
||||
Contains = shakers.Contains
|
||||
ContainsAny = shakers.ContainsAny
|
||||
Count = shakers.Count
|
||||
Equals = shakers.Equals
|
||||
EqualFold = shakers.EqualFold
|
||||
False = shakers.False
|
||||
GreaterOrEqualThan = shakers.GreaterOrEqualThan
|
||||
GreaterThan = shakers.GreaterThan
|
||||
HasPrefix = shakers.HasPrefix
|
||||
HasSuffix = shakers.HasSuffix
|
||||
Index = shakers.Index
|
||||
IndexAny = shakers.IndexAny
|
||||
IsAfter = shakers.IsAfter
|
||||
IsBefore = shakers.IsBefore
|
||||
IsBetween = shakers.IsBetween
|
||||
IsLower = shakers.IsLower
|
||||
IsUpper = shakers.IsUpper
|
||||
LessOrEqualThan = shakers.LessOrEqualThan
|
||||
LessThan = shakers.LessThan
|
||||
TimeEquals = shakers.TimeEquals
|
||||
True = shakers.True
|
||||
TimeIgnore = shakers.TimeIgnore
|
||||
)
|
||||
71
vendor/github.com/hyperhq/hypercli/pkg/integration/dockerCmd_utils.go
generated
vendored
Normal file
71
vendor/github.com/hyperhq/hypercli/pkg/integration/dockerCmd_utils.go
generated
vendored
Normal file
@@ -0,0 +1,71 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-check/check"
|
||||
)
|
||||
|
||||
var execCommand = exec.Command
|
||||
|
||||
// DockerCmdWithError executes a docker command that is supposed to fail and returns
|
||||
// the output, the exit code and the error.
|
||||
func DockerCmdWithError(dockerBinary string, args ...string) (string, int, error) {
|
||||
return RunCommandWithOutput(execCommand(dockerBinary, args...))
|
||||
}
|
||||
|
||||
// DockerCmdWithStdoutStderr executes a docker command and returns the content of the
|
||||
// stdout, stderr and the exit code. If a check.C is passed, it will fail and stop tests
|
||||
// if the error is not nil.
|
||||
func DockerCmdWithStdoutStderr(dockerBinary string, c *check.C, args ...string) (string, string, int) {
|
||||
stdout, stderr, status, err := RunCommandWithStdoutStderr(execCommand(dockerBinary, args...))
|
||||
if c != nil {
|
||||
c.Assert(err, check.IsNil, check.Commentf("%q failed with errors: %s, %v", strings.Join(args, " "), stderr, err))
|
||||
}
|
||||
return stdout, stderr, status
|
||||
}
|
||||
|
||||
// DockerCmd executes a docker command and returns the output and the exit code. If the
|
||||
// command returns an error, it will fail and stop the tests.
|
||||
func DockerCmd(dockerBinary string, c *check.C, args ...string) (string, int) {
|
||||
out, status, err := RunCommandWithOutput(execCommand(dockerBinary, args...))
|
||||
c.Assert(err, check.IsNil, check.Commentf("%q failed with errors: %s, %v", strings.Join(args, " "), out, err))
|
||||
return out, status
|
||||
}
|
||||
|
||||
// DockerCmdWithTimeout executes a docker command with a timeout, and returns the output,
|
||||
// the exit code and the error (if any).
|
||||
func DockerCmdWithTimeout(dockerBinary string, timeout time.Duration, args ...string) (string, int, error) {
|
||||
out, status, err := RunCommandWithOutputAndTimeout(execCommand(dockerBinary, args...), timeout)
|
||||
if err != nil {
|
||||
return out, status, fmt.Errorf("%q failed with errors: %v : %q", strings.Join(args, " "), err, out)
|
||||
}
|
||||
return out, status, err
|
||||
}
|
||||
|
||||
// DockerCmdInDir executes a docker command in a directory and returns the output, the
|
||||
// exit code and the error (if any).
|
||||
func DockerCmdInDir(dockerBinary string, path string, args ...string) (string, int, error) {
|
||||
dockerCommand := execCommand(dockerBinary, args...)
|
||||
dockerCommand.Dir = path
|
||||
out, status, err := RunCommandWithOutput(dockerCommand)
|
||||
if err != nil {
|
||||
return out, status, fmt.Errorf("%q failed with errors: %v : %q", strings.Join(args, " "), err, out)
|
||||
}
|
||||
return out, status, err
|
||||
}
|
||||
|
||||
// DockerCmdInDirWithTimeout executes a docker command in a directory with a timeout and
|
||||
// returns the output, the exit code and the error (if any).
|
||||
func DockerCmdInDirWithTimeout(dockerBinary string, timeout time.Duration, path string, args ...string) (string, int, error) {
|
||||
dockerCommand := execCommand(dockerBinary, args...)
|
||||
dockerCommand.Dir = path
|
||||
out, status, err := RunCommandWithOutputAndTimeout(dockerCommand, timeout)
|
||||
if err != nil {
|
||||
return out, status, fmt.Errorf("%q failed with errors: %v : %q", strings.Join(args, " "), err, out)
|
||||
}
|
||||
return out, status, err
|
||||
}
|
||||
405
vendor/github.com/hyperhq/hypercli/pkg/integration/dockerCmd_utils_test.go
generated
vendored
Normal file
405
vendor/github.com/hyperhq/hypercli/pkg/integration/dockerCmd_utils_test.go
generated
vendored
Normal file
@@ -0,0 +1,405 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"testing"
|
||||
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-check/check"
|
||||
)
|
||||
|
||||
const dockerBinary = "docker"
|
||||
|
||||
// Setup go-check for this test
|
||||
func Test(t *testing.T) {
|
||||
check.TestingT(t)
|
||||
}
|
||||
|
||||
func init() {
|
||||
check.Suite(&DockerCmdSuite{})
|
||||
}
|
||||
|
||||
type DockerCmdSuite struct{}
|
||||
|
||||
// Fake the exec.Command to use our mock.
|
||||
func (s *DockerCmdSuite) SetUpTest(c *check.C) {
|
||||
execCommand = fakeExecCommand
|
||||
}
|
||||
|
||||
// And bring it back to normal after the test.
|
||||
func (s *DockerCmdSuite) TearDownTest(c *check.C) {
|
||||
execCommand = exec.Command
|
||||
}
|
||||
|
||||
// DockerCmdWithError tests
|
||||
|
||||
func (s *DockerCmdSuite) TestDockerCmdWithError(c *check.C) {
|
||||
cmds := []struct {
|
||||
binary string
|
||||
args []string
|
||||
expectedOut string
|
||||
expectedExitCode int
|
||||
expectedError error
|
||||
}{
|
||||
{
|
||||
"doesnotexists",
|
||||
[]string{},
|
||||
"Command doesnotexists not found.",
|
||||
1,
|
||||
fmt.Errorf("exit status 1"),
|
||||
},
|
||||
{
|
||||
dockerBinary,
|
||||
[]string{"an", "error"},
|
||||
"an error has occurred",
|
||||
1,
|
||||
fmt.Errorf("exit status 1"),
|
||||
},
|
||||
{
|
||||
dockerBinary,
|
||||
[]string{"an", "exitCode", "127"},
|
||||
"an error has occurred with exitCode 127",
|
||||
127,
|
||||
fmt.Errorf("exit status 127"),
|
||||
},
|
||||
{
|
||||
dockerBinary,
|
||||
[]string{"run", "-ti", "ubuntu", "echo", "hello"},
|
||||
"hello",
|
||||
0,
|
||||
nil,
|
||||
},
|
||||
}
|
||||
for _, cmd := range cmds {
|
||||
out, exitCode, error := DockerCmdWithError(cmd.binary, cmd.args...)
|
||||
c.Assert(out, check.Equals, cmd.expectedOut, check.Commentf("Expected output %q for arguments %v, got %q", cmd.expectedOut, cmd.args, out))
|
||||
c.Assert(exitCode, check.Equals, cmd.expectedExitCode, check.Commentf("Expected exitCode %q for arguments %v, got %q", cmd.expectedExitCode, cmd.args, exitCode))
|
||||
if cmd.expectedError != nil {
|
||||
c.Assert(error, check.NotNil, check.Commentf("Expected an error %q, got nothing", cmd.expectedError))
|
||||
c.Assert(error.Error(), check.Equals, cmd.expectedError.Error(), check.Commentf("Expected error %q for arguments %v, got %q", cmd.expectedError.Error(), cmd.args, error.Error()))
|
||||
} else {
|
||||
c.Assert(error, check.IsNil, check.Commentf("Expected no error, got %v", error))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// DockerCmdWithStdoutStderr tests
|
||||
|
||||
type dockerCmdWithStdoutStderrErrorSuite struct{}
|
||||
|
||||
func (s *dockerCmdWithStdoutStderrErrorSuite) Test(c *check.C) {
|
||||
// Should fail, the test too
|
||||
DockerCmdWithStdoutStderr(dockerBinary, c, "an", "error")
|
||||
}
|
||||
|
||||
type dockerCmdWithStdoutStderrSuccessSuite struct{}
|
||||
|
||||
func (s *dockerCmdWithStdoutStderrSuccessSuite) Test(c *check.C) {
|
||||
stdout, stderr, exitCode := DockerCmdWithStdoutStderr(dockerBinary, c, "run", "-ti", "ubuntu", "echo", "hello")
|
||||
c.Assert(stdout, check.Equals, "hello")
|
||||
c.Assert(stderr, check.Equals, "")
|
||||
c.Assert(exitCode, check.Equals, 0)
|
||||
|
||||
}
|
||||
|
||||
func (s *DockerCmdSuite) TestDockerCmdWithStdoutStderrError(c *check.C) {
|
||||
// Run error suite, should fail.
|
||||
output := String{}
|
||||
result := check.Run(&dockerCmdWithStdoutStderrErrorSuite{}, &check.RunConf{Output: &output})
|
||||
c.Check(result.Succeeded, check.Equals, 0)
|
||||
c.Check(result.Failed, check.Equals, 1)
|
||||
}
|
||||
|
||||
func (s *DockerCmdSuite) TestDockerCmdWithStdoutStderrSuccess(c *check.C) {
|
||||
// Run error suite, should fail.
|
||||
output := String{}
|
||||
result := check.Run(&dockerCmdWithStdoutStderrSuccessSuite{}, &check.RunConf{Output: &output})
|
||||
c.Check(result.Succeeded, check.Equals, 1)
|
||||
c.Check(result.Failed, check.Equals, 0)
|
||||
}
|
||||
|
||||
// DockerCmd tests
|
||||
|
||||
type dockerCmdErrorSuite struct{}
|
||||
|
||||
func (s *dockerCmdErrorSuite) Test(c *check.C) {
|
||||
// Should fail, the test too
|
||||
DockerCmd(dockerBinary, c, "an", "error")
|
||||
}
|
||||
|
||||
type dockerCmdSuccessSuite struct{}
|
||||
|
||||
func (s *dockerCmdSuccessSuite) Test(c *check.C) {
|
||||
stdout, exitCode := DockerCmd(dockerBinary, c, "run", "-ti", "ubuntu", "echo", "hello")
|
||||
c.Assert(stdout, check.Equals, "hello")
|
||||
c.Assert(exitCode, check.Equals, 0)
|
||||
|
||||
}
|
||||
|
||||
func (s *DockerCmdSuite) TestDockerCmdError(c *check.C) {
|
||||
// Run error suite, should fail.
|
||||
output := String{}
|
||||
result := check.Run(&dockerCmdErrorSuite{}, &check.RunConf{Output: &output})
|
||||
c.Check(result.Succeeded, check.Equals, 0)
|
||||
c.Check(result.Failed, check.Equals, 1)
|
||||
}
|
||||
|
||||
func (s *DockerCmdSuite) TestDockerCmdSuccess(c *check.C) {
|
||||
// Run error suite, should fail.
|
||||
output := String{}
|
||||
result := check.Run(&dockerCmdSuccessSuite{}, &check.RunConf{Output: &output})
|
||||
c.Check(result.Succeeded, check.Equals, 1)
|
||||
c.Check(result.Failed, check.Equals, 0)
|
||||
}
|
||||
|
||||
// DockerCmdWithTimeout tests
|
||||
|
||||
func (s *DockerCmdSuite) TestDockerCmdWithTimeout(c *check.C) {
|
||||
cmds := []struct {
|
||||
binary string
|
||||
args []string
|
||||
timeout time.Duration
|
||||
expectedOut string
|
||||
expectedExitCode int
|
||||
expectedError error
|
||||
}{
|
||||
{
|
||||
"doesnotexists",
|
||||
[]string{},
|
||||
200 * time.Millisecond,
|
||||
`Command doesnotexists not found.`,
|
||||
1,
|
||||
fmt.Errorf(`"" failed with errors: exit status 1 : "Command doesnotexists not found."`),
|
||||
},
|
||||
{
|
||||
dockerBinary,
|
||||
[]string{"an", "error"},
|
||||
200 * time.Millisecond,
|
||||
`an error has occurred`,
|
||||
1,
|
||||
fmt.Errorf(`"an error" failed with errors: exit status 1 : "an error has occurred"`),
|
||||
},
|
||||
{
|
||||
dockerBinary,
|
||||
[]string{"a", "command", "that", "times", "out"},
|
||||
5 * time.Millisecond,
|
||||
"",
|
||||
0,
|
||||
fmt.Errorf(`"a command that times out" failed with errors: command timed out : ""`),
|
||||
},
|
||||
{
|
||||
dockerBinary,
|
||||
[]string{"run", "-ti", "ubuntu", "echo", "hello"},
|
||||
200 * time.Millisecond,
|
||||
"hello",
|
||||
0,
|
||||
nil,
|
||||
},
|
||||
}
|
||||
for _, cmd := range cmds {
|
||||
out, exitCode, error := DockerCmdWithTimeout(cmd.binary, cmd.timeout, cmd.args...)
|
||||
c.Assert(out, check.Equals, cmd.expectedOut, check.Commentf("Expected output %q for arguments %v, got %q", cmd.expectedOut, cmd.args, out))
|
||||
c.Assert(exitCode, check.Equals, cmd.expectedExitCode, check.Commentf("Expected exitCode %q for arguments %v, got %q", cmd.expectedExitCode, cmd.args, exitCode))
|
||||
if cmd.expectedError != nil {
|
||||
c.Assert(error, check.NotNil, check.Commentf("Expected an error %q, got nothing", cmd.expectedError))
|
||||
c.Assert(error.Error(), check.Equals, cmd.expectedError.Error(), check.Commentf("Expected error %q for arguments %v, got %q", cmd.expectedError.Error(), cmd.args, error.Error()))
|
||||
} else {
|
||||
c.Assert(error, check.IsNil, check.Commentf("Expected no error, got %v", error))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// DockerCmdInDir tests
|
||||
|
||||
func (s *DockerCmdSuite) TestDockerCmdInDir(c *check.C) {
|
||||
tempFolder, err := ioutil.TempDir("", "test-docker-cmd-in-dir")
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
cmds := []struct {
|
||||
binary string
|
||||
args []string
|
||||
expectedOut string
|
||||
expectedExitCode int
|
||||
expectedError error
|
||||
}{
|
||||
{
|
||||
"doesnotexists",
|
||||
[]string{},
|
||||
`Command doesnotexists not found.`,
|
||||
1,
|
||||
fmt.Errorf(`"dir:%s" failed with errors: exit status 1 : "Command doesnotexists not found."`, tempFolder),
|
||||
},
|
||||
{
|
||||
dockerBinary,
|
||||
[]string{"an", "error"},
|
||||
`an error has occurred`,
|
||||
1,
|
||||
fmt.Errorf(`"dir:%s an error" failed with errors: exit status 1 : "an error has occurred"`, tempFolder),
|
||||
},
|
||||
{
|
||||
dockerBinary,
|
||||
[]string{"run", "-ti", "ubuntu", "echo", "hello"},
|
||||
"hello",
|
||||
0,
|
||||
nil,
|
||||
},
|
||||
}
|
||||
for _, cmd := range cmds {
|
||||
// We prepend the arguments with dir:thefolder.. the fake command will check
|
||||
// that the current workdir is the same as the one we are passing.
|
||||
args := append([]string{"dir:" + tempFolder}, cmd.args...)
|
||||
out, exitCode, error := DockerCmdInDir(cmd.binary, tempFolder, args...)
|
||||
c.Assert(out, check.Equals, cmd.expectedOut, check.Commentf("Expected output %q for arguments %v, got %q", cmd.expectedOut, cmd.args, out))
|
||||
c.Assert(exitCode, check.Equals, cmd.expectedExitCode, check.Commentf("Expected exitCode %q for arguments %v, got %q", cmd.expectedExitCode, cmd.args, exitCode))
|
||||
if cmd.expectedError != nil {
|
||||
c.Assert(error, check.NotNil, check.Commentf("Expected an error %q, got nothing", cmd.expectedError))
|
||||
c.Assert(error.Error(), check.Equals, cmd.expectedError.Error(), check.Commentf("Expected error %q for arguments %v, got %q", cmd.expectedError.Error(), cmd.args, error.Error()))
|
||||
} else {
|
||||
c.Assert(error, check.IsNil, check.Commentf("Expected no error, got %v", error))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// DockerCmdInDirWithTimeout tests
|
||||
|
||||
func (s *DockerCmdSuite) TestDockerCmdInDirWithTimeout(c *check.C) {
|
||||
tempFolder, err := ioutil.TempDir("", "test-docker-cmd-in-dir")
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
cmds := []struct {
|
||||
binary string
|
||||
args []string
|
||||
timeout time.Duration
|
||||
expectedOut string
|
||||
expectedExitCode int
|
||||
expectedError error
|
||||
}{
|
||||
{
|
||||
"doesnotexists",
|
||||
[]string{},
|
||||
200 * time.Millisecond,
|
||||
`Command doesnotexists not found.`,
|
||||
1,
|
||||
fmt.Errorf(`"dir:%s" failed with errors: exit status 1 : "Command doesnotexists not found."`, tempFolder),
|
||||
},
|
||||
{
|
||||
dockerBinary,
|
||||
[]string{"an", "error"},
|
||||
200 * time.Millisecond,
|
||||
`an error has occurred`,
|
||||
1,
|
||||
fmt.Errorf(`"dir:%s an error" failed with errors: exit status 1 : "an error has occurred"`, tempFolder),
|
||||
},
|
||||
{
|
||||
dockerBinary,
|
||||
[]string{"a", "command", "that", "times", "out"},
|
||||
5 * time.Millisecond,
|
||||
"",
|
||||
0,
|
||||
fmt.Errorf(`"dir:%s a command that times out" failed with errors: command timed out : ""`, tempFolder),
|
||||
},
|
||||
{
|
||||
dockerBinary,
|
||||
[]string{"run", "-ti", "ubuntu", "echo", "hello"},
|
||||
200 * time.Millisecond,
|
||||
"hello",
|
||||
0,
|
||||
nil,
|
||||
},
|
||||
}
|
||||
for _, cmd := range cmds {
|
||||
// We prepend the arguments with dir:thefolder.. the fake command will check
|
||||
// that the current workdir is the same as the one we are passing.
|
||||
args := append([]string{"dir:" + tempFolder}, cmd.args...)
|
||||
out, exitCode, error := DockerCmdInDirWithTimeout(cmd.binary, cmd.timeout, tempFolder, args...)
|
||||
c.Assert(out, check.Equals, cmd.expectedOut, check.Commentf("Expected output %q for arguments %v, got %q", cmd.expectedOut, cmd.args, out))
|
||||
c.Assert(exitCode, check.Equals, cmd.expectedExitCode, check.Commentf("Expected exitCode %q for arguments %v, got %q", cmd.expectedExitCode, cmd.args, exitCode))
|
||||
if cmd.expectedError != nil {
|
||||
c.Assert(error, check.NotNil, check.Commentf("Expected an error %q, got nothing", cmd.expectedError))
|
||||
c.Assert(error.Error(), check.Equals, cmd.expectedError.Error(), check.Commentf("Expected error %q for arguments %v, got %q", cmd.expectedError.Error(), cmd.args, error.Error()))
|
||||
} else {
|
||||
c.Assert(error, check.IsNil, check.Commentf("Expected no error, got %v", error))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Helpers :)
|
||||
|
||||
// Type implementing the io.Writer interface for analyzing output.
|
||||
type String struct {
|
||||
value string
|
||||
}
|
||||
|
||||
// The only function required by the io.Writer interface. Will append
|
||||
// written data to the String.value string.
|
||||
func (s *String) Write(p []byte) (n int, err error) {
|
||||
s.value += string(p)
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
// Helper function that mock the exec.Command call (and call the test binary)
|
||||
func fakeExecCommand(command string, args ...string) *exec.Cmd {
|
||||
cs := []string{"-test.run=TestHelperProcess", "--", command}
|
||||
cs = append(cs, args...)
|
||||
cmd := exec.Command(os.Args[0], cs...)
|
||||
cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"}
|
||||
return cmd
|
||||
}
|
||||
|
||||
func TestHelperProcess(t *testing.T) {
|
||||
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
|
||||
return
|
||||
}
|
||||
args := os.Args
|
||||
|
||||
// Previous arguments are tests stuff, that looks like :
|
||||
// /tmp/go-build970079519/…/_test/integration.test -test.run=TestHelperProcess --
|
||||
cmd, args := args[3], args[4:]
|
||||
// Handle the case where args[0] is dir:...
|
||||
if len(args) > 0 && strings.HasPrefix(args[0], "dir:") {
|
||||
expectedCwd := args[0][4:]
|
||||
if len(args) > 1 {
|
||||
args = args[1:]
|
||||
}
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Failed to get workingdir: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
// This checks that the given path is the same as the currend working dire
|
||||
if expectedCwd != cwd {
|
||||
fmt.Fprintf(os.Stderr, "Current workdir should be %q, but is %q", expectedCwd, cwd)
|
||||
}
|
||||
}
|
||||
switch cmd {
|
||||
case dockerBinary:
|
||||
argsStr := strings.Join(args, " ")
|
||||
switch argsStr {
|
||||
case "an exitCode 127":
|
||||
fmt.Fprintf(os.Stderr, "an error has occurred with exitCode 127")
|
||||
os.Exit(127)
|
||||
case "an error":
|
||||
fmt.Fprintf(os.Stderr, "an error has occurred")
|
||||
os.Exit(1)
|
||||
case "a command that times out":
|
||||
time.Sleep(10 * time.Second)
|
||||
fmt.Fprintf(os.Stdout, "too long, should be killed")
|
||||
// A random exit code (that should never happened in tests)
|
||||
os.Exit(7)
|
||||
case "run -ti ubuntu echo hello":
|
||||
fmt.Fprintf(os.Stdout, "hello")
|
||||
default:
|
||||
fmt.Fprintf(os.Stdout, "no arguments")
|
||||
}
|
||||
default:
|
||||
fmt.Fprintf(os.Stderr, "Command %s not found.", cmd)
|
||||
os.Exit(1)
|
||||
}
|
||||
// some code here to check arguments perhaps?
|
||||
os.Exit(0)
|
||||
}
|
||||
361
vendor/github.com/hyperhq/hypercli/pkg/integration/utils.go
generated
vendored
Normal file
361
vendor/github.com/hyperhq/hypercli/pkg/integration/utils.go
generated
vendored
Normal file
@@ -0,0 +1,361 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/hyperhq/hypercli/pkg/stringutils"
|
||||
)
|
||||
|
||||
// GetExitCode returns the ExitStatus of the specified error if its type is
|
||||
// exec.ExitError, returns 0 and an error otherwise.
|
||||
func GetExitCode(err error) (int, error) {
|
||||
exitCode := 0
|
||||
if exiterr, ok := err.(*exec.ExitError); ok {
|
||||
if procExit, ok := exiterr.Sys().(syscall.WaitStatus); ok {
|
||||
return procExit.ExitStatus(), nil
|
||||
}
|
||||
}
|
||||
return exitCode, fmt.Errorf("failed to get exit code")
|
||||
}
|
||||
|
||||
// ProcessExitCode process the specified error and returns the exit status code
|
||||
// if the error was of type exec.ExitError, returns nothing otherwise.
|
||||
func ProcessExitCode(err error) (exitCode int) {
|
||||
if err != nil {
|
||||
var exiterr error
|
||||
if exitCode, exiterr = GetExitCode(err); exiterr != nil {
|
||||
// TODO: Fix this so we check the error's text.
|
||||
// we've failed to retrieve exit code, so we set it to 127
|
||||
exitCode = 127
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// IsKilled process the specified error and returns whether the process was killed or not.
|
||||
func IsKilled(err error) bool {
|
||||
if exitErr, ok := err.(*exec.ExitError); ok {
|
||||
status, ok := exitErr.Sys().(syscall.WaitStatus)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
// status.ExitStatus() is required on Windows because it does not
|
||||
// implement Signal() nor Signaled(). Just check it had a bad exit
|
||||
// status could mean it was killed (and in tests we do kill)
|
||||
return (status.Signaled() && status.Signal() == os.Kill) || status.ExitStatus() != 0
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// RunCommandWithOutput runs the specified command and returns the combined output (stdout/stderr)
|
||||
// with the exitCode different from 0 and the error if something bad happened
|
||||
func RunCommandWithOutput(cmd *exec.Cmd) (output string, exitCode int, err error) {
|
||||
exitCode = 0
|
||||
out, err := cmd.CombinedOutput()
|
||||
exitCode = ProcessExitCode(err)
|
||||
output = string(out)
|
||||
return
|
||||
}
|
||||
|
||||
// RunCommandWithStdoutStderr runs the specified command and returns stdout and stderr separately
|
||||
// with the exitCode different from 0 and the error if something bad happened
|
||||
func RunCommandWithStdoutStderr(cmd *exec.Cmd) (stdout string, stderr string, exitCode int, err error) {
|
||||
var (
|
||||
stderrBuffer, stdoutBuffer bytes.Buffer
|
||||
)
|
||||
exitCode = 0
|
||||
cmd.Stderr = &stderrBuffer
|
||||
cmd.Stdout = &stdoutBuffer
|
||||
err = cmd.Run()
|
||||
exitCode = ProcessExitCode(err)
|
||||
|
||||
stdout = stdoutBuffer.String()
|
||||
stderr = stderrBuffer.String()
|
||||
return
|
||||
}
|
||||
|
||||
// RunCommandWithOutputForDuration runs the specified command "timeboxed" by the specified duration.
|
||||
// If the process is still running when the timebox is finished, the process will be killed and .
|
||||
// It will returns the output with the exitCode different from 0 and the error if something bad happened
|
||||
// and a boolean whether it has been killed or not.
|
||||
func RunCommandWithOutputForDuration(cmd *exec.Cmd, duration time.Duration) (output string, exitCode int, timedOut bool, err error) {
|
||||
var outputBuffer bytes.Buffer
|
||||
if cmd.Stdout != nil {
|
||||
err = errors.New("cmd.Stdout already set")
|
||||
return
|
||||
}
|
||||
cmd.Stdout = &outputBuffer
|
||||
|
||||
if cmd.Stderr != nil {
|
||||
err = errors.New("cmd.Stderr already set")
|
||||
return
|
||||
}
|
||||
cmd.Stderr = &outputBuffer
|
||||
|
||||
// Start the command in the main thread..
|
||||
err = cmd.Start()
|
||||
if err != nil {
|
||||
err = fmt.Errorf("Fail to start command %v : %v", cmd, err)
|
||||
}
|
||||
|
||||
type exitInfo struct {
|
||||
exitErr error
|
||||
exitCode int
|
||||
}
|
||||
|
||||
done := make(chan exitInfo, 1)
|
||||
|
||||
go func() {
|
||||
// And wait for it to exit in the goroutine :)
|
||||
info := exitInfo{}
|
||||
info.exitErr = cmd.Wait()
|
||||
info.exitCode = ProcessExitCode(info.exitErr)
|
||||
done <- info
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-time.After(duration):
|
||||
killErr := cmd.Process.Kill()
|
||||
if killErr != nil {
|
||||
fmt.Printf("failed to kill (pid=%d): %v\n", cmd.Process.Pid, killErr)
|
||||
}
|
||||
timedOut = true
|
||||
case info := <-done:
|
||||
err = info.exitErr
|
||||
exitCode = info.exitCode
|
||||
}
|
||||
output = outputBuffer.String()
|
||||
return
|
||||
}
|
||||
|
||||
var errCmdTimeout = fmt.Errorf("command timed out")
|
||||
|
||||
// RunCommandWithOutputAndTimeout runs the specified command "timeboxed" by the specified duration.
|
||||
// It returns the output with the exitCode different from 0 and the error if something bad happened or
|
||||
// if the process timed out (and has been killed).
|
||||
func RunCommandWithOutputAndTimeout(cmd *exec.Cmd, timeout time.Duration) (output string, exitCode int, err error) {
|
||||
var timedOut bool
|
||||
output, exitCode, timedOut, err = RunCommandWithOutputForDuration(cmd, timeout)
|
||||
if timedOut {
|
||||
err = errCmdTimeout
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// RunCommand runs the specified command and returns the exitCode different from 0
|
||||
// and the error if something bad happened.
|
||||
func RunCommand(cmd *exec.Cmd) (exitCode int, err error) {
|
||||
exitCode = 0
|
||||
err = cmd.Run()
|
||||
exitCode = ProcessExitCode(err)
|
||||
return
|
||||
}
|
||||
|
||||
// RunCommandPipelineWithOutput runs the array of commands with the output
|
||||
// of each pipelined with the following (like cmd1 | cmd2 | cmd3 would do).
|
||||
// It returns the final output, the exitCode different from 0 and the error
|
||||
// if something bad happened.
|
||||
func RunCommandPipelineWithOutput(cmds ...*exec.Cmd) (output string, exitCode int, err error) {
|
||||
if len(cmds) < 2 {
|
||||
return "", 0, errors.New("pipeline does not have multiple cmds")
|
||||
}
|
||||
|
||||
// connect stdin of each cmd to stdout pipe of previous cmd
|
||||
for i, cmd := range cmds {
|
||||
if i > 0 {
|
||||
prevCmd := cmds[i-1]
|
||||
cmd.Stdin, err = prevCmd.StdoutPipe()
|
||||
|
||||
if err != nil {
|
||||
return "", 0, fmt.Errorf("cannot set stdout pipe for %s: %v", cmd.Path, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// start all cmds except the last
|
||||
for _, cmd := range cmds[:len(cmds)-1] {
|
||||
if err = cmd.Start(); err != nil {
|
||||
return "", 0, fmt.Errorf("starting %s failed with error: %v", cmd.Path, err)
|
||||
}
|
||||
}
|
||||
|
||||
var pipelineError error
|
||||
defer func() {
|
||||
// wait all cmds except the last to release their resources
|
||||
for _, cmd := range cmds[:len(cmds)-1] {
|
||||
if err := cmd.Wait(); err != nil {
|
||||
pipelineError = fmt.Errorf("command %s failed with error: %v", cmd.Path, err)
|
||||
break
|
||||
}
|
||||
}
|
||||
}()
|
||||
if pipelineError != nil {
|
||||
return "", 0, pipelineError
|
||||
}
|
||||
|
||||
// wait on last cmd
|
||||
return RunCommandWithOutput(cmds[len(cmds)-1])
|
||||
}
|
||||
|
||||
// UnmarshalJSON deserialize a JSON in the given interface.
|
||||
func UnmarshalJSON(data []byte, result interface{}) error {
|
||||
if err := json.Unmarshal(data, result); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ConvertSliceOfStringsToMap converts a slices of string in a map
|
||||
// with the strings as key and an empty string as values.
|
||||
func ConvertSliceOfStringsToMap(input []string) map[string]struct{} {
|
||||
output := make(map[string]struct{})
|
||||
for _, v := range input {
|
||||
output[v] = struct{}{}
|
||||
}
|
||||
return output
|
||||
}
|
||||
|
||||
// CompareDirectoryEntries compares two sets of FileInfo (usually taken from a directory)
|
||||
// and returns an error if different.
|
||||
func CompareDirectoryEntries(e1 []os.FileInfo, e2 []os.FileInfo) error {
|
||||
var (
|
||||
e1Entries = make(map[string]struct{})
|
||||
e2Entries = make(map[string]struct{})
|
||||
)
|
||||
for _, e := range e1 {
|
||||
e1Entries[e.Name()] = struct{}{}
|
||||
}
|
||||
for _, e := range e2 {
|
||||
e2Entries[e.Name()] = struct{}{}
|
||||
}
|
||||
if !reflect.DeepEqual(e1Entries, e2Entries) {
|
||||
return fmt.Errorf("entries differ")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ListTar lists the entries of a tar.
|
||||
func ListTar(f io.Reader) ([]string, error) {
|
||||
tr := tar.NewReader(f)
|
||||
var entries []string
|
||||
|
||||
for {
|
||||
th, err := tr.Next()
|
||||
if err == io.EOF {
|
||||
// end of tar archive
|
||||
return entries, nil
|
||||
}
|
||||
if err != nil {
|
||||
return entries, err
|
||||
}
|
||||
entries = append(entries, th.Name)
|
||||
}
|
||||
}
|
||||
|
||||
// RandomTmpDirPath provides a temporary path with rand string appended.
|
||||
// does not create or checks if it exists.
|
||||
func RandomTmpDirPath(s string, platform string) string {
|
||||
tmp := "/tmp"
|
||||
if platform == "windows" {
|
||||
tmp = os.Getenv("TEMP")
|
||||
}
|
||||
path := filepath.Join(tmp, fmt.Sprintf("%s.%s", s, stringutils.GenerateRandomAlphaOnlyString(10)))
|
||||
if platform == "windows" {
|
||||
return filepath.FromSlash(path) // Using \
|
||||
}
|
||||
return filepath.ToSlash(path) // Using /
|
||||
}
|
||||
|
||||
// ConsumeWithSpeed reads chunkSize bytes from reader before sleeping
|
||||
// for interval duration. Returns total read bytes. Send true to the
|
||||
// stop channel to return before reading to EOF on the reader.
|
||||
func ConsumeWithSpeed(reader io.Reader, chunkSize int, interval time.Duration, stop chan bool) (n int, err error) {
|
||||
buffer := make([]byte, chunkSize)
|
||||
for {
|
||||
var readBytes int
|
||||
readBytes, err = reader.Read(buffer)
|
||||
n += readBytes
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
err = nil
|
||||
}
|
||||
return
|
||||
}
|
||||
select {
|
||||
case <-stop:
|
||||
return
|
||||
case <-time.After(interval):
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ParseCgroupPaths parses 'procCgroupData', which is output of '/proc/<pid>/cgroup', and returns
|
||||
// a map which cgroup name as key and path as value.
|
||||
func ParseCgroupPaths(procCgroupData string) map[string]string {
|
||||
cgroupPaths := map[string]string{}
|
||||
for _, line := range strings.Split(procCgroupData, "\n") {
|
||||
parts := strings.Split(line, ":")
|
||||
if len(parts) != 3 {
|
||||
continue
|
||||
}
|
||||
cgroupPaths[parts[1]] = parts[2]
|
||||
}
|
||||
return cgroupPaths
|
||||
}
|
||||
|
||||
// ChannelBuffer holds a chan of byte array that can be populate in a goroutine.
|
||||
type ChannelBuffer struct {
|
||||
C chan []byte
|
||||
}
|
||||
|
||||
// Write implements Writer.
|
||||
func (c *ChannelBuffer) Write(b []byte) (int, error) {
|
||||
c.C <- b
|
||||
return len(b), nil
|
||||
}
|
||||
|
||||
// Close closes the go channel.
|
||||
func (c *ChannelBuffer) Close() error {
|
||||
close(c.C)
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReadTimeout reads the content of the channel in the specified byte array with
|
||||
// the specified duration as timeout.
|
||||
func (c *ChannelBuffer) ReadTimeout(p []byte, n time.Duration) (int, error) {
|
||||
select {
|
||||
case b := <-c.C:
|
||||
return copy(p[0:], b), nil
|
||||
case <-time.After(n):
|
||||
return -1, fmt.Errorf("timeout reading from channel")
|
||||
}
|
||||
}
|
||||
|
||||
// RunAtDifferentDate runs the specified function with the given time.
|
||||
// It changes the date of the system, which can led to weird behaviors.
|
||||
func RunAtDifferentDate(date time.Time, block func()) {
|
||||
// Layout for date. MMDDhhmmYYYY
|
||||
const timeLayout = "010203042006"
|
||||
// Ensure we bring time back to now
|
||||
now := time.Now().Format(timeLayout)
|
||||
dateReset := exec.Command("date", now)
|
||||
defer RunCommand(dateReset)
|
||||
|
||||
dateChange := exec.Command("date", date.Format(timeLayout))
|
||||
RunCommand(dateChange)
|
||||
block()
|
||||
return
|
||||
}
|
||||
492
vendor/github.com/hyperhq/hypercli/pkg/integration/utils_test.go
generated
vendored
Normal file
492
vendor/github.com/hyperhq/hypercli/pkg/integration/utils_test.go
generated
vendored
Normal file
@@ -0,0 +1,492 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestIsKilledFalseWithNonKilledProcess(t *testing.T) {
|
||||
lsCmd := exec.Command("ls")
|
||||
lsCmd.Start()
|
||||
// Wait for it to finish
|
||||
err := lsCmd.Wait()
|
||||
if IsKilled(err) {
|
||||
t.Fatalf("Expected the ls command to not be killed, was.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsKilledTrueWithKilledProcess(t *testing.T) {
|
||||
longCmd := exec.Command("top")
|
||||
// Start a command
|
||||
longCmd.Start()
|
||||
// Capture the error when *dying*
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
done <- longCmd.Wait()
|
||||
}()
|
||||
// Then kill it
|
||||
longCmd.Process.Kill()
|
||||
// Get the error
|
||||
err := <-done
|
||||
if !IsKilled(err) {
|
||||
t.Fatalf("Expected the command to be killed, was not.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunCommandWithOutput(t *testing.T) {
|
||||
echoHelloWorldCmd := exec.Command("echo", "hello", "world")
|
||||
out, exitCode, err := RunCommandWithOutput(echoHelloWorldCmd)
|
||||
expected := "hello world\n"
|
||||
if out != expected || exitCode != 0 || err != nil {
|
||||
t.Fatalf("Expected command to output %s, got %s, %v with exitCode %v", expected, out, err, exitCode)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunCommandWithOutputError(t *testing.T) {
|
||||
cmd := exec.Command("doesnotexists")
|
||||
out, exitCode, err := RunCommandWithOutput(cmd)
|
||||
expectedError := `exec: "doesnotexists": executable file not found in $PATH`
|
||||
if out != "" || exitCode != 127 || err == nil || err.Error() != expectedError {
|
||||
t.Fatalf("Expected command to output %s, got %s, %v with exitCode %v", expectedError, out, err, exitCode)
|
||||
}
|
||||
|
||||
wrongLsCmd := exec.Command("ls", "-z")
|
||||
expected := `ls: invalid option -- 'z'
|
||||
Try 'ls --help' for more information.
|
||||
`
|
||||
out, exitCode, err = RunCommandWithOutput(wrongLsCmd)
|
||||
|
||||
if out != expected || exitCode != 2 || err == nil || err.Error() != "exit status 2" {
|
||||
t.Fatalf("Expected command to output %s, got out:%s, err:%v with exitCode %v", expected, out, err, exitCode)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunCommandWithStdoutStderr(t *testing.T) {
|
||||
echoHelloWorldCmd := exec.Command("echo", "hello", "world")
|
||||
stdout, stderr, exitCode, err := RunCommandWithStdoutStderr(echoHelloWorldCmd)
|
||||
expected := "hello world\n"
|
||||
if stdout != expected || stderr != "" || exitCode != 0 || err != nil {
|
||||
t.Fatalf("Expected command to output %s, got stdout:%s, stderr:%s, err:%v with exitCode %v", expected, stdout, stderr, err, exitCode)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunCommandWithStdoutStderrError(t *testing.T) {
|
||||
cmd := exec.Command("doesnotexists")
|
||||
stdout, stderr, exitCode, err := RunCommandWithStdoutStderr(cmd)
|
||||
expectedError := `exec: "doesnotexists": executable file not found in $PATH`
|
||||
if stdout != "" || stderr != "" || exitCode != 127 || err == nil || err.Error() != expectedError {
|
||||
t.Fatalf("Expected command to output out:%s, stderr:%s, got stdout:%s, stderr:%s, err:%v with exitCode %v", "", "", stdout, stderr, err, exitCode)
|
||||
}
|
||||
|
||||
wrongLsCmd := exec.Command("ls", "-z")
|
||||
expected := `ls: invalid option -- 'z'
|
||||
Try 'ls --help' for more information.
|
||||
`
|
||||
|
||||
stdout, stderr, exitCode, err = RunCommandWithStdoutStderr(wrongLsCmd)
|
||||
if stdout != "" && stderr != expected || exitCode != 2 || err == nil || err.Error() != "exit status 2" {
|
||||
t.Fatalf("Expected command to output out:%s, stderr:%s, got stdout:%s, stderr:%s, err:%v with exitCode %v", "", expectedError, stdout, stderr, err, exitCode)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunCommandWithOutputForDurationFinished(t *testing.T) {
|
||||
cmd := exec.Command("ls")
|
||||
out, exitCode, timedOut, err := RunCommandWithOutputForDuration(cmd, 50*time.Millisecond)
|
||||
if out == "" || exitCode != 0 || timedOut || err != nil {
|
||||
t.Fatalf("Expected the command to run for less 50 milliseconds and thus not time out, but did not : out:[%s], exitCode:[%d], timedOut:[%v], err:[%v]", out, exitCode, timedOut, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunCommandWithOutputForDurationKilled(t *testing.T) {
|
||||
cmd := exec.Command("sh", "-c", "while true ; do echo 1 ; sleep .1 ; done")
|
||||
out, exitCode, timedOut, err := RunCommandWithOutputForDuration(cmd, 500*time.Millisecond)
|
||||
ones := strings.Split(out, "\n")
|
||||
if len(ones) != 6 || exitCode != 0 || !timedOut || err != nil {
|
||||
t.Fatalf("Expected the command to run for 500 milliseconds (and thus print six lines (five with 1, one empty) and time out, but did not : out:[%s], exitCode:%d, timedOut:%v, err:%v", out, exitCode, timedOut, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunCommandWithOutputForDurationErrors(t *testing.T) {
|
||||
cmd := exec.Command("ls")
|
||||
cmd.Stdout = os.Stdout
|
||||
if _, _, _, err := RunCommandWithOutputForDuration(cmd, 1*time.Millisecond); err == nil || err.Error() != "cmd.Stdout already set" {
|
||||
t.Fatalf("Expected an error as cmd.Stdout was already set, did not (err:%s).", err)
|
||||
}
|
||||
cmd = exec.Command("ls")
|
||||
cmd.Stderr = os.Stderr
|
||||
if _, _, _, err := RunCommandWithOutputForDuration(cmd, 1*time.Millisecond); err == nil || err.Error() != "cmd.Stderr already set" {
|
||||
t.Fatalf("Expected an error as cmd.Stderr was already set, did not (err:%s).", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunCommandWithOutputAndTimeoutFinished(t *testing.T) {
|
||||
cmd := exec.Command("ls")
|
||||
out, exitCode, err := RunCommandWithOutputAndTimeout(cmd, 50*time.Millisecond)
|
||||
if out == "" || exitCode != 0 || err != nil {
|
||||
t.Fatalf("Expected the command to run for less 50 milliseconds and thus not time out, but did not : out:[%s], exitCode:[%d], err:[%v]", out, exitCode, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunCommandWithOutputAndTimeoutKilled(t *testing.T) {
|
||||
cmd := exec.Command("sh", "-c", "while true ; do echo 1 ; sleep .1 ; done")
|
||||
out, exitCode, err := RunCommandWithOutputAndTimeout(cmd, 500*time.Millisecond)
|
||||
ones := strings.Split(out, "\n")
|
||||
if len(ones) != 6 || exitCode != 0 || err == nil || err.Error() != "command timed out" {
|
||||
t.Fatalf("Expected the command to run for 500 milliseconds (and thus print six lines (five with 1, one empty) and time out with an error 'command timed out', but did not : out:[%s], exitCode:%d, err:%v", out, exitCode, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunCommandWithOutputAndTimeoutErrors(t *testing.T) {
|
||||
cmd := exec.Command("ls")
|
||||
cmd.Stdout = os.Stdout
|
||||
if _, _, err := RunCommandWithOutputAndTimeout(cmd, 1*time.Millisecond); err == nil || err.Error() != "cmd.Stdout already set" {
|
||||
t.Fatalf("Expected an error as cmd.Stdout was already set, did not (err:%s).", err)
|
||||
}
|
||||
cmd = exec.Command("ls")
|
||||
cmd.Stderr = os.Stderr
|
||||
if _, _, err := RunCommandWithOutputAndTimeout(cmd, 1*time.Millisecond); err == nil || err.Error() != "cmd.Stderr already set" {
|
||||
t.Fatalf("Expected an error as cmd.Stderr was already set, did not (err:%s).", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunCommand(t *testing.T) {
|
||||
lsCmd := exec.Command("ls")
|
||||
exitCode, err := RunCommand(lsCmd)
|
||||
if exitCode != 0 || err != nil {
|
||||
t.Fatalf("Expected runCommand to run the command successfully, got: exitCode:%d, err:%v", exitCode, err)
|
||||
}
|
||||
|
||||
var expectedError string
|
||||
|
||||
exitCode, err = RunCommand(exec.Command("doesnotexists"))
|
||||
expectedError = `exec: "doesnotexists": executable file not found in $PATH`
|
||||
if exitCode != 127 || err == nil || err.Error() != expectedError {
|
||||
t.Fatalf("Expected runCommand to run the command successfully, got: exitCode:%d, err:%v", exitCode, err)
|
||||
}
|
||||
wrongLsCmd := exec.Command("ls", "-z")
|
||||
expected := 2
|
||||
expectedError = `exit status 2`
|
||||
exitCode, err = RunCommand(wrongLsCmd)
|
||||
if exitCode != expected || err == nil || err.Error() != expectedError {
|
||||
t.Fatalf("Expected runCommand to run the command successfully, got: exitCode:%d, err:%v", exitCode, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunCommandPipelineWithOutputWithNotEnoughCmds(t *testing.T) {
|
||||
_, _, err := RunCommandPipelineWithOutput(exec.Command("ls"))
|
||||
expectedError := "pipeline does not have multiple cmds"
|
||||
if err == nil || err.Error() != expectedError {
|
||||
t.Fatalf("Expected an error with %s, got err:%s", expectedError, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunCommandPipelineWithOutputErrors(t *testing.T) {
|
||||
cmd1 := exec.Command("ls")
|
||||
cmd1.Stdout = os.Stdout
|
||||
cmd2 := exec.Command("anything really")
|
||||
_, _, err := RunCommandPipelineWithOutput(cmd1, cmd2)
|
||||
if err == nil || err.Error() != "cannot set stdout pipe for anything really: exec: Stdout already set" {
|
||||
t.Fatalf("Expected an error, got %v", err)
|
||||
}
|
||||
|
||||
cmdWithError := exec.Command("doesnotexists")
|
||||
cmdCat := exec.Command("cat")
|
||||
_, _, err = RunCommandPipelineWithOutput(cmdWithError, cmdCat)
|
||||
if err == nil || err.Error() != `starting doesnotexists failed with error: exec: "doesnotexists": executable file not found in $PATH` {
|
||||
t.Fatalf("Expected an error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunCommandPipelineWithOutput(t *testing.T) {
|
||||
cmds := []*exec.Cmd{
|
||||
// Print 2 characters
|
||||
exec.Command("echo", "-n", "11"),
|
||||
// Count the number or char from stdin (previous command)
|
||||
exec.Command("wc", "-m"),
|
||||
}
|
||||
out, exitCode, err := RunCommandPipelineWithOutput(cmds...)
|
||||
expectedOutput := "2\n"
|
||||
if out != expectedOutput || exitCode != 0 || err != nil {
|
||||
t.Fatalf("Expected %s for commands %v, got out:%s, exitCode:%d, err:%v", expectedOutput, cmds, out, exitCode, err)
|
||||
}
|
||||
}
|
||||
|
||||
// Simple simple test as it is just a passthrough for json.Unmarshal
|
||||
func TestUnmarshalJSON(t *testing.T) {
|
||||
emptyResult := struct{}{}
|
||||
if err := UnmarshalJSON([]byte(""), &emptyResult); err == nil {
|
||||
t.Fatalf("Expected an error, got nothing")
|
||||
}
|
||||
result := struct{ Name string }{}
|
||||
if err := UnmarshalJSON([]byte(`{"name": "name"}`), &result); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.Name != "name" {
|
||||
t.Fatalf("Expected result.name to be 'name', was '%s'", result.Name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertSliceOfStringsToMap(t *testing.T) {
|
||||
input := []string{"a", "b"}
|
||||
actual := ConvertSliceOfStringsToMap(input)
|
||||
for _, key := range input {
|
||||
if _, ok := actual[key]; !ok {
|
||||
t.Fatalf("Expected output to contains key %s, did not: %v", key, actual)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompareDirectoryEntries(t *testing.T) {
|
||||
tmpFolder, err := ioutil.TempDir("", "integration-cli-utils-compare-directories")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(tmpFolder)
|
||||
|
||||
file1 := path.Join(tmpFolder, "file1")
|
||||
file2 := path.Join(tmpFolder, "file2")
|
||||
os.Create(file1)
|
||||
os.Create(file2)
|
||||
|
||||
fi1, err := os.Stat(file1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fi1bis, err := os.Stat(file1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fi2, err := os.Stat(file2)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
cases := []struct {
|
||||
e1 []os.FileInfo
|
||||
e2 []os.FileInfo
|
||||
shouldError bool
|
||||
}{
|
||||
// Empty directories
|
||||
{
|
||||
[]os.FileInfo{},
|
||||
[]os.FileInfo{},
|
||||
false,
|
||||
},
|
||||
// Same FileInfos
|
||||
{
|
||||
[]os.FileInfo{fi1},
|
||||
[]os.FileInfo{fi1},
|
||||
false,
|
||||
},
|
||||
// Different FileInfos but same names
|
||||
{
|
||||
[]os.FileInfo{fi1},
|
||||
[]os.FileInfo{fi1bis},
|
||||
false,
|
||||
},
|
||||
// Different FileInfos, different names
|
||||
{
|
||||
[]os.FileInfo{fi1},
|
||||
[]os.FileInfo{fi2},
|
||||
true,
|
||||
},
|
||||
}
|
||||
for _, elt := range cases {
|
||||
err := CompareDirectoryEntries(elt.e1, elt.e2)
|
||||
if elt.shouldError && err == nil {
|
||||
t.Fatalf("Should have return an error, did not with %v and %v", elt.e1, elt.e2)
|
||||
}
|
||||
if !elt.shouldError && err != nil {
|
||||
t.Fatalf("Should have not returned an error, but did : %v with %v and %v", err, elt.e1, elt.e2)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME make an "unhappy path" test for ListTar without "panicking" :-)
|
||||
func TestListTar(t *testing.T) {
|
||||
tmpFolder, err := ioutil.TempDir("", "integration-cli-utils-list-tar")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(tmpFolder)
|
||||
|
||||
// Let's create a Tar file
|
||||
srcFile := path.Join(tmpFolder, "src")
|
||||
tarFile := path.Join(tmpFolder, "src.tar")
|
||||
os.Create(srcFile)
|
||||
cmd := exec.Command("/bin/sh", "-c", "tar cf "+tarFile+" "+srcFile)
|
||||
_, err = cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
reader, err := os.Open(tarFile)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer reader.Close()
|
||||
|
||||
entries, err := ListTar(reader)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(entries) != 1 && entries[0] != "src" {
|
||||
t.Fatalf("Expected a tar file with 1 entry (%s), got %v", srcFile, entries)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRandomTmpDirPath(t *testing.T) {
|
||||
path := RandomTmpDirPath("something", runtime.GOOS)
|
||||
|
||||
prefix := "/tmp/something"
|
||||
if runtime.GOOS == "windows" {
|
||||
prefix = os.Getenv("TEMP") + `\something`
|
||||
}
|
||||
expectedSize := len(prefix) + 11
|
||||
|
||||
if !strings.HasPrefix(path, prefix) {
|
||||
t.Fatalf("Expected generated path to have '%s' as prefix, got %s'", prefix, path)
|
||||
}
|
||||
if len(path) != expectedSize {
|
||||
t.Fatalf("Expected generated path to be %d, got %d", expectedSize, len(path))
|
||||
}
|
||||
}
|
||||
|
||||
func TestConsumeWithSpeed(t *testing.T) {
|
||||
reader := strings.NewReader("1234567890")
|
||||
chunksize := 2
|
||||
|
||||
bytes1, err := ConsumeWithSpeed(reader, chunksize, 1*time.Second, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if bytes1 != 10 {
|
||||
t.Fatalf("Expected to have read 10 bytes, got %d", bytes1)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestConsumeWithSpeedWithStop(t *testing.T) {
|
||||
reader := strings.NewReader("1234567890")
|
||||
chunksize := 2
|
||||
|
||||
stopIt := make(chan bool)
|
||||
|
||||
go func() {
|
||||
time.Sleep(1 * time.Millisecond)
|
||||
stopIt <- true
|
||||
}()
|
||||
|
||||
bytes1, err := ConsumeWithSpeed(reader, chunksize, 20*time.Millisecond, stopIt)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if bytes1 != 2 {
|
||||
t.Fatalf("Expected to have read 2 bytes, got %d", bytes1)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestParseCgroupPathsEmpty(t *testing.T) {
|
||||
cgroupMap := ParseCgroupPaths("")
|
||||
if len(cgroupMap) != 0 {
|
||||
t.Fatalf("Expected an empty map, got %v", cgroupMap)
|
||||
}
|
||||
cgroupMap = ParseCgroupPaths("\n")
|
||||
if len(cgroupMap) != 0 {
|
||||
t.Fatalf("Expected an empty map, got %v", cgroupMap)
|
||||
}
|
||||
cgroupMap = ParseCgroupPaths("something:else\nagain:here")
|
||||
if len(cgroupMap) != 0 {
|
||||
t.Fatalf("Expected an empty map, got %v", cgroupMap)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseCgroupPaths(t *testing.T) {
|
||||
cgroupMap := ParseCgroupPaths("2:memory:/a\n1:cpuset:/b")
|
||||
if len(cgroupMap) != 2 {
|
||||
t.Fatalf("Expected a map with 2 entries, got %v", cgroupMap)
|
||||
}
|
||||
if value, ok := cgroupMap["memory"]; !ok || value != "/a" {
|
||||
t.Fatalf("Expected cgroupMap to contains an entry for 'memory' with value '/a', got %v", cgroupMap)
|
||||
}
|
||||
if value, ok := cgroupMap["cpuset"]; !ok || value != "/b" {
|
||||
t.Fatalf("Expected cgroupMap to contains an entry for 'cpuset' with value '/b', got %v", cgroupMap)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChannelBufferTimeout(t *testing.T) {
|
||||
expected := "11"
|
||||
|
||||
buf := &ChannelBuffer{make(chan []byte, 1)}
|
||||
defer buf.Close()
|
||||
|
||||
go func() {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
io.Copy(buf, strings.NewReader(expected))
|
||||
}()
|
||||
|
||||
// Wait long enough
|
||||
b := make([]byte, 2)
|
||||
_, err := buf.ReadTimeout(b, 50*time.Millisecond)
|
||||
if err == nil && err.Error() != "timeout reading from channel" {
|
||||
t.Fatalf("Expected an error, got %s", err)
|
||||
}
|
||||
|
||||
// Wait for the end :)
|
||||
time.Sleep(150 * time.Millisecond)
|
||||
}
|
||||
|
||||
func TestChannelBuffer(t *testing.T) {
|
||||
expected := "11"
|
||||
|
||||
buf := &ChannelBuffer{make(chan []byte, 1)}
|
||||
defer buf.Close()
|
||||
|
||||
go func() {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
io.Copy(buf, strings.NewReader(expected))
|
||||
}()
|
||||
|
||||
// Wait long enough
|
||||
b := make([]byte, 2)
|
||||
_, err := buf.ReadTimeout(b, 200*time.Millisecond)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if string(b) != expected {
|
||||
t.Fatalf("Expected '%s', got '%s'", expected, string(b))
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME doesn't work
|
||||
// func TestRunAtDifferentDate(t *testing.T) {
|
||||
// var date string
|
||||
|
||||
// // Layout for date. MMDDhhmmYYYY
|
||||
// const timeLayout = "20060102"
|
||||
// expectedDate := "20100201"
|
||||
// theDate, err := time.Parse(timeLayout, expectedDate)
|
||||
// if err != nil {
|
||||
// t.Fatal(err)
|
||||
// }
|
||||
|
||||
// RunAtDifferentDate(theDate, func() {
|
||||
// cmd := exec.Command("date", "+%Y%M%d")
|
||||
// out, err := cmd.Output()
|
||||
// if err != nil {
|
||||
// t.Fatal(err)
|
||||
// }
|
||||
// date = string(out)
|
||||
// })
|
||||
// }
|
||||
Reference in New Issue
Block a user