def test_nat_network_operation(self): fake_client = self.generate_client() gateway = fake_client._vdc_gateway # used wrong operation with self.assertRaises(cfy_exc.NonRecoverableError): public_nat.nat_network_operation( fake_client, gateway, "unknow", "DNAT", "1.2.3.4", "2.3.4.5", "11", "11", "TCP" ) # run correct operation/rule fake_ctx = self.generate_relation_context() fake_ctx._target.instance.runtime_properties = { public_nat.PORT_REPLACEMENT: {}} for operation in [vcloud_network_plugin.DELETE, vcloud_network_plugin.CREATE]: for rule_type in ["SNAT", "DNAT"]: with mock.patch( 'vcloud_network_plugin.public_nat.ctx', fake_ctx ): with mock.patch( 'vcloud_plugin_common.ctx', fake_ctx ): public_nat.nat_network_operation( fake_client, gateway, operation, rule_type, "1.2.3.4", "2.3.4.5", "11", "11", "TCP" ) if rule_type == "DNAT": if operation == vcloud_network_plugin.DELETE: gateway.del_nat_rule.assert_called_with( 'DNAT', '1.2.3.4', '11', '2.3.4.5', '11', 'TCP' ) else: gateway.add_nat_rule.assert_called_with( 'DNAT', '1.2.3.4', '11', '2.3.4.5', '11', 'TCP' ) else: if operation == vcloud_network_plugin.DELETE: gateway.del_nat_rule.assert_called_with( 'SNAT', '2.3.4.5', 'any', '1.2.3.4', 'any', 'any' ) else: gateway.add_nat_rule.assert_called_with( 'SNAT', '2.3.4.5', 'any', '1.2.3.4', 'any', 'any' )
def test_nat_network_operation(self): fake_client = self.generate_client() gateway = fake_client._vdc_gateway # used wrong operation with self.assertRaises(cfy_exc.NonRecoverableError): public_nat.nat_network_operation( fake_client, gateway, "unknow", "DNAT", "1.2.3.4", "2.3.4.5", "11", "11", "TCP" ) # run correct operation/rule for operation in [vcloud_network_plugin.DELETE, vcloud_network_plugin.CREATE]: for rule_type in ["SNAT", "DNAT"]: # cleanup properties fake_ctx = self.generate_relation_context() fake_ctx._target.instance.runtime_properties = { public_nat.PORT_REPLACEMENT: {}} fake_ctx._source.instance.runtime_properties = {} # checks with mock.patch( 'vcloud_network_plugin.public_nat.ctx', fake_ctx ): with mock.patch( 'vcloud_plugin_common.ctx', fake_ctx ): public_nat.nat_network_operation( fake_client, gateway, operation, rule_type, "1.2.3.4", "2.3.4.5", "11", "11", "TCP" ) if rule_type == "DNAT": if operation == vcloud_network_plugin.DELETE: gateway.del_nat_rule.assert_called_with( 'DNAT', '1.2.3.4', '11', '2.3.4.5', '11', 'TCP' ) else: gateway.add_nat_rule.assert_called_with( 'DNAT', '1.2.3.4', '11', '2.3.4.5', '11', 'TCP' ) else: if operation == vcloud_network_plugin.DELETE: gateway.del_nat_rule.assert_called_with( 'SNAT', '2.3.4.5', 'any', '1.2.3.4', 'any', 'any' ) else: gateway.add_nat_rule.assert_called_with( 'SNAT', '2.3.4.5', 'any', '1.2.3.4', 'any', 'any' ) # cleanup properties fake_ctx = self.generate_relation_context() fake_ctx._target.instance.runtime_properties = { public_nat.PORT_REPLACEMENT: {}} fake_ctx._source.instance.runtime_properties = {} # save ssh port with mock.patch( 'vcloud_network_plugin.public_nat.ctx', fake_ctx ): with mock.patch( 'vcloud_plugin_common.ctx', fake_ctx ): public_nat.nat_network_operation( fake_client, gateway, vcloud_network_plugin.CREATE, "DNAT", "1.2.3.4", "2.3.4.5", "43", "22", "TCP" ) self.assertEqual( {'port_replacement': {'1.2.3.4:43': 43}}, fake_ctx._target.instance.runtime_properties ) self.assertEqual( {'ssh_port': '43', 'ssh_public_ip': '1.2.3.4'}, fake_ctx._source.instance.runtime_properties ) # error with type with self.assertRaises(cfy_exc.NonRecoverableError): public_nat.nat_network_operation( fake_client, gateway, vcloud_network_plugin.CREATE, "QNAT", "1.2.3.4", "2.3.4.5", "43", "22", "TCP" )
def test_nat_network_operation(self): fake_client = self.generate_client() gateway = fake_client._vdc_gateway # used wrong operation with self.assertRaises(cfy_exc.NonRecoverableError): public_nat.nat_network_operation(fake_client, gateway, "unknow", "DNAT", "1.2.3.4", "2.3.4.5", "11", "11", "TCP") # run correct operation/rule for operation in [ vcloud_network_plugin.DELETE, vcloud_network_plugin.CREATE ]: for rule_type in ["SNAT", "DNAT"]: # cleanup properties fake_ctx = self.generate_relation_context() fake_ctx._target.instance.runtime_properties = { public_nat.PORT_REPLACEMENT: {} } fake_ctx._source.instance.runtime_properties = {} # checks with mock.patch('vcloud_network_plugin.public_nat.ctx', fake_ctx): with mock.patch('vcloud_plugin_common.ctx', fake_ctx): public_nat.nat_network_operation( fake_client, gateway, operation, rule_type, "1.2.3.4", "2.3.4.5", "11", "11", "TCP") if rule_type == "DNAT": if operation == vcloud_network_plugin.DELETE: gateway.del_nat_rule.assert_called_with( 'DNAT', '1.2.3.4', '11', '2.3.4.5', '11', 'TCP') else: gateway.add_nat_rule.assert_called_with( 'DNAT', '1.2.3.4', '11', '2.3.4.5', '11', 'TCP') else: if operation == vcloud_network_plugin.DELETE: gateway.del_nat_rule.assert_called_with( 'SNAT', '2.3.4.5', 'any', '1.2.3.4', 'any', 'any') else: gateway.add_nat_rule.assert_called_with( 'SNAT', '2.3.4.5', 'any', '1.2.3.4', 'any', 'any') # cleanup properties fake_ctx = self.generate_relation_context() fake_ctx._target.instance.runtime_properties = { public_nat.PORT_REPLACEMENT: {} } fake_ctx._source.instance.runtime_properties = {} # save ssh port with mock.patch('vcloud_network_plugin.public_nat.ctx', fake_ctx): with mock.patch('vcloud_plugin_common.ctx', fake_ctx): public_nat.nat_network_operation(fake_client, gateway, vcloud_network_plugin.CREATE, "DNAT", "1.2.3.4", "2.3.4.5", "43", "22", "TCP") self.assertEqual({'port_replacement': { '1.2.3.4:43': 43 }}, fake_ctx._target.instance.runtime_properties) self.assertEqual({ 'ssh_port': '43', 'ssh_public_ip': '1.2.3.4' }, fake_ctx._source.instance.runtime_properties) # error with type with self.assertRaises(cfy_exc.NonRecoverableError): public_nat.nat_network_operation( fake_client, gateway, vcloud_network_plugin.CREATE, "QNAT", "1.2.3.4", "2.3.4.5", "43", "22", "TCP")