-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer_pid_test.go
180 lines (152 loc) · 5.35 KB
/
container_pid_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright 2024 Harald Albrecht.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package morbyd
import (
"context"
"errors"
"time"
"github.com/docker/docker/api/types"
"github.com/thediveo/morbyd/run"
"github.com/thediveo/morbyd/session"
mock "go.uber.org/mock/gomock"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gleak"
. "github.com/thediveo/success"
)
var _ = Describe("getting container PIDs", Ordered, func() {
BeforeEach(func() {
goodgos := Goroutines()
Eventually(Goroutines).Within(2 * time.Second).ProbeEvery(100 * time.Second).
ShouldNot(HaveLeaked(goodgos))
})
Context("mocked", func() {
It("retries until PID becomes available", func(ctx context.Context) {
ctrl := mock.NewController(GinkgoT())
sess := Successful(NewSession(ctx,
WithMockController(ctrl, "ContainerInspect")))
DeferCleanup(func(ctx context.Context) {
sess.Close(ctx)
})
rec := sess.Client().(*MockClient).EXPECT()
rec.ContainerInspect(Any, Any).Return(types.ContainerJSON{
ContainerJSONBase: &types.ContainerJSONBase{},
}, nil)
rec.ContainerInspect(Any, Any).Return(types.ContainerJSON{
ContainerJSONBase: &types.ContainerJSONBase{
State: &types.ContainerState{
Pid: 42,
},
},
}, nil)
cntr := &Container{Session: sess, ID: "bad1dea"}
Expect(cntr.PID(ctx)).To(Equal(42))
})
It("waits for a restart", func(ctx context.Context) {
ctrl := mock.NewController(GinkgoT())
sess := Successful(NewSession(ctx,
WithMockController(ctrl, "ContainerInspect")))
DeferCleanup(func(ctx context.Context) {
sess.Close(ctx)
})
rec := sess.Client().(*MockClient).EXPECT()
rec.ContainerInspect(Any, Any).Return(types.ContainerJSON{
ContainerJSONBase: &types.ContainerJSONBase{
State: &types.ContainerState{
Dead: true,
Restarting: true,
},
},
}, nil)
rec.ContainerInspect(Any, Any).Return(types.ContainerJSON{
ContainerJSONBase: &types.ContainerJSONBase{
State: &types.ContainerState{
Pid: 42,
},
},
}, nil)
cntr := &Container{Session: sess, ID: "bad1dea"}
Expect(cntr.PID(ctx)).To(Equal(42))
})
It("gives up when there's no chance of a restart", func(ctx context.Context) {
ctrl := mock.NewController(GinkgoT())
sess := Successful(NewSession(ctx,
WithMockController(ctrl, "ContainerInspect")))
DeferCleanup(func(ctx context.Context) {
sess.Close(ctx)
})
rec := sess.Client().(*MockClient).EXPECT()
rec.ContainerInspect(Any, Any).Return(types.ContainerJSON{
ContainerJSONBase: &types.ContainerJSONBase{},
}, nil)
rec.ContainerInspect(Any, Any).Return(types.ContainerJSON{
ContainerJSONBase: &types.ContainerJSONBase{
State: &types.ContainerState{
OOMKilled: true,
},
},
}, nil)
cntr := &Container{Session: sess, ID: "bad1dea"}
Expect(cntr.PID(ctx)).Error().To(HaveOccurred())
})
It("reports an error when container cannot be inspected", func(ctx context.Context) {
ctrl := mock.NewController(GinkgoT())
sess := Successful(NewSession(ctx,
WithMockController(ctrl, "ContainerInspect")))
DeferCleanup(func(ctx context.Context) {
sess.Close(ctx)
})
rec := sess.Client().(*MockClient).EXPECT()
rec.ContainerInspect(Any, Any).Return(types.ContainerJSON{}, errors.New("error IJK305I"))
cntr := &Container{Session: sess, ID: "bad1dea"}
Expect(cntr.PID(ctx)).Error().To(HaveOccurred())
})
It("returns when its nap gets cancelled", func(ctx context.Context) {
ctrl := mock.NewController(GinkgoT())
sess := Successful(NewSession(ctx,
WithMockController(ctrl, "ContainerInspect")))
DeferCleanup(func(ctx context.Context) {
sess.Close(ctx)
})
rec := sess.Client().(*MockClient).EXPECT()
// Ignore the cancelled context so we can get to the short nap attack.
rec.ContainerInspect(Any, Any).Return(types.ContainerJSON{
ContainerJSONBase: &types.ContainerJSONBase{},
}, nil)
ctx, cancel := context.WithCancel(ctx)
cancel()
cntr := &Container{Session: sess, ID: "bad1dea"}
Expect(cntr.PID(ctx)).Error().To(HaveOccurred())
})
})
Context("handling a failed container", func() {
It("doesn't wait endless for PID of failed container", func(ctx context.Context) {
sess := Successful(NewSession(ctx, session.WithAutoCleaning("test.morbid=container.pid")))
DeferCleanup(func(ctx context.Context) { sess.Close(ctx) })
By("creating a crashed container")
cntr := Successful(sess.Run(ctx,
"busybox",
run.WithCommand("/bin/sh", "-c", "this feels wrong"),
run.WithAutoRemove(),
run.WithCombinedOutput(GinkgoWriter)))
By("(not) getting PID of crashed container")
Eventually(func(ctx context.Context) error {
_, err := cntr.PID(ctx)
return err
}).WithContext(ctx).
Within(2 * time.Second).ProbeEvery(100 * time.Millisecond).
ShouldNot(Succeed())
})
})
})