Signed-off-by: Pradeep Chhetri <pradeep@indix.com>
| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,121 @@ |
| 0 |
+package main |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "os/exec" |
|
| 4 |
+ "testing" |
|
| 5 |
+ "time" |
|
| 6 |
+) |
|
| 7 |
+ |
|
| 8 |
+// non-blocking wait with 0 exit code |
|
| 9 |
+func TestWaitNonBlockedExitZero(t *testing.T) {
|
|
| 10 |
+ defer deleteAllContainers() |
|
| 11 |
+ |
|
| 12 |
+ runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "true") |
|
| 13 |
+ out, _, err := runCommandWithOutput(runCmd) |
|
| 14 |
+ if err != nil {
|
|
| 15 |
+ t.Fatal(out, err) |
|
| 16 |
+ } |
|
| 17 |
+ containerID := stripTrailingCharacters(out) |
|
| 18 |
+ |
|
| 19 |
+ status := "true" |
|
| 20 |
+ for i := 0; status != "false"; i++ {
|
|
| 21 |
+ runCmd = exec.Command(dockerBinary, "inspect", "--format='{{.State.Running}}'", containerID)
|
|
| 22 |
+ status, _, err = runCommandWithOutput(runCmd) |
|
| 23 |
+ if err != nil {
|
|
| 24 |
+ t.Fatal(status, err) |
|
| 25 |
+ } |
|
| 26 |
+ status = stripTrailingCharacters(status) |
|
| 27 |
+ |
|
| 28 |
+ time.Sleep(time.Second) |
|
| 29 |
+ if i >= 60 {
|
|
| 30 |
+ t.Fatal("Container should have stopped by now")
|
|
| 31 |
+ } |
|
| 32 |
+ } |
|
| 33 |
+ |
|
| 34 |
+ runCmd = exec.Command(dockerBinary, "wait", containerID) |
|
| 35 |
+ out, _, err = runCommandWithOutput(runCmd) |
|
| 36 |
+ |
|
| 37 |
+ if err != nil || stripTrailingCharacters(out) != "0" {
|
|
| 38 |
+ t.Fatal("failed to set up container", out, err)
|
|
| 39 |
+ } |
|
| 40 |
+ |
|
| 41 |
+ logDone("wait - non-blocking wait with 0 exit code")
|
|
| 42 |
+} |
|
| 43 |
+ |
|
| 44 |
+// blocking wait with 0 exit code |
|
| 45 |
+func TestWaitBlockedExitZero(t *testing.T) {
|
|
| 46 |
+ defer deleteAllContainers() |
|
| 47 |
+ |
|
| 48 |
+ runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "sleep 10") |
|
| 49 |
+ out, _, err := runCommandWithOutput(runCmd) |
|
| 50 |
+ if err != nil {
|
|
| 51 |
+ t.Fatal(out, err) |
|
| 52 |
+ } |
|
| 53 |
+ containerID := stripTrailingCharacters(out) |
|
| 54 |
+ |
|
| 55 |
+ runCmd = exec.Command(dockerBinary, "wait", containerID) |
|
| 56 |
+ out, _, err = runCommandWithOutput(runCmd) |
|
| 57 |
+ |
|
| 58 |
+ if err != nil || stripTrailingCharacters(out) != "0" {
|
|
| 59 |
+ t.Fatal("failed to set up container", out, err)
|
|
| 60 |
+ } |
|
| 61 |
+ |
|
| 62 |
+ logDone("wait - blocking wait with 0 exit code")
|
|
| 63 |
+} |
|
| 64 |
+ |
|
| 65 |
+// non-blocking wait with random exit code |
|
| 66 |
+func TestWaitNonBlockedExitRandom(t *testing.T) {
|
|
| 67 |
+ defer deleteAllContainers() |
|
| 68 |
+ |
|
| 69 |
+ runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "exit 99") |
|
| 70 |
+ out, _, err := runCommandWithOutput(runCmd) |
|
| 71 |
+ if err != nil {
|
|
| 72 |
+ t.Fatal(out, err) |
|
| 73 |
+ } |
|
| 74 |
+ containerID := stripTrailingCharacters(out) |
|
| 75 |
+ |
|
| 76 |
+ status := "true" |
|
| 77 |
+ for i := 0; status != "false"; i++ {
|
|
| 78 |
+ runCmd = exec.Command(dockerBinary, "inspect", "--format='{{.State.Running}}'", containerID)
|
|
| 79 |
+ status, _, err = runCommandWithOutput(runCmd) |
|
| 80 |
+ if err != nil {
|
|
| 81 |
+ t.Fatal(status, err) |
|
| 82 |
+ } |
|
| 83 |
+ status = stripTrailingCharacters(status) |
|
| 84 |
+ |
|
| 85 |
+ time.Sleep(time.Second) |
|
| 86 |
+ if i >= 60 {
|
|
| 87 |
+ t.Fatal("Container should have stopped by now")
|
|
| 88 |
+ } |
|
| 89 |
+ } |
|
| 90 |
+ |
|
| 91 |
+ runCmd = exec.Command(dockerBinary, "wait", containerID) |
|
| 92 |
+ out, _, err = runCommandWithOutput(runCmd) |
|
| 93 |
+ |
|
| 94 |
+ if err != nil || stripTrailingCharacters(out) != "99" {
|
|
| 95 |
+ t.Fatal("failed to set up container", out, err)
|
|
| 96 |
+ } |
|
| 97 |
+ |
|
| 98 |
+ logDone("wait - non-blocking wait with random exit code")
|
|
| 99 |
+} |
|
| 100 |
+ |
|
| 101 |
+// blocking wait with random exit code |
|
| 102 |
+func TestWaitBlockedExitRandom(t *testing.T) {
|
|
| 103 |
+ defer deleteAllContainers() |
|
| 104 |
+ |
|
| 105 |
+ runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "sleep 10; exit 99") |
|
| 106 |
+ out, _, err := runCommandWithOutput(runCmd) |
|
| 107 |
+ if err != nil {
|
|
| 108 |
+ t.Fatal(out, err) |
|
| 109 |
+ } |
|
| 110 |
+ containerID := stripTrailingCharacters(out) |
|
| 111 |
+ |
|
| 112 |
+ runCmd = exec.Command(dockerBinary, "wait", containerID) |
|
| 113 |
+ out, _, err = runCommandWithOutput(runCmd) |
|
| 114 |
+ |
|
| 115 |
+ if err != nil || stripTrailingCharacters(out) != "99" {
|
|
| 116 |
+ t.Fatal("failed to set up container", out, err)
|
|
| 117 |
+ } |
|
| 118 |
+ |
|
| 119 |
+ logDone("wait - blocking wait with random exit code")
|
|
| 120 |
+} |