-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v2: add test case for Manager.EventChan() behavior
This test case demonstrates that now the oom monitor goroutine terminates when a cgroup is removed. Systemd is responsible for cgroup removal so a "cat" process is spawned in a scope, with stdin attached to a pipe. When the pipe is closed the process terminates, systemd notices and removes the cgroup, which results in the test case finishing. Signed-off-by: Jeremi Piotrowski <[email protected]>
- Loading branch information
Showing
3 changed files
with
101 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
Copyright The containerd Authors. | ||
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 v2 | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
"testing" | ||
"time" | ||
|
||
"go.uber.org/goleak" | ||
) | ||
|
||
func TestEventChanCleanupOnCgroupRemoval(t *testing.T) { | ||
checkCgroupMode(t) | ||
|
||
cmd := exec.Command("cat") | ||
stdin, err := cmd.StdinPipe() | ||
if err != nil { | ||
t.Fatalf("Failed to create cat process: %v", err) | ||
} | ||
if err := cmd.Start(); err != nil { | ||
t.Fatalf("Failed to start cat process: %v", err) | ||
} | ||
proc := cmd.Process | ||
if proc == nil { | ||
t.Fatal("Process is nil") | ||
} | ||
|
||
group := fmt.Sprintf("testing-watcher-%d.scope", proc.Pid) | ||
c, err := NewSystemd("", group, proc.Pid, &Resources{}) | ||
if err != nil { | ||
t.Fatalf("Failed to init new cgroup manager: %v", err) | ||
} | ||
|
||
evCh, errCh := c.EventChan() | ||
|
||
// give event goroutine a chance to start | ||
time.Sleep(500 * time.Millisecond) | ||
|
||
if err := stdin.Close(); err != nil { | ||
t.Fatalf("Failed closing stdin: %v", err) | ||
} | ||
if err := cmd.Wait(); err != nil { | ||
t.Fatalf("Failed waiting for cmd: %v", err) | ||
} | ||
|
||
done := false | ||
for !done { | ||
select { | ||
case <-evCh: | ||
case err := <-errCh: | ||
if err != nil { | ||
t.Fatalf("Unexpected error on error channel: %v", err) | ||
} | ||
done = true | ||
case <-time.After(5 * time.Second): | ||
t.Fatal("Timed out") | ||
} | ||
} | ||
goleak.VerifyNone(t) | ||
} |