Browse code

integcli: add util to fetch container status

Docker-DCO-1.1-Signed-off-by: Cristian Staretu <cristian.staretu@gmail.com> (github: unclejack)

unclejack authored on 2014/08/13 23:30:45
Showing 1 changed files
... ...
@@ -486,6 +486,36 @@ func getIDByName(name string) (string, error) {
486 486
 	return inspectField(name, "Id")
487 487
 }
488 488
 
489
+// getContainerState returns the exit code of the container
490
+// and true if it's running
491
+// the exit code should be ignored if it's running
492
+func getContainerState(t *testing.T, id string) (int, bool, error) {
493
+	var (
494
+		exitStatus int
495
+		running    bool
496
+	)
497
+	out, exitCode, err := dockerCmd(t, "inspect", "--format={{.State.Running}} {{.State.ExitCode}}", id)
498
+	if err != nil || exitCode != 0 {
499
+		return 0, false, fmt.Errorf("'%s' doesn't exist: %s", id, err)
500
+	}
501
+
502
+	out = strings.Trim(out, "\n")
503
+	splitOutput := strings.Split(out, " ")
504
+	if len(splitOutput) != 2 {
505
+		return 0, false, fmt.Errorf("failed to get container state: output is broken")
506
+	}
507
+	if splitOutput[0] == "true" {
508
+		running = true
509
+	}
510
+	if n, err := strconv.Atoi(splitOutput[1]); err == nil {
511
+		exitStatus = n
512
+	} else {
513
+		return 0, false, fmt.Errorf("failed to get container state: couldn't parse integer")
514
+	}
515
+
516
+	return exitStatus, running, nil
517
+}
518
+
489 519
 func buildImageWithOut(name, dockerfile string, useCache bool) (string, string, error) {
490 520
 	args := []string{"build", "-t", name}
491 521
 	if !useCache {