Esempio n. 1
0
class SensorsTest(unittest.TestCase):
    def setUp(self):
        # ACTIONS
        self.sensors = Sensors()

    def test_add_success(self):
        self.sensors = Sensors()
        self.sensors.add(
            name='SenseTmpDirState',
            shell=
            'if [ -d "/tmp/goap_tmp" ]; then echo -n "exist"; else echo -n "not_exist"; fi',
            binding='tmp_dir_state')
        self.sensors.add(
            name='SenseTmpDirContent',
            shell=
            '[ -f /tmp/goap_tmp/.token ] && echo -n "token_found" || echo -n "token_not_found"',
            binding='tmp_dir_content')
        assert 'SenseTmpDirState' == str(
            self.sensors.get(name='SenseTmpDirState'))
        assert 'SenseTmpDirContent' == str(
            self.sensors.get(name='SenseTmpDirContent'))

    def test_remove_sensor_success(self):
        assert self.sensors.remove(name='SenseTmpDirContent') is True

    def test_remove_sensor_error(self):
        assert self.sensors.remove(name='CreateAPP') is False
Esempio n. 2
0
def setup_sensors():
    sensors = Sensors()
    sensors.add(
        name='SenseTmpDirState',
        shell=
        'if [ -d "/tmp/goap_tmp" ]; then echo -n "exist"; else echo -n "not_exist"; fi',
        binding='tmp_dir_state')
    sensors.add(
        name='SenseTmpDirContent',
        shell=
        '[ -f /tmp/goap_tmp/.token ] && echo -n "token_found" || echo -n "token_not_found"',
        binding='tmp_dir_content')
    return sensors
Esempio n. 3
0
class AutomatonTest(unittest.TestCase):
    @staticmethod
    def __reset_environment():
        if path.isdir('/tmp/goap_tmp'):
            subprocess.call(['rm', '-rf', '/tmp/goap_tmp'])

    def __print(self):
        self.print.pprint(
            'Acknowledge world: {}, Action Plan: {}, Result: {}'.format(
                self.automaton.world_state, self.automaton.action_plan,
                self.automaton.actions_response))

    def setUp(self):
        self.print = PrettyPrinter(indent=4)

        self.goal = {
            "tmp_dir_state": "exist",
            "tmp_dir_content": "token_found",
        }
        self.actions = Actions()
        self.sensors = Sensors()
        self.sensors.add(
            name='SenseTmpDirState',
            shell=
            'if [ -d "/tmp/goap_tmp" ]; then echo -n "exist"; else echo -n "not_exist"; fi',
            binding='tmp_dir_state')
        self.sensors.add(
            name='SenseTmpDirContent',
            shell=
            '[ -f /tmp/goap_tmp/.token ] && echo -n "token_found" || echo -n "token_not_found"',
            binding='tmp_dir_content')
        self.actions.add(name='CreateTmpDir',
                         pre_conditions={
                             'tmp_dir_state': 'not_exist',
                             'tmp_dir_content': 'token_not_found'
                         },
                         effects={
                             'tmp_dir_state': 'exist',
                             'tmp_dir_content': 'token_not_found'
                         },
                         shell='mkdir -p /tmp/goap_tmp')
        self.actions.add(name='CreateToken',
                         pre_conditions={
                             'tmp_dir_state': 'exist',
                             'tmp_dir_content': 'token_not_found'
                         },
                         effects={
                             'tmp_dir_state': 'exist',
                             'tmp_dir_content': 'token_found'
                         },
                         shell='touch /tmp/goap_tmp/.token')
        world_state_matrix = {
            "tmp_dir_state": 'Unknown',
            "tmp_dir_content": 'Unknown',
        }
        self.automaton = Automaton(name='directory_watcher',
                                   actions=self.actions,
                                   sensors=self.sensors,
                                   world_state=world_state_matrix)

    def test_sensing(self):
        self.__reset_environment()
        self.automaton.input_goal(self.goal)
        self.automaton.sense()
        assert self.automaton.world_state == {
            'tmp_dir_state': 'not_exist',
            'tmp_dir_content': 'token_not_found'
        }

    def test_planning(self):
        create_tmp_dir = self.actions.get('CreateTmpDir')
        create_token = self.actions.get('CreateToken')
        self.__reset_environment()
        self.automaton.input_goal(self.goal)
        self.automaton.sense()
        self.automaton.plan()
        action_plan = [
            action[2]['object'] for action in self.automaton.action_plan
        ]
        assert action_plan == [create_tmp_dir, create_token]

    def test_acting(self):
        self.__reset_environment()
        self.automaton.input_goal(self.goal)
        self.automaton.sense()
        self.automaton.plan()
        self.automaton.act()
        assert path.isdir('/tmp/goap_tmp') and path.isfile(
            '/tmp/goap_tmp/.token')
Esempio n. 4
0
 aws_actions.add(
     name='CreateDB',
     pre_conditions={'vpc_state': 'available', 'db_state': 'unavailable', 'app_state': 'unavailable'},
     effects={'vpc_state': 'available', 'db_state': 'available', 'app_state': 'unavailable'},
     shell='echo "db created"'
 )
 aws_actions.add(
     name='CreateApp',
     pre_conditions={'vpc_state': 'available', 'db_state': 'available', 'app_state': 'unavailable'},
     effects={'vpc_state': 'available', 'db_state': 'available', 'app_state': 'running'},
     shell='echo "app created" > /tmp/CreateApp.out'
 )
 aws_sensors = Sensors()
 aws_sensors.add(
     name='FindProjectVPC',
     shell='aws ec2 describe-vpcs --filters "Name=tag-key,Values=Name","Name=tag-value,Values=vpc_plataformas_stg" --query "Vpcs[].State" --output text',
     binding='vpc_state'
 )
 aws_sensors.add(
     name='FindProjectDB',
     shell='aws rds describe-db-instances --filters "Name=db-instance-id,Values=rds-oraculo" --query "DBInstances[].DBInstanceStatus" --output text',
     binding='db_state'
 )
 aws_sensors.add(
     name='CheckAppState',
     shell='echo "unavailable"',
     binding='app_state'
 )
 ai = Automaton(name='infra_builder', actions=aws_actions, sensors=aws_sensors, world_state=world_state_matrix)
 # Control
 # what is the environment status? what does the sensors return? ai has a goal?
Esempio n. 5
0
 aws_actions.add(name='CreateApp',
                 pre_conditions={
                     'vpc_state': 'available',
                     'db_state': 'available',
                     'app_state': 'unavailable'
                 },
                 effects={
                     'vpc_state': 'available',
                     'db_state': 'available',
                     'app_state': 'running'
                 },
                 shell='echo "app created" > /tmp/CreateApp.out')
 aws_sensors = Sensors()
 aws_sensors.add(
     name='FindProjectVPC',
     shell=
     'aws ec2 describe-vpcs --filters "Name=tag-key,Values=Name","Name=tag-value,Values=vpc_plataformas_stg" --query "Vpcs[].State" --output text',
     binding='vpc_state')
 aws_sensors.add(
     name='FindProjectDB',
     shell=
     'aws rds describe-db-instances --filters "Name=db-instance-id,Values=rds-oraculo" --query "DBInstances[].DBInstanceStatus" --output text',
     binding='db_state')
 aws_sensors.add(name='CheckAppState',
                 shell='echo "unavailable"',
                 binding='app_state')
 ai = Automaton(name='infra_builder',
                actions=aws_actions,
                sensors=aws_sensors,
                world_state=world_state_matrix)
 # Control