github.com/demonoid81/containerd@v1.3.4/sys/oom_unix_test.go (about)

     1  // +build !windows
     2  
     3  /*
     4     Copyright The containerd Authors.
     5  
     6     Licensed under the Apache License, Version 2.0 (the "License");
     7     you may not use this file except in compliance with the License.
     8     You may obtain a copy of the License at
     9  
    10         http://www.apache.org/licenses/LICENSE-2.0
    11  
    12     Unless required by applicable law or agreed to in writing, software
    13     distributed under the License is distributed on an "AS IS" BASIS,
    14     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15     See the License for the specific language governing permissions and
    16     limitations under the License.
    17  */
    18  
    19  package sys
    20  
    21  import (
    22  	"errors"
    23  	"os"
    24  	"os/exec"
    25  	"testing"
    26  	"time"
    27  
    28  	"gotest.tools/assert"
    29  	is "gotest.tools/assert/cmp"
    30  )
    31  
    32  func TestSetPositiveOomScoreAdjustment(t *testing.T) {
    33  	adjustment, err := adjustOom(123)
    34  	if err != nil {
    35  		t.Error(err)
    36  		return
    37  	}
    38  	assert.Check(t, is.Equal(adjustment, 123))
    39  }
    40  
    41  func TestSetNegativeOomScoreAdjustmentWhenPrivileged(t *testing.T) {
    42  	if RunningUnprivileged() {
    43  		t.Skip("Needs to be run as root")
    44  		return
    45  	}
    46  
    47  	adjustment, err := adjustOom(-123)
    48  	if err != nil {
    49  		t.Error(err)
    50  		return
    51  	}
    52  	assert.Check(t, is.Equal(adjustment, -123))
    53  }
    54  
    55  func TestSetNegativeOomScoreAdjustmentWhenUnprivilegedHasNoEffect(t *testing.T) {
    56  	if RunningPrivileged() {
    57  		t.Skip("Needs to be run as non-root")
    58  		return
    59  	}
    60  
    61  	adjustment, err := adjustOom(-123)
    62  	if err != nil {
    63  		t.Error(err)
    64  		return
    65  	}
    66  	assert.Check(t, is.Equal(adjustment, 0))
    67  }
    68  
    69  func adjustOom(adjustment int) (int, error) {
    70  	cmd := exec.Command("sleep", "100")
    71  	if err := cmd.Start(); err != nil {
    72  		return 0, err
    73  	}
    74  
    75  	pid, err := waitForPid(cmd.Process)
    76  	if err != nil {
    77  		return 0, err
    78  	}
    79  
    80  	if err := SetOOMScore(pid, adjustment); err != nil {
    81  		return 0, err
    82  	}
    83  
    84  	return GetOOMScoreAdj(pid)
    85  }
    86  
    87  func waitForPid(process *os.Process) (int, error) {
    88  	c := make(chan int)
    89  	go func() {
    90  		for {
    91  			pid := process.Pid
    92  			if pid != 0 {
    93  				c <- pid
    94  			}
    95  		}
    96  	}()
    97  
    98  	select {
    99  	case pid := <-c:
   100  		return pid, nil
   101  	case <-time.After(10 * time.Second):
   102  		return 0, errors.New("process did not start in 10 seconds")
   103  	}
   104  }