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
def test_if_response_not_201_raise_error(): token = 'my-test-token-123' aut = Automator(token) droplet_id = 212611563 full_url = 'https://api.digitalocean.com/v2/droplets/%s/actions' % droplet_id responses.reset() error_404_response(full_url) with pytest.raises(TurnoffDropletError): aut.turnoff_droplet(droplet_id) responses.reset()
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
def step_impl(context): aut = Automator(context.token) aut.turnoff_droplet(context.droplet_id)
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)
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() for droplet in droplets: if droplet['name'] == name: aut.turnoff_droplet(droplet['id']) aut.destroy_droplet(droplet['id'])