-
Notifications
You must be signed in to change notification settings - Fork 14
test: add e2e instance test #1037
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a39c1ca
test: add instance test
Laure-di 7a77ca0
last update
Laure-di a5e389b
Delete docs/module.rst
Laure-di 7b3c226
tests: skip api-test
Laure-di b998fe3
fix: remove try except from teardown, make const
Laure-di 9ba8427
Update scaleway/tests/test_instance.py
Laure-di e7f28fd
fix: remove try except from teardown, make const
Laure-di 74b9c21
fix: add waiter additionnal volume
Laure-di fb485ab
remove unnecessary else
Laure-di 8b1641a
Improve code readability
Laure-di 48f4106
chore: fix make test
Laure-di File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| import logging | ||
| import sys | ||
| from typing import List | ||
| import unittest | ||
| import uuid | ||
| import time | ||
|
|
||
| from scaleway_core.client import Client | ||
| from scaleway.instance.v1.api import InstanceV1API | ||
| from scaleway.block.v1alpha1 import BlockV1Alpha1API | ||
| from scaleway.instance.v1.types import Server, VolumeServerTemplate | ||
| from scaleway.block.v1alpha1.types import Volume, CreateVolumeRequestFromEmpty | ||
|
|
||
| logger = logging.getLogger() | ||
| logger.level = logging.DEBUG | ||
| stream_handler = logging.StreamHandler(sys.stdout) | ||
| logger.addHandler(stream_handler) | ||
|
|
||
| server_name = f"test-sdk-python-{uuid.uuid4().hex[:6]}" | ||
| max_retry = 10 | ||
| interval = 0.1 | ||
| volume_size = 10 | ||
| commercial_type = "DEV1-S" | ||
| zone = "fr-par-1" | ||
|
|
||
|
|
||
| class TestE2EServerCreation(unittest.TestCase): | ||
| def setUp(self) -> None: | ||
| self.zone = zone | ||
| self.client = Client.from_config_file_and_env() | ||
| self.instanceAPI = InstanceV1API(self.client, bypass_validation=True) | ||
| self.blockAPI = BlockV1Alpha1API(self.client, bypass_validation=True) | ||
| self._server = None | ||
| self._volumes = [] | ||
|
|
||
| def tearDown(self) -> None: | ||
| for volume in self._volumes: | ||
| self.instanceAPI.detach_server_volume( | ||
| server_id=self._server.id, volume_id=volume.id | ||
| ) | ||
| logger.info("✅ Volume {volume.id} has been detach") | ||
|
|
||
| self.blockAPI.delete_volume(volume_id=volume.id) | ||
| logger.info("✅ Volume {volume.id} has been deleted") | ||
|
|
||
| if self._server: | ||
| self.instanceAPI.delete_server(zone=self.zone, server_id=self._server.id) | ||
| logger.info(f"🗑️ Deleted server: {self._server.id}") | ||
|
|
||
| def wait_test_instance_server(self, server_id): | ||
| interval = interval | ||
|
|
||
| for i in range(1, max_retry): | ||
| interval *= i | ||
| s = self.instanceAPI.get_server(zone=self.zone, server_id=server_id) | ||
|
|
||
| if s.state == "running": | ||
| logger.info(f"✅ Server {server_id} is running.") | ||
| break | ||
|
|
||
| time.sleep(interval) | ||
|
|
||
| self.fail("Server did not reach 'running' state in time.") | ||
|
|
||
| def create_test_instance_server(self) -> Server: | ||
| volume = { | ||
| "0": VolumeServerTemplate( | ||
| volume_type="sbs_volume", name="my-volume", size=volume_size | ||
| ) | ||
| } | ||
|
|
||
| server = self.instanceAPI._create_server( | ||
| commercial_type=commercial_type, | ||
| zone=self.zone, | ||
| name=server_name, | ||
| dynamic_ip_required=True, | ||
| volumes=volume, | ||
| ) | ||
| logger.info(f"✅ Created server: {server.id}") | ||
|
|
||
| self._server = server.server | ||
|
|
||
| self.wait_test_instance_server(server_id=server.server.id) | ||
|
|
||
| return server.server | ||
|
|
||
| def create_test_from_empty_volume(self, number) -> List[Volume]: | ||
| volumes: List[Volume] = {} | ||
|
|
||
| for i in range(number): | ||
| volume = self.blockAPI.create_volume( | ||
| from_empty=CreateVolumeRequestFromEmpty(size=10), | ||
| ) | ||
| logger.info("✅ Created server: {volume.id}") | ||
|
|
||
| self.blockAPI.wait_for_volume(volume_id=volume.id, zone=self.zone) | ||
|
|
||
| self._volumes.append(volume) # Ensure cleanup in tearDown | ||
| volumes.append(volume) | ||
|
|
||
| return volumes | ||
|
|
||
| def test_attach_aditionnal_volume(self): | ||
| server = self.create_test_instance_server() | ||
| additional_volumes = self.create_test_from_empty_volume(1) | ||
| additional_volume = additional_volumes.values()[0] | ||
|
|
||
| self.assertIsNotNone(server.id) | ||
| self.assertEqual(server.zone, self.zone) | ||
|
|
||
| self.assertIsNotNone(additional_volume.id) | ||
| self.assertEqual(additional_volume.size, 10) | ||
| logger.info(f"✅ Volume created with ID: {additional_volume.id}") | ||
|
|
||
| self.instanceAPI.attach_server_volume( | ||
| server_id=server.id, volume_id=additional_volume.id | ||
| ) | ||
|
|
||
| self.blockAPI.wait_for_volume(volume_id=additional_volume.id, zone=self.zone) | ||
|
|
||
| logger.info(f"🔗 Attached volume {additional_volume.id} to server {server.id}") | ||
|
|
||
| updated_server = self.instanceAPI.get_server( | ||
| zone=self.zone, server_id=server.id | ||
| ) | ||
| attached_volumes = updated_server.volumes or {} | ||
| attached_volume_ids = [v.volume.id for v in attached_volumes.values()] | ||
| self.assertIn(additional_volume.id, attached_volume_ids) | ||
| logger.info( | ||
| f"✅ Volume {additional_volume.id} is attached to server {server.id}" | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should remove logging from the fixtures