Skip to content
This repository was archived by the owner on Aug 29, 2018. It is now read-only.

Commit 3ce34f8

Browse files
Remove use of docker.GetContainer
1 parent 449062c commit 3ce34f8

File tree

4 files changed

+30
-110
lines changed

4 files changed

+30
-110
lines changed

cleanup/cleanup_failures.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package cleanup
33
import (
44
"github.com/openshift/geard/docker"
55
"os"
6-
"time"
76
"strings"
7+
"time"
88
)
99

1010
type FailureCleanup struct {
@@ -42,17 +42,17 @@ func (r *FailureCleanup) Clean(ctx *CleanerContext) {
4242

4343
retentionAge, err := time.ParseDuration(r.retentionAge)
4444
for _, cinfo := range gears {
45-
container, err := client.GetContainer(cinfo.ID, false)
45+
container, err := client.InspectContainer(cinfo.ID)
4646
if err != nil {
4747
ctx.LogError.Printf("Unable to retrieve container information for %s: %s", container.Name, err.Error())
4848
continue
4949
}
5050

5151
// Happy container or not...
5252
if 0 == container.State.ExitCode ||
53-
container.State.Running ||
54-
strings.HasSuffix(container.Name, "-data") ||
55-
time.Since(container.State.FinishedAt) < retentionAge {
53+
container.State.Running ||
54+
strings.HasSuffix(container.Name, "-data") ||
55+
time.Since(container.State.FinishedAt) < retentionAge {
5656
continue
5757
}
5858

@@ -69,4 +69,3 @@ func (r *FailureCleanup) Clean(ctx *CleanerContext) {
6969
}
7070
}
7171
}
72-

cmd/switchns/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func runCommandInContainer(name string, command []string, environment []string)
159159
os.Exit(3)
160160
}
161161

162-
container, err := client.GetContainer(name, false)
162+
container, err := client.InspectContainer(name)
163163
if err != nil {
164164
fmt.Printf("Unable to locate container named %v\n", name)
165165
os.Exit(3)

docker/docker.go

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"path/filepath"
1111
"strconv"
1212
"strings"
13-
"time"
1413
)
1514

1615
type containerLookupResult struct {
@@ -34,27 +33,6 @@ func (d *DockerClient) ForceCleanContainer(ID string) error {
3433
return d.client.RemoveContainer(docker.RemoveContainerOptions{ID, true, true})
3534
}
3635

37-
func lookupContainer(containerName string, client *docker.Client, waitForContainer bool) containerLookupResult {
38-
timeout := 0
39-
if waitForContainer {
40-
timeout = 60
41-
}
42-
43-
for i := 0; i <= timeout; i++ {
44-
if container, err := client.InspectContainer(containerName); err != nil {
45-
if !strings.HasPrefix(err.Error(), "No such container") {
46-
return containerLookupResult{nil, err}
47-
}
48-
if timeout > 0 {
49-
time.Sleep(time.Second)
50-
}
51-
} else {
52-
return containerLookupResult{container, nil}
53-
}
54-
}
55-
return containerLookupResult{nil, fmt.Errorf("container not active")}
56-
}
57-
5836
func GetConnection(dockerSocket string) (*DockerClient, error) {
5937
var (
6038
client *docker.Client
@@ -87,23 +65,6 @@ func (d *DockerClient) InspectContainer(containerName string) (*docker.Container
8765
return c, err
8866
}
8967

90-
func (d *DockerClient) GetContainer(containerName string, waitForContainer bool) (*docker.Container, error) {
91-
timeoutChannel := make(chan containerLookupResult, 1)
92-
var container *docker.Container
93-
go func() { timeoutChannel <- lookupContainer(containerName, d.client, waitForContainer) }()
94-
select {
95-
case cInfo := <-timeoutChannel:
96-
if cInfo.Error != nil {
97-
return nil, cInfo.Error
98-
}
99-
container = cInfo.Container
100-
case <-time.After(time.Minute):
101-
return nil, fmt.Errorf("timeout waiting for container")
102-
}
103-
104-
return container, nil
105-
}
106-
10768
func (d *DockerClient) GetImage(imageName string) (*docker.Image, error) {
10869
if img, err := d.client.InspectImage(imageName); err != nil {
10970
if err == docker.ErrNoSuchImage {
@@ -121,7 +82,7 @@ func (d *DockerClient) GetImage(imageName string) (*docker.Image, error) {
12182
func (d *DockerClient) GetContainerIPs(ids []string) (map[string]string, error) {
12283
ips := make(map[string]string)
12384
for _, id := range ids {
124-
if cInfo, err := d.GetContainer(id, false); err == nil {
85+
if cInfo, err := d.InspectContainer(id); err == nil {
12586
ips[cInfo.NetworkSettings.IPAddress] = id
12687
}
12788
}

tests/integration_test.go

Lines changed: 23 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@ import (
2020
)
2121

2222
const (
23-
CONTAINER_STATE_CHANGE_TIMEOUT = time.Second * 15
24-
DOCKER_STATE_CHANGE_TIMEOUT = time.Second * 5
25-
SYSTEMD_ACTION_DELAY = time.Second * 1
26-
CONTAINER_CHECK_INTERVAL = time.Second / 20
27-
TestImage = "pmorie/sti-html-app"
28-
EnvImage = "ccoleman/envtest"
23+
TimeoutContainerStateChange = time.Second * 15
24+
TimeoutDockerStateChange = time.Second * 5
25+
TimeoutDockerWait = time.Second * 2
26+
27+
IntervalContainerCheck = time.Second / 20
28+
IntervalHttpCheck = time.Second / 10
29+
30+
TestImage = "pmorie/sti-html-app"
31+
EnvImage = "ccoleman/envtest"
2932
)
3033

3134
//Hookup gocheck with go test
@@ -72,7 +75,10 @@ func (s *IntegrationTestSuite) assertFileAbsent(c *chk.C, path string) {
7275
}
7376

7477
func (s *IntegrationTestSuite) getContainerPid(id containers.Identifier) int {
75-
container, _ := s.dockerClient.GetContainer(id.ContainerFor(), true)
78+
container, err := s.dockerClient.InspectContainer(id.ContainerFor())
79+
if err != nil {
80+
return 0
81+
}
7682
return container.State.Pid
7783
}
7884

@@ -164,7 +170,7 @@ func (s *IntegrationTestSuite) assertContainerStarts(c *chk.C, id containers.Ide
164170
return false
165171
}
166172

167-
if !until(CONTAINER_STATE_CHANGE_TIMEOUT, time.Second/20, isRunning) {
173+
if !until(TimeoutContainerStateChange, time.Second/20, isRunning) {
168174
c.Errorf("Timeout during start of %s, never got to 'active' state", id)
169175
c.FailNow()
170176
}
@@ -182,7 +188,7 @@ func (s *IntegrationTestSuite) assertContainerStarts(c *chk.C, id containers.Ide
182188
return done
183189
}
184190

185-
if !until(time.Second*2, time.Second/10, isContainerUp) {
191+
if !until(TimeoutDockerWait, IntervalHttpCheck, isContainerUp) {
186192
if !failed {
187193
c.Errorf("Docker never reported the container running %s", id)
188194
}
@@ -215,12 +221,12 @@ func (s *IntegrationTestSuite) assertContainerStops(c *chk.C, id containers.Iden
215221
return false
216222
}
217223

218-
if !until(CONTAINER_STATE_CHANGE_TIMEOUT, CONTAINER_CHECK_INTERVAL, isStopped) {
224+
if !until(TimeoutContainerStateChange, IntervalContainerCheck, isStopped) {
219225
c.Errorf("Timeout during start of %s, never got to 'inactive' state", id)
220226
c.FailNow()
221227
}
222228

223-
_, err := s.dockerClient.GetContainer(id.ContainerFor(), false)
229+
_, err := s.dockerClient.InspectContainer(id.ContainerFor())
224230
if err == nil {
225231
c.Errorf("Container %s is still active in docker, should be stopped and removed", id.ContainerFor())
226232
c.FailNow()
@@ -241,7 +247,7 @@ func (s *IntegrationTestSuite) assertContainerRestarts(c *chk.C, id containers.I
241247
return false
242248
}
243249

244-
if !until(CONTAINER_STATE_CHANGE_TIMEOUT, CONTAINER_CHECK_INTERVAL, isStarted) {
250+
if !until(TimeoutContainerStateChange, IntervalContainerCheck, isStarted) {
245251
active, sub := s.unitState(id)
246252
c.Errorf("Timeout during restart of %s, never got back to 'active' state (%s/%s)", id, active, sub)
247253
c.FailNow()
@@ -260,7 +266,7 @@ func (s *IntegrationTestSuite) assertContainerRestarts(c *chk.C, id containers.I
260266
return done
261267
}
262268

263-
if !until(time.Second*2, time.Second/10, isContainerUp) {
269+
if !until(TimeoutDockerWait, IntervalHttpCheck, isContainerUp) {
264270
if !failed {
265271
c.Errorf("Docker never reported the container running %s", id)
266272
}
@@ -291,7 +297,7 @@ func (s *IntegrationTestSuite) SetUpSuite(c *chk.C) {
291297
containers, err := s.dockerClient.ListContainers()
292298
c.Assert(err, chk.IsNil)
293299
for _, cinfo := range containers {
294-
container, err := s.dockerClient.GetContainer(cinfo.ID, false)
300+
container, err := s.dockerClient.InspectContainer(cinfo.ID)
295301
c.Assert(err, chk.IsNil)
296302
if strings.HasPrefix(container.Name, "IntTest") {
297303
s.dockerClient.ForceCleanContainer(cinfo.ID)
@@ -366,7 +372,6 @@ func (s *IntegrationTestSuite) TestSimpleInstallWithEnv(c *chk.C) {
366372
c.Log(string(data))
367373
c.Assert(err, chk.IsNil)
368374
s.assertContainerStarts(c, id)
369-
//time.Sleep(time.Second * 5) // startup time is indeterminate unfortunately because gear init --post continues to run
370375

371376
cmd = exec.Command("/usr/bin/gear", "status", hostContainerId)
372377
data, err = cmd.CombinedOutput()
@@ -409,7 +414,7 @@ func (s *IntegrationTestSuite) TestIsolateInstallAndStartImage(c *chk.C) {
409414
}
410415
return false
411416
}
412-
if !until(time.Second*15, time.Second/10, httpAlive) {
417+
if !until(TimeoutContainerStateChange, IntervalHttpCheck, httpAlive) {
413418
c.Errorf("Unable to retrieve a 200 status code from port %d", ports[0].External)
414419
c.FailNow()
415420
}
@@ -469,7 +474,7 @@ func (s *IntegrationTestSuite) TestStartStopContainer(c *chk.C) {
469474
}
470475
return false
471476
}
472-
if !until(time.Second*15, time.Second/10, httpAlive) {
477+
if !until(TimeoutContainerStateChange, IntervalHttpCheck, httpAlive) {
473478
c.Errorf("Unable to retrieve a 200 status code from port %d", ports[0].External)
474479
c.FailNow()
475480
}
@@ -584,7 +589,7 @@ func (s *IntegrationTestSuite) TestLongContainerName(c *chk.C) {
584589
}
585590
return false
586591
}
587-
if !until(time.Second*15, time.Second/10, httpAlive) {
592+
if !until(TimeoutContainerStateChange, IntervalHttpCheck, httpAlive) {
588593
c.Errorf("Unable to retrieve a 200 status code from port %d", ports[0].External)
589594
c.FailNow()
590595
}
@@ -619,51 +624,6 @@ func (s *IntegrationTestSuite) TestContainerNetLinks(c *chk.C) {
619624
c.Assert(strings.Contains(string(data), "tcp dpt:tproxy to:74.125.239.114"), chk.Equals, true)
620625
}
621626

622-
// func (s *IntegrationTestSuite) TestSocketActivatedInstallAndStartImage(c *chk.C) {
623-
// id, err := containers.NewIdentifier("IntTest007")
624-
// c.Assert(err, chk.IsNil)
625-
// s.containerIds = append(s.containerIds, id)
626-
//
627-
// hostContainerId := fmt.Sprintf("%v/%v", s.daemonURI, id)
628-
//
629-
// cmd := exec.Command("/usr/bin/gear", "install", "pmorie/sti-html-app", hostContainerId, "--start", "--ports=8080:4005", "--socket-activated")
630-
// data, err := cmd.CombinedOutput()
631-
// c.Log(string(data))
632-
// c.Assert(err, chk.IsNil)
633-
//
634-
// s.assertFilePresent(c, id.UnitPathFor(), 0664, true)
635-
// paths, err := filepath.Glob(id.VersionedUnitPathFor("*"))
636-
// c.Assert(err, chk.IsNil)
637-
// for _, p := range paths {
638-
// s.assertFilePresent(c, p, 0664, true)
639-
// }
640-
//
641-
// ports, err := containers.GetExistingPorts(id)
642-
// c.Assert(err, chk.IsNil)
643-
// c.Assert(len(ports), chk.Equals, 1)
644-
//
645-
// t := time.NewTicker(time.Second/10)
646-
// defer t.Stop()
647-
// for true {
648-
// select {
649-
// case <-t.C:
650-
// resp, err := http.Get(fmt.Sprintf("http://0.0.0.0:%v", ports[0].External))
651-
// if err == nil {
652-
// c.Logf("attempting http .. response code %v", resp.StatusCode)
653-
// if resp.StatusCode == 200 {
654-
// break
655-
// }
656-
// }else{
657-
// c.Logf("attempting http .. error %v", err)
658-
// }
659-
// case <-time.After(time.Second * 15):
660-
// c.Fail()
661-
// }
662-
// }
663-
// s.assertFilePresent(c, filepath.Join(id.RunPathFor(), "container-init.sh"), 0700, false)
664-
// s.assertContainerState(c, id, CONTAINER_STARTED)
665-
// }
666-
667627
func (s *IntegrationTestSuite) TearDownSuite(c *chk.C) {
668628
for _, id := range s.containerIds {
669629
hostContainerId := fmt.Sprintf("%v/%v", s.daemonURI, id)

0 commit comments

Comments
 (0)