Browse code

Add integration test for history option

Parse the history output to locate the size fields and check
whether they are the correct format or not. Use Column name SIZE
to mark start and end indices of the size fields

Fixes #12578

Signed-off-by: Ankush Agarwal <ankushagarwal11@gmail.com>

Ankush Agarwal authored on 2015/04/21 13:47:56
Showing 1 changed files
... ...
@@ -3,6 +3,8 @@ package main
3 3
 import (
4 4
 	"fmt"
5 5
 	"os/exec"
6
+	"regexp"
7
+	"strconv"
6 8
 	"strings"
7 9
 
8 10
 	"github.com/go-check/check"
... ...
@@ -122,3 +124,40 @@ func (s *DockerSuite) TestHistoryImageWithComment(c *check.C) {
122 122
 	}
123 123
 
124 124
 }
125
+
126
+func (s *DockerSuite) TestHistoryHumanOptionFalse(c *check.C) {
127
+	out, _, _ := runCommandWithOutput(exec.Command(dockerBinary, "history", "--human=false", "busybox"))
128
+	lines := strings.Split(out, "\n")
129
+	sizeColumnRegex, _ := regexp.Compile("SIZE +")
130
+	indices := sizeColumnRegex.FindStringIndex(lines[0])
131
+	startIndex := indices[0]
132
+	endIndex := indices[1]
133
+	for i := 1; i < len(lines)-1; i++ {
134
+		if endIndex > len(lines[i]) {
135
+			endIndex = len(lines[i])
136
+		}
137
+		sizeString := lines[i][startIndex:endIndex]
138
+		if _, err := strconv.Atoi(strings.TrimSpace(sizeString)); err != nil {
139
+			c.Fatalf("The size '%s' was not an Integer", sizeString)
140
+		}
141
+	}
142
+}
143
+
144
+func (s *DockerSuite) TestHistoryHumanOptionTrue(c *check.C) {
145
+	out, _, _ := runCommandWithOutput(exec.Command(dockerBinary, "history", "--human=true", "busybox"))
146
+	lines := strings.Split(out, "\n")
147
+	sizeColumnRegex, _ := regexp.Compile("SIZE +")
148
+	humanSizeRegex, _ := regexp.Compile("^\\d+.*B$") // Matches human sizes like 10 MB, 3.2 KB, etc
149
+	indices := sizeColumnRegex.FindStringIndex(lines[0])
150
+	startIndex := indices[0]
151
+	endIndex := indices[1]
152
+	for i := 1; i < len(lines)-1; i++ {
153
+		if endIndex > len(lines[i]) {
154
+			endIndex = len(lines[i])
155
+		}
156
+		sizeString := lines[i][startIndex:endIndex]
157
+		if matchSuccess := humanSizeRegex.MatchString(strings.TrimSpace(sizeString)); !matchSuccess {
158
+			c.Fatalf("The size '%s' was not in human format", sizeString)
159
+		}
160
+	}
161
+}