def test_get_jobs(self): """Test get_jobs API call""" # Set up a mock HTTP server body = read_file('data/jenkins/jenkins_jobs.json') httpretty.register_uri(httpretty.GET, JOBS_URL, body=body, status=200) client = JenkinsClient(SERVER_URL) response = client.get_jobs(client.base_url) self.assertEqual(response, body)
def test_get_builds(self): """Test get_builds API call""" # Set up a mock HTTP server body = read_file('data/jenkins_job_builds.json') httpretty.register_uri(httpretty.GET, JENKINS_JOB_BUILDS_URL_1, body=body, status=200) client = JenkinsClient(JENKINS_SERVER_URL) response = client.get_builds(JENKINS_JOB_BUILDS_1) self.assertEqual(response, body)
def test_http_retry_requests(self): """Test whether failed requests are properly handled""" httpretty.register_uri(httpretty.GET, JOBS_URL, body="", status=408) client = JenkinsClient(SERVER_URL, sleep_time=0.1) before = float(time.time()) expected = before + (client.sleep_time * JenkinsClient.MAX_RETRIES) with self.assertRaises(requests.exceptions.RetryError): _ = client.get_jobs(client.base_url) after = float(time.time()) self.assertTrue(expected <= after)
def test_init(self): """Test initialization""" target_sleep_time = 0.1 client = JenkinsClient(SERVER_URL, sleep_time=target_sleep_time) self.assertEqual(client.base_url, SERVER_URL) self.assertEqual(client.blacklist_jobs, None) self.assertEqual(client.sleep_time, target_sleep_time) self.assertIsNone(client.auth) client = JenkinsClient(SERVER_URL, user=USER, api_token=TOKEN) self.assertEqual(client.base_url, SERVER_URL) self.assertEqual(client.blacklist_jobs, None) self.assertEqual(client.auth, (USER, TOKEN))
def test_get_jobs_auth_api_token(self): """Test get_jobs API call with username and API token""" # Set up a mock HTTP server body = read_file('data/jenkins/jenkins_jobs.json') httpretty.register_uri(httpretty.GET, JOBS_URL, body=body, status=200) client = JenkinsClient(SERVER_URL, user=USER, api_token=TOKEN) response = client.get_jobs(client.base_url) req = httpretty.last_request() self.assertEqual(response, body) authorization = [ h for h in req.headers._headers if h[0] == 'Authorization' ][0] self.assertEqual(req.method, 'GET') self.assertIn('Basic', authorization[1])
def test_connection_error(self): """Test that HTTP connection error is correctly handled""" # Set up a mock HTTP server body = read_file('data/jenkins/jenkins_job_builds.json') httpretty.register_uri(httpretty.GET, JENKINS_JOB_BUILDS_URL_1_DEPTH_1, body=body, status=408) client = JenkinsClient(JENKINS_SERVER_URL, sleep_time=0.1) start = float(time.time()) expected = start + (sum([i * client.sleep_time for i in range(client.MAX_RETRIES)])) with self.assertRaises(requests.exceptions.RequestException): _ = client.get_builds(JENKINS_JOB_BUILDS_1) end = float(time.time()) self.assertGreater(end, expected)
def test_init(self): """Test initialization""" target_sleep_time = 0.1 client = JenkinsClient(JENKINS_SERVER_URL, sleep_time=target_sleep_time) self.assertEqual(client.base_url, JENKINS_SERVER_URL) self.assertEqual(client.blacklist_jobs, None) self.assertEqual(client.sleep_time, target_sleep_time)
def test_init(self): """Test initialization""" client = JenkinsClient(JENKINS_SERVER_URL)