def test_gbprepo_to_xylem_data_on_ok_input():
    from xylem.gbpdistro_support import gbprepo_to_xylem_data

    simple_gbpdistro = {"release-name": "foorte", "repositories": {}, "type": "gbp"}
    targets = {"foorte": ["lucid", "oneiric"]}
    # make sure our sample files work for the above checks before
    # proceeding to real data
    xylem_data = gbprepo_to_xylem_data(simple_gbpdistro, targets)
    assert xylem_data is not None
    assert {} == xylem_data

    gbpdistro_data = {
        "release-name": "foorte",
        "repositories": {
            "common_msgs": dict(
                target="all",
                url="git://github.com/wg-debs/common_msgs.git",
                packages={"foo": "subdir/foo", "bar": "subdir/bar"},
            ),
            "gazebo": dict(target=["lucid", "natty"], url="git://github.com/wg-debs/gazebo.git"),
            "foo-bar": dict(target=["precise"], url="git://github.com/wg-debs/gazebo.git", packages={"foo-bar": None}),
        },
        "type": "gbp",
    }

    xylem_data = gbprepo_to_xylem_data(gbpdistro_data, targets)
    for k in ["foo", "bar", "gazebo", "foo-bar"]:
        assert k in xylem_data, k

    # all targets and name transform
    # These are from the 'common_msgs' repo above.
    pkgs = ["foo", "bar"]
    v = "ros-foorte-%s"
    for pkg in pkgs:
        for p in ["lucid", "oneiric"]:
            rule = xylem_data[pkg]["ubuntu"][p]
            assert rule["apt"]["packages"] == [v % pkg], rule["apt"]["packages"]
        for p in ["maverick", "natty"]:
            assert p not in xylem_data[k]["ubuntu"]

    # target overrides
    pkg = "gazebo"
    v = "ros-foorte-gazebo"
    for p in ["lucid", "natty"]:
        rule = xylem_data[pkg]["ubuntu"][p]
        assert rule["apt"]["packages"] == [v], rule["apt"]["packages"]
    for p in ["oneiric", "precise"]:
        assert p not in xylem_data[pkg]["ubuntu"]

    # target overrides
    # These are from the 'foo-bar' repo above.
    v = "ros-foorte-foo-bar"
    for pkg in ["foo-bar"]:
        for p in ["precise"]:
            rule = xylem_data[pkg]["ubuntu"][p]
            assert rule["apt"]["packages"] == [v], rule["apt"]["packages"]
        for p in ["oneiric", "natty", "lucid"]:
            assert p not in xylem_data[pkg]["ubuntu"]
def test_gbprepo_to_xylem_data_on_bad_inputs():
    from xylem.gbpdistro_support import gbprepo_to_xylem_data
    from xylem import InvalidData

    simple_gbpdistro = {"release-name": "foorte", "repositories": {}, "type": "gbp"}
    targets = {"foorte": ["lucid", "oneiric"]}
    # test bad data
    try:
        gbprepo_to_xylem_data(simple_gbpdistro, [targets])
        assert False, "should have raised"
    except InvalidData:
        pass
    try:
        gbprepo_to_xylem_data({"targets": 1, "repositories": [], "type": "gbp"}, targets)
        assert False, "should have raised"
    except InvalidData:
        pass
    try:
        gbprepo_to_xylem_data([], targets)
        assert False, "should have raised"
    except InvalidData:
        pass
    # release-name must be in targets
    try:
        gbprepo_to_xylem_data({"release-name": "barte", "repositories": [], "type": "gbp"}, targets)
        assert False, "should have raised"
    except InvalidData:
        pass
    # gbp-distros must be list of dicts
    try:
        gbprepo_to_xylem_data({"release-name": "foorte", "repositories": [1], "type": "gbp"}, targets)
        assert False, "should have raised"
    except InvalidData:
        pass
    # gbp-distro target must be 'all' or a list of strings
    try:
        bad_example = {"name": "common", "target": [1], "url": "git://github.com/wg-debs/common_msgs.git"}
        gbprepo_to_xylem_data({"release-name": "foorte", "repositories": [bad_example], "type": "gbp"}, targets)
        assert False, "should have raised"
    except InvalidData:
        pass
def test_gbprepo_to_xylem_data_on_bad_inputs():
    from xylem.gbpdistro_support import gbprepo_to_xylem_data
    from xylem import InvalidData
    simple_gbpdistro = {
        'release-name': 'foorte',
        'repositories': {},
        'type': 'gbp'
    }
    targets = {'foorte': ['lucid', 'oneiric']}
    # test bad data
    try:
        gbprepo_to_xylem_data(simple_gbpdistro, [targets])
        assert False, "should have raised"
    except InvalidData:
        pass
    try:
        gbprepo_to_xylem_data({
            'targets': 1,
            'repositories': [],
            'type': 'gbp'
        }, targets)
        assert False, "should have raised"
    except InvalidData:
        pass
    try:
        gbprepo_to_xylem_data([], targets)
        assert False, "should have raised"
    except InvalidData:
        pass
    # release-name must be in targets
    try:
        gbprepo_to_xylem_data(
            {
                'release-name': 'barte',
                'repositories': [],
                'type': 'gbp'
            }, targets)
        assert False, "should have raised"
    except InvalidData:
        pass
    # gbp-distros must be list of dicts
    try:
        gbprepo_to_xylem_data(
            {
                'release-name': 'foorte',
                'repositories': [1],
                'type': 'gbp'
            }, targets)
        assert False, "should have raised"
    except InvalidData:
        pass
    # gbp-distro target must be 'all' or a list of strings
    try:
        bad_example = {
            'name': 'common',
            'target': [1],
            'url': 'git://github.com/wg-debs/common_msgs.git'
        }
        gbprepo_to_xylem_data(
            {
                'release-name': 'foorte',
                'repositories': [bad_example],
                'type': 'gbp'
            }, targets)
        assert False, "should have raised"
    except InvalidData:
        pass
def test_gbprepo_to_xylem_data_on_ok_input():
    from xylem.gbpdistro_support import gbprepo_to_xylem_data
    simple_gbpdistro = {
        'release-name': 'foorte',
        'repositories': {},
        'type': 'gbp'
    }
    targets = {'foorte': ['lucid', 'oneiric']}
    # make sure our sample files work for the above checks before
    # proceeding to real data
    xylem_data = gbprepo_to_xylem_data(simple_gbpdistro, targets)
    assert xylem_data is not None
    assert {} == xylem_data

    gbpdistro_data = {
        'release-name': 'foorte',
        'repositories': {
            'common_msgs':
            dict(target='all',
                 url='git://github.com/wg-debs/common_msgs.git',
                 packages={
                     'foo': 'subdir/foo',
                     'bar': 'subdir/bar'
                 }),
            'gazebo':
            dict(target=['lucid', 'natty'],
                 url='git://github.com/wg-debs/gazebo.git'),
            'foo-bar':
            dict(target=['precise'],
                 url='git://github.com/wg-debs/gazebo.git',
                 packages={'foo-bar': None}),
        },
        'type': 'gbp',
    }

    xylem_data = gbprepo_to_xylem_data(gbpdistro_data, targets)
    for k in ['foo', 'bar', 'gazebo', 'foo-bar']:
        assert k in xylem_data, k

    # all targets and name transform
    # These are from the 'common_msgs' repo above.
    pkgs = ['foo', 'bar']
    v = 'ros-foorte-%s'
    for pkg in pkgs:
        for p in ['lucid', 'oneiric']:
            rule = xylem_data[pkg]['ubuntu'][p]
            assert rule['apt']['packages'] == [v % pkg
                                               ], rule['apt']['packages']
        for p in ['maverick', 'natty']:
            assert p not in xylem_data[k]['ubuntu']

    # target overrides
    pkg = 'gazebo'
    v = 'ros-foorte-gazebo'
    for p in ['lucid', 'natty']:
        rule = xylem_data[pkg]['ubuntu'][p]
        assert rule['apt']['packages'] == [v], rule['apt']['packages']
    for p in ['oneiric', 'precise']:
        assert p not in xylem_data[pkg]['ubuntu']

    # target overrides
    # These are from the 'foo-bar' repo above.
    v = 'ros-foorte-foo-bar'
    for pkg in ['foo-bar']:
        for p in ['precise']:
            rule = xylem_data[pkg]['ubuntu'][p]
            assert rule['apt']['packages'] == [v], rule['apt']['packages']
        for p in ['oneiric', 'natty', 'lucid']:
            assert p not in xylem_data[pkg]['ubuntu']