def test_push(self): """Tests standard push command""" ctx = Context() # Force the return of fake service self.mock_api_service.get_applications.return_value = self.GET_APP_RESPONSE a = Application.push(ctx, source_directory=self.SOURCE_DIR, name=self.APP_NAME, bound_services=self.BOUND_SERVICES, env=self.ENV) assert a == Application(self.APP_NAME, self.APP_ID, self.APP_STATE, self.APP_INSTANCES, self.APP_URLS, self.APP_BINDINGS) assert a._client == self.user_admin # Now push the application but with non-standard client a = Application.push(ctx, source_directory=self.SOURCE_DIR, name=self.APP_NAME, bound_services=self.BOUND_SERVICES, env=self.ENV, client=self.user_luser) assert a == Application(self.APP_NAME, self.APP_ID, self.APP_STATE, self.APP_INSTANCES, self.APP_URLS, self.APP_BINDINGS) assert a._client == self.user_luser
def test_repr(self): """Ensure the __repr__ doesn't crash anything.""" a = Application(self.APP_NAME, self.APP_ID, self.APP_STATE, self.APP_INSTANCES, self.APP_URLS, self.APP_BINDINGS) assert a.__repr__( ) == "Application (name=" + self.APP_NAME + ", app_id=" + self.APP_ID + ")"
def test_lt_comparator(self): """Ensure the lt comparator doesn't crash anything.""" a = Application(self.APP_NAME, "00100", self.APP_STATE, self.APP_INSTANCES, self.APP_URLS, self.APP_BINDINGS) b = Application(self.APP_NAME, "01000", self.APP_STATE, self.APP_INSTANCES, self.APP_URLS, self.APP_BINDINGS) assert a < b
def test_app_get(self): """Check that get returns proper details""" example_response = { "command": "sudo rm -rf /", "detected_buildpack": "propably outdated", "disk_quota": "insufficient", "routes": [{ "host": "not the one accessible", "domain": { "name": "The one that you won't afford" } }, { "host": "also not the one accessible", "domain": { "name": "Also the one that you won't afford" } }], "environment_json": "ls = rm -rf", "instances": None, "memory": "Good, but only short-term", "package_updated_at": "That's personal", "running_instances": "There are no instances, go figure...", "services": [{ "name": "service_01" }, { "name": "service_00" }] } expected_response = copy.deepcopy(example_response) del expected_response["routes"] del expected_response["services"] expected_response["domains"] = [ "also not the one accessible.Also the one that you won't afford", "not the one accessible.The one that you won't afford" ] expected_response["service_names"] = ["service_00", "service_01"] self.mock_api_service.get_application.return_value = example_response a = Application(self.APP_NAME, self.APP_ID, self.APP_STATE, self.APP_INSTANCES, self.APP_URLS, self.APP_BINDINGS) assert expected_response == a.get()
def test_set_scheme(self): """Checks behaviour of method for different inputs""" results_http = [] results_https = [] for domain in self.TEST_DOMAINS: results_http.append( Application._set_scheme(url=domain, scheme='http')) results_https.append( Application._set_scheme(url=domain, scheme='https')) for domain in results_http: assert domain == 'http://tap.com' for domain in results_https: assert domain == 'https://tap.com'
def test_request_session(self): """Check if proper apis are called for start and stop operations""" raw_req = {} req = {"request": ""} # Return itself, no need to create a new mock in this case self.mock_requests.session.return_value = self.mock_requests self.mock_requests.prepare_request.return_value = req self.mock_requests.Request.return_value = raw_req rsp = {"response": ""} self.mock_requests.send.return_value = rsp a = Application(self.APP_NAME, self.APP_ID, self.APP_STATE, self.APP_INSTANCES, self.APP_URLS, self.APP_BINDINGS) path = "predefined" method = "GET" scheme = "http" hostname = "host" data = {"some_data": "123"} params = {"params": "321"} body = {"body": "body"} raw = True expected_url = scheme + "://" + hostname + "/" + path assert rsp == a.api_request(path, method, scheme, hostname, data, params, body, raw) self.mock_requests.prepare_request.assert_called_with(raw_req) self.mock_requests.Request.assert_called_with(method=method, url=expected_url, params=params, data=data, json=body) self.mock_requests.send.assert_called_with(req) # Now the response won't be OK, assertion error should raise class Object: pass rsp = Object() rsp.ok = False rsp.status_code = 404 rsp.text = "Not found" self.mock_requests.send.return_value = rsp with pytest.raises(UnexpectedResponseError): a.api_request(path, method, scheme, hostname, data, params, body, False)
def test_push_fail(self): """Try pushing the app, but it doesn't appear in the response""" ctx = Context() # Force the return of fake service self.mock_api_service_bad.api_get_app_list.return_value = self.GET_APP_BAD_RESPONSE with pytest.raises(AssertionError): a = Application.push(ctx, self.SOURCE_DIR, self.APP_NAME, self.BOUND_SERVICES, self.ENV)
def test_update_manifest_no_envs(self): """Try not to add any envs""" j = copy.deepcopy(MANIFEST) j = Application.update_manifest(j, APP_NAME, SERVICES, None) assert j["type"] == APP_TYPE assert j["name"] == APP_NAME assert j["instances"] == NO_INSTANCES assert j["services"] == SERVICES assert j["env"][ENV_NAME] == ENV_VAL assert len(j["env"]) == 1
def test_push_has_stopped(self): """Pushes an application that will be in stopped state. Verifies that the is_stopped property doesn't crash anything.""" ctx = Context() # Force the return of fake service rep = copy.deepcopy(self.GET_APP_RESPONSE) rep.data[0]["state"] = "stopped" self.mock_api_service.get_applications.return_value = rep a = Application.push(ctx, source_directory=self.SOURCE_DIR, name=self.APP_NAME, bound_services=self.BOUND_SERVICES, env=self.ENV) assert a == Application(self.APP_NAME, self.APP_ID, "stopped", self.APP_INSTANCES, self.APP_URLS, self.APP_BINDINGS) assert a._client == self.user_admin assert a.is_stopped == True assert a.is_running == False
def test_update_manifest(self): """Tests if update manifest method works.""" j = copy.deepcopy(MANIFEST) j = Application.update_manifest(j, APP_NAME, SERVICES, ENVS) assert j["type"] == APP_TYPE assert j["name"] == APP_NAME assert j["instances"] == NO_INSTANCES assert j["services"] == SERVICES assert j["env"][ENV_NAME] == ENV_VAL assert j["env"][NEW_ENV_01] == NEW_ENV_01_VAL assert j["env"][NEW_ENV_02] == NEW_ENV_02_VAL
def test_push_not_created(self): """Try pushing the app, the push succedeed, but somehow the application isn't visible. Ensure the message is informative.""" ctx = Context() # Force the return of fake service self.mock_api_service_bad.api_get_app_list.return_value = [] with pytest.raises(AssertionError) as ex: a = Application.push(ctx, self.SOURCE_DIR, self.APP_NAME, self.BOUND_SERVICES, self.ENV) msg = "App " + self.APP_NAME + " has not been created on the Platform" assert msg in ex.__str__()
def test_update_manifest_with_no_envs(self): """Update a manifest that has no envs defined""" j = copy.deepcopy(MANIFEST) del j["env"] j = Application.update_manifest(j, APP_NAME, SERVICES, ENVS) assert j["type"] == APP_TYPE assert j["name"] == APP_NAME assert j["instances"] == NO_INSTANCES assert j["services"] == SERVICES assert j["env"][NEW_ENV_01] == NEW_ENV_01_VAL assert j["env"][NEW_ENV_02] == NEW_ENV_02_VAL assert len(j["env"]) == 2
def test_update_manifest_delete_services(self): """Deletes services provided already present in manifest""" j = copy.deepcopy(MANIFEST) j = Application.update_manifest(j, APP_NAME, None, ENVS) assert j["type"] == APP_TYPE assert j["name"] == APP_NAME assert j["instances"] == NO_INSTANCES with pytest.raises(KeyError): j["services"] assert j["env"][ENV_NAME] == ENV_VAL assert j["env"][NEW_ENV_01] == NEW_ENV_01_VAL assert j["env"][NEW_ENV_02] == NEW_ENV_02_VAL
def test_update_manifest_with_proxy(self): """Check if cf_proxy is present we get proxy env""" j = copy.deepcopy(MANIFEST) j = Application.update_manifest(j, APP_NAME, SERVICES, ENVS) assert j["type"] == APP_TYPE assert j["name"] == APP_NAME assert j["instances"] == NO_INSTANCES assert j["services"] == SERVICES assert j["env"][ENV_NAME] == ENV_VAL assert j["env"][NEW_ENV_01] == NEW_ENV_01_VAL assert j["env"][NEW_ENV_02] == NEW_ENV_02_VAL assert j["env"]["http_proxy"] == "http://" + self.HTTP_PROXY + ":911" assert j["env"]["https_proxy"] == "https://" + self.HTTP_PROXY + ":912"
def test_update_manifest_no_env_with_proxy_no_env(self): """Check if cf_proxy is present we get proxy env, no env is present in manifest, no envs were provided""" j = copy.deepcopy(MANIFEST) del j["env"] j = Application.update_manifest(j, APP_NAME, SERVICES, None) assert j["type"] == APP_TYPE assert j["name"] == APP_NAME assert j["instances"] == NO_INSTANCES assert j["services"] == SERVICES assert j["env"]["http_proxy"] == "http://" + self.HTTP_PROXY + ":911" assert j["env"]["https_proxy"] == "https://" + self.HTTP_PROXY + ":912" assert len(j["env"]) == 2
def test_push_tap_exception(self): """Try pushing the app, but exception was raised by TapCli""" ctx = Context() # Force the return of fake service self.mock_api_service.api_get_app_list.return_value = self.GET_APP_RESPONSE # Force the exception raising mock_TapCli = MagicMock() mock_TapCli.push.side_effect = Exception("Bang, bang!") self.mock_tap_cli_exception.return_value = mock_TapCli with pytest.raises(Exception): a = Application.push(ctx, self.SOURCE_DIR, self.APP_NAME, self.BOUND_SERVICES, self.ENV) self.mock_api_service.delete_application.assert_called_once_with( self.APP_ID, self.user_admin)
def test_hash(self): """Ensure the __hash__ doesn't crash anything.""" a = Application(self.APP_NAME, self.APP_ID, self.APP_STATE, self.APP_INSTANCES, self.APP_URLS, self.APP_BINDINGS) assert a.__hash__() == hash((a.name, a.app_id))
def test_start_stop(self): """Check if proper apis are called for start and stop operations""" a = Application(self.APP_NAME, self.APP_ID, self.APP_STATE, self.APP_INSTANCES, self.APP_URLS, self.APP_BINDINGS) a.start() self.mock_api_service.start_application.assert_called_once_with( self.APP_ID, self.user_admin) a.stop() self.mock_api_service.stop_application.assert_called_once_with( self.APP_ID, self.user_admin) # Provide a non-standard client a = Application(self.APP_NAME, self.APP_ID, self.APP_STATE, [], [], [], None, self.user_luser) a.start() self.mock_api_service.start_application.assert_called_with( self.APP_ID, self.user_luser) a.stop() self.mock_api_service.stop_application.assert_called_with( self.APP_ID, self.user_luser)