From 8c1adfafd52d96efaaede3aa7a52f0bc49e3b1e9 Mon Sep 17 00:00:00 2001 From: Dheeraj Date: Wed, 29 Mar 2017 17:21:47 +0530 Subject: [PATCH] More complete fix for 39692 The existing fix did not work if the profile name itself had a dash `-`. For example - `virtual-guest`. This commit fixes that by using `split('- ')` rather than `split('-')`. This commit also provides two simple tests for the `list_()` function to emulate behaviour of both old and new tuned-adm versions Fixes #39692 --- salt/modules/tuned.py | 2 +- tests/unit/modules/tuned_test.py | 67 ++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 tests/unit/modules/tuned_test.py diff --git a/salt/modules/tuned.py b/salt/modules/tuned.py index 33a1485c99..45d54b512b 100644 --- a/salt/modules/tuned.py +++ b/salt/modules/tuned.py @@ -51,7 +51,7 @@ def list_(): result.pop() # Output can be : " - - " (v2.7.1) # or " - " (v2.4.1) - result = [i.split('-')[1].strip() for i in result] + result = [i.split('- ')[1].strip() for i in result] return result diff --git a/tests/unit/modules/tuned_test.py b/tests/unit/modules/tuned_test.py new file mode 100644 index 0000000000..bacead588d --- /dev/null +++ b/tests/unit/modules/tuned_test.py @@ -0,0 +1,67 @@ +# -*- coding: utf-8 -*- +from salt.modules import tuned + +from salttesting import skipIf, TestCase +from salttesting.helpers import ensure_in_syspath +from salttesting.mock import NO_MOCK, NO_MOCK_REASON, MagicMock, patch, call + + +tuned.__salt__ = {} + + +@skipIf(NO_MOCK, NO_MOCK_REASON) +class TunedListTestCase(TestCase): + def test_v_241(self): + """ + Test the list_ function for older tuned-adm (v2.4.1) + as shipped with CentOS-6 + """ + tuned_list = '''Available profiles: +- throughput-performance +- virtual-guest +- latency-performance +- laptop-battery-powersave +- laptop-ac-powersave +- virtual-host +- desktop-powersave +- server-powersave +- spindown-disk +- sap +- enterprise-storage +- default +Current active profile: throughput-performance''' + mock_cmd = MagicMock(return_value=tuned_list) + with patch.dict(tuned.__salt__, {'cmd.run': mock_cmd}): + self.assertEqual( + tuned.list_(), + ['throughput-performance', 'virtual-guest', + 'latency-performance', 'laptop-battery-powersave', + 'laptop-ac-powersave', 'virtual-host', + 'desktop-powersave', 'server-powersave', + 'spindown-disk', 'sap', 'enterprise-storage', 'default']) + + def test_v_271(self): + """ + Test the list_ function for older tuned-adm (v2.7.1) + as shipped with CentOS-7 + """ + tuned_list = '''Available profiles: +- balanced - General non-specialized tuned profile +- desktop - Optmize for the desktop use-case +- latency-performance - Optimize for deterministic performance +- network-latency - Optimize for deterministic performance +- network-throughput - Optimize for streaming network throughput. +- powersave - Optimize for low power-consumption +- throughput-performance - Broadly applicable tuning that provides-- +- virtual-guest - Optimize for running inside a virtual-guest. +- virtual-host - Optimize for running KVM guests +Current active profile: virtual-guest +''' + mock_cmd = MagicMock(return_value=tuned_list) + with patch.dict(tuned.__salt__, {'cmd.run': mock_cmd}): + self.assertEqual( + tuned.list_(), + ['balanced', 'desktop', 'latency-performance', + 'network-latency', 'network-throughput', 'powersave', + 'throughput-performance', 'virtual-guest', + 'virtual-host'])