def test_new_list_none(self): ''' try to create a valid new list with action none ''' self.args.new = True self.args.delete = False self.args.list = ['a_new_list'] self.args.type = 'block' self.args.action = 'none' self.args.match = None self.args.variable = None self.args.block_length = None # create a new environment env = Environment(self.args) # create a new list Lists(self.args, env) self.assertEqual(len(env.config['lists']), 1) self.assertEqual(env.config['lists'][0]['name'], 'a_new_list') self.assertEqual(env.config['lists'][0]['type'], 'block') self.assertEqual(env.config['lists'][0]['action_none'], True) self.assertEqual(env.config['lists'][0]['action_block'], False)
def test_load_config(self): ''' test init and load of a config file ''' # create a new config file Environment(self.args) # load an existing config file self.args.init = False env = Environment(self.args) # ensure apikey and config are populated self.assertEqual(env.apikey, 'MYTESTAPIKEY') self.assertTrue(env.config['services']) self.assertFalse(env.config['lists']) self.assertEqual(env.config['log'], '')
def test_load_config_override_services(self): ''' test runtime override of args.services ''' # create a new config file Environment(self.args) # load an existing config file and replace services in running config self.args.init = False self.args.service = ['SERVICE1', 'SERVICE2'] env = Environment(self.args) # ensure override services are targeted self.assertEqual( len(env.config['services']), 2 )
def test_commit(self): ''' test local portion of commit operations (create env.to_remote) ''' self.args.new = True self.args.delete = False self.args.list = ['a_new_list'] self.args.type = 'block' self.args.action = 'block' self.args.match = 'exact' self.args.variable = None self.args.block_length = None # create a new environment, populate it with our list env = Environment(self.args) env.mock_remote = True Lists(self.args, env) # add items to the list self.args.add = True self.args.remove = False self.args.clean = False self.args.removeall = False self.args.item = ['!10.0.0.0/8'] self.args.file = None Items(self.args, env) self.args.item = ['2a04:4e42:10::313/128'] Items(self.args, env) # commit to 'remote' service State().commit(env, 'remote') # ensure SERVICEID is copied into env.to_remote self.assertEqual(env.to_remote['service_id'], 'SERVICEID') # ensure snippet contains config block and logic for our list self.assertRegex( env.to_remote['snippet']['content'], r'\n#fastlyblocklist_list {"name": "a_new_list".*"items": \[\]}\n') self.assertRegex( env.to_remote['snippet']['content'], r'\n\s*if \(var\.ip ~ fastlyblocklist_a_new_list\) {\n') # ensure our list is converted to ACL self.assertEqual(env.to_remote['acls'][0]['name'], 'fastlyblocklist_a_new_list') self.assertEqual(env.to_remote['acls'][0]['items'][0]['ip'], '10.0.0.0') self.assertEqual(env.to_remote['acls'][0]['items'][0]['negated'], '1') self.assertEqual(env.to_remote['acls'][0]['items'][0]['subnet'], 8) self.assertEqual(env.to_remote['acls'][0]['items'][1]['ip'], '2a04:4e42:10::313') self.assertEqual(env.to_remote['acls'][0]['items'][1]['negated'], '0') self.assertEqual(env.to_remote['acls'][0]['items'][1]['subnet'], 128)
def test_init_service_defaults(self): ''' ensure service config defaults are set during init ''' # create a new environment env = Environment(self.args) # ensure service defaults self.assertEqual(env.config['services'][0]['type'], 'recv') self.assertEqual(env.config['services'][0]['priority'], '10') self.assertEqual(env.config['services'][0]['options']['edge_only'], True) self.assertEqual(env.config['services'][0]['options']['var_ip'], 'client.ip')
def test_init(self): ''' test init of a new config file ''' # create a new environment env = Environment(self.args) # ensure apikey and config are populated self.assertEqual(env.apikey, 'MYTESTAPIKEY') self.assertTrue(env.config['services']) self.assertFalse(env.config['lists']) self.assertEqual(env.config['log'], '')
def test_save_config(self): ''' test saving a config file ''' # create a new config file Environment(self.args) # load existing config file, change something, and save self.args.init = False self.args.service = ['SERVICE1', 'SERVICE2'] Environment(self.args).save_config() # load the modified config file self.args.service = [] env = Environment(self.args) # ensure updated config is loaded self.assertEqual( len(env.config['services']), 2 )
def test_load_config_auto_generate(self): ''' test init of a new config file where args.init is not provided ''' # auto init a config file when one doesn't exist self.args.init = False env = Environment(self.args) # ensure apikey and config are populated self.assertEqual(env.apikey, 'MYTESTAPIKEY') self.assertTrue(env.config['services']) self.assertFalse(env.config['lists']) self.assertEqual(env.config['log'], '')
def test_save(self): ''' test create, save, and load of a config file ''' # create a new config file Environment(self.args) # load existing config file, change something self.args.init = False self.args.save = True self.args.service = ['SERVICE1', 'SERVICE2'] env = Environment(self.args) # save the changes using State().save() State().save(env) # load the modified config file self.args.service = [] env = Environment(self.args) # ensure updated config is loaded self.assertEqual(len(env.config['services']), 2)
def test_init_config_exists(self): ''' test init of a new config file where another exists ''' # create a 'valid' config file with open('tests.blocklist', 'w') as file_apikey: file_apikey.write('{}') # try to create a new config over existing self.args.force = False with self.assertRaisesRegex( SystemExit, "config file exists" ): Environment(self.args)
def test_new_list_var_bad(self): ''' try to create a new var list where no args.variable provided ''' self.args.new = True self.args.delete = False self.args.list = ['a_new_list'] self.args.type = 'var' self.args.action = 'block' self.args.match = 'exact' self.args.variable = None self.args.block_length = None # create a new environment env = Environment(self.args) with self.assertRaisesRegex(SystemExit, "no list variable defined"): Lists(self.args, env)
def test_init_config_exists_force(self): ''' test init of a new config file where another exists and args.force is provided ''' # create a 'valid' config file with open('tests.blocklist', 'w') as file_apikey: file_apikey.write('{}') # force create a new config over existing self.args.force = True env = Environment(self.args) # ensure apikey and config are populated self.assertEqual(env.apikey, 'MYTESTAPIKEY') self.assertTrue(env.config['services']) self.assertFalse(env.config['lists']) self.assertEqual(env.config['log'], '')
def test_new_list_temp(self): ''' try to create a valid new temp list ''' self.args.new = True self.args.delete = False self.args.list = ['a_new_list'] self.args.type = 'temp' self.args.action = 'block' self.args.match = None self.args.variable = None self.args.block_length = 600 # create a new environment env = Environment(self.args) Lists(self.args, env) self.assertEqual(len(env.config['lists']), 1) self.assertEqual(env.config['lists'][0]['type'], 'temp') self.assertEqual(env.config['lists'][0]['block_length'], 600)
def test_new_list_var(self): ''' try to create a valid new var list ''' self.args.new = True self.args.delete = False self.args.list = ['a_new_list'] self.args.type = 'var' self.args.action = 'block' self.args.match = 'exact' self.args.variable = 'req.whatever' self.args.block_length = None # create a new environment env = Environment(self.args) Lists(self.args, env) self.assertEqual(len(env.config['lists']), 1) self.assertEqual(env.config['lists'][0]['type'], 'var') self.assertEqual(env.config['lists'][0]['match'], 'exact') self.assertEqual(env.config['lists'][0]['variable'], 'req.whatever')
def test_delete_list(self): ''' try to create and delete a valid list ''' self.args.new = True self.args.delete = False self.args.list = ['a_new_list'] self.args.type = 'var' self.args.action = 'block' self.args.match = 'exact' self.args.variable = 'req.whatever' self.args.block_length = None # create a new environment env = Environment(self.args) Lists(self.args, env) self.args.new = False self.args.delete = True Lists(self.args, env) self.assertFalse(env.config['lists'])
def test_delete_list_bad(self): ''' try to delete a list where no args.list provided ''' self.args.new = True self.args.delete = False self.args.list = ['a_new_list'] self.args.type = 'var' self.args.action = 'block' self.args.match = 'exact' self.args.variable = 'req.whatever' self.args.block_length = None # create a new environment env = Environment(self.args) Lists(self.args, env) self.args.new = False self.args.delete = True self.args.list = [] with self.assertRaisesRegex(SystemExit, r"no list name\(s\) defined"): Lists(self.args, env)
def __init__(self, thread_name, thread_type, parameter_server, rhost): self.environment = Environment( thread_name, thread_type, parameter_server, rhost) self.thread_name = thread_name self.thread_type = thread_type self.util = Utilty()
def test_sync(self): ''' test local portion of sync operations (env.from_remote to env.config) ''' # create a new environment env = Environment(self.args) env.mock_remote = True # create an env.from_remote object env.from_remote = { 'service_id': 'REMOTESERVICEID', 'version': 1, 'snippet': { 'name': 'REMOTE_SNIPPET_NAME', 'type': 'recv', 'priority': 10, 'content': '#fastlyblocklist_list {"name": "my_test_list", ' '"type": "block", "action_block": true, ' '"action_log": true, "action_none": false, ' '"match": "exact", "variable": null, ' '"block_length": 600, "items": []}\n' }, 'acls': [{ 'name': 'fastlyblocklist_my_test_list', 'items': [{ 'comment': '', 'subnet': 8, 'service_id': 'REMOTESERVICEID', 'negated': '1', 'deleted_at': None, 'ip': '10.0.0.0' }, { 'comment': '', 'subnet': None, 'service_id': 'REMOTESERVICEID', 'negated': '0', 'deleted_at': None, 'ip': '2a04:4e42:10::313' }] }], 'dicts': [] } # sync env.from_remote to local env.config State().sync(env, 'remote') # ensure remote made it into our local config self.assertEqual(env.config['services'][0]['id'], 'REMOTESERVICEID') self.assertEqual(env.config['services'][0]['snippet_name'], 'REMOTE_SNIPPET_NAME') self.assertEqual(env.config['lists'][0]['name'], 'my_test_list') self.assertEqual(env.config['lists'][0]['type'], 'block') self.assertTrue(env.config['lists'][0]['action_block']) self.assertEqual(env.config['lists'][0]['items'][0], '!10.0.0.0/8') self.assertEqual(env.config['lists'][0]['items'][1], '2a04:4e42:10::313/128')
action = None while action is None: try: action = input('what should I do? {} >>> '.format(actions)) if action == 'quit': return True agent.step(action) except ValueError: action = None return False if __name__ == '__main__': # define the gridworld environment env = Environment([[-10, 0, 0, 50], [0, 10, 100, 0, -100, 500], [0, 0, None, 10, None, -10, None], [None, 0, 5, 10, None, 5, 0]]) # start at a random position pos = random.choice(env.positions) # simple reward function def reward(state): return env.value(state) # try discount=0.1 and discount=0.9 DISCOUNT = 0.1 LEARNING_RATE = 1 agent = QLearner(pos, env, reward,