def test_get_all_droplets_call_requests_get_once(self):
     aut = Automator('whatever-test-token')
     stashed = aut.requests.get
     aut.requests.get = MagicMock()
     aut.get_all_droplets()
     aut.requests.get.assert_called_once()
     aut.requests.get = stashed
 def test_get_all_droplets_should_return_list(self):
     aut = Automator('whatever-test-token')
     list_droplets_response()
     stashed = aut.requests.get
     aut.requests.get = MagicMock()
     aut.requests.get.side_effect = self.get_responses()
     assert type(aut.get_all_droplets()) is list
     aut.requests.get = stashed
 def test_get_all_droplets_call_requests_w_args(self):
     aut = Automator('whatever-test-token')
     stashed = aut.requests.get
     aut.requests.get = MagicMock()
     aut.get_all_droplets()
     aut.requests.get.assert_called_once_with(
         'https://api.digitalocean.com/v2/droplets')
     aut.requests.get = stashed
 def test_get_all_droplets_item_should_contain_keys(self):
     some_expected_keys = ['id', 'name', 'region']
     aut = Automator('whatever-test-token')
     list_droplets_response()
     stashed = aut.requests.get
     aut.requests.get = MagicMock()
     aut.requests.get.side_effect = self.get_responses()
     assert set(some_expected_keys) <= set(aut.get_all_droplets()[0])
     aut.requests.get = stashed
Ejemplo n.º 5
0
def test_if_response_status_204_return_completed():
    token = 'my-test-token-123'
    aut = Automator(token)
    droplet_id = 212611563

    # response mock w/ 204 response
    responses.reset()
    destroy_droplet_success_response(droplet_id)

    result = aut.destroy_droplet(droplet_id)
    assert result == 'completed'
Ejemplo n.º 6
0
def test_if_response_not_204_raise_error():
    token = 'my-test-token-123'
    aut = Automator(token)
    droplet_id = 212611563

    full_url = 'https://api.digitalocean.com/v2/droplets/%s' % droplet_id

    responses.reset()
    error_404_response(full_url)

    with pytest.raises(DestroyDropletError):
        aut.destroy_droplet(droplet_id)
def test_if_response_not_201_raise_error():
    token = 'my-test-token-123'
    aut = Automator(token)
    floating_ip = '135.155.152.2'
    droplet_id = 42143152
    url = 'https://api.digitalocean.com/v2/floating_ips/%s/actions' % floating_ip

    responses.reset()
    error_404_response(url)

    with pytest.raises(FloatingIpAssignmentError):
        aut.assign_floating_ip_to_droplet(floating_ip, droplet_id)
        responses.reset()
def test_if_response_status_201_return_completed():
    token = 'my-test-token-123'
    aut = Automator(token)
    floating_ip = '135.155.152.2'
    droplet_id = 42143152
    # response mock w/ 201 response
    assign_floating_ip_success_response(floating_ip, droplet_id)

    stashed = aut._Automator__check_action_status
    aut._Automator__check_action_status = MagicMock(return_value='completed')

    result = aut.assign_floating_ip_to_droplet(floating_ip, droplet_id)
    assert result == 'completed'
    aut._Automator__check_action_status = stashed
    def test_request_get_should_be_called_3_times(self):
        responses.reset()
        aut = Automator('whatever-test-token')

        # mocks
        list_droplets_response()
        stashed = aut.requests.get
        aut.requests.get = MagicMock()
        aut.requests.get.side_effect = self.get_responses()

        aut.get_all_droplets()

        assert aut.requests.get.call_count == 3
        aut.requests.get = stashed
Ejemplo n.º 10
0
def test_if_response_status_201_return_completed():
    token = 'my-test-token-123'
    aut = Automator(token)
    droplet_id = 212611563
    # response mock w/ 201 response
    shutdown_droplet_success_response(droplet_id)

    stashed = aut._Automator__check_action_status

    aut._Automator__check_action_status = MagicMock(return_value='completed')

    result = aut.turnoff_droplet(droplet_id)
    assert result == 'completed'
    aut._Automator__check_action_status = stashed
Ejemplo n.º 11
0
def step_impl(context):
    aut = Automator(context.token)

    droplet_data = {
        "name": "t1.techno24x7.com",
        "region": "nyc1",
        "size": "s-8vcpu-16gb",
        "image": 68259296,
        "ssh_keys": [27410347, 27608055, 27590881],
        "private_networking": True,
        "vpc_uuid": "47e5c00a-2b23-4dac-bed4-0e44659941f3",
        "monitoring": True
    }

    droplet = Droplet(**droplet_data)

    aut.create_droplet_from_snapshot(droplet)
def test_assert_request_post_is_called_with_args():
    floating_ip = '124.52.234.6'
    droplet_id = 42312552

    full_url = 'https://api.digitalocean.com/v2/floating_ips/%s/actions' % floating_ip
    data = {"type": "assign", "droplet_id": 8219222}
    json_headers = {'Content-Type': 'application/json'}

    token = 'my-test-token-123'
    aut = Automator(token)

    stashed = aut.requests.post
    stashed2 = aut._Automator__check_action_status

    # mock response w/ success status
    responses.reset()
    assign_floating_ip_success_response(floating_ip, droplet_id)
    mocked_response = requests.post(full_url)

    aut._Automator__check_action_status = MagicMock(return_value='completed')
    aut.requests.post = MagicMock(return_value=mocked_response)

    aut.assign_floating_ip_to_droplet('124.52.234.6', 8219222)

    aut.requests.post.assert_called_once_with(full_url,
                                              json=data,
                                              headers=json_headers)

    aut.requests.post = stashed
    aut._Automator__check_action_status = stashed2
Ejemplo n.º 13
0
def test_assert_requests_post_is_called_once():
    token = 'my-test-token-123'
    aut = Automator(token)
    stashed = aut.requests.post
    droplet_id = 212611563

    full_url = 'https://api.digitalocean.com/v2/droplets/%s/actions' % droplet_id

    data = {"type": "shutdown"}
    json_headers = {'Content-Type': 'application/json'}

    # mock response w/ success status
    responses.reset()
    shutdown_droplet_success_response(droplet_id)
    mocked_response = requests.post(full_url)

    stashed = aut._Automator__check_action_status
    stashed2 = aut.requests.post
    aut._Automator__check_action_status = MagicMock(return_value='completed')
    aut.requests.post = MagicMock(return_value=mocked_response)

    aut.turnoff_droplet(droplet_id)

    aut.requests.post.assert_called_once_with(full_url,
                                              json=data,
                                              headers=json_headers)

    aut._Automator__check_action_status.assert_called_once_with(
        mocked_response.json()['action']['id'])

    aut._Automator__check_action_status = stashed
    aut.requests.post = stashed2
Ejemplo n.º 14
0
def test_assert_requests_delete_is_called_once_with_args():
    token = 'my-test-token-123'
    aut = Automator(token)
    droplet_id = 212611563

    full_url = 'https://api.digitalocean.com/v2/droplets/%s' % droplet_id

    # stash to mock
    stashed2 = aut.requests.delete

    # mocks
    responses.reset()
    destroy_droplet_success_response(droplet_id)
    mocked_response = requests.delete(full_url)
    aut.requests.delete = MagicMock(return_value=mocked_response)

    # test
    aut.destroy_droplet(droplet_id)
    aut.requests.delete.assert_called_once_with(full_url)

    # pop stashed
    aut.requests.delete = stashed2
    def test_sum_all_droplets_should_be_the_same_as_meta(self):
        # test failing cauz of responses bug: https://github.com/getsentry/responses/issues/352
        # responses.reset()
        # aut = Automator('whatever-test-token')
        # meta_total = get_json_from_file(
        #     'list_droplets_page_1.json')['meta']['total']
        # list_droplets_response()
        # assert len(aut.get_all_droplets()) == meta_total

        # rewriting temporary:
        aut = Automator('whatever-test-token')
        meta_total = get_json_from_file(
            'list_droplets_page_1.json')['meta']['total']

        # mocks
        list_droplets_response()
        stashed = aut.requests.get
        aut.requests.get = MagicMock()
        aut.requests.get.side_effect = self.get_responses()

        assert len(aut.get_all_droplets()) == meta_total

        aut.requests.get = stashed
def test_assert_requests_post_is_called_once():
    token = 'my-test-token-123'
    aut = Automator(token)
    stashed = aut.requests.post
    floating_ip = '124.52.234.6'
    droplet_id = 42312552

    full_url = 'https://api.digitalocean.com/v2/floating_ips/%s/actions' % floating_ip

    # mock response w/ success status
    responses.reset()
    assign_floating_ip_success_response(floating_ip, droplet_id)
    mocked_response = requests.post(full_url)

    stashed = aut._Automator__check_action_status
    stashed2 = aut.requests.post
    aut._Automator__check_action_status = MagicMock(return_value='completed')
    aut.requests.post = MagicMock(return_value=mocked_response)
    aut.assign_floating_ip_to_droplet(floating_ip, droplet_id)

    aut.requests.post.assert_called_once()

    aut._Automator__check_action_status = stashed
    aut.requests.post = stashed2
def step_impl(context):
    aut = Automator(context.token)
    aut.assign_floating_ip_to_droplet(context.floating_ip, context.droplet_id)
from pydoautomator import Automator, Droplet
import os

digital_ocean_token = os.getenv("DO_TOKEN")

aut = Automator(digital_ocean_token)

# requires ENV
name = os.getenv('DROPLET_HOST')
floating = os.getenv('FLOATING_IP')
snapshot = os.getenv('SNAPSHOT_ID')


droplet_data = {
    "name": name,
    "region": "nyc1",
    "size": "s-8vcpu-16gb",
    "image": snapshot,
    "ssh_keys": [27410347, 27608055, 27590881, 28749641],
    "private_networking": True,
    "vpc_uuid": "47e5c00a-2b23-4dac-bed4-0e44659941f3",
    "monitoring": True
}

droplet = Droplet(**droplet_data)

droplet_id = aut.create_droplet_from_snapshot(droplet)

action_status = aut.assign_floating_ip_to_droplet(floating, droplet_id)

if action_status == 'completed':
import ipdb
from pydoautomator import Automator
import os

digital_ocean_token = os.getenv("DO_TOKEN")

# requires ENV
name = os.getenv('DROPLET_HOST')
floating = os.getenv('FLOATING_IP')
snapshot = os.getenv('SNAPSHOT_ID')


aut = Automator(digital_ocean_token)

droplets = aut.get_all_droplets()

print('trying to turn off droplet named %s' % name )
for droplet in droplets:
    if droplet['name'] == name:
        droplet_id = droplet['id']
        print('turning off droplet id %s' % droplet_id)
        aut.turnoff_droplet(droplet_id)
        print('destroying droplet id %s' % droplet_id)
        aut.destroy_droplet(droplet_id)
Ejemplo n.º 20
0
def step_impl(context):
    aut = Automator(context.token)
    aut.turnoff_droplet(context.droplet_id)
def step_impl(context):
    aut = Automator(context.token)
    aut.destroy_droplet(context.droplet_id)