|
| 1 | +## Harvester Test |
| 2 | + |
| 3 | +This instruction aims to explain how to write an E2E (end to end) integration and API test under [harvester_e2e_tests](../../harvester_e2e_tests/) |
| 4 | + |
| 5 | +- When adding a new Harvester API and the corresponding resource **does not already exist** under [Harvester API Manager](../../apiclient/harvester_api/managers/), create a new file with the **plural form** of the resource name. |
| 6 | + - For example, if the resource is "noodle", search for `noodle` or `noodles` under [Harvester API Manager](../../apiclient/harvester_api/managers/). Refer to the [image API](../../apiclient/harvester_api/managers/images.py) or [storageclass API](../../apiclient/harvester_api/managers/storageclasses.py) examples |
| 7 | + - Harvester API will be used in [integration tests](../../harvester_e2e_tests/integrations/) and [API tests](../../harvester_e2e_tests/apis/) |
| 8 | + - Load the new resource [here](../../apiclient/harvester_api/api.py) and register it [here](../../apiclient/harvester_api/managers/__init__.py) |
| 9 | + - Don't need to search [rancher API](../../apiclient/rancher_api/) |
| 10 | + |
| 11 | +- When writing **integration tests**, refer to the existing [tests directory](../../harvester_e2e_tests/integrations). |
| 12 | + - Refer to the [images example](../../harvester_e2e_tests/integrations/test_1_images.py) |
| 13 | + |
| 14 | +- When writing **API tests**, refer to the existing [tests directory](../../harvester_e2e_tests/apis/). |
| 15 | + - Refer to the [images example](../../harvester_e2e_tests/apis/test_images.py) |
| 16 | + |
| 17 | +- Each test case **must include at least one passing and one failing scenario**. |
| 18 | + |
| 19 | +- Follow these standard status codes to validate Harvester API responses: |
| 20 | + - `201`: Resource successfully created |
| 21 | + - `200`: Operation succeeded (general case) |
| 22 | + - `422`: Operation failed |
| 23 | + |
| 24 | +## Test Template |
| 25 | + |
| 26 | +Use this template when writing a new test. |
| 27 | + |
| 28 | +When calling API, don't use underline function or `create_data` function. If need, create another function or add more parameters to extend current function. |
| 29 | + |
| 30 | +### Shared Fixture Template |
| 31 | + |
| 32 | +```python |
| 33 | +@pytest.fixture(scope="module or class") |
| 34 | +def shared_fixture(): |
| 35 | + """ |
| 36 | + Shared fixture for test cases. |
| 37 | + """ |
| 38 | + # 1. Prepare the environment and data |
| 39 | + code, data = api_client.sss.xxx() |
| 40 | + |
| 41 | + # 2. Assert the result |
| 42 | + assert code == xxxx, f"Expected status code xxxx, got {code} with data: {data}" |
| 43 | + |
| 44 | + output = { |
| 45 | + "key1": "value1", |
| 46 | + "key2": "value2", |
| 47 | + } |
| 48 | + |
| 49 | + yield output |
| 50 | + |
| 51 | + # 3. Teardown if needed |
| 52 | + api_client.sss.teardown_method() |
| 53 | +``` |
| 54 | + |
| 55 | +### Test Case Template |
| 56 | + |
| 57 | +```python |
| 58 | +@pytest.mark.p0 |
| 59 | +@pytest.mark.skip_version_if("???", reason="???") |
| 60 | +class TestXXXX: |
| 61 | + """ |
| 62 | + Test description. |
| 63 | + """ |
| 64 | + |
| 65 | + @pytest.mark.p0 |
| 66 | + def test_xxxx(self, ${some fixtures and shared_fixture}): |
| 67 | + """ |
| 68 | + Test description. |
| 69 | + """ |
| 70 | + # 1. Prepare the environment and data |
| 71 | + shared_data = shared_fixture |
| 72 | + |
| 73 | + # 2. Call the API |
| 74 | + code, data = api_client.sss.xxx() |
| 75 | + |
| 76 | + # 3. Assert the result |
| 77 | + assert code == xxxx, f"Expected status code xxxx, got {code} with data: {data}" |
| 78 | +``` |
| 79 | + |
| 80 | +## Harvester API Manager Template |
| 81 | + |
| 82 | +Use this template when implementing a new Harvester API manager. |
| 83 | + |
| 84 | +```python |
| 85 | +import base64 |
| 86 | +from .base import BaseManager, DEFAULT_NAMESPACE |
| 87 | + |
| 88 | + |
| 89 | +class XxxsManager(BaseManager): |
| 90 | + PATH_fmt = "/API/v1/namespaces/{ns}/xxx/{name}" |
| 91 | + CREATE_fmt = "v1/harvester/xxx" |
| 92 | + |
| 93 | + def create_data(self, name, namespace, data, annotations=None): |
| 94 | + return { |
| 95 | + "type": "xxx", |
| 96 | + "metadata": { |
| 97 | + "namespace": namespace, |
| 98 | + "name": name, |
| 99 | + "annotations": annotations, |
| 100 | + }, |
| 101 | + } |
| 102 | + |
| 103 | + def create(self, name, data, namespace=DEFAULT_NAMESPACE, annotations=None, **kwargs): |
| 104 | + data = self.create_data(name, namespace, data, annotations=annotations) |
| 105 | + return self._create(self.CREATE_fmt, json=data, **kwargs) |
| 106 | + |
| 107 | + def get(self, name, namespace=DEFAULT_NAMESPACE, *, raw=False): |
| 108 | + return self._get(self.PATH_fmt.format(ns=namespace, name=name), raw=raw) |
| 109 | + |
| 110 | + def delete(self, name, namespace=DEFAULT_NAMESPACE, *, raw=False): |
| 111 | + return self._delete(self.PATH_fmt.format(ns=namespace, name=name), raw=raw) |
| 112 | +``` |
| 113 | + |
| 114 | + |
| 115 | +## Useful Fixtures |
| 116 | + |
| 117 | +### `ImageChecker` |
| 118 | + |
| 119 | +After a image is created or deleted, we need to check if it's done. So, we'll use the methods in [image_checker](../../harvester_e2e_tests/fixtures/images.py) to verfify it. |
0 commit comments