def test_managed(self): ''' Test to ensure that the named interface is configured properly ''' with patch('salt.states.network.salt.utils.network', MockNetwork()), \ patch('salt.states.network.salt.loader', MockGrains()): ret = {'name': 'salt', 'changes': {}, 'result': False, 'comment': ''} change = {'interface': '--- \n+++ \n@@ -1 +1 @@\n-A\n+B', 'status': 'Interface salt restart to validate'} mock = MagicMock(side_effect=[AttributeError, 'A', 'A', 'A', 'A', 'A']) with patch.dict(network.__salt__, {"ip.get_interface": mock}): self.assertDictEqual(network.managed('salt', 'stack', test='a'), ret) mock = MagicMock(return_value='B') with patch.dict(network.__salt__, {"ip.build_interface": mock}): mock = MagicMock(side_effect=AttributeError) with patch.dict(network.__salt__, {"ip.get_bond": mock}): self.assertDictEqual(network.managed('salt', 'bond', test='a'), ret) ret.update({'comment': 'Interface salt is set to be' ' updated:\n--- \n+++ \n@@ -1 +1 @@\n-A\n+B', 'result': None}) self.assertDictEqual(network.managed('salt', 'stack', test='a'), ret) mock = MagicMock(return_value=True) with patch.dict(network.__salt__, {"ip.down": mock}): with patch.dict(network.__salt__, {"ip.up": mock}): ret.update({'comment': 'Interface salt updated.', 'result': True, 'changes': change}) self.assertDictEqual(network.managed('salt', 'stack'), ret) with patch.dict(network.__grains__, {"A": True}): with patch.dict(network.__salt__, {"saltutil.refresh_modules": mock} ): ret.update({'result': True, 'changes': {'interface': '--- \n+' '++ \n@@ -1 +1 @@\n-A' '\n+B', 'status': 'Interface' ' salt down'}}) self.assertDictEqual(network.managed('salt', 'stack', False), ret)
def test_managed(self): """ Test to ensure that the named interface is configured properly """ with patch("salt.states.network.salt.utils.network", MockNetwork()), patch( "salt.states.network.salt.loader", MockGrains() ): ret = {"name": "salt", "changes": {}, "result": False, "comment": ""} change = { "interface": "--- \n+++ \n@@ -1 +1 @@\n-A\n+B", "status": "Interface salt restart to validate", } dunder_salt = { "ip.get_interface": MagicMock( side_effect=[AttributeError, "A", "A", "A", "A", "A"] ), "ip.build_interface": MagicMock(return_value="B"), "saltutil.refresh_grains": MagicMock(return_value=True), } with patch.dict(network.__salt__, dunder_salt): with patch.dict( network.__salt__, {"ip.get_bond": MagicMock(side_effect=AttributeError)}, ): self.assertDictEqual( network.managed("salt", type="bond", test=True), ret ) ret.update( { "comment": ( "Interface salt is set to be" " updated:\n--- \n+++ \n@@ -1 +1 @@\n-A\n+B" ), "result": None, } ) self.assertDictEqual( network.managed("salt", type="stack", test=True), ret ) ipupdown = MagicMock(return_value=True) with patch.dict( network.__salt__, {"ip.down": ipupdown, "ip.up": ipupdown} ): ret.update( { "comment": "Interface salt updated.", "result": True, "changes": change, } ) self.assertDictEqual(network.managed("salt", type="stack"), ret) with patch.dict(network.__grains__, {"A": True}): ret.update( { "result": True, "changes": { "interface": "--- \n+++ \n@@ -1 +1 @@\n-A\n+B", "status": "Interface salt down", }, } ) self.assertDictEqual( network.managed("salt", type="stack", enabled=False), ret ) mock = MagicMock(return_value=True) with patch.dict(network.__salt__, {"ip.down": mock}): with patch.dict( network.__salt__, {"saltutil.refresh_modules": mock} ): change = { "interface": "--- \n+++ \n@@ -1 +1 @@\n-A\n+B", "status": "Interface lo:alias1 down", } ret.update( { "name": "lo:alias1", "comment": "Interface lo:alias1 updated.", "result": True, "changes": change, } ) self.assertDictEqual( network.managed("lo:alias1", type="eth", enabled=False), ret, )