def test_update_qtable_from_remote_qtable(self, mock1, mock2): blue_angel_offers = cloudomate_controller.options( self.providers["blueangelhost"]) self.qtable.self_state = VPSState("blueangelhost", blue_angel_offers[0].name) self.qtable.init_qtable_and_environment(self.providers) self.qtable.init_alpha_and_beta() qtable2 = QTable() qtable2.self_state = VPSState("blueangelhost", blue_angel_offers[1].name) qtable2.init_qtable_and_environment(self.providers) qtable2.init_alpha_and_beta() vps_options_list = cloudomate_controller.options(self.providers) vps_option = vps_options_list[0] provider_offer_ID = str(self.providers.keys()[0]).lower() + "_" + str( vps_option.name).lower() provider_offer_ID_other = str( self.providers.keys()[0]).lower() + "_" + str( vps_options_list[1].name).lower() self.qtable.update_qtable([], provider_offer_ID_other, True, 0.5) self.qtable.set_self_state( VPSState("blueangelhost", blue_angel_offers[1].name)) qtable2.update_qtable([], provider_offer_ID_other, True, 0.6) qtable2.set_self_state( VPSState("blueangelhost", blue_angel_offers[1].name)) self.qtable.update_qtable([qtable2.qtable], provider_offer_ID, True, 0.3) assert (qtable2.qtable != self.qtable.qtable) assert (qtable2.qtable[provider_offer_ID_other][provider_offer_ID] < self.qtable.qtable[provider_offer_ID_other][provider_offer_ID])
class TestCore(unittest.TestCase): def test_create_wallet_testnet(self): self.settings = plebnet_settings.Init.wallets_testnet self.logger = logger.log self.wallet = wallet_controller.create_wallet self.wallet_created = plebnet_settings.Init.wallets_testnet_created self.initiated = plebnet_settings.Init.wallets_initiate_once self.write = plebnet_settings.write plebnet_settings.Init.wallets_testnet = MagicMock(return_value=True) logger.log = MagicMock() wallet_controller.create_wallet = MagicMock(return_value=True) plebnet_settings.Init.wallets_testnet_created = MagicMock() plebnet_settings.Init.wallets_initiate_once = MagicMock() plebnet_settings.write = MagicMock() Core.create_wallet() self.assertEqual(os.environ.get('TESTNET'), '1') os.environ['TESTNET'] = '0' plebnet_settings.Init.wallets_testnet = self.settings logger.log = self.logger wallet_controller.create_wallet = self.wallet plebnet_settings.Init.wallets_testnet_created = self.wallet_created plebnet_settings.Init.wallets_initiate_once = self.initiated plebnet_settings.write = self.write def test_create_wallet_bitcoin(self): self.settings = plebnet_settings.Init.wallets_testnet self.logger = logger.log self.wallet = wallet_controller.create_wallet self.initiated = plebnet_settings.Init.wallets_initiate_once self.write = plebnet_settings.setting.Settings.write plebnet_settings.Init.wallets_testnet = MagicMock(return_value=False) logger.log = MagicMock() wallet_controller.create_wallet = MagicMock(return_value=True) plebnet_settings.Init.wallets_initiate_once = MagicMock() plebnet_settings.setting.Settings.write = MagicMock() Core.create_wallet() plebnet_settings.Init.wallets_initiate_once.assert_called_once() plebnet_settings.setting.Settings.write.assert_called_once() plebnet_settings.Init.wallets_testnet = self.settings logger.log = self.logger wallet_controller.create_wallet = self.wallet plebnet_settings.Init.wallets_initiate_once = self.initiated plebnet_settings.setting.Settings.write = self.write def test_check_tribler_true(self): self.running = tribler_controller.running self.logger = logger.log tribler_controller.running = MagicMock(return_value=True) logger.log = MagicMock() assert Core.check_tribler() tribler_controller.running = self.running logger.log = self.logger def test_check_tribler_false(self): self.running = tribler_controller.running self.start = tribler_controller.start self.logger = logger.log tribler_controller.start = MagicMock() tribler_controller.running = MagicMock(return_value=False) logger.log = MagicMock() self.assertFalse(Core.check_tribler()) tribler_controller.running = self.running logger.log = self.logger tribler_controller.start = self.start def test_setup(self): self.logger = logger.log self.provider = cloudomate_controller.get_vps_providers self.settings = plebnet_settings.Init.wallets_testnet self.fake = fake_generator.generate_child_account self.irc_nic = plebnet_settings.Init.irc_nick self.irc_def = plebnet_settings.Init.irc_nick_def self.save = PlebNetConfig.save self.init_client = irc_handler.init_irc_client self.start_client = irc_handler.start_irc_client self.success = logger.success self.load = PlebNetConfig.load self.exit = plebnet_settings.Init.tribler_exitnode args = MagicMock() args.testnet = True logger.log = MagicMock() fake_generator.generate_child_account = MagicMock() plebnet_settings.Init.wallets_testnet = MagicMock() cloudomate_controller.get_vps_providers = MagicMock() plebnet_settings.Init.irc_nick = MagicMock() plebnet_settings.Init.irc_nick_def = MagicMock() PlebNetConfig.save = MagicMock() PlebNetConfig.load = MagicMock() irc_handler.init_irc_client = MagicMock() irc_handler.start_irc_client = MagicMock() logger.success = MagicMock() plebnet_settings.Init.tribler_exitnode = MagicMock() Core.setup(args) logger.success.assert_called_once() logger.log = self.logger cloudomate_controller.get_vps_providers = self.provider plebnet_settings.Init.wallets_testnet = self.settings fake_generator.generate_child_account = self.fake plebnet_settings.Init.irc_nick = self.irc_nic plebnet_settings.Init.irc_nick_def = self.irc_def PlebNetConfig.save = self.save PlebNetConfig.load = self.load irc_handler.init_irc_client = self.init_client irc_handler.start_irc_client = self.start_client logger.success = self.success plebnet_settings.Init.tribler_exitnode = self.exit @mock.patch('plebnet.agent.core.attempt_purchase') @mock.patch('plebnet.settings.plebnet_settings.Init.wallets_initiate_once', return_value=False) @mock.patch( 'plebnet.settings.plebnet_settings.Init.wallets_testnet_created', return_value=True) @mock.patch('plebnet.agent.core.check_tribler', return_value=False) @mock.patch('plebnet.agent.core.vpn_is_running', return_value=True) @mock.patch('plebnet.agent.core.create_wallet') @mock.patch('plebnet.agent.core.select_provider') @mock.patch('plebnet.agent.core.strategies') @mock.patch('plebnet.agent.core.install_vps') @mock.patch('plebnet.controllers.market_controller.has_matchmakers', return_value=True) @mock.patch('plebnet.agent.config.PlebNetConfig.load', return_value=False) def test_check(self, mock1, mock2, mock3, mock4, mock5, mock6, mock7, mock8, mock9, mock10, mock11): self.logger = logger.log logger.log = MagicMock() Core.strategies['test']().apply = MagicMock() Core.check() os.environ['TESTNET'] = '0' plebnet_settings.Init.wallets_testnet_created = MagicMock( return_value=False) Core.check_tribler = MagicMock(return_value=True) Core.check() Core.install_vps.assert_called_once() Core.strategies['test']().apply.assert_called_once() Core.create_wallet.assert_called_once() logger.log = self.logger @mock.patch('plebnet.controllers.cloudomate_controller.get_vps_providers', return_value=CaseInsensitiveDict( {'blueangelhost': blueAngel.BlueAngelHost})) @mock.patch('plebnet.controllers.cloudomate_controller.options', return_value=[ VpsOption(name='Advanced', storage=2, cores=2, memory=2, bandwidth="4.0", connection="1", price=100.0, purchase_url="mock"), VpsOption(name='Basic Plan', storage=2, cores=2, memory=2, bandwidth="1.0", connection="1", price=10.0, purchase_url="mock") ]) def test_attempt_purchase(self, mock1, mock2): self.log = logger.log self.testnet = plebnet_settings.Init.wallets_testnet self.get = PlebNetConfig.get self.get_balance = market_controller.get_balance self.calculate_price = cloudomate_controller.calculate_price self.purchase_choice = cloudomate_controller.purchase_choice self.set = PlebNetConfig.set self.save = PlebNetConfig.save self.vpn = Core.attempt_purchase_vpn self.new = PlebNetConfig.increment_child_index self.provider = cloudomate_controller.get_vps_providers self.fg = fake_generator.generate_child_account self.qtable = QTable() self.providers = cloudomate_controller.get_vps_providers() providers = cloudomate_controller.get_vps_providers() self.qtable.init_qtable_and_environment(providers) self.qtable.set_self_state(VPSState("blueangelhost", "Basic Plan")) logger.log = MagicMock() plebnet_settings.Init.wallets_testnet = MagicMock(return_value=False) PlebNetConfig.get = MagicMock( return_value=['blueangelhost', 'Basic Plan', 0]) market_controller.get_balance = MagicMock(return_value=100000000) Core.qtable = self.qtable Core.attempt_purchase_vpn = MagicMock(return_value=False) cloudomate_controller.calculate_price = MagicMock(return_value=0.01) cloudomate_controller.purchase_choice = MagicMock( return_value=plebnet_settings.SUCCESS) PlebNetConfig.set = MagicMock() PlebNetConfig.save = MagicMock() PlebNetConfig.increment_child_index = MagicMock() cloudomate_controller.get_vps_providers = MagicMock() fake_generator.generate_child_account = MagicMock() Core.config = PlebNetConfig qtable_copy = copy.deepcopy(Core.qtable.qtable) Core.attempt_purchase() self.assertLess( qtable_copy['blueangelhost_basic plan'] ['blueangelhost_basic plan'], Core.qtable.qtable['blueangelhost_basic plan'] ['blueangelhost_basic plan']) qtable_copy = copy.deepcopy(Core.qtable.qtable) cloudomate_controller.purchase_choice = MagicMock( return_value=plebnet_settings.FAILURE) Core.attempt_purchase() self.assertGreater( qtable_copy['blueangelhost_basic plan'] ['blueangelhost_basic plan'], Core.qtable.qtable['blueangelhost_basic plan'] ['blueangelhost_basic plan']) logger.log = self.log plebnet_settings.Init.wallets_testnet = self.testnet PlebNetConfig.get = self.get market_controller.get_balance = self.get_balance cloudomate_controller.calculate_price = self.calculate_price cloudomate_controller.purchase_choice = self.purchase_choice Core.attempt_purchase_vpn = self.vpn PlebNetConfig.set = self.set PlebNetConfig.save = self.save PlebNetConfig.increment_child_index = self.new cloudomate_controller.get_vps_providers = self.provider fake_generator.generate_child_account = self.fg def test_install_vps(self): self.ias = server_installer.install_available_servers server_installer.install_available_servers = MagicMock() Core.install_vps() server_installer.install_available_servers.assert_called_once() server_installer.install_available_servers = self.ias def test_attempt_purchase_vpn(self): self.vpnset = plebnet_settings.Init.vpn_host self.vpnpro = cloudomate_controller.get_vpn_providers self.wall = plebnet_settings.Init.wallets_testnet self.bal = market_controller.get_balance self.price = cloudomate_controller.calculate_price_vpn self.pur = cloudomate_controller.purchase_choice_vpn self.suc = logger.success self.log = logger.log self.err = logger.error cloudomate_controller.purchase_choice_vpn = MagicMock( return_value=plebnet_settings.SUCCESS) plebnet_settings.Init.vpn_host = MagicMock() cloudomate_controller.get_vpn_providers = MagicMock( return_value='String') plebnet_settings.Init.wallets_testnet = MagicMock(return_value=True) market_controller.get_balance = MagicMock(return_value=1100000) cloudomate_controller.calculate_price_vpn = MagicMock( return_value=0.01) logger.success = MagicMock() logger.log = MagicMock() logger.error = MagicMock() Core.attempt_purchase_vpn() logger.success.assert_called_once() cloudomate_controller.purchase_choice_vpn = MagicMock( return_value=plebnet_settings.FAILURE) plebnet_settings.Init.wallets_testnet = MagicMock(return_value=False) Core.attempt_purchase_vpn() logger.error.assert_called_once() plebnet_settings.Init.vpn_host = self.vpnset cloudomate_controller.get_vpn_providers = self.vpnpro plebnet_settings.Init.wallets_testnet = self.wall market_controller.get_balance = self.bal cloudomate_controller.calculate_price_vpn = self.price cloudomate_controller.purchase_choice_vpn = self.pur logger.success = self.suc logger.log = self.log logger.error = self.err def test_vpn_is_running(self): self.pid = plebnet_settings.Init.vpn_pid self.run = plebnet_settings.Init.vpn_running self.call = subprocess.call plebnet_settings.Init.vpn_pid = MagicMock(return_value='String') plebnet_settings.Init.vpn_running = MagicMock() subprocess.call = MagicMock(return_value=0) assert Core.vpn_is_running() subprocess.call = MagicMock(return_value=1) self.assertFalse(Core.vpn_is_running()) plebnet_settings.Init.vpn_pid = self.pid plebnet_settings.Init.vpn_running = self.run subprocess.call = self.call def test_check_vpn_install(self): self.vpn_installed = plebnet_settings.Init.vpn_installed self.log = logger.log self.ospath = os.path.join self.path = plebnet_settings.Init.vpn_config_path self.pre = plebnet_settings.Init.vpn_own_prefix self.cre = plebnet_settings.Init.vpn_credentials_name self.nam = plebnet_settings.Init.vpn_config_name self.lis = os.listdir self.isf = os.path.isfile self.civ = Core.install_vpn self.cpr = plebnet_settings.Init.vpn_child_prefix self.usr = os.path.expanduser self.vpn_running = Core.vpn_is_running plebnet_settings.Init.vpn_installed = MagicMock(return_value=True) logger.log = MagicMock() os.path.join = MagicMock(return_value='String') Core.vpn_is_running = MagicMock(return_value=True) plebnet_settings.Init.vpn_config_path = MagicMock() plebnet_settings.Init.vpn_own_prefix = MagicMock() plebnet_settings.Init.vpn_credentials_name = MagicMock( return_value='cred_name') plebnet_settings.Init.vpn_config_name = MagicMock( return_value='config_name') os.listdir = MagicMock(return_value=[]) os.path.isfile = MagicMock(return_value=False) os.path.expanduser = MagicMock() assert Core.check_vpn_install() Core.vpn_is_running = MagicMock(return_value=False) os.path.isfile = MagicMock(return_value=True) Core.install_vpn = MagicMock(return_value=False) plebnet_settings.Init.vpn_installed = MagicMock(return_value=False) self.assertFalse(Core.check_vpn_install()) Core.install_vpn = MagicMock(return_value=True) assert Core.check_vpn_install() os.listdir = MagicMock( return_value=['child_pre0config_name', 'child_pre0cred_name']) plebnet_settings.Init.vpn_child_prefix = MagicMock( return_value='child_pre') os.path.isfile = MagicMock(return_value=False) self.assertFalse(Core.check_vpn_install()) plebnet_settings.Init.vpn_installed = self.vpn_installed logger.log = self.log os.path.join = self.ospath plebnet_settings.Init.vpn_config_path = self.path plebnet_settings.Init.vpn_own_prefix = self.pre plebnet_settings.Init.vpn_credentials_name = self.cre plebnet_settings.Init.vpn_config_name = self.nam os.listdir = self.lis os.path.isfile = self.isf Core.install_vpn = self.civ plebnet_settings.Init.vpn_child_prefix = self.cpr os.path.expanduser = self.usr Core.vpn_is_running = self.vpn_running
class TestQTable(unittest.TestCase): qtable = {} providers = {} option = {} @mock.patch('plebnet.controllers.cloudomate_controller.get_vps_providers', return_value=CaseInsensitiveDict( {'blueangelhost': blueAngel.BlueAngelHost})) @mock.patch('plebnet.controllers.cloudomate_controller.options', return_value=[ VpsOption(name='Advanced', storage=2, cores=2, memory=2, bandwidth="mock", connection="1", price=100.0, purchase_url="mock"), VpsOption(name='Basic Plan', storage=2, cores=2, memory=2, bandwidth="mock", connection="1", price=10.0, purchase_url="mock") ]) def setUp(self, mock1, mock2): self.qtable = QTable() self.providers = cloudomate_controller.get_vps_providers() def tearDown(self): del self.qtable del self.providers @mock.patch('plebnet.controllers.cloudomate_controller.get_vps_providers', return_value=CaseInsensitiveDict( {'blueangelhost': blueAngel.BlueAngelHost})) @mock.patch('plebnet.controllers.cloudomate_controller.options', return_value=[ VpsOption(name='Advanced', storage=2, cores=2, memory=2, bandwidth="mock", connection="1", price=100.0, purchase_url="mock"), VpsOption(name='Basic Plan', storage=2, cores=2, memory=2, bandwidth="mock", connection="1", price=10.0, purchase_url="mock") ]) def test_init_providers(self, mock1, mock2): assert (len(self.qtable.providers_offers) == 0) self.qtable.init_providers_offers(self.providers) assert (len(self.qtable.providers_offers) > 0) @mock.patch('plebnet.controllers.cloudomate_controller.get_vps_providers', return_value=CaseInsensitiveDict( {'blueangelhost': blueAngel.BlueAngelHost})) @mock.patch('plebnet.controllers.cloudomate_controller.options', return_value=[ VpsOption(name='Advanced', storage=2, cores=2, memory=2, bandwidth="mock", connection="1", price=100.0, purchase_url="mock"), VpsOption(name='Basic Plan', storage=2, cores=2, memory=2, bandwidth="mock", connection="1", price=10.0, purchase_url="mock") ]) def test_init_qtable_and_environment(self, mock1, mock2): assert (len(self.qtable.environment) == 0) self.qtable.init_qtable_and_environment(self.providers) assert (len(self.qtable.environment) > 0) assert (len(self.qtable.qtable) > 0) @mock.patch('plebnet.controllers.cloudomate_controller.get_vps_providers', return_value=CaseInsensitiveDict( {'blueangelhost': blueAngel.BlueAngelHost})) @mock.patch('plebnet.controllers.cloudomate_controller.options', return_value=[ VpsOption(name='Advanced', storage=2, cores=2, memory=2, bandwidth="mock", connection="1", price=100.0, purchase_url="mock"), VpsOption(name='Basic Plan', storage=2, cores=2, memory=2, bandwidth="mock", connection="1", price=10.0, purchase_url="mock") ]) def test_calculate_measure(self, mock1, mock2): provider_offer = ProviderOffer(provider_name="mock provider", name="mock name", bandwidth=3, price=5, memory=2) assert (self.qtable.calculate_measure(provider_offer) == 0.024) @mock.patch('plebnet.controllers.cloudomate_controller.get_vps_providers', return_value=CaseInsensitiveDict( {'blueangelhost': blueAngel.BlueAngelHost})) @mock.patch('plebnet.controllers.cloudomate_controller.options', return_value=[ VpsOption(name='Advanced', storage=2, cores=2, memory=2, bandwidth="mock", connection="1", price=100.0, purchase_url="mock"), VpsOption(name='Basic Plan', storage=2, cores=2, memory=2, bandwidth="mock", connection="1", price=10.0, purchase_url="mock") ]) def test_calculate_measure_unmetric_bandwidth(self, mock1, mock2): self.qtable.init_qtable_and_environment(self.providers) assert (self.qtable.calculate_measure( self.qtable.providers_offers[1]) == 0.01) @mock.patch('plebnet.controllers.cloudomate_controller.get_vps_providers', return_value=CaseInsensitiveDict( {'blueangelhost': blueAngel.BlueAngelHost})) @mock.patch('plebnet.controllers.cloudomate_controller.options', return_value=[ VpsOption(name='Advanced', storage=2, cores=2, memory=2, bandwidth="mock", connection="1", price=100.0, purchase_url="mock"), VpsOption(name='Basic Plan', storage=2, cores=2, memory=2, bandwidth="mock", connection="1", price=10.0, purchase_url="mock") ]) def test_update_environment_positive(self, mock1, mock2): blue_angel_offers = cloudomate_controller.options( self.providers["blueangelhost"]) self.qtable.self_state = VPSState("blueangelhost", blue_angel_offers[0].name) self.qtable.init_qtable_and_environment(self.providers) environment_copy = copy.deepcopy(self.qtable.environment) vps_options_list = cloudomate_controller.options(self.providers) vps_option = vps_options_list[0] provider_offer_ID = str(self.providers.keys()[0]).lower() + "_" + str( vps_option.name).lower() provider_offer_ID_other = str( self.providers.keys()[0]).lower() + "_" + str( vps_options_list[1].name).lower() self.qtable.update_environment(provider_offer_ID, True, 0) assert (environment_copy != self.qtable.environment) assert (environment_copy[provider_offer_ID_other] [provider_offer_ID_other] == self.qtable. environment[provider_offer_ID_other][provider_offer_ID_other]) assert (environment_copy[provider_offer_ID][provider_offer_ID] < self.qtable.environment[provider_offer_ID][provider_offer_ID]) assert (self.qtable.environment[provider_offer_ID][provider_offer_ID] == 0.4) @mock.patch('plebnet.controllers.cloudomate_controller.get_vps_providers', return_value=CaseInsensitiveDict( {'blueangelhost': blueAngel.BlueAngelHost})) @mock.patch('plebnet.controllers.cloudomate_controller.options', return_value=[ VpsOption(name='Advanced', storage=2, cores=2, memory=2, bandwidth="mock", connection="1", price=100.0, purchase_url="mock"), VpsOption(name='Basic Plan', storage=2, cores=2, memory=2, bandwidth="mock", connection="1", price=10.0, purchase_url="mock") ]) def test_update_environment_negative(self, mock1, mock2): blue_angel_offers = cloudomate_controller.options( self.providers["blueangelhost"]) self.qtable.self_state = VPSState("blueangelhost", blue_angel_offers[0].name) self.qtable.init_qtable_and_environment(self.providers) environment_copy = copy.deepcopy(self.qtable.environment) vps_options_list = cloudomate_controller.options(self.providers) vps_option = vps_options_list[0] provider_offer_ID = str(self.providers.keys()[0]).lower() + "_" + str( vps_option.name).lower() provider_offer_ID_other = str( self.providers.keys()[0]).lower() + "_" + str( vps_options_list[1].name).lower() self.qtable.update_environment(provider_offer_ID, False, 0) assert (environment_copy != self.qtable.environment) assert (environment_copy[provider_offer_ID][provider_offer_ID] > self.qtable.environment[provider_offer_ID][provider_offer_ID]) assert (environment_copy[provider_offer_ID][provider_offer_ID_other] == self.qtable.environment[provider_offer_ID] [provider_offer_ID_other]) assert (self.qtable.environment[provider_offer_ID][provider_offer_ID] == -0.4) @mock.patch('plebnet.controllers.cloudomate_controller.get_vps_providers', return_value=CaseInsensitiveDict( {'blueangelhost': blueAngel.BlueAngelHost})) @mock.patch('plebnet.controllers.cloudomate_controller.options', return_value=[ VpsOption(name='Advanced', storage=2, cores=2, memory=2, bandwidth="mock", connection="1", price=100.0, purchase_url="mock"), VpsOption(name='Basic Plan', storage=2, cores=2, memory=2, bandwidth="mock", connection="1", price=10.0, purchase_url="mock") ]) def test_update_values_positive(self, mock1, mock2): blue_angel_offers = cloudomate_controller.options( self.providers["blueangelhost"]) self.qtable.self_state = VPSState("blueangelhost", blue_angel_offers[0].name) self.qtable.init_qtable_and_environment(self.providers) self.qtable.init_alpha_and_beta() qtable_copy = copy.deepcopy(self.qtable.qtable) vps_options_list = cloudomate_controller.options(self.providers) vps_option = vps_options_list[0] provider_offer_ID = str(self.providers.keys()[0]).lower() + "_" + str( vps_option.name).lower() provider_offer_ID_other = str( self.providers.keys()[0]).lower() + "_" + str( vps_options_list[1].name).lower() self.qtable.update_qtable([], provider_offer_ID, True, 0) assert (qtable_copy != self.qtable.qtable) assert (qtable_copy[provider_offer_ID_other][provider_offer_ID] < self.qtable.qtable[provider_offer_ID_other][provider_offer_ID]) assert (round( self.qtable.qtable[provider_offer_ID_other][provider_offer_ID], 2) == 0.64) @mock.patch('plebnet.controllers.cloudomate_controller.get_vps_providers', return_value=CaseInsensitiveDict( {'blueangelhost': blueAngel.BlueAngelHost})) @mock.patch('plebnet.controllers.cloudomate_controller.options', return_value=[ VpsOption(name='Advanced', storage=2, cores=2, memory=2, bandwidth="mock", connection="1", price=100.0, purchase_url="mock"), VpsOption(name='Basic Plan', storage=2, cores=2, memory=2, bandwidth="mock", connection="1", price=10.0, purchase_url="mock") ]) def test_update_qtable_from_remote_qtable(self, mock1, mock2): blue_angel_offers = cloudomate_controller.options( self.providers["blueangelhost"]) self.qtable.self_state = VPSState("blueangelhost", blue_angel_offers[0].name) self.qtable.init_qtable_and_environment(self.providers) self.qtable.init_alpha_and_beta() qtable2 = QTable() qtable2.self_state = VPSState("blueangelhost", blue_angel_offers[1].name) qtable2.init_qtable_and_environment(self.providers) qtable2.init_alpha_and_beta() vps_options_list = cloudomate_controller.options(self.providers) vps_option = vps_options_list[0] provider_offer_ID = str(self.providers.keys()[0]).lower() + "_" + str( vps_option.name).lower() provider_offer_ID_other = str( self.providers.keys()[0]).lower() + "_" + str( vps_options_list[1].name).lower() self.qtable.update_qtable([], provider_offer_ID_other, True, 0.5) self.qtable.set_self_state( VPSState("blueangelhost", blue_angel_offers[1].name)) qtable2.update_qtable([], provider_offer_ID_other, True, 0.6) qtable2.set_self_state( VPSState("blueangelhost", blue_angel_offers[1].name)) self.qtable.update_qtable([qtable2.qtable], provider_offer_ID, True, 0.3) assert (qtable2.qtable != self.qtable.qtable) assert (qtable2.qtable[provider_offer_ID_other][provider_offer_ID] < self.qtable.qtable[provider_offer_ID_other][provider_offer_ID]) @mock.patch('plebnet.controllers.cloudomate_controller.get_vps_providers', return_value=CaseInsensitiveDict( {'blueangelhost': blueAngel.BlueAngelHost})) @mock.patch('plebnet.controllers.cloudomate_controller.options', return_value=[ VpsOption(name='Advanced', storage=2, cores=2, memory=2, bandwidth="mock", connection="1", price=100.0, purchase_url="mock"), VpsOption(name='Basic Plan', storage=2, cores=2, memory=2, bandwidth="mock", connection="1", price=10.0, purchase_url="mock") ]) def test_init_alphatable(self, mock1, mock2): blue_angel_offers = cloudomate_controller.options( self.providers["blueangelhost"]) self.qtable.self_state = VPSState("blueangelhost", blue_angel_offers[0].name) self.qtable.init_qtable_and_environment(self.providers) self.qtable.init_alpha_and_beta() vps_options_list = cloudomate_controller.options(self.providers) vps_option = vps_options_list[0] provider_offer_ID = str(self.providers.keys()[0]).lower() + "_" + str( vps_option.name).lower() provider_offer_ID_other = str( self.providers.keys()[0]).lower() + "_" + str( vps_options_list[1].name).lower() assert (self.qtable.alphatable[provider_offer_ID_other] [provider_offer_ID] == 0.8) assert (self.qtable.betatable[provider_offer_ID_other] [provider_offer_ID] == 0.2) assert (self.qtable.number_of_updates[provider_offer_ID_other] [provider_offer_ID] == 0) @mock.patch('plebnet.controllers.cloudomate_controller.get_vps_providers', return_value=CaseInsensitiveDict( {'blueangelhost': blueAngel.BlueAngelHost})) @mock.patch('plebnet.controllers.cloudomate_controller.options', return_value=[ VpsOption(name='Advanced', storage=2, cores=2, memory=2, bandwidth="mock", connection="1", price=100.0, purchase_url="mock"), VpsOption(name='Basic Plan', storage=2, cores=2, memory=2, bandwidth="mock", connection="1", price=10.0, purchase_url="mock") ]) def test_update_values_negative(self, mock1, mock2): blue_angel_offers = cloudomate_controller.options( self.providers["blueangelhost"]) self.qtable.self_state = VPSState("blueangelhost", blue_angel_offers[0].name) self.qtable.init_qtable_and_environment(self.providers) self.qtable.init_alpha_and_beta() qtable_copy = copy.deepcopy(self.qtable.qtable) vps_options_list = cloudomate_controller.options(self.providers) vps_option = vps_options_list[1] provider_offer_ID = str(self.providers.keys()[0]).lower() + "_" + str( vps_option.name).lower() provider_offer_ID_other = str( self.providers.keys()[0]).lower() + "_" + str( vps_options_list[1].name).lower() self.qtable.update_qtable([], provider_offer_ID_other, False) assert (qtable_copy != self.qtable.qtable) assert (qtable_copy[provider_offer_ID_other][provider_offer_ID_other] > self.qtable.qtable[provider_offer_ID_other] [provider_offer_ID_other]) assert (round( self.qtable.qtable[provider_offer_ID_other][provider_offer_ID], 2) == -0.32) @mock.patch('plebnet.controllers.cloudomate_controller.get_vps_providers', return_value=CaseInsensitiveDict( {'blueangelhost': blueAngel.BlueAngelHost})) @mock.patch('plebnet.controllers.cloudomate_controller.options', return_value=[ VpsOption(name='Advanced', storage=2, cores=2, memory=2, bandwidth="mock", connection="1", price=100.0, purchase_url="mock"), VpsOption(name='Basic Plan', storage=2, cores=2, memory=2, bandwidth="mock", connection="1", price=10.0, purchase_url="mock") ]) def test_find_provider(self, mock1, mock2): self.qtable.init_qtable_and_environment(self.providers) self.qtable.set_self_state(VPSState("blueangelhost", "Advanced")) provider_name = self.qtable.find_provider("blueangelhost_basic plan") assert (provider_name == "blueangelhost") @mock.patch('plebnet.controllers.cloudomate_controller.get_vps_providers', return_value=CaseInsensitiveDict( {'blueangelhost': blueAngel.BlueAngelHost})) @mock.patch('plebnet.controllers.cloudomate_controller.options', return_value=[ VpsOption(name='Advanced', storage=2, cores=2, memory=2, bandwidth="mock", connection="1", price=100.0, purchase_url="mock"), VpsOption(name='Basic Plan', storage=2, cores=2, memory=2, bandwidth="mock", connection="1", price=10.0, purchase_url="mock") ]) def test_kth_score(self, mock1, mock2): self.qtable.init_qtable_and_environment(self.providers) self.qtable.set_self_state(VPSState("blueangelhost", "Advanced")) self.qtable.init_alpha_and_beta() vps_options_list = cloudomate_controller.options(self.providers) vps_option = vps_options_list[0] provider_offer_ID = str(self.providers.keys()[0]).lower() + "_" + str( vps_option.name).lower() self.qtable.update_qtable([], provider_offer_ID, True, 0) assert (self.qtable.get_kth_score(self.providers, 1) == 0) @mock.patch('plebnet.controllers.cloudomate_controller.get_vps_providers', return_value=CaseInsensitiveDict( {'blueangelhost': blueAngel.BlueAngelHost})) @mock.patch('plebnet.controllers.cloudomate_controller.options', return_value=[ VpsOption(name='Advanced', storage=2, cores=2, memory=2, bandwidth="mock", connection="1", price=100.0, purchase_url="mock"), VpsOption(name='Basic Plan', storage=2, cores=2, memory=2, bandwidth="mock", connection="1", price=10.0, purchase_url="mock") ]) def test_choose_k_option(self, mock1, mock2): self.qtable.init_qtable_and_environment(self.providers) self.qtable.set_self_state(VPSState("blueangelhost", "Advanced")) self.qtable.init_alpha_and_beta() vps_options_list = cloudomate_controller.options(self.providers) vps_option = vps_options_list[0] provider_offer_ID = str(self.providers.keys()[0]).lower() + "_" + str( vps_option.name).lower() self.qtable.update_qtable([], provider_offer_ID, True, 0) option = self.qtable.choose_k_option(self.providers, 0) assert (option["option_name"] == "Advanced") assert (option["price"] == 100.0) @mock.patch('plebnet.settings.plebnet_settings.Init.irc_nick', return_value="plebbot1") @mock.patch('plebnet.controllers.cloudomate_controller.get_vps_providers', return_value=CaseInsensitiveDict( {'blueangelhost': blueAngel.BlueAngelHost})) @mock.patch('plebnet.controllers.cloudomate_controller.options', return_value=[ VpsOption(name='Advanced', storage=2, cores=2, memory=2, bandwidth="mock", connection="1", price=100.0, purchase_url="mock"), VpsOption(name='Basic Plan', storage=2, cores=2, memory=2, bandwidth="mock", connection="1", price=10.0, purchase_url="mock") ]) def test_choose_option(self, mock1, mock2, mock3): self.qtable.init_qtable_and_environment(self.providers) self.qtable.set_self_state(VPSState("blueangelhost", "Advanced")) random.expovariate = MagicMock(return_value=0.55) option = self.qtable.choose_option(self.providers) assert (option["option_name"] == "Basic Plan") assert (option["price"] == 10.0) @mock.patch('plebnet.settings.plebnet_settings.Init.irc_nick', return_value="plebbot1") def test_create_initial_tree(self, mock1): self.qtable.set_self_state(VPSState("blueangelhost", "Advanced")) self.qtable.create_initial_tree() assert (self.qtable.tree == "plebbot1") def test_get_no_replications(self): self.qtable.tree = "plebbot1.2.3" self.assertEqual(self.qtable.get_no_replications(), 3) def get_ID(self, provider_offer): return str(provider_offer.provider_name).lower() + "_" + str( provider_offer.name).lower()