Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit 5b90053

Browse files
authored
Merge pull request #225 from hyperhq/integration-test
Optimize integration test
2 parents d8a9672 + dbb0fa2 commit 5b90053

9 files changed

+95
-19
lines changed

hack/generate-hyper-conf-qa.sh

+5-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ else
1919
echo "========== Task: test BRANCH ${BRANCH} =========="
2020
fi
2121

22+
if [[ "${ACCESS_KEY}" == "" ]] || [[ "${SECRET_KEY}" == "" ]];then
23+
echo "Error: Please set ACCESS_KEY and SECRET_KEY"
24+
exit 1
25+
fi
2226

2327
if [[ "$@" != "./build.sh" ]];then
2428
#ensure config for hyper cli
@@ -88,7 +92,7 @@ fi
8892
#execute command
8993
if [[ $# -ne 0 ]];then
9094
echo "========== Test Cmd: $@ =========="
91-
cd /go/src/github.com/hyperhq/hypercli/integration-cli && eval $@
95+
cd /go/src/github.com/hyperhq/hypercli/integration-cli && $@
9296
if [[ "$@" == "./build.sh" ]];then
9397
#show make result
9498
if [[ $? -eq 0 ]];then

integration-cli/README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -345,10 +345,17 @@ $ docker run -it --rm \
345345
$ docker run -it --rm \
346346
-e ACCESS_KEY=${ACCESS_KEY} \
347347
-e SECRET_KEY=${SECRET_KEY} \
348-
-e BRANCH="#221" \
348+
-e BRANCH="#221" \
349349
hyperhq/hypercli-auto-test:qa go test -check.f TestCli -timeout 180m
350350
351351
352+
//test `specified case name`
353+
$ docker run -it --rm \
354+
-e ACCESS_KEY="${ACCESS_KEY}" \
355+
-e SECRET_KEY="${SECRET_KEY}" \
356+
hyperhq/hypercli-auto-test:qa go test -check.f 'TestCliInfo|TestCliFip' -timeout 180m
357+
358+
352359
//test `specified branch` with `packet` apirouter
353360
$ docker run -it --rm \
354361
-e ACCESS_KEY=${ACCESS_KEY} \

integration-cli/check_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,23 @@ func (s *DockerSuite) TearDownTest(c *check.C) {
3535
deleteAllImages()
3636
deleteAllSnapshots()
3737
deleteAllVolumes()
38+
deleteAllFips()
3839
//deleteAllNetworks()
3940
}
4041

4142
func init() {
4243
check.Suite(&DockerRegistrySuite{
4344
ds: &DockerSuite{},
4445
})
46+
47+
fmt.Printf("INFO: clear resources(containers, images, snapshots, volumes, fips)\n")
48+
deleteAllContainers()
49+
deleteAllImages()
50+
deleteAllSnapshots()
51+
deleteAllVolumes()
52+
deleteAllFips()
53+
54+
fmt.Println("INFO: finish init")
4555
}
4656

4757
type DockerRegistrySuite struct {

integration-cli/docker_utils.go

+38-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ func init() {
114114
if os.Getenv("DOCKER_HOST") != "" {
115115
flag_host = "--host=" + os.Getenv("DOCKER_HOST")
116116
}
117-
fmt.Println("finish init")
118117
}
119118

120119
// Daemon represents a Docker daemon for the testing framework.
@@ -845,6 +844,44 @@ func deleteAllImages() error {
845844
return nil
846845
}
847846

847+
func deleteAllFips() error {
848+
fips, err := getAllFips()
849+
if err != nil {
850+
return err
851+
}
852+
853+
for _, v := range strings.Split(fips,"\n") {
854+
if v == "" {
855+
continue
856+
}
857+
fip := strings.Trim(v, " ")
858+
releaseFipCmd := exec.Command(dockerBinary, flag_host, "fip", "release", fip)
859+
out, exitCode, err := runCommandWithOutput(releaseFipCmd)
860+
if exitCode != 0 && err == nil {
861+
err = fmt.Errorf("failed to release fip: %v\n", out)
862+
return err
863+
}
864+
}
865+
return nil
866+
}
867+
868+
func getAllFips() (string, error) {
869+
//load via pipe
870+
getFipsCmd := exec.Command(dockerBinary, flag_host, "fip", "ls")
871+
grepCmd := exec.Command("grep", "-v", "^Floating IP")
872+
873+
getFipOut, err := getFipsCmd.StdoutPipe()
874+
getFipsCmd.Start()
875+
876+
grepCmd.Stdin = getFipOut
877+
output, err := grepCmd.Output()
878+
879+
if err != nil {
880+
err = fmt.Errorf("failed to get a list of fips: %v\n", output)
881+
}
882+
return string(output), err
883+
}
884+
848885
//status only support : created, restarting, running, exited (https://github.com/getdvm/hyper-api-router/blob/master/pkg/apiserver/router/local/container.go#L204)
849886
/*
850887
func getPausedContainers() (string, error) {

integration-cli/hyper_cli_links_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ func (s *DockerSuite) TestCliLinksEnvs(c *check.C) {
192192
testRequires(c, DaemonIsLinux)
193193
printTestCaseName()
194194
defer printTestDuration(time.Now())
195+
pullImageIfNotExist("busybox")
195196
dockerCmd(c, "run", "-d", "-e", "e1=", "-e", "e2=v2", "-e", "e3=v3=v3", "--name=first", "busybox", "top")
196197
out, _ := dockerCmd(c, "run", "--name=second", "--link=first:first", "busybox", "env")
197198
/* FIXME

integration-cli/hyper_cli_logs_test.go

+17-3
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,11 @@ func (s *DockerSuite) TestCliLogsTimestamps(c *check.C) {
6969
testLen := 100
7070
pullImageIfNotExist("busybox")
7171
out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo =; done;", testLen))
72+
time.Sleep(5 * time.Second)
7273

7374
id := strings.TrimSpace(out)
7475
dockerCmd(c, "stop", id)
76+
time.Sleep(5 * time.Second)
7577

7678
out, _ = dockerCmd(c, "logs", "-t", id)
7779

@@ -99,9 +101,11 @@ func (s *DockerSuite) TestCliLogsSeparateStderr(c *check.C) {
99101
pullImageIfNotExist("busybox")
100102
msg := "stderr_log"
101103
out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("echo %s 1>&2", msg))
104+
time.Sleep(5 * time.Second)
102105

103106
id := strings.TrimSpace(out)
104107
dockerCmd(c, "stop", id)
108+
time.Sleep(5 * time.Second)
105109

106110
stdout, stderr, _ := dockerCmdWithStdoutStderr(c, "logs", id)
107111

@@ -120,9 +124,11 @@ func (s *DockerSuite) TestCliLogsStderrInStdout(c *check.C) {
120124
pullImageIfNotExist("busybox")
121125
msg := "stderr_log"
122126
out, _ := dockerCmd(c, "run", "-d", "-t", "busybox", "sh", "-c", fmt.Sprintf("echo %s 1>&2", msg))
127+
time.Sleep(5 * time.Second)
123128

124129
id := strings.TrimSpace(out)
125130
dockerCmd(c, "stop", id)
131+
time.Sleep(5 * time.Second)
126132

127133
stdout, stderr, _ := dockerCmdWithStdoutStderr(c, "logs", id)
128134
c.Assert(stderr, checker.Equals, "")
@@ -139,9 +145,11 @@ func (s *DockerSuite) TestCliLogsTail(c *check.C) {
139145
testLen := 100
140146
pullImageIfNotExist("busybox")
141147
out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo =; done;", testLen))
148+
time.Sleep(5 * time.Second)
142149

143150
id := strings.TrimSpace(out)
144151
dockerCmd(c, "stop", id)
152+
time.Sleep(5 * time.Second)
145153

146154
out, _ = dockerCmd(c, "logs", "--tail", "5", id)
147155

@@ -169,9 +177,11 @@ func (s *DockerSuite) TestCliLogsFollowStopped(c *check.C) {
169177
testRequires(c, DaemonIsLinux)
170178
pullImageIfNotExist("busybox")
171179
out, _ := dockerCmd(c, "run", "-d", "busybox", "echo", "hello")
180+
time.Sleep(5 * time.Second)
172181

173182
id := strings.TrimSpace(out)
174183
dockerCmd(c, "stop", id)
184+
time.Sleep(5 * time.Second)
175185

176186
logsCmd := exec.Command(dockerBinary, "logs", "-f", id)
177187
c.Assert(logsCmd.Start(), checker.IsNil)
@@ -198,7 +208,8 @@ func (s *DockerSuite) TestCliLogsSince(c *check.C) {
198208
pullImageIfNotExist("busybox")
199209
name := "testlogssince"
200210
dockerCmd(c, "run", "--name="+name, "-d", "busybox", "/bin/sh", "-c", "for i in $(seq 1 30); do sleep 2; echo log$i; done")
201-
time.Sleep(5*time.Second)
211+
time.Sleep(10*time.Second)
212+
202213
out, _ := dockerCmd(c, "logs", "-t", name)
203214

204215
log2Line := strings.Split(strings.Split(out, "\n")[1], " ")
@@ -238,17 +249,19 @@ func (s *DockerSuite) TestCliLogsSinceFutureFollow(c *check.C) {
238249
testRequires(c, DaemonIsLinux)
239250
pullImageIfNotExist("busybox")
240251
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", `for i in $(seq 1 50); do date +%s; sleep 1; done`)
252+
time.Sleep(5 * time.Second)
241253
id := strings.TrimSpace(out)
242254

243255
now := daemonTime(c).Unix()
244256
since := now - 5
257+
245258
out, _ = dockerCmd(c, "logs", "-f", fmt.Sprintf("--since=%v", since), id)
246259
lines := strings.Split(strings.TrimSpace(out), "\n")
247260
c.Assert(lines, checker.Not(checker.HasLen), 0)
248261
for _, v := range lines {
249262
ts, err := strconv.ParseInt(v, 10, 64)
250263
c.Assert(err, checker.IsNil, check.Commentf("cannot parse timestamp output from log: '%v'\nout=%s", v, out))
251-
c.Assert(ts >= since, checker.Equals, true, check.Commentf("earlier log found. since=%v logdate=%v", since, ts))
264+
c.Assert( (ts+1) >= since, checker.Equals, true, check.Commentf("earlier log found. since=%v logdate=%v", since, ts))
252265
}
253266
}
254267

@@ -260,13 +273,14 @@ func (s *DockerSuite) TestCliLogsFollowSlowStdoutConsumer(c *check.C) {
260273
testRequires(c, DaemonIsLinux)
261274
pullImageIfNotExist("busybox")
262275
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", `usleep 600000;yes X | head -c 200000`)
263-
276+
time.Sleep(5 * time.Second)
264277
id := strings.TrimSpace(out)
265278

266279
stopSlowRead := make(chan bool)
267280

268281
go func() {
269282
exec.Command(dockerBinary, "stop", id).Run()
283+
time.Sleep(5 * time.Second)
270284
stopSlowRead <- true
271285
}()
272286

integration-cli/hyper_cli_ps_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,6 @@ func (s *DockerSuite) TestCliPsListContainersBase(c *check.C) {
117117
}
118118

119119
func assertContainerList(out string, expected []string) bool {
120-
printTestCaseName()
121-
defer printTestDuration(time.Now())
122120

123121
lines := strings.Split(strings.Trim(out, "\n "), "\n")
124122
if len(lines)-1 != len(expected) {

integration-cli/hyper_cli_restart_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@ func (s *DockerSuite) TestCliRestartStoppedContainer(c *check.C) {
1616
testRequires(c, DaemonIsLinux)
1717
pullImageIfNotExist("busybox")
1818
out, _ := dockerCmd(c, "run", "-d", "busybox", "echo", "foobar")
19-
19+
time.Sleep(5 * time.Second)
2020
cleanedContainerID := strings.TrimSpace(out)
2121
c.Assert(waitExited(cleanedContainerID, 30*time.Second), checker.IsNil)
2222

2323
out, _ = dockerCmd(c, "logs", cleanedContainerID)
2424
c.Assert(out, checker.Equals, "foobar\n")
2525

2626
dockerCmd(c, "restart", cleanedContainerID)
27+
time.Sleep(5 * time.Second)
2728

2829
out, _ = dockerCmd(c, "logs", cleanedContainerID)
2930
c.Assert(out, checker.Equals, "foobar\n")
@@ -35,7 +36,7 @@ func (s *DockerSuite) TestCliRestartRunningContainer(c *check.C) {
3536
testRequires(c, DaemonIsLinux)
3637
pullImageIfNotExist("busybox")
3738
out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", "echo foobar && sleep 30 && echo 'should not print this'")
38-
39+
time.Sleep(5 * time.Second)
3940
cleanedContainerID := strings.TrimSpace(out)
4041

4142
c.Assert(waitRun(cleanedContainerID), checker.IsNil)
@@ -44,6 +45,7 @@ func (s *DockerSuite) TestCliRestartRunningContainer(c *check.C) {
4445
c.Assert(out, checker.Equals, "foobar\n")
4546

4647
dockerCmd(c, "restart", "-t", "1", cleanedContainerID)
48+
time.Sleep(5 * time.Second)
4749

4850
out, _ = dockerCmd(c, "logs", cleanedContainerID)
4951

integration-cli/hyper_cli_run_test.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,14 @@ func (s *DockerSuite) TestCliRunDetachedContainerIDPrinting(c *check.C) {
127127
defer printTestDuration(time.Now())
128128
pullImageIfNotExist("busybox")
129129
out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
130+
time.Sleep(5 * time.Second)
130131

131132
out = strings.TrimSpace(out)
132133
dockerCmd(c, "stop", out)
134+
time.Sleep(5 * time.Second)
133135

134136
rmOut, _ := dockerCmd(c, "rm", out)
137+
time.Sleep(5 * time.Second)
135138

136139
rmOut = strings.TrimSpace(rmOut)
137140
if rmOut != out {
@@ -995,6 +998,7 @@ func (s *DockerSuite) TestCliRunUnknownCommand(c *check.C) {
995998

996999
cID := strings.TrimSpace(out)
9971000
_, _, err := dockerCmdWithError("start", cID)
1001+
time.Sleep(5 * time.Second)
9981002

9991003
// Windows and Linux are different here by architectural design. Linux will
10001004
// fail to start the container, so an error is expected. Windows will
@@ -1106,23 +1110,22 @@ func (s *DockerSuite) TestCliRunRestartMaxRetries(c *check.C) {
11061110
printTestCaseName()
11071111
defer printTestDuration(time.Now())
11081112
pullImageIfNotExist("busybox")
1109-
out, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:3", "busybox", "sh", "-c", "sleep 15; false")
1110-
timeout := 60 * time.Second
1111-
if daemonPlatform == "windows" {
1112-
timeout = 45 * time.Second
1113-
}
1113+
1114+
RETRY_COUNT := "3"
1115+
out, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:" + RETRY_COUNT, "busybox", "sh", "-c", "sleep 35; false")
1116+
timeout := 120 * time.Second
11141117

11151118
time.Sleep(timeout)
11161119
id := strings.TrimSpace(string(out))
11171120

11181121
count := inspectField(c, id, "RestartCount")
1119-
if count != "3" {
1120-
c.Fatalf("Container was restarted %s times, expected %d", count, 3)
1122+
if count != RETRY_COUNT {
1123+
c.Fatalf("Container was restarted %s times, expected %d", count, RETRY_COUNT)
11211124
}
11221125

11231126
MaximumRetryCount := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
1124-
if MaximumRetryCount != "3" {
1125-
c.Fatalf("Container Maximum Retry Count is %s, expected %s", MaximumRetryCount, "3")
1127+
if MaximumRetryCount != RETRY_COUNT {
1128+
c.Fatalf("Container Maximum Retry Count is %s, expected %s", MaximumRetryCount, RETRY_COUNT)
11261129
}
11271130
}
11281131

0 commit comments

Comments
 (0)