def test_manual_update(self):
        """
        Test that the manual update actually updates the state in the cache
        """
        path = '/foo'
        foo = 'foo'
        bar = 'bar'

        state = ApplicationState(application_host=foo, configuration_path=path)
        cache = self._create_app_state_cache()
        cache._cache.update({path: state.to_dictionary()})

        self.zoo_keeper.get(self.configuration.override_node).AndReturn(
            ('{}', None))
        self.zoo_keeper.set(self.configuration.override_node,
                            '{"/foo": {"application_host": "bar"}}')
        self.zoo_keeper.get(self.configuration.override_node).AndReturn(
            ('{"/foo": {"application_host": "bar"}}', None))
        self.time_estimate_cache.get_graphite_data('/foo/bar/head').AndReturn(
            {})
        self.mox.ReplayAll()

        # test that the state's attribute actually changes
        cache.manual_update(path, 'application_host', bar)
        result = cache._get_existing_attribute(path, 'application_host')

        self.assertEqual(bar, result)
Exemple #2
0
    def _walk(self, path, result):
        """
        :type path: str
        :type result: zoom.www.messages.application_states.ApplicationStatesMessage
        """
        try:
            children = self._zoo_keeper.get_children(path,
                                                     watch=self._on_update)

            if children:
                for child in children:
                    self._walk(zk_path_join(path, child), result)
            else:
                app_state = self._get_application_state(path)
                result.update(
                    {app_state.configuration_path: app_state.to_dictionary()})

        except NoNodeError:
            result.update({
                path:
                ApplicationState(configuration_path=path,
                                 delete=True).to_dictionary(),
            })

        except Exception:
            logging.exception('An unhandled Exception has occurred while '
                              'running ApplicationStateCache.walk.')
Exemple #3
0
class TestApplicationState(TestCase):
    def setUp(self):
        self.state = ApplicationState(application_name="1",
                                      configuration_path="2",
                                      application_status="3",
                                      application_host=None,
                                      completion_time=1388556000,
                                      trigger_time="6",
                                      error_state="7",
                                      delete="8",
                                      local_mode="9",
                                      login_user="******",
                                      fqdn="11",
                                      last_command="12")

    def test_to_dictionary(self):
        self.assertEquals(
            {
                'application_name': "1",
                'configuration_path': "2",
                'application_status': "unknown",
                'application_host': "",
                'completion_time': '2014-01-01 00:00:00',
                'trigger_time': "6",
                'error_state': "7",
                'delete': "8",
                'local_mode': "9",
                'login_user': "******",
                'fqdn': "11",
                'last_command': "12"
            },
            self.state.to_dictionary()
        )
 def setUp(self):
     self.state = ApplicationState(application_name="1",
                                   configuration_path="2",
                                   application_status="3",
                                   application_host=None,
                                   last_update=1388556000,
                                   start_stop_time="6",
                                   error_state="7",
                                   delete="8",
                                   local_mode="9",
                                   login_user="******",
                                   last_command="12",
                                   pd_disabled=False,
                                   grayed=True,
                                   read_only=True,
                                   load_times=1,
                                   restart_count=0,
                                   platform=0)
    def test_manual_update(self):
        """
        Test that the manual update actually updates the state in the cache
        """
        path = '/foo'
        foo = 'foo'
        bar = 'bar'

        state = ApplicationState(application_host=foo, configuration_path=path)
        cache = self._create_app_state_cache()
        cache._cache.update({path: state.to_dictionary()})

        self.zoo_keeper.get(self.configuration.override_node).AndReturn(('{}', None))
        self.zoo_keeper.set(self.configuration.override_node, '{"application_host": {"/foo": "bar"}}')
        self.zoo_keeper.get(self.configuration.override_node).AndReturn(('{"application_host": {"/foo": "bar"}}', None))
        self.mox.ReplayAll()

        # test that the state's attribute actually changes
        cache.manual_update(path, 'application_host', bar)
        result = cache._get_existing_attribute(path, 'application_host')

        self.assertEqual(bar, result)
Exemple #6
0
 def setUp(self):
     self.state = ApplicationState(application_name="1",
                                   configuration_path="2",
                                   application_status="3",
                                   application_host=None,
                                   completion_time=1388556000,
                                   trigger_time="6",
                                   error_state="7",
                                   delete="8",
                                   local_mode="9",
                                   login_user="******",
                                   fqdn="11",
                                   last_command="12")
    def test_get_existing_attribute(self):
        """
        Test pulling values from existing application states
        """
        path = '/foo'
        host = 'FOOHOST'
        default_val = 'default_val'

        state = ApplicationState(application_host=host, configuration_path=path)
        cache = self._create_app_state_cache()
        cache._cache.update({path: state.to_dictionary()})

        # test return of existing attribute
        result = cache._get_existing_attribute(path, 'application_host')
        self.assertEqual(host, result)

        # state exists, attribute does not
        result = cache._get_existing_attribute(path, 'foo', default=default_val)
        self.assertEqual(default_val, result)

        # state does not exist
        result = cache._get_existing_attribute('', 'application_host')
        self.assertEqual(False, result)
    def test_get_existing_attribute(self):
        """
        Test pulling values from existing application states
        """
        path = '/foo'
        host = 'FOOHOST'
        default_val = 'default_val'

        state = ApplicationState(application_host=host,
                                 configuration_path=path)
        cache = self._create_app_state_cache()
        cache._cache.update({path: state.to_dictionary()})

        # test return of existing attribute
        result = cache._get_existing_attribute(path, 'application_host')
        self.assertEqual(host, result)

        # state exists, attribute does not
        result = cache._get_existing_attribute(path, 'foo')
        self.assertEqual(None, result)

        # state does not exist
        result = cache._get_existing_attribute('', 'application_host')
        self.assertEqual(None, result)
 def setUp(self):
     self.state = ApplicationState(
         application_name="1",
         configuration_path="2",
         application_status="3",
         application_host=None,
         last_update=1388556000,
         start_stop_time="6",
         error_state="7",
         delete="8",
         local_mode="9",
         login_user="******",
         last_command="12",
         pd_disabled=False,
         grayed=True,
         read_only=True,
         platform=0,
     )
class TestApplicationState(TestCase):
    def setUp(self):
        self.state = ApplicationState(application_name="1",
                                      configuration_path="2",
                                      application_status="3",
                                      application_host=None,
                                      last_update=1388556000,
                                      start_stop_time="6",
                                      error_state="7",
                                      delete="8",
                                      local_mode="9",
                                      login_user="******",
                                      last_command="12",
                                      pd_disabled=False,
                                      grayed=True,
                                      read_only=True,
                                      load_times=1,
                                      restart_count=0,
                                      platform=0)

    def test_to_dictionary(self):
        self.assertEquals(
            {
                'application_name': "1",
                'configuration_path': "2",
                'application_status': "unknown",
                'application_host': "",
                'last_update': '2014-01-01 00:00:00',
                'start_stop_time': "6",
                'error_state': "7",
                'delete': "8",
                'local_mode': "9",
                'login_user': "******",
                'last_command': "12",
                'pd_disabled': False,
                'grayed': True,
                'read_only': True,
                'load_times': 1,
                'restart_count': 0,
                'platform': 0
            },
            self.state.to_dictionary()
        )
class TestApplicationState(TestCase):
    def setUp(self):
        self.state = ApplicationState(
            application_name="1",
            configuration_path="2",
            application_status="3",
            application_host=None,
            last_update=1388556000,
            start_stop_time="6",
            error_state="7",
            delete="8",
            local_mode="9",
            login_user="******",
            last_command="12",
            pd_disabled=False,
            grayed=True,
            read_only=True,
            platform=0,
        )

    def test_to_dictionary(self):
        self.assertEquals(
            {
                "application_name": "1",
                "configuration_path": "2",
                "application_status": "unknown",
                "application_host": "",
                "last_update": "2014-01-01 00:00:00",
                "start_stop_time": "6",
                "error_state": "7",
                "delete": "8",
                "local_mode": "9",
                "login_user": "******",
                "last_command": "12",
                "pd_disabled": False,
                "grayed": True,
                "read_only": True,
                "platform": 0,
            },
            self.state.to_dictionary(),
        )
Exemple #12
0
    def _get_application_state(self, path):
        """
        :type path: str
        :rtype: zoom.entities.application_state.ApplicationState
        """
        data, stat = self._get_app_details(path)

        # persistent node
        if stat.ephemeralOwner == 0:
            # watch node to see if children are created
            self._zoo_keeper.get_children(path, watch=self._on_update)
            host = data.get('host', 'Unknown')
            name = data.get('name', os.path.basename(path))
            agent_path = zk_path_join(self._configuration.agent_state_path,
                                      host)

            # if the agent is down, update state and mode with unknown
            agent_up = bool(
                self._zoo_keeper.exists(agent_path,
                                        watch=self._on_agent_state_update))

            valid = True
            if host in (None, 'Unknown'):
                data['state'] = 'invalid'
                data['mode'] = 'unknown'
                valid = False
            elif not agent_up:
                data['state'] = 'unknown'
                data['mode'] = 'unknown'
                valid = False
            elif agent_up:
                d, s = self._zoo_keeper.get(agent_path)
                registered_comps = json.loads(d).get('components', [])
                if name not in registered_comps:
                    data['state'] = 'invalid'
                    data['mode'] = 'unknown'
                    valid = False

            self._update_mapping(host, {path: valid})

            application_state = ApplicationState(
                application_name=name,
                configuration_path=path,
                application_status=ApplicationStatus.STOPPED,
                application_host=host,
                last_update=stat.last_modified,
                start_stop_time=data.get('start_stop_time', ''),
                error_state=data.get('state', 'unknown'),
                local_mode=data.get('mode', 'unknown'),
                login_user=data.get('login_user', 'Zoom'),
                read_only=data.get('read_only', False),
                last_command=self._get_last_command(data),
                pd_disabled=self._get_existing_attribute(path, 'pd_disabled'),
                grayed=self._get_existing_attribute(path, 'grayed'),
                platform=data.get('platform', 'unknown'),
                restart_count=data.get('restart_count', 0),
                load_times=self._time_estimate_cache.get_graphite_data(path))

        # ephemeral node
        else:
            # watch node to see if it goes away
            self._zoo_keeper.get_children(os.path.dirname(path),
                                          watch=self._on_update)

            host = os.path.basename(path)
            # if it is running, path = /app/path/HOSTNAME
            # need to convert to /app/path to get the app_details
            config_path = os.path.dirname(path)
            parent_data, parent_stat = self._get_app_details(config_path)

            self._update_mapping(host, {config_path: True})

            application_state = ApplicationState(
                application_name=parent_data.get(
                    'name', os.path.basename(config_path)),
                configuration_path=config_path,
                application_status=ApplicationStatus.RUNNING,
                application_host=host,
                last_update=stat.last_modified,
                start_stop_time=parent_data.get('start_stop_time', ''),
                error_state=parent_data.get('state', 'unknown'),
                local_mode=parent_data.get('mode', 'unknown'),
                login_user=parent_data.get('login_user', 'Zoom'),
                read_only=parent_data.get('read_only', False),
                last_command=self._get_last_command(parent_data),
                pd_disabled=self._get_existing_attribute(
                    config_path, 'pd_disabled'),
                grayed=self._get_existing_attribute(config_path, 'grayed'),
                platform=parent_data.get('platform', 'unknown'),
                restart_count=parent_data.get('restart_count', 0),
                load_times=self._time_estimate_cache.get_graphite_data(
                    config_path))

        return application_state