Buckets:

download
raw
19.8 kB
import dataclasses
import unittest
from uuid import uuid4
import requests
import requests_mock
from codecarbon.core.api_client import ApiClient
from codecarbon.core.schemas import ExperimentCreate, OrganizationCreate, ProjectCreate
from codecarbon.output import EmissionsData
conf = {
"os": "macOS-10.15.7-x86_64-i386-64bit",
"python_version": "3.8.0",
"codecarbon_version": "2.1.3",
"cpu_count": 12,
"cpu_model": "Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz",
"gpu_count": 4,
"gpu_model": "NVIDIA",
"longitude": -7.6174,
"latitude": 33.5822,
"region": "EUROPE",
"provider": "GCP",
"ram_total_size": 83948.22,
"tracking_mode": "Machine",
}
class TestApi(unittest.TestCase):
def test_get_headers_prefers_api_key_over_access_token(self):
api = ApiClient(
endpoint_url="http://test.com",
api_key="api-key",
access_token="access-token",
create_run_automatically=False,
)
headers = api._get_headers()
self.assertEqual(headers["x-api-token"], "api-key")
self.assertNotIn("Authorization", headers)
def test_set_access_token_updates_client(self):
api = ApiClient(endpoint_url="http://test.com", create_run_automatically=False)
api.set_access_token("updated-token")
self.assertEqual(api.access_token, "updated-token")
def test_api_read_only(self):
api = ApiClient(
endpoint_url="http://test.com",
api_key="Toto",
conf=None,
create_run_automatically=False,
)
self.assertIsNone(api.experiment_id)
self.assertEqual(api.api_key, "Toto")
self.assertEqual(api.url, "http://test.com")
self.assertIsNone(api.run_id)
def test_call_api(self):
with requests_mock.Mocker() as m:
m.post(
"http://test.com/runs",
json={
"id": "82ba0923-0713-4da1-9e57-cea70b460ee9",
"timestamp": "2021-04-04T08:43:00+02:00",
"experiment_id": "8edb03e1-9a28-452a-9c93-a3b6560136d7",
"os": "macOS-10.15.7-x86_64-i386-64bit",
"python_version": "3.8.0",
"codecarbon_version": "2.1.3",
"cpu_count": 12,
"cpu_model": "Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz",
"gpu_count": 4,
"gpu_model": "NVIDIA",
"longitude": -7.6174,
"latitude": 33.5822,
"region": "EUROPE",
"provider": "AWS",
"ram_total_size": 83948.22,
"tracking_mode": "Machine",
},
status_code=201,
)
api = ApiClient(
experiment_id="experiment_id",
endpoint_url="http://test.com",
api_key="Toto",
conf=conf,
)
assert api.run_id == "82ba0923-0713-4da1-9e57-cea70b460ee9"
with requests_mock.Mocker() as m:
m.post("http://test.com/emissions", status_code=201)
carbon_emission = EmissionsData(
timestamp="222",
project_name="",
run_id=uuid4(),
experiment_id="test",
duration=1.5,
emissions=2.0,
emissions_rate=2.0,
cpu_energy=2,
gpu_energy=0,
ram_energy=1,
cpu_power=3.0,
gpu_power=0,
ram_power=0.15,
energy_consumed=3.0,
water_consumed=0.0,
country_name="Groland",
country_iso_code="GRD",
region="EU",
on_cloud="N",
cloud_provider="",
cloud_region="",
os="Linux",
python_version="3.8.0",
codecarbon_version="2.1.3",
gpu_count=4,
gpu_model="NVIDIA",
cpu_count=12,
cpu_model="Intel",
longitude=-7.6174,
latitude=33.5822,
ram_total_size=83948.22,
tracking_mode="Machine",
cpu_utilization_percent=12.5,
gpu_utilization_percent=34.5,
ram_utilization_percent=56.5,
wue=0.8,
)
assert api.add_emission(dataclasses.asdict(carbon_emission))
payload = m.last_request.json()
assert payload["cpu_utilization_percent"] == 12.5
assert payload["gpu_utilization_percent"] == 34.5
assert payload["ram_utilization_percent"] == 56.5
assert payload["wue"] == 0.8
def test_create_run_rounds_coordinates(self):
with requests_mock.Mocker() as m:
m.post("http://test.com/runs", json={"id": "run-1"}, status_code=201)
api = ApiClient(
endpoint_url="http://test.com",
experiment_id="exp-1",
conf=conf,
create_run_automatically=False,
)
api._create_run("exp-1")
payload = m.last_request.json()
self.assertEqual(payload["longitude"], -7.6)
self.assertEqual(payload["latitude"], 33.6)
def test_create_run_keeps_unknown_coordinates_null(self):
offline_conf = dict(conf, longitude=None, latitude=None)
with requests_mock.Mocker() as m:
m.post("http://test.com/runs", json={"id": "run-1"}, status_code=201)
api = ApiClient(
endpoint_url="http://test.com",
experiment_id="exp-1",
conf=offline_conf,
create_run_automatically=False,
)
self.assertEqual(api._create_run("exp-1"), "run-1")
payload = m.last_request.json()
self.assertIsNone(payload["longitude"])
self.assertIsNone(payload["latitude"])
def test_check_auth_raises_on_error(self):
with requests_mock.Mocker() as m:
m.get("http://test.com/auth/check", text="bad", status_code=401)
api = ApiClient(
endpoint_url="http://test.com",
access_token="token",
create_run_automatically=False,
)
with self.assertRaises(requests.exceptions.HTTPError):
api.check_auth()
def test_check_organization_exists_raises_when_list_fails(self):
with requests_mock.Mocker() as m:
m.get("http://test.com/organizations", text="bad", status_code=500)
api = ApiClient(
endpoint_url="http://test.com",
create_run_automatically=False,
)
with self.assertRaises(requests.exceptions.HTTPError):
api.check_organization_exists("missing")
def test_create_organization_skips_when_name_exists(self):
organization = OrganizationCreate(name="existing", description="desc")
existing_org = {"id": "org-1", "name": "existing"}
with requests_mock.Mocker() as m:
m.get("http://test.com/organizations", json=[existing_org], status_code=200)
api = ApiClient(
endpoint_url="http://test.com",
create_run_automatically=False,
)
self.assertEqual(api.create_organization(organization), existing_org)
self.assertEqual(m.call_count, 1)
def test_add_emission_returns_false_when_run_creation_fails(self):
api = ApiClient(
endpoint_url="http://test.com",
experiment_id="exp-1",
conf=conf,
create_run_automatically=False,
)
api._create_run = lambda experiment_id: None
self.assertFalse(
api.add_emission(
{
"duration": 2,
"emissions": 1.0,
"emissions_rate": 1.0,
"cpu_power": 1.0,
"gpu_power": 0.0,
"ram_power": 0.5,
"cpu_energy": 0.1,
"gpu_energy": 0.0,
"ram_energy": 0.1,
"energy_consumed": 0.2,
}
)
)
def test_add_emission_skips_short_duration(self):
api = ApiClient(
endpoint_url="http://test.com",
experiment_id="exp-1",
conf=conf,
create_run_automatically=False,
)
api.run_id = "run-1"
self.assertFalse(
api.add_emission(
{
"duration": 0.5,
"emissions": 1.0,
"emissions_rate": 1.0,
"cpu_power": 1.0,
"gpu_power": 0.0,
"ram_power": 0.5,
"cpu_energy": 0.1,
"gpu_energy": 0.0,
"ram_energy": 0.1,
"energy_consumed": 0.2,
}
)
)
def test_add_emission_raises_on_unsuccessful_post(self):
with requests_mock.Mocker() as m:
m.post("http://test.com/emissions", text="bad", status_code=500)
api = ApiClient(
endpoint_url="http://test.com",
experiment_id="exp-1",
conf=conf,
create_run_automatically=False,
)
api.run_id = "run-1"
with self.assertRaises(requests.exceptions.HTTPError):
api.add_emission(
{
"duration": 2,
"emissions": 1.0,
"emissions_rate": 1.0,
"cpu_power": 1.0,
"gpu_power": 0.0,
"ram_power": 0.5,
"cpu_energy": 0.1,
"gpu_energy": 0.0,
"ram_energy": 0.1,
"energy_consumed": 0.2,
}
)
def test_create_run_raises_on_unsuccessful_status(self):
with requests_mock.Mocker() as m:
m.post("http://test.com/runs", text="bad", status_code=400)
api = ApiClient(
endpoint_url="http://test.com",
experiment_id="experiment_id",
api_key="Toto",
conf=conf,
create_run_automatically=False,
)
with self.assertRaises(requests.exceptions.HTTPError):
api._create_run("experiment_id")
self.assertIsNone(api.run_id)
def test_create_run_raises_on_unexpected_2xx_status(self):
with requests_mock.Mocker() as m:
m.post("http://test.com/runs", json={}, status_code=200)
api = ApiClient(
endpoint_url="http://test.com",
experiment_id="experiment_id",
api_key="Toto",
conf=conf,
create_run_automatically=False,
)
with self.assertRaises(requests.exceptions.HTTPError) as ctx:
api._create_run("experiment_id")
self.assertIn("Unexpected status 200", str(ctx.exception))
self.assertIsNone(api.run_id)
def test_add_emission_raises_on_unexpected_2xx_status(self):
with requests_mock.Mocker() as m:
m.post("http://test.com/emissions", json={}, status_code=200)
api = ApiClient(
endpoint_url="http://test.com",
experiment_id="exp-1",
conf=conf,
create_run_automatically=False,
)
api.run_id = "run-1"
with self.assertRaises(requests.exceptions.HTTPError) as ctx:
api.add_emission(
{
"duration": 2,
"emissions": 1.0,
"emissions_rate": 1.0,
"cpu_power": 1.0,
"gpu_power": 0.0,
"ram_power": 0.5,
"cpu_energy": 0.1,
"gpu_energy": 0.0,
"ram_energy": 0.1,
"energy_consumed": 0.2,
}
)
self.assertIn("Unexpected status 200", str(ctx.exception))
def test_list_experiments_from_project_raises_on_error(self):
with requests_mock.Mocker() as m:
m.get(
"http://test.com/projects/proj-1/experiments",
text="bad",
status_code=500,
)
api = ApiClient(
endpoint_url="http://test.com",
create_run_automatically=False,
)
with self.assertRaises(requests.exceptions.HTTPError):
api.list_experiments_from_project("proj-1")
def test_set_experiment_updates_value(self):
api = ApiClient(endpoint_url="http://test.com", create_run_automatically=False)
api.set_experiment("exp-2")
self.assertEqual(api.experiment_id, "exp-2")
def test_add_experiment_raises_on_error(self):
experiment = ExperimentCreate(
timestamp="2024-01-01T00:00:00+00:00",
name="exp",
description="desc",
on_cloud=False,
project_id="proj-1",
)
with requests_mock.Mocker() as m:
m.post("http://test.com/experiments", text="bad", status_code=500)
api = ApiClient(
endpoint_url="http://test.com",
create_run_automatically=False,
)
with self.assertRaises(requests.exceptions.HTTPError):
api.add_experiment(experiment)
def test_get_experiment_raises_on_error(self):
with requests_mock.Mocker() as m:
m.get("http://test.com/experiments/exp-1", text="bad", status_code=404)
api = ApiClient(
endpoint_url="http://test.com",
create_run_automatically=False,
)
with self.assertRaises(requests.exceptions.HTTPError):
api.get_experiment("exp-1")
def test_create_run_raises_on_connection_error(self):
with requests_mock.Mocker() as m:
m.post(
"http://test.com/runs",
exc=requests.exceptions.ConnectionError("API unreachable"),
)
with self.assertRaises(requests.exceptions.ConnectionError):
ApiClient(
experiment_id="experiment_id",
endpoint_url="http://test.com",
api_key="Toto",
conf=conf,
)
def test_create_run_raises_on_unexpected_error(self):
"""A non HTTP, non connection error is logged then re-raised as is."""
with requests_mock.Mocker() as m:
m.post("http://test.com/runs", exc=requests.exceptions.Timeout("too slow"))
with self.assertRaises(requests.exceptions.Timeout):
ApiClient(
experiment_id="experiment_id",
endpoint_url="http://test.com",
api_key="Toto",
conf=conf,
)
def test_add_emission_raises_on_unexpected_error(self):
"""A non HTTP error raised while posting an emission is re-raised as is."""
with requests_mock.Mocker() as m:
m.post(
"http://test.com/emissions", exc=requests.exceptions.Timeout("too slow")
)
api = ApiClient(
endpoint_url="http://test.com",
experiment_id="exp-1",
conf=conf,
create_run_automatically=False,
)
api.run_id = "run-1"
with self.assertRaises(requests.exceptions.Timeout):
api.add_emission(
{
"duration": 2,
"emissions": 1.0,
"emissions_rate": 1.0,
"cpu_power": 1.0,
"gpu_power": 0.0,
"ram_power": 0.5,
"cpu_energy": 0.1,
"gpu_energy": 0.0,
"ram_energy": 0.1,
"energy_consumed": 0.2,
}
)
def test_create_organization_raises_on_error(self):
organization = OrganizationCreate(name="new-org", description="desc")
with requests_mock.Mocker() as m:
# No organization with that name yet, so the creation is attempted.
m.get("http://test.com/organizations", json=[], status_code=200)
m.post("http://test.com/organizations", text="bad", status_code=500)
api = ApiClient(
endpoint_url="http://test.com",
create_run_automatically=False,
)
with self.assertRaises(requests.exceptions.HTTPError):
api.create_organization(organization)
def test_get_organization_raises_on_error(self):
with requests_mock.Mocker() as m:
m.get("http://test.com/organizations/org-1", text="bad", status_code=404)
api = ApiClient(
endpoint_url="http://test.com",
create_run_automatically=False,
)
with self.assertRaises(requests.exceptions.HTTPError):
api.get_organization("org-1")
def test_update_organization_raises_on_error(self):
organization = OrganizationCreate(name="org", description="desc")
organization.id = "org-1"
with requests_mock.Mocker() as m:
m.patch("http://test.com/organizations/org-1", text="bad", status_code=500)
api = ApiClient(
endpoint_url="http://test.com",
create_run_automatically=False,
)
with self.assertRaises(requests.exceptions.HTTPError):
api.update_organization(organization)
def test_list_projects_from_organization_raises_on_error(self):
with requests_mock.Mocker() as m:
m.get(
"http://test.com/organizations/org-1/projects",
text="bad",
status_code=500,
)
api = ApiClient(
endpoint_url="http://test.com",
create_run_automatically=False,
)
with self.assertRaises(requests.exceptions.HTTPError):
api.list_projects_from_organization("org-1")
def test_create_project_raises_on_error(self):
project = ProjectCreate(
name="project", description="desc", organization_id="org-1"
)
with requests_mock.Mocker() as m:
m.post("http://test.com/projects", text="bad", status_code=500)
api = ApiClient(
endpoint_url="http://test.com",
create_run_automatically=False,
)
with self.assertRaises(requests.exceptions.HTTPError):
api.create_project(project)
def test_get_project_raises_on_error(self):
with requests_mock.Mocker() as m:
m.get("http://test.com/projects/proj-1", text="bad", status_code=404)
api = ApiClient(
endpoint_url="http://test.com",
create_run_automatically=False,
)
with self.assertRaises(requests.exceptions.HTTPError):
api.get_project("proj-1")

Xet Storage Details

Size:
19.8 kB
·
Xet hash:
6cdcf8b43548fb1f5e5a6f1ffb5f3846180a272701c3877b21dc0af3d5713009

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.