#315 Change EventState and ArtifactBuildState enums back to single int representation again.
Merged 6 months ago by jkaluza. Opened 6 months ago by jkaluza.
jkaluza/freshmaker dev-url-fix  into  master

file modified
+5 -2

@@ -299,7 +299,8 @@ 

              return

  

          self.state = state

-         EventState(state).counter.inc()

+         if EventState(state).counter:

+             EventState(state).counter.inc()

          if state_reason is not None:

              self.state_reason = state_reason

  

@@ -495,7 +496,9 @@ 

              return

  

          self.state = state

-         ArtifactBuildState(state).counter.inc()

+         if ArtifactBuildState(state).counter:

+             ArtifactBuildState(state).counter.inc()

+ 

          self.state_reason = state_reason

          if self.state in [ArtifactBuildState.DONE.value,

                            ArtifactBuildState.FAILED.value,

file modified
+38 -14

@@ -36,29 +36,53 @@ 

  

  class ArtifactBuildState(Enum):

  

-     def __init__(self, value, counter):

+     def __init__(self, value):

          self._value_ = value

-         self.counter = counter

  

-     BUILD = (0, None)

-     DONE = (1, freshmaker_artifact_build_done_counter)

-     FAILED = (2, freshmaker_artifact_build_failed_counter)

-     CANCELED = (3, freshmaker_artifact_build_canceled_counter)

-     PLANNED = (4, None)

+         counters = [

+             None,

+             freshmaker_artifact_build_done_counter,

+             freshmaker_artifact_build_failed_counter,

+             freshmaker_artifact_build_canceled_counter,

+             None

+         ]

+ 

+         if type(value) == int:

+             self.counter = counters[value]

+         else:

+             self.counter = None

+ 

+     BUILD = 0

+     DONE = 1

+     FAILED = 2

+     CANCELED = 3

+     PLANNED = 4

  

  

  class EventState(Enum):

  

-     def __init__(self, value, counter):

+     def __init__(self, value):

          self._value_ = value

-         self.counter = counter

  

-     INITIALIZED = (0, None)

+         counters = [

+             None,

+             None,

+             freshmaker_event_complete_counter,

+             freshmaker_event_failed_counter,

+             freshmaker_event_skipped_counter

+         ]

+ 

+         if type(value) == int:

+             self.counter = counters[value]

+         else:

+             self.counter = None

+ 

+     INITIALIZED = 0

      # some artifacts has been found and under building

-     BUILDING = (1, None)

+     BUILDING = 1

      # event is handled successfully

-     COMPLETE = (2, freshmaker_event_complete_counter)

+     COMPLETE = 2

      # error happens while handling the event

-     FAILED = (3, freshmaker_event_failed_counter)

+     FAILED = 3

      # no action to take upon the event

-     SKIPPED = (4, freshmaker_event_skipped_counter)

+     SKIPPED = 4

file modified
+25 -8

@@ -22,6 +22,7 @@ 

  import fedmsg.config

  import mock

  import freshmaker

+ import queue

  

  from freshmaker import app, db, events, models, login_manager

  from tests import helpers

@@ -50,24 +51,30 @@ 

  

      def test_monitor_api_structure(self):

          resp = self.client.get('/api/1/monitor/metrics/')

-         assert len([l.startswith('# TYPE')

-                     for l in resp.get_data(as_text=True).splitlines()]) == 104

-         assert len([l.startswith('# HELP')

-                     for l in resp.get_data(as_text=True).splitlines()]) == 104

+         self.assertEqual(

+             len([l.startswith('# TYPE')

+                  for l in resp.get_data(as_text=True).splitlines()]), 140)

+         self.assertEqual(

+             len([l.startswith('# HELP')

+                  for l in resp.get_data(as_text=True).splitlines()]), 140)

  

  

  class ConsumerTest(helpers.ModelsTestCase):

      def setUp(self):

+         super(ConsumerTest, self). setUp()

          self.client = app.test_client()

  

      def tearDown(self):

-         pass

+         super(ConsumerTest, self). tearDown()

  

      def _create_consumer(self):

          hub = mock.MagicMock()

          hub.config = fedmsg.config.load_config()

          hub.config['freshmakerconsumer'] = True

-         return freshmaker.consumer.FreshmakerConsumer(hub)

+         consumer = freshmaker.consumer.FreshmakerConsumer(hub)

+         # Cleanup the queue.

+         consumer.incoming = queue.Queue()

+         return consumer

  

      def _module_state_change_msg(self, state=None):

          msg = {'body': {

@@ -83,6 +90,14 @@ 

  

          return msg

  

+     def _get_monitor_value(self, key):

+         resp = self.client.get('/api/1/monitor/metrics/')

+         for line in resp.get_data(as_text=True).splitlines():

+             k, v = line.split(" ")[:2]

+             if k == key:

+                 return int(float(v))

+         return None

+ 

      @mock.patch("freshmaker.handlers.mbs.module_state_change.MBSModuleStateChangeHandler.handle")

      @mock.patch("freshmaker.consumer.get_global_consumer")

      def test_consumer_processing_message(self, global_consumer, handle):

@@ -95,11 +110,13 @@ 

          global_consumer.return_value = consumer

          handle.return_value = [freshmaker.events.TestingEvent("ModuleBuilt handled")]

  

+         prev_counter_value = self._get_monitor_value("messaging_received_passed_total")

+ 

          msg = self._module_state_change_msg()

          consumer.consume(msg)

  

          event = consumer.incoming.get()

          self.assertEqual(event.msg_id, "ModuleBuilt handled")

  

-         resp = self.client.get('/api/1/monitor/metrics/')

-         assert 'messaging_received_passed 1.0' in resp.get_data(as_text=True).splitlines()

+         counter_value = self._get_monitor_value("messaging_received_passed_total")

+         self.assertEqual(prev_counter_value + 1, counter_value)

As part of monitor code, the enum values for EventState and ArtifactBuildState have been
changed to (int, counter) format. This made the code like EventState(0) not working, because
Enum class only knew '(0, None)' enum value, not just '0'.

This commit changes Enum values back to integer and sets 'counter' in init method
of both enum subclasses.

This commit also fixes monitor tests in test_monitor.py.

Pull-Request has been merged by jkaluza

6 months ago