|
9 | 9 |
|
10 | 10 | "github.com/opencontainers/runc/libcontainer/cgroups" |
11 | 11 | "github.com/opencontainers/runc/libcontainer/configs" |
| 12 | + "github.com/opencontainers/runc/libcontainer/system" |
12 | 13 | ) |
13 | 14 |
|
14 | 15 | type mockCgroupManager struct { |
@@ -216,3 +217,65 @@ func TestGetContainerState(t *testing.T) { |
216 | 217 | } |
217 | 218 | } |
218 | 219 | } |
| 220 | + |
| 221 | +func TestGetContainerStateAfterUpdate(t *testing.T) { |
| 222 | + var ( |
| 223 | + pid = os.Getpid() |
| 224 | + ) |
| 225 | + stat, err := system.Stat(pid) |
| 226 | + if err != nil { |
| 227 | + t.Fatal(err) |
| 228 | + } |
| 229 | + container := &linuxContainer{ |
| 230 | + id: "myid", |
| 231 | + config: &configs.Config{ |
| 232 | + Namespaces: []configs.Namespace{ |
| 233 | + {Type: configs.NEWPID}, |
| 234 | + {Type: configs.NEWNS}, |
| 235 | + {Type: configs.NEWNET}, |
| 236 | + {Type: configs.NEWUTS}, |
| 237 | + {Type: configs.NEWIPC}, |
| 238 | + }, |
| 239 | + Cgroups: &configs.Cgroup{ |
| 240 | + Resources: &configs.Resources{ |
| 241 | + Memory: 1024, |
| 242 | + }, |
| 243 | + }, |
| 244 | + }, |
| 245 | + initProcess: &mockProcess{ |
| 246 | + _pid: pid, |
| 247 | + started: stat.StartTime, |
| 248 | + }, |
| 249 | + cgroupManager: &mockCgroupManager{}, |
| 250 | + } |
| 251 | + container.state = &createdState{c: container} |
| 252 | + state, err := container.State() |
| 253 | + if err != nil { |
| 254 | + t.Fatal(err) |
| 255 | + } |
| 256 | + if state.InitProcessPid != pid { |
| 257 | + t.Fatalf("expected pid %d but received %d", pid, state.InitProcessPid) |
| 258 | + } |
| 259 | + if state.InitProcessStartTime != stat.StartTime { |
| 260 | + t.Fatalf("expected process start time %d but received %d", stat.StartTime, state.InitProcessStartTime) |
| 261 | + } |
| 262 | + if state.Config.Cgroups.Resources.Memory != 1024 { |
| 263 | + t.Fatalf("expected Memory to be 1024 but received %q", state.Config.Cgroups.Memory) |
| 264 | + } |
| 265 | + |
| 266 | + // Set initProcessStartTime so we fake to be running |
| 267 | + container.initProcessStartTime = state.InitProcessStartTime |
| 268 | + container.state = &runningState{c: container} |
| 269 | + newConfig := container.Config() |
| 270 | + newConfig.Cgroups.Resources.Memory = 2048 |
| 271 | + if err := container.Set(newConfig); err != nil { |
| 272 | + t.Fatal(err) |
| 273 | + } |
| 274 | + state, err = container.State() |
| 275 | + if err != nil { |
| 276 | + t.Fatal(err) |
| 277 | + } |
| 278 | + if state.Config.Cgroups.Resources.Memory != 2048 { |
| 279 | + t.Fatalf("expected Memory to be 2048 but received %q", state.Config.Cgroups.Memory) |
| 280 | + } |
| 281 | +} |
0 commit comments