|
| 1 | +# Copyright 2026 PerfKitBenchmarker Authors. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Tests for perfkitbenchmarker.linux_benchmarks.swap_encryption_benchmark.""" |
| 15 | + |
| 16 | +import unittest |
| 17 | +from unittest import mock |
| 18 | + |
| 19 | +from perfkitbenchmarker.linux_benchmarks import swap_encryption_benchmark |
| 20 | +from tests import pkb_common_test_case |
| 21 | + |
| 22 | + |
| 23 | +class GetConfigTest(pkb_common_test_case.PkbCommonTestCase): |
| 24 | + """Tests that BENCHMARK_CONFIG is well-formed and loadable.""" |
| 25 | + |
| 26 | + def test_get_config_returns_dict(self): |
| 27 | + config = swap_encryption_benchmark.GetConfig({}) |
| 28 | + self.assertIsInstance(config, dict) |
| 29 | + |
| 30 | + def test_get_config_has_container_cluster(self): |
| 31 | + # configs.LoadConfig returns the inner benchmark dict directly (no benchmark |
| 32 | + # name wrapper), so top-level keys are 'container_cluster', 'description', etc. |
| 33 | + config = swap_encryption_benchmark.GetConfig({}) |
| 34 | + self.assertIn('container_cluster', config) |
| 35 | + |
| 36 | + def test_get_config_benchmark_nodepool_present(self): |
| 37 | + config = swap_encryption_benchmark.GetConfig({}) |
| 38 | + nodepools = config['container_cluster']['nodepools'] |
| 39 | + self.assertIn( |
| 40 | + swap_encryption_benchmark._BENCHMARK_NODEPOOL, |
| 41 | + nodepools, |
| 42 | + ) |
| 43 | + |
| 44 | + def test_get_config_swap_config_present_on_benchmark_nodepool(self): |
| 45 | + config = swap_encryption_benchmark.GetConfig({}) |
| 46 | + nodepool = config['container_cluster']['nodepools'][ |
| 47 | + swap_encryption_benchmark._BENCHMARK_NODEPOOL |
| 48 | + ] |
| 49 | + self.assertIn('swap_config', nodepool) |
| 50 | + self.assertTrue(nodepool['swap_config'].get('enabled', False)) |
| 51 | + |
| 52 | + |
| 53 | +class ParseCipherTest(pkb_common_test_case.PkbCommonTestCase): |
| 54 | + """Tests for _parse_cipher() output parsing.""" |
| 55 | + |
| 56 | + def test_parse_cipher_standard_aes_xts(self): |
| 57 | + # Typical dmsetup status line: <name> <start>-<end> crypt <cipher> ... |
| 58 | + status = '0 67108864 crypt aes-xts-plain64 0 8:16 0 1 sector_size:4096' |
| 59 | + self.assertEqual( |
| 60 | + swap_encryption_benchmark._parse_cipher(status), 'aes-xts-plain64' |
| 61 | + ) |
| 62 | + |
| 63 | + def test_parse_cipher_returns_empty_when_no_crypt_token(self): |
| 64 | + status = '0 67108864 linear 8:16 0' |
| 65 | + self.assertEqual(swap_encryption_benchmark._parse_cipher(status), '') |
| 66 | + |
| 67 | + def test_parse_cipher_returns_empty_on_empty_string(self): |
| 68 | + self.assertEqual(swap_encryption_benchmark._parse_cipher(''), '') |
| 69 | + |
| 70 | + def test_parse_cipher_crypt_at_end_returns_empty(self): |
| 71 | + # 'crypt' present but no token after it. |
| 72 | + status = 'something crypt' |
| 73 | + self.assertEqual(swap_encryption_benchmark._parse_cipher(status), '') |
| 74 | + |
| 75 | + def test_parse_cipher_not_encrypted_string(self): |
| 76 | + # Output from the benchmark when dm-crypt not active. |
| 77 | + status = 'not_encrypted' |
| 78 | + self.assertEqual(swap_encryption_benchmark._parse_cipher(status), '') |
| 79 | + |
| 80 | + |
| 81 | +class DetectSwapDeviceTest(pkb_common_test_case.PkbCommonTestCase): |
| 82 | + """Tests for _detect_swap_device() with mocked PodExec.""" |
| 83 | + |
| 84 | + def _make_ds(self, pod_exec_output): |
| 85 | + ds = mock.Mock() |
| 86 | + ds.PodExec.return_value = (pod_exec_output, '') |
| 87 | + return ds |
| 88 | + |
| 89 | + def test_detect_swap_device_returns_device_basename(self): |
| 90 | + # /proc/swaps first device column (after header skip via awk NR>1). |
| 91 | + ds = self._make_ds('/dev/dm-0\n') |
| 92 | + result = swap_encryption_benchmark._detect_swap_device(ds) |
| 93 | + self.assertEqual(result, 'dm-0') |
| 94 | + |
| 95 | + def test_detect_swap_device_returns_first_device_when_multiple(self): |
| 96 | + ds = self._make_ds('/dev/dm-0\n/dev/dm-1\n') |
| 97 | + result = swap_encryption_benchmark._detect_swap_device(ds) |
| 98 | + self.assertEqual(result, 'dm-0') |
| 99 | + |
| 100 | + def test_detect_swap_device_returns_empty_when_no_swap(self): |
| 101 | + ds = self._make_ds('') |
| 102 | + result = swap_encryption_benchmark._detect_swap_device(ds) |
| 103 | + self.assertEqual(result, '') |
| 104 | + |
| 105 | + def test_detect_swap_device_returns_empty_on_pod_exec_exception(self): |
| 106 | + ds = mock.Mock() |
| 107 | + ds.PodExec.side_effect = Exception('pod not found') |
| 108 | + result = swap_encryption_benchmark._detect_swap_device(ds) |
| 109 | + self.assertEqual(result, '') |
| 110 | + |
| 111 | + |
| 112 | +class BuildMetadataTest(pkb_common_test_case.PkbCommonTestCase): |
| 113 | + """Tests for _build_metadata() with mocked PodExec.""" |
| 114 | + |
| 115 | + def test_build_metadata_includes_swap_device(self): |
| 116 | + ds = mock.Mock() |
| 117 | + ds.PodExec.return_value = ('5.15.0-gke-1234\n', '') |
| 118 | + meta = swap_encryption_benchmark._build_metadata(ds, 'dm-0') |
| 119 | + self.assertEqual(meta['swap_device'], 'dm-0') |
| 120 | + |
| 121 | + def test_build_metadata_swap_device_unknown_when_empty(self): |
| 122 | + ds = mock.Mock() |
| 123 | + ds.PodExec.return_value = ('5.15.0\n', '') |
| 124 | + meta = swap_encryption_benchmark._build_metadata(ds, '') |
| 125 | + self.assertEqual(meta['swap_device'], 'unknown') |
| 126 | + |
| 127 | + def test_build_metadata_includes_kernel_version(self): |
| 128 | + ds = mock.Mock() |
| 129 | + ds.PodExec.return_value = ('5.15.0-gke-1234\n', '') |
| 130 | + meta = swap_encryption_benchmark._build_metadata(ds, 'dm-0') |
| 131 | + self.assertEqual(meta['kernel_version'], '5.15.0-gke-1234') |
| 132 | + |
| 133 | + def test_build_metadata_kernel_version_absent_on_pod_exec_exception(self): |
| 134 | + ds = mock.Mock() |
| 135 | + ds.PodExec.side_effect = Exception('timeout') |
| 136 | + meta = swap_encryption_benchmark._build_metadata(ds, 'dm-0') |
| 137 | + self.assertNotIn('kernel_version', meta) |
| 138 | + |
| 139 | + |
| 140 | +if __name__ == '__main__': |
| 141 | + unittest.main() |
0 commit comments