|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +import unittest |
| 19 | +from argparse import ArgumentTypeError |
| 20 | + |
| 21 | +from rocker.ulimit_extension import Ulimit |
| 22 | + |
| 23 | + |
| 24 | +class UlimitTest(unittest.TestCase): |
| 25 | + """Unit tests for the Ulimit class.""" |
| 26 | + |
| 27 | + def setUp(self): |
| 28 | + self._instance = Ulimit() |
| 29 | + |
| 30 | + def _is_arg_translation_ok(self, mock_cliargs, expected): |
| 31 | + is_ok = False |
| 32 | + message_string = "" |
| 33 | + try: |
| 34 | + docker_args = self._instance.get_docker_args( |
| 35 | + {self._instance.get_name(): [mock_cliargs]}) |
| 36 | + is_ok = docker_args == expected |
| 37 | + message_string = f"Expected: '{expected}', got: '{docker_args}'" |
| 38 | + except ArgumentTypeError: |
| 39 | + message_string = "Incorrect argument format" |
| 40 | + return (is_ok, message_string) |
| 41 | + |
| 42 | + def test_args_single_soft(self): |
| 43 | + """Test single soft limit argument.""" |
| 44 | + mock_cliargs = ["rtprio=99"] |
| 45 | + expected = " --ulimit rtprio=99" |
| 46 | + self.assertTrue(*self._is_arg_translation_ok(mock_cliargs, expected)) |
| 47 | + |
| 48 | + def test_args_multiple_soft(self): |
| 49 | + """Test multiple soft limit arguments.""" |
| 50 | + mock_cliargs = ["rtprio=99", "memlock=102400"] |
| 51 | + expected = " --ulimit rtprio=99 --ulimit memlock=102400" |
| 52 | + self.assertTrue(*self._is_arg_translation_ok(mock_cliargs, expected)) |
| 53 | + |
| 54 | + def test_args_single_hard(self): |
| 55 | + """Test single hard limit argument.""" |
| 56 | + mock_cliargs = ["nofile=1024:524288"] |
| 57 | + expected = " --ulimit nofile=1024:524288" |
| 58 | + self.assertTrue(*self._is_arg_translation_ok(mock_cliargs, expected)) |
| 59 | + |
| 60 | + def test_args_multiple_hard(self): |
| 61 | + """Test multiple hard limit arguments.""" |
| 62 | + mock_cliargs = ["nofile=1024:524288", "rtprio=90:99"] |
| 63 | + expected = " --ulimit nofile=1024:524288 --ulimit rtprio=90:99" |
| 64 | + self.assertTrue(*self._is_arg_translation_ok(mock_cliargs, expected)) |
| 65 | + |
| 66 | + def test_args_multiple_mix(self): |
| 67 | + """Test multiple mixed limit arguments.""" |
| 68 | + mock_cliargs = ["rtprio=99", "memlock=102400", "nofile=1024:524288"] |
| 69 | + expected = " --ulimit rtprio=99 --ulimit memlock=102400 --ulimit nofile=1024:524288" |
| 70 | + self.assertTrue(*self._is_arg_translation_ok(mock_cliargs, expected)) |
| 71 | + |
| 72 | + def test_args_wrong_single_soft(self): |
| 73 | + """Test if single soft limit argument is wrong.""" |
| 74 | + mock_cliargs = ["rtprio99"] |
| 75 | + expected = " --ulimit rtprio99" |
| 76 | + self.assertFalse(*self._is_arg_translation_ok(mock_cliargs, expected)) |
| 77 | + |
| 78 | + def test_args_wrong_multiple_soft(self): |
| 79 | + """Test if multiple soft limit arguments are wrong.""" |
| 80 | + mock_cliargs = ["rtprio=99", "memlock102400"] |
| 81 | + expected = " --ulimit rtprio=99 --ulimit memlock=102400" |
| 82 | + self.assertFalse(*self._is_arg_translation_ok(mock_cliargs, expected)) |
| 83 | + |
| 84 | + def test_args_wrong_single_hard(self): |
| 85 | + """Test if single hard limit arguments are wrong.""" |
| 86 | + mock_cliargs = ["nofile=1024:524288:"] |
| 87 | + expected = " --ulimit nofile=1024:524288" |
| 88 | + self.assertFalse(*self._is_arg_translation_ok(mock_cliargs, expected)) |
| 89 | + |
| 90 | + def test_args_wrong_multiple_hard(self): |
| 91 | + """Test if multiple hard limit arguments are wrong.""" |
| 92 | + mock_cliargs = ["nofile1024524288", "rtprio=90:99"] |
| 93 | + expected = " --ulimit nofile=1024:524288 --ulimit rtprio=90:99" |
| 94 | + self.assertFalse(*self._is_arg_translation_ok(mock_cliargs, expected)) |
| 95 | + |
| 96 | + def test_args_wrong_multiple_mix(self): |
| 97 | + """Test if multiple mixed limit arguments are wrong.""" |
| 98 | + mock_cliargs = ["rtprio=:", "memlock102400", "nofile1024:524288:"] |
| 99 | + expected = " --ulimit rtprio=99 --ulimit memlock=102400 --ulimit nofile=1024:524288" |
| 100 | + self.assertFalse(*self._is_arg_translation_ok(mock_cliargs, expected)) |
0 commit comments