async def test_put_file_default_client(self): server_addr = f"http://localhost:{self.server.port}" with async_mock.patch("builtins.open", mock_open(read_data="data")): result = await put_file( f"{server_addr}/succeed", {"tails": "/tmp/dummy/path"}, {"genesis": "..."}, json=True, ) assert result == [1] assert self.succeed_calls == 1
async def test_put_file_fail(self): server_addr = f"http://localhost:{self.server.port}" with async_mock.patch("builtins.open", mock_open(read_data="data")): with self.assertRaises(PutError): result = await put_file( f"{server_addr}/fail", {"tails": "/tmp/dummy/path"}, {"genesis": "..."}, max_attempts=2, json=True, ) assert self.fail_calls == 2
def setUp(self): self.env_vars = {"EXAMPLE_VAR": "VALUE", "OTHER_VAR": ANY} self.p_getenv = Mock(side_effect=self.retrieve_env_var) start_patch(self, Example, os, new=Mock(getenv=self.p_getenv)) self.p_stderr = Mock() start_patch(self, Example, sys, new=Mock(stderr=self.p_stderr)) self.post_return = {"elements": [100, 2, 15, 28], "other_thing": "This is a thing (other)"} post = Mock(return_value=Mock(status_code=200, json=Mock(return_value=self.post_return))) self.post = post self.requests = start_patch(self, Example, requests, new=Mock(name="requests", post=post)) self.open = start_patch(self, target='builtins.open', new=mock_open(read_data=json.dumps([{"field": 1}, {"field": 2}]))) self.timer = Mock() self.timer_constructor = start_patch(self, Example, Timer, return_value=self.timer) self.example = Example()
def test_MagicMock_returned_by_default(self): self.assertIsInstance(asynctest.mock_open(), asynctest.MagicMock)
def test_MagicMock_returned_by_default(self): self.assertIsInstance(asynctest.mock_open(), asynctest.MagicMock)
async def setUp(self): # Mock DB if not getenv("OSMLCMTEST_DB_NOMOCK"): self.db = DbMemory() self.db.create_list( "vnfds", yaml.load(descriptors.db_vnfds_text, Loader=yaml.Loader)) self.db.create_list( "nsds", yaml.load(descriptors.db_nsds_text, Loader=yaml.Loader)) self.db.create_list( "nsrs", yaml.load(descriptors.db_nsrs_text, Loader=yaml.Loader)) self.db.create_list( "vim_accounts", yaml.load(descriptors.db_vim_accounts_text, Loader=yaml.Loader)) self.db.create_list( "k8sclusters", yaml.load(descriptors.db_k8sclusters_text, Loader=yaml.Loader)) self.db.create_list( "nslcmops", yaml.load(descriptors.db_nslcmops_text, Loader=yaml.Loader)) self.db.create_list( "vnfrs", yaml.load(descriptors.db_vnfrs_text, Loader=yaml.Loader)) self.db_vim_accounts = yaml.load(descriptors.db_vim_accounts_text, Loader=yaml.Loader) # Mock kafka self.msg = asynctest.Mock(MsgKafka()) # Mock filesystem if not getenv("OSMLCMTEST_FS_NOMOCK"): self.fs = asynctest.Mock(FsLocal()) self.fs.get_params.return_value = { "path": getenv("OSMLCMTEST_PACKAGES_PATH", "./test/temp/packages") } self.fs.file_open = asynctest.mock_open() # self.fs.file_open.return_value.__enter__.return_value = asynctest.MagicMock() # called on a python "with" # self.fs.file_open.return_value.__enter__.return_value.read.return_value = "" # empty file # Mock TaskRegistry self.lcm_tasks = asynctest.Mock(TaskRegistry()) self.lcm_tasks.lock_HA.return_value = True self.lcm_tasks.waitfor_related_HA.return_value = None self.lcm_tasks.lookfor_related.return_value = ("", []) # Mock VCA - K8s if not getenv("OSMLCMTEST_VCA_K8s_NOMOCK"): ns.K8sJujuConnector = asynctest.MagicMock(ns.K8sJujuConnector) ns.K8sHelmConnector = asynctest.MagicMock(ns.K8sHelmConnector) if not getenv("OSMLCMTEST_VCA_NOMOCK"): ns.N2VCJujuConnector = asynctest.MagicMock(ns.N2VCJujuConnector) ns.N2VCJujuConnectorLCM = asynctest.MagicMock( ns.N2VCJujuConnectorLCM) ns.LCMHelmConn = asynctest.MagicMock(ns.LCMHelmConn) # Create NsLCM class self.my_ns = ns.NsLcm(self.db, self.msg, self.fs, self.lcm_tasks, lcm_config, self.loop) self.my_ns._wait_dependent_n2vc = asynctest.CoroutineMock() # Mock logging if not getenv("OSMLCMTEST_LOGGING_NOMOCK"): self.my_ns.logger = asynctest.Mock(self.my_ns.logger) # Mock VCA - N2VC if not getenv("OSMLCMTEST_VCA_NOMOCK"): pub_key = getenv("OSMLCMTEST_NS_PUBKEY", "ssh-rsa test-pub-key [email protected]") # self.my_ns.n2vc = asynctest.Mock(N2VC()) self.my_ns.n2vc.GetPublicKey.return_value = getenv( "OSMLCM_VCA_PUBKEY", "public_key") # allow several versions of n2vc self.my_ns.n2vc.FormatApplicationName = asynctest.Mock( side_effect=self._n2vc_FormatApplicationName()) self.my_ns.n2vc.DeployCharms = asynctest.CoroutineMock( side_effect=self._n2vc_DeployCharms) self.my_ns.n2vc.create_execution_environment = asynctest.CoroutineMock( side_effect=self._n2vc_CreateExecutionEnvironment) self.my_ns.n2vc.install_configuration_sw = asynctest.CoroutineMock( return_value=pub_key) self.my_ns.n2vc.get_ee_ssh_public__key = asynctest.CoroutineMock( return_value=pub_key) self.my_ns.n2vc.exec_primitive = asynctest.CoroutineMock( side_effect=self._return_uuid) self.my_ns.n2vc.GetPrimitiveStatus = asynctest.CoroutineMock( return_value="completed") self.my_ns.n2vc.GetPrimitiveOutput = asynctest.CoroutineMock( return_value={ "result": "ok", "pubkey": pub_key }) self.my_ns.n2vc.delete_execution_environment = asynctest.CoroutineMock( return_value=None) self.my_ns.n2vc.get_public_key = asynctest.CoroutineMock( return_value=getenv("OSMLCM_VCA_PUBKEY", "public_key")) self.my_ns.n2vc.delete_namespace = asynctest.CoroutineMock( return_value=None) # Mock RO if not getenv("OSMLCMTEST_RO_NOMOCK"): # self.my_ns.RO = asynctest.Mock(ROclient.ROClient(self.loop, **lcm_config["ro_config"])) # TODO first time should be empty list, following should return a dict self.my_ns.RO.get_list = asynctest.CoroutineMock( self.my_ns.RO.get_list, return_value=[]) self.my_ns.RO.create = asynctest.CoroutineMock( self.my_ns.RO.create, side_effect=self._ro_create()) self.my_ns.RO.show = asynctest.CoroutineMock( self.my_ns.RO.show, side_effect=self._ro_show()) self.my_ns.RO.create_action = asynctest.CoroutineMock( self.my_ns.RO.create_action, return_value={ "vm-id": { "vim_result": 200, "description": "done" } }) self.my_ns.RO.delete = asynctest.CoroutineMock( self.my_ns.RO.delete, return_value={"action_id": "del"})