Beispiel #1
0
def test_properties():
    assert str(
        Lizzy('https://lizzy.example',
              '7E5770K3N').stacks_url) == 'https://lizzy.example/api/stacks'
    assert str(
        Lizzy('https://lizzy-2.example',
              '7E5770K3N').stacks_url) == 'https://lizzy-2.example/api/stacks'
Beispiel #2
0
def test_delete(monkeypatch):
    mock_delete = MagicMock()
    monkeypatch.setattr('requests.delete', mock_delete)

    lizzy = Lizzy('https://lizzy.example', '7E5770K3N')
    lizzy.delete('574CC')

    header = make_header('7E5770K3N')
    mock_delete.assert_called_once_with('https://lizzy.example/stacks/574CC', headers=header, verify=False)
Beispiel #3
0
def test_delete(monkeypatch):
    mock_delete = MagicMock()
    monkeypatch.setattr('requests.delete', mock_delete)

    lizzy = Lizzy('https://lizzy.example', '7E5770K3N')
    lizzy.delete('574CC')

    header = make_header('7E5770K3N')
    mock_delete.assert_called_once_with('https://lizzy.example/stacks/574CC', headers=header, verify=False)
Beispiel #4
0
def test_traffic(monkeypatch):
    mock_patch = MagicMock()
    mock_patch.return_value = FakeResponse(200, '["stack1","stack2"]')
    monkeypatch.setattr('requests.patch', mock_patch)

    lizzy = Lizzy('https://lizzy.example', '7E5770K3N')
    lizzy.traffic('574CC', 42)

    header = make_header('7E5770K3N')
    mock_patch.assert_called_once_with('https://lizzy.example/stacks/574CC', headers=header, data='{"new_traffic": 42}',
                                       verify=False)
Beispiel #5
0
def test_traffic(monkeypatch):
    mock_patch = MagicMock()
    mock_patch.return_value = FakeResponse(200, '["stack1","stack2"]')
    monkeypatch.setattr('requests.patch', mock_patch)

    lizzy = Lizzy('https://lizzy.example', '7E5770K3N')
    lizzy.traffic('574CC', 42)

    header = make_header('7E5770K3N')
    mock_patch.assert_called_once_with('https://lizzy.example/stacks/574CC', headers=header, data='{"new_traffic": 42}',
                                       verify=False)
Beispiel #6
0
def test_get_stack(monkeypatch):
    mock_get = MagicMock()
    mock_get.return_value = FakeResponse(200, '{"stack":"fake"}')
    monkeypatch.setattr('requests.get', mock_get)

    lizzy = Lizzy('https://lizzy.example', '7E5770K3N')
    stack = lizzy.get_stack('574CC')

    header = make_header('7E5770K3N')
    mock_get.assert_called_once_with('https://lizzy.example/stacks/574CC', None, headers=header, verify=False)

    assert stack['stack'] == 'fake'
Beispiel #7
0
def test_get_stacks(monkeypatch):
    mock_get = MagicMock()
    mock_get.return_value = FakeResponse(200, '["stack1","stack2"]')
    monkeypatch.setattr('requests.get', mock_get)

    lizzy = Lizzy('https://lizzy.example', '7E5770K3N')
    stacks = lizzy.get_stacks()

    header = make_header('7E5770K3N')
    mock_get.assert_called_once_with('https://lizzy.example/stacks', None, headers=header, verify=False)

    assert stacks == ["stack1", "stack2"]
Beispiel #8
0
def test_get_stacks(monkeypatch):
    mock_get = MagicMock()
    mock_get.return_value = FakeResponse(200, '["stack1","stack2"]')
    monkeypatch.setattr('requests.get', mock_get)

    lizzy = Lizzy('https://lizzy.example', '7E5770K3N')
    stacks = lizzy.get_stacks()

    header = make_header('7E5770K3N')
    mock_get.assert_called_once_with('https://lizzy.example/stacks', None, headers=header, verify=False)

    assert stacks == ["stack1", "stack2"]
Beispiel #9
0
def test_get_stack(monkeypatch):
    mock_get = MagicMock()
    mock_get.return_value = FakeResponse(200, '{"stack":"fake"}')
    monkeypatch.setattr('requests.get', mock_get)

    lizzy = Lizzy('https://lizzy.example', '7E5770K3N')
    stack = lizzy.get_stack('574CC')

    header = make_header('7E5770K3N')
    mock_get.assert_called_once_with('https://lizzy.example/stacks/574CC', None, headers=header, verify=False)

    assert stack['stack'] == 'fake'
Beispiel #10
0
def test_delete(monkeypatch, stack_id, region, dry_run):
    mock_delete = MagicMock()
    monkeypatch.setattr('requests.delete', mock_delete)

    lizzy = Lizzy('https://lizzy.example', '7E5770K3N')
    lizzy.delete(stack_id, region=region, dry_run=dry_run)

    header = make_header('7E5770K3N')
    url = 'https://lizzy.example/api/stacks/{}'.format(stack_id)
    expected_data = {"region": region, "dry_run": dry_run}
    mock_delete.assert_called_once_with(url,
                                        json=expected_data,
                                        headers=header,
                                        verify=False)
Beispiel #11
0
def test_delete(monkeypatch, stack_id, region, dry_run):
    mock_delete = MagicMock()
    monkeypatch.setattr('requests.delete', mock_delete)

    lizzy = Lizzy('https://lizzy.example', '7E5770K3N')
    lizzy.delete(stack_id, region=region, dry_run=dry_run)

    header = make_header('7E5770K3N')
    url = 'https://lizzy.example/api/stacks/{}'.format(stack_id)
    expected_data = {"region": region, "dry_run": dry_run}
    mock_delete.assert_called_once_with(url,
                                        json=expected_data,
                                        headers=header,
                                        verify=False)
Beispiel #12
0
def test_traffic(monkeypatch):
    mock_patch = MagicMock()
    mock_patch.return_value = FakeResponse(200, '["stack1","stack2"]')
    monkeypatch.setattr('requests.patch', mock_patch)

    lizzy = Lizzy('https://lizzy.example', '7E5770K3N')
    lizzy.traffic('574CC', 42)

    header = make_header('7E5770K3N')
    mock_patch.assert_called_once_with(
        'https://lizzy.example/api/stacks/574CC',
        headers=header,
        data=None,
        json={"new_traffic": 42},
        verify=False)

    # call with region payload
    mock_patch.reset_mock()
    lizzy = Lizzy('https://lizzy.example', '7E5770K3N')
    lizzy.traffic('574CC', 42, region='ab-foo-7')

    header = make_header('7E5770K3N')
    mock_patch.assert_called_once_with(
        'https://lizzy.example/api/stacks/574CC',
        headers=header,
        data=None,
        json={
            'new_traffic': 42,
            'region': 'ab-foo-7'
        },
        verify=False)
Beispiel #13
0
def test_get_traffic(monkeypatch):
    mock_request = MagicMock()
    mock_request.return_value = FakeResponse(200, '{"weight": 100.0}')
    monkeypatch.setattr('requests.get', mock_request)

    lizzy = Lizzy('https://lizzy.example', '7E5770K3N')
    lizzy.get_traffic('lizzy-test')

    header = make_header('7E5770K3N')
    mock_request.assert_called_once_with(
        'https://lizzy.example/api/stacks/lizzy-test/traffic',
        None,
        headers=header,
        verify=False)

    mock_request.reset_mock()
    mock_request.return_value = FakeResponse(200, '["stack1","stack2"]')

    lizzy = Lizzy('https://lizzy.example', '7E5770K3N')
    lizzy.get_traffic('574CC', region='ab-foo-7')

    header = make_header('7E5770K3N')
    url = 'https://lizzy.example/api/stacks/574CC/traffic?region=ab-foo-7'
    mock_request.assert_called_once_with(url,
                                         None,
                                         headers=header,
                                         verify=False)
Beispiel #14
0
def test_scale(monkeypatch):
    mock_patch = MagicMock()
    monkeypatch.setattr('requests.patch', mock_patch)

    lizzy = Lizzy('https://lizzy.example', '7E5770K3N')
    lizzy.scale('574CC', 3)

    header = make_header('7E5770K3N')
    mock_patch.assert_called_once_with(
        'https://lizzy.example/api/stacks/574CC',
        headers=header,
        data=None,
        json={"new_scale": 3},
        verify=False)

    # call with region payload
    mock_patch.reset_mock()
    lizzy = Lizzy('https://lizzy.example', '7E5770K3N')
    lizzy.scale('574CC', 3, region='ab-foo-7')

    header = make_header('7E5770K3N')
    mock_patch.assert_called_once_with(
        'https://lizzy.example/api/stacks/574CC',
        headers=header,
        data=None,
        json={
            'new_scale': 3,
            'region': 'ab-foo-7'
        },
        verify=False)
Beispiel #15
0
def test_wait_for_deployment(monkeypatch):
    monkeypatch.setattr('time.sleep', MagicMock())
    mock_get_stack = MagicMock()
    mock_get_stack.side_effect = [{'status': 'CF:SOME_STATE'}, {'status': 'CF:SOME_STATE'},
                                  {'status': 'CF:SOME_OTHER_STATE'}, {'status': 'CF:CREATE_COMPLETE'}]
    monkeypatch.setattr('lizzy_client.lizzy.Lizzy.get_stack', mock_get_stack)

    lizzy = Lizzy('https://lizzy.example', '7E5770K3N')
    states = list(lizzy.wait_for_deployment('574CC1D'))

    assert states == ['CF:SOME_STATE', 'CF:SOME_STATE', 'CF:SOME_OTHER_STATE', 'CF:CREATE_COMPLETE']

    mock_get_stack.reset_mock()
    mock_get_stack.side_effect = [{'wrong_key': 'CF:SOME_STATE'}, {'wrong_key': 'CF:SOME_STATE'},
                                  {'wrong_key': 'CF:SOME_STATE'}]
    states = list(lizzy.wait_for_deployment('574CC1D'))
    assert states == ["Failed to get stack (2 retries left): KeyError('status',).",
                      "Failed to get stack (1 retries left): KeyError('status',).",
                      "Failed to get stack (0 retries left): KeyError('status',).", ]
Beispiel #16
0
def test_wait_for_deployment(monkeypatch):
    monkeypatch.setattr('time.sleep', MagicMock())
    mock_get_stack = MagicMock()
    mock_get_stack.side_effect = [{'status': 'CF:SOME_STATE'}, {'status': 'CF:SOME_STATE'},
                                  {'status': 'CF:SOME_OTHER_STATE'}, {'status': 'CF:CREATE_COMPLETE'}]
    monkeypatch.setattr('lizzy_client.lizzy.Lizzy.get_stack', mock_get_stack)

    lizzy = Lizzy('https://lizzy.example', '7E5770K3N')
    states = list(lizzy.wait_for_deployment('574CC1D'))

    assert states == ['CF:SOME_STATE', 'CF:SOME_STATE', 'CF:SOME_OTHER_STATE', 'CF:CREATE_COMPLETE']

    mock_get_stack.reset_mock()
    mock_get_stack.side_effect = [{'wrong_key': 'CF:SOME_STATE'}, {'wrong_key': 'CF:SOME_STATE'},
                                  {'wrong_key': 'CF:SOME_STATE'}]
    states = list(lizzy.wait_for_deployment('574CC1D'))
    assert states == ["Failed to get stack (2 retries left): KeyError('status',).",
                      "Failed to get stack (1 retries left): KeyError('status',).",
                      "Failed to get stack (0 retries left): KeyError('status',).", ]
Beispiel #17
0
def test_traffic(monkeypatch):
    mock_patch = MagicMock()
    mock_patch.return_value = FakeResponse(200, '["stack1","stack2"]')
    monkeypatch.setattr('requests.patch', mock_patch)

    lizzy = Lizzy('https://lizzy.example', '7E5770K3N')
    lizzy.traffic('574CC', 42)

    header = make_header('7E5770K3N')
    mock_patch.assert_called_once_with('https://lizzy.example/api/stacks/574CC',
                                       headers=header,
                                       data=None,
                                       json={"new_traffic": 42},
                                       verify=False)

    # call with region payload
    mock_patch.reset_mock()
    lizzy = Lizzy('https://lizzy.example', '7E5770K3N')
    lizzy.traffic('574CC', 42, region='ab-foo-7')

    header = make_header('7E5770K3N')
    mock_patch.assert_called_once_with('https://lizzy.example/api/stacks/574CC',
                                       headers=header,
                                       data=None,
                                       json={'new_traffic': 42,
                                             'region': 'ab-foo-7'},
                                       verify=False)
Beispiel #18
0
def test_new_stack(monkeypatch, version, parameters, region, disable_rollback,
                   dry_run, force, tags, keep_stacks, new_traffic):
    mock_post = MagicMock()
    mock_post.return_value = FakeResponse(200, STACK1)
    monkeypatch.setattr('requests.post', mock_post)
    lizzy = Lizzy('https://lizzy.example', '7E5770K3N')
    stack, output = lizzy.new_stack(keep_stacks=keep_stacks,
                                    new_traffic=new_traffic,
                                    senza_yaml={'MyDefinition': 'Values'},
                                    stack_version=version,
                                    disable_rollback=disable_rollback,
                                    parameters=parameters,
                                    dry_run=dry_run,
                                    region=region,
                                    tags=tags)
    stack_name = stack['stack_name']
    assert stack_name == 'lizzy-bus'

    header = make_header('7E5770K3N')
    data = {
        'keep_stacks': keep_stacks,
        'new_traffic': new_traffic,
        'parameters': parameters,
        'disable_rollback': disable_rollback,
        'dry_run': dry_run,
        'senza_yaml': "{MyDefinition: Values}\n",
        'stack_version': version,
        'tags': tags
    }

    # only pass region when it is not None
    if region:
        data['region'] = region

    mock_post.assert_called_once_with('https://lizzy.example/api/stacks',
                                      headers=header,
                                      json=data,
                                      data=None,
                                      verify=False)
Beispiel #19
0
def test_new_stack(monkeypatch,
                   version, parameters, region, disable_rollback, dry_run,
                   force, tags, keep_stacks, new_traffic):
    mock_post = MagicMock()
    mock_post.return_value = FakeResponse(200, STACK1)
    monkeypatch.setattr('requests.post', mock_post)
    lizzy = Lizzy('https://lizzy.example', '7E5770K3N')
    stack, output = lizzy.new_stack(keep_stacks=keep_stacks,
                                    new_traffic=new_traffic,
                                    senza_yaml={'MyDefinition': 'Values'},
                                    stack_version=version,
                                    disable_rollback=disable_rollback,
                                    parameters=parameters,
                                    dry_run=dry_run,
                                    region=region,
                                    tags=tags)
    stack_name = stack['stack_name']
    assert stack_name == 'lizzy-bus'

    header = make_header('7E5770K3N')
    data = {'keep_stacks': keep_stacks,
            'new_traffic': new_traffic,
            'parameters': parameters,
            'disable_rollback': disable_rollback,
            'dry_run': dry_run,
            'senza_yaml': "{MyDefinition: Values}\n",
            'stack_version': version,
            'tags': tags}

    # only pass region when it is not None
    if region:
        data['region'] = region

    mock_post.assert_called_once_with('https://lizzy.example/api/stacks',
                                      headers=header,
                                      json=data,
                                      data=None,
                                      verify=False)
Beispiel #20
0
def test_new_stack(monkeypatch):
    test_dir = os.path.dirname(__file__)
    yaml_path = os.path.join(test_dir, 'test_config.yaml')  # we can use any file for this test
    with open(yaml_path) as yaml_file:
        senza_yaml = yaml_file.read()

    mock_post = MagicMock()
    mock_post.return_value = FakeResponse(200, '{"stack_id":"574CC1D"}')
    monkeypatch.setattr('requests.post', mock_post)

    lizzy = Lizzy('https://lizzy.example', '7E5770K3N')
    stack_id = lizzy.new_stack('10', 2, 42, yaml_path, None, [])

    header = make_header('7E5770K3N')
    data = {'image_version': "10",
            'keep_stacks': 2,
            'new_traffic': 42,
            'parameters': [],
            'senza_yaml': senza_yaml}
    mock_post.assert_called_once_with('https://lizzy.example/stacks', headers=header,
                                      data=json.dumps(data),
                                      json=None,
                                      verify=False)

    assert stack_id == "574CC1D"

    mock_post.reset_mock()
    data_with_ver = {'image_version': "10",
                     'keep_stacks': 2,
                     'new_traffic': 42,
                     'parameters': [],
                     'senza_yaml': senza_yaml,
                     "application_version": "420", }
    lizzy.new_stack('10', 2, 42, yaml_path, "420", [])
    mock_post.assert_called_once_with('https://lizzy.example/stacks', headers=header,
                                      data=json.dumps(data_with_ver),
                                      json=None,
                                      verify=False)

    mock_post.reset_mock()
    data_with_params = {'image_version': "10",
                        'keep_stacks': 2,
                        'new_traffic': 42,
                        'parameters': ['abc', 'def'],
                        'senza_yaml': senza_yaml,
                        "application_version": "420", }
    lizzy.new_stack('10', 2, 42, yaml_path, "420", ['abc', 'def'])
    mock_post.assert_called_once_with('https://lizzy.example/stacks', headers=header,
                                      data=json.dumps(data_with_params),
                                      json=None,
                                      verify=False)
Beispiel #21
0
def test_get_traffic(monkeypatch):
    mock_request = MagicMock()
    mock_request.return_value = FakeResponse(200, '{"weight": 100.0}')
    monkeypatch.setattr('requests.get', mock_request)

    lizzy = Lizzy('https://lizzy.example', '7E5770K3N')
    lizzy.get_traffic('lizzy-test')

    header = make_header('7E5770K3N')
    mock_request.assert_called_once_with(
        'https://lizzy.example/api/stacks/lizzy-test/traffic', None,
        headers=header, verify=False)

    mock_request.reset_mock()
    mock_request.return_value = FakeResponse(200, '["stack1","stack2"]')

    lizzy = Lizzy('https://lizzy.example', '7E5770K3N')
    lizzy.get_traffic('574CC', region='ab-foo-7')

    header = make_header('7E5770K3N')
    url = 'https://lizzy.example/api/stacks/574CC/traffic?region=ab-foo-7'
    mock_request.assert_called_once_with(url, None, headers=header,
                                         verify=False)
Beispiel #22
0
def test_new_stack(monkeypatch):
    test_dir = os.path.dirname(__file__)
    yaml_path = os.path.join(test_dir, 'test_config.yaml')  # we can use any file for this test
    with open(yaml_path) as yaml_file:
        senza_yaml = yaml_file.read()

    mock_post = MagicMock()
    mock_post.return_value = FakeResponse(200, '{"stack_id":"574CC1D"}')
    monkeypatch.setattr('requests.post', mock_post)

    lizzy = Lizzy('https://lizzy.example', '7E5770K3N')
    stack_id = lizzy.new_stack(image_version='10',
                               keep_stacks=2,
                               new_traffic=42,
                               senza_yaml_path=yaml_path,
                               application_version=None,
                               stack_version=None,
                               disable_rollback=True,
                               parameters=[])

    header = make_header('7E5770K3N')
    data = {'image_version': "10",
            'keep_stacks': 2,
            'new_traffic': 42,
            'parameters': [],
            'disable_rollback': True,
            'senza_yaml': senza_yaml}
    mock_post.assert_called_once_with('https://lizzy.example/stacks', headers=header,
                                      data=json.dumps(data,  sort_keys=True),
                                      json=None,
                                      verify=False)

    mock_post.reset_mock()
    lizzy = Lizzy('https://lizzy.example', '7E5770K3N')
    stack_id = lizzy.new_stack(image_version='10',
                               keep_stacks=2,
                               new_traffic=42,
                               senza_yaml_path=yaml_path,
                               application_version=None,
                               stack_version=None,
                               disable_rollback=False,
                               parameters=[])

    header = make_header('7E5770K3N')
    data = {'image_version': "10",
            'keep_stacks': 2,
            'new_traffic': 42,
            'parameters': [],
            'disable_rollback': False,
            'senza_yaml': senza_yaml}
    mock_post.assert_called_once_with('https://lizzy.example/stacks', headers=header,
                                      data=json.dumps(data,  sort_keys=True),
                                      json=None,
                                      verify=False)

    assert stack_id == "574CC1D"

    mock_post.reset_mock()
    data_with_ver = {'image_version': "10",
                     'keep_stacks': 2,
                     'new_traffic': 42,
                     'parameters': [],
                     'senza_yaml': senza_yaml,
                     'disable_rollback': False,
                     "application_version": "420"}
    lizzy.new_stack('10', 2, 42, yaml_path, None, "420", False, [])
    mock_post.assert_called_once_with('https://lizzy.example/stacks', headers=header,
                                      data=json.dumps(data_with_ver,  sort_keys=True),
                                      json=None,
                                      verify=False)

    mock_post.reset_mock()
    data_with_params = {'image_version': "10",
                        'keep_stacks': 2,
                        'new_traffic': 42,
                        'parameters': ['abc', 'def'],
                        'senza_yaml': senza_yaml,
                        'disable_rollback': True,
                        "application_version": "420", }
    lizzy.new_stack('10', 2, 42, yaml_path, None, "420", True, ['abc', 'def'])
    mock_post.assert_called_once_with('https://lizzy.example/stacks',
                                      headers=header,
                                      data=json.dumps(data_with_params, sort_keys=True),
                                      json=None,
                                      verify=False)

    mock_post.reset_mock()
    data_with_stack_version = {'image_version': "10",
                               'keep_stacks': 2,
                               'new_traffic': 42,
                               'parameters': ['abc', 'def'],
                               'senza_yaml': senza_yaml,
                               'disable_rollback': True,
                               "application_version": "420",
                               "stack_version": "7", }
    lizzy.new_stack('10', 2, 42, yaml_path, "7", "420", True, ['abc', 'def'])
    mock_post.assert_called_once_with('https://lizzy.example/stacks',
                                      headers=header,
                                      data=json.dumps(data_with_stack_version, sort_keys=True),
                                      json=None,
                                      verify=False)