def setUp(self):
     logging.basicConfig(level=logging.NOTSET, format='%(asctime)s %(levelname)s %(name)s pid(%(process)d) Message: %(message)s', filename='/tmp/imagefactory-unittests.log')
     self.expected_state_transitions = (("NEW","INITIALIZING"),("INITIALIZING","PENDING"),("PENDING","FINISHING"),("FINISHING","COMPLETED"))
     self.if_agent = ImageFactoryAgent("localhost")
     self.if_agent.start()
     self.connection = cqpid.Connection("localhost")
     self.connection.open()
     self.console_session = ConsoleSession(self.connection)
     self.console_session.setAgentFilter("[and, [eq, _vendor, [quote, 'redhat.com']], [eq, _product, [quote, 'imagefactory']]]")
     self.console_session.open()
     self.console = MockConsole(self.console_session, self.if_agent.image_factory_addr)
     self.console.start()
     time.sleep(5) # Give the agent some time to show up.
class TestImageFactoryAgent(unittest.TestCase):
    def setUp(self):
        logging.basicConfig(level=logging.NOTSET, format='%(asctime)s %(levelname)s %(name)s pid(%(process)d) Message: %(message)s', filename='/tmp/imagefactory-unittests.log')
        self.expected_state_transitions = (("NEW","INITIALIZING"),("INITIALIZING","PENDING"),("PENDING","FINISHING"),("FINISHING","COMPLETED"))
        self.if_agent = ImageFactoryAgent("localhost")
        self.if_agent.start()
        self.connection = cqpid.Connection("localhost")
        self.connection.open()
        self.console_session = ConsoleSession(self.connection)
        self.console_session.setAgentFilter("[and, [eq, _vendor, [quote, 'redhat.com']], [eq, _product, [quote, 'imagefactory']]]")
        self.console_session.open()
        self.console = MockConsole(self.console_session, self.if_agent.image_factory_addr)
        self.console.start()
        time.sleep(5) # Give the agent some time to show up.

    def tearDown(self):
        del self.expected_state_transitions
        self.console.cancel()
        del self.console
        self.console_session.close()
        self.connection.close()
        del self.console_session
        del self.connection
        self.if_agent.shutdown()
        del self.if_agent

    def testImageFactoryAgent(self):
        """Test agent registration, method calls, and events"""
        # test for the correct version of the qmf2 bindings
        self.assertTrue(hasattr(AgentSession(self.connection), "raiseEvent"))

        # test the build failure qmf event raised by BuildAdaptor
        self.assertEqual(len(self.console.test_failure_events), 1)
        self.assertEqual(len(self.console.real_failure_events), 0, "Unexpected failure events raised!\n%s" % (self.console.real_failure_events, ))
        self.assertEqual(self.console.build_adaptor_addr_fail, self.console.test_failure_events[0]["data"]["addr"])
        # test that exceptions are passed properly by the agent handler
        self.assertIsInstance(self.console.image_exception, Exception)
        self.assertEqual(str(self.console.image_exception), "Wrong number of arguments: expected 2, got 0")

        # test that the agent registered and consoles can see it.
        try:
            self.assertIsNotNone(self.console.agent)
        except AttributeError:
            self.fail("No imagefactory agent found...")

        # test that image returns what we expect
        try:
            self.assertIsNotNone(self.console.build_adaptor_addr_success)
        except AttributeError:
            self.fail("image did not return a DataAddr for build_adaptor...")

        # test that status changes in build adaptor create QMF events the consoles see.
        agent_name = self.console.agent.getName()
        self.assertEqual(len(self.expected_state_transitions), len(self.console.build_status_events))
        for event in self.console.build_status_events:
            index = self.console.build_status_events.index(event)
            self.assertEqual(agent_name, event["agent"].getName())
            properties = event["data"]
            self.assertIsNotNone(properties)
            self.assertEqual(self.console.build_adaptor_addr_success, properties["addr"])
            self.assertEqual(self.expected_state_transitions[index][0],properties["old_status"])
            self.assertEqual(self.expected_state_transitions[index][1],properties["new_status"])

        # test that provider_image returns what we expect
        try:
            self.assertIsNotNone(self.console.build_adaptor_addr_push)
        except AttributeError:
            self.fail("provider_image did not return a DataAddr for build_adaptor...")

        # test that status changes in build adaptor create QMF events the consoles see.
        self.assertGreater(len(self.console.push_status_events), 0)
        # self.assertEqual(len(self.expected_state_transitions), len(self.console.push_status_events))
        # for event in self.console.push_status_events:
        #     index = self.console.push_status_events.index(event)
        #     self.assertEqual(agent_name, event["agent"].getName())
        #     properties = event["data"].getProperties()
        #     self.assertIsNotNone(properties)
        #     self.assertEqual(self.console.build_adaptor_addr_push, properties["addr"])
        #     self.assertEqual(self.expected_state_transitions[index][0],properties["old_status"])
        #     self.assertEqual(self.expected_state_transitions[index][1],properties["new_status"])

        self.assertIsNotNone(self.console.import_image_ids)
        self.assertEqual(len(self.console.import_image_ids), 4)
        self.assertTrue('image' in self.console.import_image_ids)
        self.assertTrue('build' in self.console.import_image_ids)
        self.assertTrue('target_image' in self.console.import_image_ids)
        self.assertTrue('provider_image' in self.console.import_image_ids)

        # test instance_states method
        self.assertIsNotNone(self.console.image_factory_states)
        self.assertIsNotNone(self.console.build_adaptor_states)