test_digital_ocean.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import pytest
  2. from gpustack.cloud_providers.digital_ocean import DigitalOceanClient
  3. from gpustack.cloud_providers.abstract import CloudInstanceCreate, InstanceState
  4. from gpustack.cloud_providers.abstract import Volume
  5. class DummyClient:
  6. def __init__(self, *args, **kwargs):
  7. pass
  8. class droplets:
  9. @staticmethod
  10. async def create(body):
  11. return {'droplet': {'id': '12345'}}
  12. @staticmethod
  13. async def destroy(external_id):
  14. return None
  15. @staticmethod
  16. async def destroy_with_associated_resources_dangerous(
  17. external_id: str, x_dangerous: bool
  18. ):
  19. return None
  20. @staticmethod
  21. async def get(external_id):
  22. return {
  23. 'droplet': {
  24. 'id': external_id,
  25. 'name': 'test-droplet',
  26. 'image': {'slug': 'ubuntu-20-04-x64'},
  27. 'size_slug': 's-1vcpu-1gb',
  28. 'region': {'slug': 'nyc3'},
  29. 'networks': {'v4': [{'type': 'public', 'ip_address': '1.2.3.4'}]},
  30. 'status': 'active',
  31. 'volume_ids': ['vol-1'],
  32. }
  33. }
  34. class ssh_keys:
  35. @staticmethod
  36. async def create(body):
  37. return {'ssh_key': {'id': 'ssh-1'}}
  38. @staticmethod
  39. async def delete(id):
  40. return None
  41. class volumes:
  42. @staticmethod
  43. async def create(body):
  44. return {'volume': {'id': 'vol-1'}}
  45. class volume_actions:
  46. @staticmethod
  47. async def post_by_id(volume_id, body):
  48. return {"action": {"status": "completed"}}
  49. @pytest.fixture
  50. def do_client(monkeypatch):
  51. monkeypatch.setattr('gpustack.cloud_providers.digital_ocean.Client', DummyClient)
  52. return DigitalOceanClient(token='dummy-token')
  53. @pytest.mark.asyncio
  54. async def test_create_instance(do_client):
  55. instance = CloudInstanceCreate(
  56. name='test-droplet',
  57. image='ubuntu-20-04-x64',
  58. type='s-1vcpu-1gb',
  59. region='nyc3',
  60. ssh_key_id='ssh-1',
  61. user_data=None,
  62. labels={'env': 'test'},
  63. )
  64. droplet_id = await do_client.create_instance(instance)
  65. assert droplet_id == '12345'
  66. @pytest.mark.asyncio
  67. async def test_delete_instance(do_client):
  68. await do_client.delete_instance('12345')
  69. @pytest.mark.asyncio
  70. async def test_get_instance(do_client):
  71. instance = await do_client.get_instance('12345')
  72. assert instance is not None
  73. assert instance.external_id == '12345'
  74. assert instance.status == InstanceState.RUNNING
  75. assert instance.ip_address == '1.2.3.4'
  76. @pytest.mark.asyncio
  77. async def test_wait_for_started(do_client):
  78. instance = await do_client.get_instance('12345')
  79. started = await do_client.wait_for_started(instance, backoff=0, limit=2)
  80. assert started.status == InstanceState.RUNNING
  81. @pytest.mark.asyncio
  82. async def test_create_ssh_key(do_client):
  83. ssh_id = await do_client.create_ssh_key(
  84. 'worker1', 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC'
  85. )
  86. assert ssh_id == 'ssh-1'
  87. @pytest.mark.asyncio
  88. async def test_delete_ssh_key(do_client):
  89. await do_client.delete_ssh_key('ssh-1')
  90. @pytest.mark.asyncio
  91. async def test_create_volumes_and_attach(do_client):
  92. volume_ids = await do_client.create_volumes_and_attach(
  93. 12, '12345', 'nyc3', Volume(size_gb=10, format='ext4')
  94. )
  95. assert volume_ids == ['vol-1']