mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
integration-cli: have helper functions use testing.Helper()
Signed-off-by: Tibor Vass <tibor@docker.com>
(cherry picked from commit bad6f3bf73
)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
15aa73ea4c
commit
05933ab2d4
1 changed files with 23 additions and 0 deletions
|
@ -50,11 +50,13 @@ func dockerCmdWithResult(args ...string) *icmd.Result {
|
||||||
}
|
}
|
||||||
|
|
||||||
func findContainerIP(c *testing.T, id string, network string) string {
|
func findContainerIP(c *testing.T, id string, network string) string {
|
||||||
|
c.Helper()
|
||||||
out, _ := dockerCmd(c, "inspect", fmt.Sprintf("--format='{{ .NetworkSettings.Networks.%s.IPAddress }}'", network), id)
|
out, _ := dockerCmd(c, "inspect", fmt.Sprintf("--format='{{ .NetworkSettings.Networks.%s.IPAddress }}'", network), id)
|
||||||
return strings.Trim(out, " \r\n'")
|
return strings.Trim(out, " \r\n'")
|
||||||
}
|
}
|
||||||
|
|
||||||
func getContainerCount(c *testing.T) int {
|
func getContainerCount(c *testing.T) int {
|
||||||
|
c.Helper()
|
||||||
const containers = "Containers:"
|
const containers = "Containers:"
|
||||||
|
|
||||||
result := icmd.RunCommand(dockerBinary, "info")
|
result := icmd.RunCommand(dockerBinary, "info")
|
||||||
|
@ -75,6 +77,7 @@ func getContainerCount(c *testing.T) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
func inspectFieldAndUnmarshall(c *testing.T, name, field string, output interface{}) {
|
func inspectFieldAndUnmarshall(c *testing.T, name, field string, output interface{}) {
|
||||||
|
c.Helper()
|
||||||
str := inspectFieldJSON(c, name, field)
|
str := inspectFieldJSON(c, name, field)
|
||||||
err := json.Unmarshal([]byte(str), output)
|
err := json.Unmarshal([]byte(str), output)
|
||||||
if c != nil {
|
if c != nil {
|
||||||
|
@ -99,6 +102,7 @@ func inspectFieldWithError(name, field string) (string, error) {
|
||||||
|
|
||||||
// Deprecated: use cli.Inspect
|
// Deprecated: use cli.Inspect
|
||||||
func inspectField(c *testing.T, name, field string) string {
|
func inspectField(c *testing.T, name, field string) string {
|
||||||
|
c.Helper()
|
||||||
out, err := inspectFilter(name, fmt.Sprintf(".%s", field))
|
out, err := inspectFilter(name, fmt.Sprintf(".%s", field))
|
||||||
if c != nil {
|
if c != nil {
|
||||||
assert.NilError(c, err)
|
assert.NilError(c, err)
|
||||||
|
@ -108,6 +112,7 @@ func inspectField(c *testing.T, name, field string) string {
|
||||||
|
|
||||||
// Deprecated: use cli.Inspect
|
// Deprecated: use cli.Inspect
|
||||||
func inspectFieldJSON(c *testing.T, name, field string) string {
|
func inspectFieldJSON(c *testing.T, name, field string) string {
|
||||||
|
c.Helper()
|
||||||
out, err := inspectFilter(name, fmt.Sprintf("json .%s", field))
|
out, err := inspectFilter(name, fmt.Sprintf("json .%s", field))
|
||||||
if c != nil {
|
if c != nil {
|
||||||
assert.NilError(c, err)
|
assert.NilError(c, err)
|
||||||
|
@ -117,6 +122,7 @@ func inspectFieldJSON(c *testing.T, name, field string) string {
|
||||||
|
|
||||||
// Deprecated: use cli.Inspect
|
// Deprecated: use cli.Inspect
|
||||||
func inspectFieldMap(c *testing.T, name, path, field string) string {
|
func inspectFieldMap(c *testing.T, name, path, field string) string {
|
||||||
|
c.Helper()
|
||||||
out, err := inspectFilter(name, fmt.Sprintf("index .%s %q", path, field))
|
out, err := inspectFilter(name, fmt.Sprintf("index .%s %q", path, field))
|
||||||
if c != nil {
|
if c != nil {
|
||||||
assert.NilError(c, err)
|
assert.NilError(c, err)
|
||||||
|
@ -169,6 +175,7 @@ func inspectMountPointJSON(j, destination string) (types.MountPoint, error) {
|
||||||
|
|
||||||
// Deprecated: use cli.Inspect
|
// Deprecated: use cli.Inspect
|
||||||
func inspectImage(c *testing.T, name, filter string) string {
|
func inspectImage(c *testing.T, name, filter string) string {
|
||||||
|
c.Helper()
|
||||||
args := []string{"inspect", "--type", "image"}
|
args := []string{"inspect", "--type", "image"}
|
||||||
if filter != "" {
|
if filter != "" {
|
||||||
format := fmt.Sprintf("{{%s}}", filter)
|
format := fmt.Sprintf("{{%s}}", filter)
|
||||||
|
@ -181,6 +188,7 @@ func inspectImage(c *testing.T, name, filter string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func getIDByName(c *testing.T, name string) string {
|
func getIDByName(c *testing.T, name string) string {
|
||||||
|
c.Helper()
|
||||||
id, err := inspectFieldWithError(name, "Id")
|
id, err := inspectFieldWithError(name, "Id")
|
||||||
assert.NilError(c, err)
|
assert.NilError(c, err)
|
||||||
return id
|
return id
|
||||||
|
@ -188,6 +196,7 @@ func getIDByName(c *testing.T, name string) string {
|
||||||
|
|
||||||
// Deprecated: use cli.Build
|
// Deprecated: use cli.Build
|
||||||
func buildImageSuccessfully(c *testing.T, name string, cmdOperators ...cli.CmdOperator) {
|
func buildImageSuccessfully(c *testing.T, name string, cmdOperators ...cli.CmdOperator) {
|
||||||
|
c.Helper()
|
||||||
buildImage(name, cmdOperators...).Assert(c, icmd.Success)
|
buildImage(name, cmdOperators...).Assert(c, icmd.Success)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -201,6 +210,7 @@ func buildImage(name string, cmdOperators ...cli.CmdOperator) *icmd.Result {
|
||||||
// The file is truncated if it already exists.
|
// The file is truncated if it already exists.
|
||||||
// Fail the test when error occurs.
|
// Fail the test when error occurs.
|
||||||
func writeFile(dst, content string, c *testing.T) {
|
func writeFile(dst, content string, c *testing.T) {
|
||||||
|
c.Helper()
|
||||||
// Create subdirectories if necessary
|
// Create subdirectories if necessary
|
||||||
assert.Assert(c, os.MkdirAll(path.Dir(dst), 0700) == nil)
|
assert.Assert(c, os.MkdirAll(path.Dir(dst), 0700) == nil)
|
||||||
f, err := os.OpenFile(dst, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0700)
|
f, err := os.OpenFile(dst, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0700)
|
||||||
|
@ -214,6 +224,7 @@ func writeFile(dst, content string, c *testing.T) {
|
||||||
// Return the contents of file at path `src`.
|
// Return the contents of file at path `src`.
|
||||||
// Fail the test when error occurs.
|
// Fail the test when error occurs.
|
||||||
func readFile(src string, c *testing.T) (content string) {
|
func readFile(src string, c *testing.T) (content string) {
|
||||||
|
c.Helper()
|
||||||
data, err := ioutil.ReadFile(src)
|
data, err := ioutil.ReadFile(src)
|
||||||
assert.NilError(c, err)
|
assert.NilError(c, err)
|
||||||
|
|
||||||
|
@ -226,6 +237,7 @@ func containerStorageFile(containerID, basename string) string {
|
||||||
|
|
||||||
// docker commands that use this function must be run with the '-d' switch.
|
// docker commands that use this function must be run with the '-d' switch.
|
||||||
func runCommandAndReadContainerFile(c *testing.T, filename string, command string, args ...string) []byte {
|
func runCommandAndReadContainerFile(c *testing.T, filename string, command string, args ...string) []byte {
|
||||||
|
c.Helper()
|
||||||
result := icmd.RunCommand(command, args...)
|
result := icmd.RunCommand(command, args...)
|
||||||
result.Assert(c, icmd.Success)
|
result.Assert(c, icmd.Success)
|
||||||
contID := strings.TrimSpace(result.Combined())
|
contID := strings.TrimSpace(result.Combined())
|
||||||
|
@ -236,6 +248,7 @@ func runCommandAndReadContainerFile(c *testing.T, filename string, command strin
|
||||||
}
|
}
|
||||||
|
|
||||||
func readContainerFile(c *testing.T, containerID, filename string) []byte {
|
func readContainerFile(c *testing.T, containerID, filename string) []byte {
|
||||||
|
c.Helper()
|
||||||
f, err := os.Open(containerStorageFile(containerID, filename))
|
f, err := os.Open(containerStorageFile(containerID, filename))
|
||||||
assert.NilError(c, err)
|
assert.NilError(c, err)
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
@ -246,6 +259,7 @@ func readContainerFile(c *testing.T, containerID, filename string) []byte {
|
||||||
}
|
}
|
||||||
|
|
||||||
func readContainerFileWithExec(c *testing.T, containerID, filename string) []byte {
|
func readContainerFileWithExec(c *testing.T, containerID, filename string) []byte {
|
||||||
|
c.Helper()
|
||||||
result := icmd.RunCommand(dockerBinary, "exec", containerID, "cat", filename)
|
result := icmd.RunCommand(dockerBinary, "exec", containerID, "cat", filename)
|
||||||
result.Assert(c, icmd.Success)
|
result.Assert(c, icmd.Success)
|
||||||
return []byte(result.Combined())
|
return []byte(result.Combined())
|
||||||
|
@ -253,6 +267,7 @@ func readContainerFileWithExec(c *testing.T, containerID, filename string) []byt
|
||||||
|
|
||||||
// daemonTime provides the current time on the daemon host
|
// daemonTime provides the current time on the daemon host
|
||||||
func daemonTime(c *testing.T) time.Time {
|
func daemonTime(c *testing.T) time.Time {
|
||||||
|
c.Helper()
|
||||||
if testEnv.IsLocalDaemon() {
|
if testEnv.IsLocalDaemon() {
|
||||||
return time.Now()
|
return time.Now()
|
||||||
}
|
}
|
||||||
|
@ -271,6 +286,7 @@ func daemonTime(c *testing.T) time.Time {
|
||||||
// daemonUnixTime returns the current time on the daemon host with nanoseconds precision.
|
// daemonUnixTime returns the current time on the daemon host with nanoseconds precision.
|
||||||
// It return the time formatted how the client sends timestamps to the server.
|
// It return the time formatted how the client sends timestamps to the server.
|
||||||
func daemonUnixTime(c *testing.T) string {
|
func daemonUnixTime(c *testing.T) string {
|
||||||
|
c.Helper()
|
||||||
return parseEventTime(daemonTime(c))
|
return parseEventTime(daemonTime(c))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -306,6 +322,7 @@ func appendBaseEnv(isTLS bool, env ...string) []string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func createTmpFile(c *testing.T, content string) string {
|
func createTmpFile(c *testing.T, content string) string {
|
||||||
|
c.Helper()
|
||||||
f, err := ioutil.TempFile("", "testfile")
|
f, err := ioutil.TempFile("", "testfile")
|
||||||
assert.NilError(c, err)
|
assert.NilError(c, err)
|
||||||
|
|
||||||
|
@ -337,6 +354,7 @@ func waitInspectWithArgs(name, expr, expected string, timeout time.Duration, arg
|
||||||
}
|
}
|
||||||
|
|
||||||
func getInspectBody(c *testing.T, version, id string) []byte {
|
func getInspectBody(c *testing.T, version, id string) []byte {
|
||||||
|
c.Helper()
|
||||||
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithVersion(version))
|
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithVersion(version))
|
||||||
assert.NilError(c, err)
|
assert.NilError(c, err)
|
||||||
defer cli.Close()
|
defer cli.Close()
|
||||||
|
@ -348,12 +366,14 @@ func getInspectBody(c *testing.T, version, id string) []byte {
|
||||||
// Run a long running idle task in a background container using the
|
// Run a long running idle task in a background container using the
|
||||||
// system-specific default image and command.
|
// system-specific default image and command.
|
||||||
func runSleepingContainer(c *testing.T, extraArgs ...string) string {
|
func runSleepingContainer(c *testing.T, extraArgs ...string) string {
|
||||||
|
c.Helper()
|
||||||
return runSleepingContainerInImage(c, "busybox", extraArgs...)
|
return runSleepingContainerInImage(c, "busybox", extraArgs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run a long running idle task in a background container using the specified
|
// Run a long running idle task in a background container using the specified
|
||||||
// image and the system-specific command.
|
// image and the system-specific command.
|
||||||
func runSleepingContainerInImage(c *testing.T, image string, extraArgs ...string) string {
|
func runSleepingContainerInImage(c *testing.T, image string, extraArgs ...string) string {
|
||||||
|
c.Helper()
|
||||||
args := []string{"run", "-d"}
|
args := []string{"run", "-d"}
|
||||||
args = append(args, extraArgs...)
|
args = append(args, extraArgs...)
|
||||||
args = append(args, image)
|
args = append(args, image)
|
||||||
|
@ -408,6 +428,7 @@ func waitForGoroutines(expected int) error {
|
||||||
|
|
||||||
// getErrorMessage returns the error message from an error API response
|
// getErrorMessage returns the error message from an error API response
|
||||||
func getErrorMessage(c *testing.T, body []byte) string {
|
func getErrorMessage(c *testing.T, body []byte) string {
|
||||||
|
c.Helper()
|
||||||
var resp types.ErrorResponse
|
var resp types.ErrorResponse
|
||||||
assert.Assert(c, json.Unmarshal(body, &resp) == nil)
|
assert.Assert(c, json.Unmarshal(body, &resp) == nil)
|
||||||
return strings.TrimSpace(resp.Message)
|
return strings.TrimSpace(resp.Message)
|
||||||
|
@ -418,6 +439,7 @@ type reducer func(...interface{}) interface{}
|
||||||
|
|
||||||
func pollCheck(t *testing.T, f checkF, compare func(x interface{}) assert.BoolOrComparison) poll.Check {
|
func pollCheck(t *testing.T, f checkF, compare func(x interface{}) assert.BoolOrComparison) poll.Check {
|
||||||
return func(poll.LogT) poll.Result {
|
return func(poll.LogT) poll.Result {
|
||||||
|
t.Helper()
|
||||||
v, comment := f(t)
|
v, comment := f(t)
|
||||||
if assert.Check(t, compare(v)) {
|
if assert.Check(t, compare(v)) {
|
||||||
return poll.Success()
|
return poll.Success()
|
||||||
|
@ -428,6 +450,7 @@ func pollCheck(t *testing.T, f checkF, compare func(x interface{}) assert.BoolOr
|
||||||
|
|
||||||
func reducedCheck(r reducer, funcs ...checkF) checkF {
|
func reducedCheck(r reducer, funcs ...checkF) checkF {
|
||||||
return func(c *testing.T) (interface{}, string) {
|
return func(c *testing.T) (interface{}, string) {
|
||||||
|
c.Helper()
|
||||||
var values []interface{}
|
var values []interface{}
|
||||||
var comments []string
|
var comments []string
|
||||||
for _, f := range funcs {
|
for _, f := range funcs {
|
||||||
|
|
Loading…
Reference in a new issue