1
2
3
4
19
20 package oom
21
22 import (
23 "os"
24
25 "github.com/opencontainers/runc/libcontainer/cgroups"
26 "testing"
27
28 "github.com/stretchr/testify/assert"
29 )
30
31
32
33
34 func sequenceToPidLister(pidListSequence [][]int) func(string) ([]int, error) {
35 var numCalls int
36 return func(cgroupName string) ([]int, error) {
37 numCalls++
38 if len(pidListSequence) == 0 {
39 return []int{}, nil
40 } else if numCalls > len(pidListSequence) {
41 return pidListSequence[len(pidListSequence)-1], nil
42 }
43 return pidListSequence[numCalls-1], nil
44 }
45 }
46
47
48
49 func applyOOMScoreAdjContainerTester(pidListSequence [][]int, maxTries int, appliedPids []int, expectedError bool, t *testing.T) {
50 pidOOMs := make(map[int]bool)
51
52
53 oomAdjuster := NewOOMAdjuster()
54 oomAdjuster.ApplyOOMScoreAdj = func(pid int, oomScoreAdj int) error {
55 pidOOMs[pid] = true
56 return nil
57 }
58 oomAdjuster.pidLister = sequenceToPidLister(pidListSequence)
59 err := oomAdjuster.ApplyOOMScoreAdjContainer("", 100, maxTries)
60
61
62 if expectedError && err == nil {
63 t.Errorf("Expected error %+v when running ApplyOOMScoreAdjContainer but got no error", expectedError)
64 return
65 } else if !expectedError && err != nil {
66 t.Errorf("Expected no error but got error %+v when running ApplyOOMScoreAdjContainer", err)
67 return
68 } else if err != nil {
69 return
70 }
71
72 if len(appliedPids) != len(pidOOMs) {
73 t.Errorf("Applied OOM scores to incorrect number of processes - %+v vs %v", appliedPids, pidOOMs)
74 return
75 }
76 for _, pid := range appliedPids {
77 if !pidOOMs[pid] {
78 t.Errorf("Failed to apply OOM scores to process %d", pid)
79 }
80 }
81 }
82
83 func TestOOMScoreAdjContainer(t *testing.T) {
84 pidListSequenceEmpty := [][]int{}
85 applyOOMScoreAdjContainerTester(pidListSequenceEmpty, 3, nil, true, t)
86
87 pidListSequence1 := [][]int{
88 {1, 2},
89 }
90 applyOOMScoreAdjContainerTester(pidListSequence1, 1, []int{1, 2}, false, t)
91
92 pidListSequenceLag := [][]int{
93 {},
94 {},
95 {},
96 {1, 2, 4},
97 }
98 for i := 1; i < 4; i++ {
99 applyOOMScoreAdjContainerTester(pidListSequenceLag, i, nil, true, t)
100 }
101 applyOOMScoreAdjContainerTester(pidListSequenceLag, 4, []int{1, 2, 4}, false, t)
102 }
103
104 func TestPidListerFailure(t *testing.T) {
105 _, err := getPids("/does/not/exist")
106 assert.True(t, cgroups.IsNotFound(err) || os.IsNotExist(err), "expected getPids to return not exists error. Got %v", err)
107 }
108
View as plain text