def setUp(self):
        """
        Set up KubeJobsExecutor objects
        """

        self.job_id1 = "kj-000001"
        self.job_id2 = "kj-000002"

        self.job1 = KubeJobsExecutor(self.job_id1)
        self.job1.k8s = MockKube(self.job_id1)
        self.job1.waiting_time_before_delete_job_resources = 0
        self.job1.db_connector = PersistenceMock()
        self.job1.rds = MockRedis()
        self.job2 = KubeJobsExecutor(self.job_id2)
        self.job2.k8s = MockKube(self.job_id2)
        self.job2.waiting_time_before_delete_job_resources = 0
        self.job2.db_connector = PersistenceMock()
        self.job2.rds = MockRedis()

        with open('broker/tests/unit/mocks/body_request.json') as f:
            self.jsonRequest = json.load(f)
    def test_start_stop_application(self):

        with requests_mock.Mocker() as m:
            m.get("http://test.test", text="content\n")

            m.post("http://0.0.0.0:5001/monitoring/%s" % self.job1.app_id,
                   text="")
            m.put("http://0.0.0.0:5001/monitoring/%s/stop" % self.job1.app_id,
                  text="")

            m.post("http://0.0.0.0:5000/scaling/%s" % self.job1.app_id,
                   text="")
            m.put("http://0.0.0.0:5000/scaling/%s/stop" % self.job1.app_id,
                  text="")

            m.post("http://0.0.0.0:5002/visualizing/%s" % self.job1.app_id,
                   text="")
            m.put("http://0.0.0.0:5002/visualizing/%s/stop" % self.job1.app_id,
                  text="")
            m.get("http://0.0.0.0:5002/visualizing/%s" % self.job1.app_id,
                  text="{'url': 'http://mock.com'}")

            self.job1.rds = MockRedis()

            self.assertEqual(self.job1.get_application_state(), "created")

            thread_job1 = threading.Thread(target=self.job1.start_application,
                                           args=([self.jsonRequest]))

            thread_job1.start()

            next_states_job1 = ["ongoing", "completed"]
            while thread_job1.is_alive():
                current_state = self.job1.get_application_state()
                if current_state in next_states_job1:
                    next_states_job1.remove(current_state)

            self.assertTrue(len(next_states_job1) == 0)
    def test_stop_job(self):

        with requests_mock.Mocker() as m:
            m.get("http://test.test", text="content\ncontent\ncontent\n")

            m.post("http://0.0.0.0:5001/monitoring/%s" % self.job2.app_id,
                   text="")
            m.put("http://0.0.0.0:5001/monitoring/%s/stop" % self.job2.app_id)

            m.post("http://0.0.0.0:5000/scaling/%s" % self.job2.app_id,
                   text="")
            m.put("http://0.0.0.0:5000/scaling/%s/stop" % self.job2.app_id,
                  text="")

            m.post("http://0.0.0.0:5002/visualizing/%s" % self.job2.app_id,
                   text="")
            m.put("http://0.0.0.0:5002/visualizing/%s/stop" % self.job2.app_id,
                  text="")
            m.get("http://0.0.0.0:5002/visualizing/%s" % self.job2.app_id,
                  text="{'url': 'http://mock.com'}")

            self.job2.rds = MockRedis()

            self.assertEqual(self.job2.get_application_state(), "created")

            thread_job2 = threading.Thread(target=self.job2.start_application,
                                           args=([self.jsonRequest]))
            thread_job2.start()

            while thread_job2.is_alive():
                current_state = self.job2.get_application_state()
                if current_state == "ongoing" and self.job2.rds.map != {}:
                    self.job2.stop_application()

            self.assertTrue(self.job2.get_application_state() == "completed")

            with self.assertRaises(Exception):
                self.job2.rds.delete("job")