def matches_when_properties_all_match():
    matcher = has_attrs(
        username=equal_to("bob"),
        email_address=equal_to("*****@*****.**"),
    )
    
    assert_equal(matched(), matcher.match(User("bob", "*****@*****.**")))
def test_app_update_rollback():
    """Tests that an updated app can be rolled back to its initial version."""

    app_def = apps.readiness_and_health_app("app-update-rollback")
    app_id = app_def["id"]

    # First deployment
    client = marathon.create_client()
    client.add_app(app_def)
    deployment_wait(service_id=app_id)

    tasks = client.get_tasks(app_id)
    assert_that(tasks, has_len(equal_to(1)))

    # Second deployment
    app_def['instances'] = 2
    client.update_app(app_id, app_def)
    deployment_wait(service_id=app_id)

    tasks = client.get_tasks(app_id)
    assert_that(tasks, has_len(equal_to(2)))

    # Third deployment with rollback
    # provides a testing delay to rollback in the meantime
    app_def['readinessChecks'][0]['intervalSeconds'] = 30
    app_def['instances'] = 1
    deployment_id = client.update_app(app_id, app_def)
    client.rollback_deployment(deployment_id)
    deployment_wait(service_id=app_id)

    # update to 1 instance is rollback to 2
    tasks = client.get_tasks(app_id)
    assert_that(tasks, has_len(equal_to(2)))
def description_contains_descriptions_of_submatchers():
    matcher = contains_exactly(equal_to("apple"), equal_to("banana"))
    
    assert_equal(
        "iterable containing these 2 elements in any order:\n * 'apple'\n * 'banana'",
        matcher.describe()
    )
def mismatches_when_duplicate_is_missing():
    matcher = includes(equal_to("apple"), equal_to("apple"))

    assert_equal(
        unmatched("was missing element:\n * 'apple'\nThese elements were in the iterable, but did not match the missing element:\n * 'apple': already matched"),
        matcher.match(["apple"])
    )
def mismatches_when_item_is_missing():
    matcher = includes(equal_to("apple"), equal_to("banana"), equal_to("coconut"))

    assert_equal(
        unmatched("was missing element:\n * 'banana'\nThese elements were in the iterable, but did not match the missing element:\n * 'coconut': was 'coconut'\n * 'apple': already matched"),
        matcher.match(["coconut", "apple"])
    )
def mismatches_when_item_is_missing():
    matcher = is_sequence(equal_to("apple"), equal_to("banana"), equal_to("coconut"))
    
    assert_equal(
        unmatched("element at index 2 was missing"),
        matcher.match(["apple", "banana"])
    )
def description_contains_descriptions_of_submatchers():
    matcher = is_sequence(equal_to("apple"), equal_to("banana"))
    
    assert_equal(
        "iterable containing in order:\n 0: 'apple'\n 1: 'banana'",
        matcher.describe()
    )
def mismatches_when_items_are_in_wrong_order():
    matcher = is_sequence(equal_to("apple"), equal_to("banana"))
    
    assert_equal(
        unmatched("element at index 0 mismatched:\n * was 'banana'"),
        matcher.match(["banana", "apple"])
    )
def description_contains_descriptions_of_submatchers():
    matcher = includes(equal_to("apple"), equal_to("banana"))

    assert_equal(
        "iterable including elements:\n * 'apple'\n * 'banana'",
        matcher.describe()
    )
def matches_when_submatchers_all_match():
    matcher = any_of(
        has_attr("username", equal_to("bob")),
        has_attr("email_address", equal_to("*****@*****.**")),
    )
    
    assert_equal(matched(), matcher.match(User("bob", "*****@*****.**")))
def description_contains_descriptions_of_submatchers():
    matcher = any_of(
        has_attr("username", equal_to("bob")),
        has_attr("email_address", equal_to("*****@*****.**")),
    )
    
    assert_equal(
        "any of:\n * object with attribute username: '******'\n * object with attribute email_address: '*****@*****.**'",
        matcher.describe()
    )
def mismatches_when_property_is_missing():
    matcher = has_attrs(
        ("username", equal_to("bob")),
        ("email_address", equal_to("*****@*****.**")),
    )
    
    assert_equal(
        unmatched("was missing attribute username"),
        matcher.match("bobbity")
    )
def matches_when_any_submatchers_match():
    matcher = any_of(
        equal_to("bob"),
        equal_to("jim"),
    )
    
    assert_equal(
        matched(),
        matcher.match("bob"),
    )
def can_pass_properties_as_list_of_tuples():
    matcher = has_attrs(
        ("username", equal_to("bob")),
        ("email_address", equal_to("*****@*****.**")),
    )
    
    assert_equal(
        "object with attributes:\n * username: '******'\n * email_address: '*****@*****.**'",
        matcher.describe()
    )
def explanation_of_mismatch_contains_mismatch_of_property():
    matcher = has_attrs(
        username=equal_to("bob"),
        email_address=equal_to("*****@*****.**"),
    )
    
    assert_equal(
        unmatched("attribute email_address was '*****@*****.**'"),
        matcher.match(User("bob", "*****@*****.**"))
    )
def mismatches_when_no_submatchers_match():
    matcher = any_of(
        equal_to("bob"),
        equal_to("jim"),
    )
    
    assert_equal(
        unmatched("did not match any of:\n * 'bob' [was 'alice']\n * 'jim' [was 'alice']"),
        matcher.match("alice"),
    )
def test_launch_docker_grace_period(marathon_service_name):
    """Tests 'taskKillGracePeriodSeconds' option using a Docker container in a Marathon environment.
       Read more details about this test in `test_root_marathon.py::test_launch_mesos_root_marathon_grace_period`
    """

    app_id = '/launch-docker-grace-period-app'
    app_def = apps.docker_http_server(app_id)
    app_def['container']['docker']['image'] = 'kensipe/python-test'

    default_grace_period = 3
    grace_period = 20
    app_def['taskKillGracePeriodSeconds'] = grace_period
    app_def['cmd'] = 'python test.py'
    task_name = app_id.lstrip('/')

    client = marathon.create_client()
    client.add_app(app_def)
    deployment_wait(service_id=app_id)

    tasks = get_service_task(marathon_service_name, task_name)
    assert tasks is not None

    client.scale_app(app_id, 0)
    tasks = get_service_task(marathon_service_name, task_name)
    assert tasks is not None

    # tasks should still be here after the default_graceperiod
    time.sleep(default_grace_period + 1)
    tasks = get_service_task(marathon_service_name, task_name)
    assert tasks is not None

    # but not after the set grace_period
    time.sleep(grace_period)
    assert_that(lambda: get_service_task(marathon_service_name, task_name),
                eventually(equal_to(None), max_attempts=30))
Example #18
0
def wait_for_service_endpoint(service_name, timeout_sec=120, path=""):
    """
    Checks the service url. Waits for exhibitor to start up (up to 20 minutes) and then checks the url on all masters.

    if available it returns true,
    on expiration throws an exception
    """

    def master_service_status_code(url):
        logger.info('Querying %s', url)
        auth = DCOSAcsAuth(dcos_acs_token())

        response = requests.get(
            url=url,
            timeout=5,
            auth=auth,
            verify=verify_ssl())

        return response.status_code

    schema = 'https' if ee_version() == 'strict' or ee_version() == 'permissive' else 'http'
    logger.info('Waiting for service /service/{}/{} to become available on all masters'.format(service_name, path))

    for ip in dcos_masters_public_ips():
        url = "{}://{}/service/{}/{}".format(schema, ip, service_name, path)
        assert_that(lambda: master_service_status_code(url), eventually(equal_to(200), max_attempts=timeout_sec/5))
def mismatches_when_contains_extra_item():
    matcher = contains_exactly(equal_to("apple"))
    
    assert_equal(
        unmatched("had extra elements:\n * 'coconut'"),
        matcher.match(["coconut", "apple"])
    )
def mismatches_when_item_is_expected_but_iterable_is_empty():
    matcher = includes(equal_to("apple"))

    assert_equal(
        unmatched("iterable was empty"),
        matcher.match([])
    )
def description_uses_singular_when_there_is_one_submatcher():
    matcher = contains_exactly(equal_to("apple"))

    assert_equal(
        "iterable containing 1 element:\n * 'apple'",
        matcher.describe()
    )
def mismatches_when_actual_is_not_iterable():
    matcher = includes(equal_to("apple"))

    assert_equal(
        unmatched("was not iterable\nwas 0"),
        matcher.match(0)
    )
Example #23
0
def add_package_repo(
        repo_name,
        repo_url,
        index=None,
        wait_for_package=None,
        expect_prev_version=None):
    """ Add a repository to the list of package sources

        :param repo_name: name of the repository to add
        :type repo_name: str
        :param repo_url: location of the repository to add
        :type repo_url: str
        :param index: index (precedence) for this repository
        :type index: int
        :param wait_for_package: the package whose version should change after the repo is added
        :type wait_for_package: str, or None

        :return: True if successful, False otherwise
        :rtype: bool
    """

    package_manager = _get_package_manager()
    if wait_for_package:
        prev_version = package_manager.get_package_version(wait_for_package, None)
    if not package_manager.add_repo(repo_name, repo_url, index):
        return False
    if wait_for_package:
        try:
            assert_that(lambda: package_version_changed_predicate(package_manager, wait_for_package, prev_version),
                        eventually(equal_to(True)))
        except AssertionError:
            return False
    return True
def mismatches_when_item_in_iterable_does_not_match():
    matcher = all_elements(equal_to("apple"))

    assert_equal(
        unmatched("element at index 1 mismatched: was 'orange'"),
        matcher.match(["apple", "orange"])
    )
def description_contains_descriptions_of_submatcher():
    matcher = all_elements(equal_to("apple"))

    assert_equal(
        "all elements of iterable match: 'apple'",
        matcher.describe()
    )
Example #26
0
def wait_for_task_completion(task_id):
    """ Block until the task completes

        :param task_id: task ID
        :type task_id: str

        :rtype: None
    """
    assert_that(lambda: task_completed(task_id), eventually(equal_to(True)))
def can_pass_properties_as_dictionary():
    matcher = has_attrs({
        "username": equal_to("bob"),
    })
    
    assert_equal(
        "object with attributes:\n * username: '******'",
        matcher.describe()
    )
def description_contains_descriptions_of_properties():
    matcher = has_attrs(
        username=equal_to("bob"),
    )
    
    assert_equal(
        "object with attributes:\n * username: '******'",
        matcher.describe()
    )
Example #29
0
def test_can_use_matchers_instead_of_values_for_keyword_arguments(mocks):
    return_value = "Whoopee!"
    mock = mocks.mock()
    expects(mock).save.with_args(value=equal_to("Blah")).returns(return_value)
    
    assert_raises(UnexpectedInvocationError, lambda: mock.save())
    assert_raises(UnexpectedInvocationError, lambda: mock.save(key="word"))
    assert_raises(UnexpectedInvocationError, lambda: mock.save("positional"))
    assert_raises(UnexpectedInvocationError, lambda: mock.save("positional", key="word"))
    
    assert mock.save(value="Blah") is return_value
def test_launch_mesos_container_with_docker_image():
    """Launches a Mesos container with a Docker image."""

    app_def = apps.ucr_docker_http_server(app_id='/launch-mesos-container-with-docker-image-app')
    app_id = app_def["id"]

    client = marathon.create_client()
    client.add_app(app_def)
    deployment_wait(service_id=app_id)

    assert_that(lambda: client.get_tasks(app_id),
                eventually(has_len(equal_to(1)), max_attempts=30))

    app = client.get_app(app_id)
    assert app['container']['type'] == 'MESOS', "The container type is not MESOS"
Example #31
0
def mismatches_when_contains_extra_item():
    matcher = contains_exactly(equal_to("apple"))

    assert_equal(unmatched("had extra elements:\n * 'coconut'"),
                 matcher.match(["coconut", "apple"]))
Example #32
0
 def test_string_is_unchanged(self):
     query = schema.String()
     assert_that(query.to_json_value("42"), equal_to("42"))
def matches_when_all_submatchers_match_one_item_with_no_items_leftover():
    matcher = includes(equal_to("apple"), equal_to("banana"))

    assert_equal(matched(), matcher.match(["apple", "banana"]))
    assert_equal(matched(), matcher.match(["apple", "banana", "coconut"]))
Example #34
0
def wait_for_service_endpoint_removal(service_name, timeout_sec=120):
    wait_fixed = timeout_sec * 1000 / 24
    return assert_that(
        lambda: service_unavailable_predicate(service_name),
        eventually(equal_to(True), wait_fixed=wait_fixed, max_attempts=24))
Example #35
0
def mismatches_when_item_is_expected_but_iterable_is_empty():
    matcher = includes(equal_to("apple"))

    assert_equal(unmatched("iterable was empty"), matcher.match([]))
Example #36
0
def mismatches_when_item_is_missing():
    matcher = is_sequence(equal_to("apple"), equal_to("banana"),
                          equal_to("coconut"))

    assert_equal(unmatched("element at index 2 was missing"),
                 matcher.match(["apple", "banana"]))
Example #37
0
def description_contains_descriptions_of_submatchers():
    matcher = contains_exactly(equal_to("apple"), equal_to("banana"))

    assert_equal(
        "iterable containing these 2 elements in any order:\n * 'apple'\n * 'banana'",
        matcher.describe())
Example #38
0
def description_contains_description_of_property():
    matcher = has_feature("name", lambda user: user.username, equal_to("bob"))
    assert_equal("name: 'bob'", matcher.describe())
Example #39
0
def mismatches_when_items_are_in_wrong_order():
    matcher = is_sequence(equal_to("apple"), equal_to("banana"))

    assert_equal(unmatched("element at index 0 mismatched:\n  * was 'banana'"),
                 matcher.match(["banana", "apple"]))
Example #40
0
def explanation_of_mismatch_contains_mismatch_of_feature():
    matcher = has_feature("name", lambda user: user.username, equal_to("bob"))
    assert_equal(unmatched("name: was 'bobbity'"),
                 matcher.match(User("bobbity")))
Example #41
0
def mismatches_when_feature_extraction_fails():
    # TODO:
    return
    matcher = has_feature("name", lambda user: user.username, equal_to("bob"))
    assert_equal(unmatched(""), matcher.match("bobbity"))
Example #42
0
def matches_when_feature_has_correct_value():
    matcher = has_feature("name", lambda user: user.username, equal_to("bob"))
    assert_equal(matched(), matcher.match(User("bob")))
Example #43
0
def matches_when_all_submatchers_match_one_item_with_no_items_leftover():
    matcher = contains_exactly(equal_to("apple"), equal_to("banana"))

    assert_equal(matched(), matcher.match(["banana", "apple"]))
Example #44
0
def description_contains_descriptions_of_submatchers():
    matcher = is_sequence(equal_to("apple"), equal_to("banana"))

    assert_equal("iterable containing in order:\n  0: 'apple'\n  1: 'banana'",
                 matcher.describe())
Example #45
0
def description_uses_singular_when_there_is_one_submatcher():
    matcher = contains_exactly(equal_to("apple"))

    assert_equal("iterable containing 1 element:\n * 'apple'",
                 matcher.describe())
Example #46
0
def test_when_param_does_not_exist_on_params_then_error_is_raised():
    params = schema.Params("book", {})
    
    error = pytest.raises(ValueError, lambda: params.author)
    
    assert_that(str(error.value), equal_to("book has no param author"))
Example #47
0
def wait_for_mesos_task_removal(task_name, timeout_sec=120):
    wait_fixed = timeout_sec * 1000 / 24
    assert_that(
        lambda: mesos_task_not_present_predicate(task_name),
        eventually(equal_to(True), wait_fixed=wait_fixed, max_attempts=24))
Example #48
0
 def test_bool_is_unchanged(self):
     query = schema.Boolean()
     assert_that(query.to_json_value(True), equal_to(True))
Example #49
0
def mismatches_when_actual_is_not_iterable():
    matcher = includes(equal_to("apple"))

    assert_equal(unmatched("was not iterable\nwas 0"), matcher.match(0))
Example #50
0
 def test_float_is_unchanged(self):
     query = schema.Float()
     assert_that(query.to_json_value(4.2), equal_to(4.2))
def matches_when_there_are_extra_items():
    matcher = includes(equal_to("apple"))

    assert_equal(matched(), matcher.match(["coconut", "apple"]))
Example #52
0
 def test_int_is_unchanged(self):
     query = schema.Int()
     assert_that(query.to_json_value(42), equal_to(42))
def matches_when_property_has_correct_value():
    assert_equal(matched(),
                 has_attr("username", equal_to("bob")).match(User("bob")))
Example #54
0
def description_includes_description_of_negated_matcher():
    assert_equal("not: 'hello'", not_(equal_to("hello")).describe())
def description_contains_descriptions_of_submatchers():
    matcher = includes(equal_to("apple"), equal_to("banana"))

    assert_equal("iterable including elements:\n  * 'apple'\n  * 'banana'",
                 matcher.describe())
def mismatches_when_property_is_missing():
    assert_equal(unmatched("was missing attribute username"),
                 has_attr("username", equal_to("bob")).match("bobbity"))
Example #57
0
def does_not_match_when_negated_matcher_matches():
    assert_equal(unmatched("matched: 1"), not_(equal_to(1)).match(1))
def explanation_of_mismatch_contains_mismatch_of_property():
    assert_equal(unmatched("attribute username was 'bobbity'"),
                 has_attr("username", equal_to("bob")).match(User("bobbity")))
Example #59
0
def matches_when_negated_matcher_does_not_match():
    assert_equal(matched(), not_(equal_to(1)).match(2))
def description_contains_description_of_property():
    assert_equal("object with attribute username: '******'",
                 has_attr("username", equal_to("bob")).describe())