1# Copyright (c) 2012, Willow Garage, Inc.
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are met:
6#
7#     * Redistributions of source code must retain the above copyright
8#       notice, this list of conditions and the following disclaimer.
9#     * Redistributions in binary form must reproduce the above copyright
10#       notice, this list of conditions and the following disclaimer in the
11#       documentation and/or other materials provided with the distribution.
12#     * Neither the name of the Willow Garage, Inc. nor the names of its
13#       contributors may be used to endorse or promote products derived from
14#       this software without specific prior written permission.
15#
16# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26# POSSIBILITY OF SUCH DAMAGE.
27
28import os
29try:
30    from urllib.request import urlopen
31except ImportError:
32    from urllib2 import urlopen
33
34
35def get_test_dir():
36    return os.path.abspath(os.path.join(os.path.dirname(__file__),
37                                        'sources.list.d'))
38
39
40def test_url_constants():
41    from rosdep2.gbpdistro_support import FUERTE_GBPDISTRO_URL
42    for url_name, url in [
43            ('FUERTE_GBPDISTRO_URL', FUERTE_GBPDISTRO_URL)]:
44        try:
45            f = urlopen(url)
46            f.read()
47            f.close()
48        except Exception:
49            assert False, 'URL [%s][%s] failed to download' % (url_name, url)
50
51
52def test_get_gbprepo_as_rosdep_data():
53    from rosdep2.rosdistrohelper import get_index
54    from rosdep2.gbpdistro_support import get_gbprepo_as_rosdep_data
55    distro = sorted(
56        name for name, info in get_index().distributions.items()
57        if info.get('distribution_type') == 'ros1')[0]
58    data = get_gbprepo_as_rosdep_data(distro)
59    for k in ['ros', 'catkin', 'genmsg']:
60        assert k in data, data
61    assert data['ros']['ubuntu']
62
63    try:
64        get_gbprepo_as_rosdep_data('fooNonExistantDistro')
65        assert False, 'should have raised'
66    except RuntimeError:
67        pass
68
69
70def test_download_gbpdistro_as_rosdep_data():
71    from rosdep2.gbpdistro_support import download_gbpdistro_as_rosdep_data
72    from rosdep2.gbpdistro_support import FUERTE_GBPDISTRO_URL
73    from rosdep2.rep3 import REP3_TARGETS_URL
74    from rosdep2 import DownloadFailure
75    data = download_gbpdistro_as_rosdep_data(FUERTE_GBPDISTRO_URL)
76    # don't go beyond this, this test is just making sure the download
77    # plumbing is correct, not the loader.
78    for k in ['ros', 'catkin', 'genmsg']:
79        assert k in data, data
80    assert data['ros']['ubuntu']
81
82    # try with bad url to trigger exception handling
83    try:
84        # override targets URL with bad URL
85        download_gbpdistro_as_rosdep_data(
86            FUERTE_GBPDISTRO_URL,
87            targets_url='http://bad.ros.org/foo.yaml')
88        assert False, 'should have raised'
89    except DownloadFailure:
90        pass
91    try:
92        # use targets URL, which should have a bad format
93        download_gbpdistro_as_rosdep_data(REP3_TARGETS_URL)
94        assert False, 'should have raised'
95    except DownloadFailure:
96        pass
97
98
99def test_gbprepo_to_rosdep_data_on_bad_inputs():
100    from rosdep2.gbpdistro_support import gbprepo_to_rosdep_data
101    from rosdep2 import InvalidData
102    simple_gbpdistro = {'release-name': 'foorte',
103                        'repositories': {},
104                        'type': 'gbp'}
105    targets = {'foorte': ['lucid', 'oneiric']}
106    # test bad data
107    try:
108        gbprepo_to_rosdep_data(simple_gbpdistro, [targets])
109        assert False, 'should have raised'
110    except InvalidData:
111        pass
112    try:
113        gbprepo_to_rosdep_data({
114            'targets': 1,
115            'repositories': [],
116            'type': 'gbp'}, targets)
117        assert False, 'should have raised'
118    except InvalidData:
119        pass
120    try:
121        gbprepo_to_rosdep_data([], targets)
122        assert False, 'should have raised'
123    except InvalidData:
124        pass
125    # release-name must be in targets
126    try:
127        gbprepo_to_rosdep_data({
128            'release-name': 'barte',
129            'repositories': [],
130            'type': 'gbp'}, targets)
131        assert False, 'should have raised'
132    except InvalidData:
133        pass
134    # gbp-distros must be list of dicts
135    try:
136        gbprepo_to_rosdep_data({
137            'release-name': 'foorte',
138            'repositories': [1],
139            'type': 'gbp'}, targets)
140        assert False, 'should have raised'
141    except InvalidData:
142        pass
143    # gbp-distro target must be 'all' or a list of strings
144    try:
145        bad_example = {'name': 'common',
146                       'target': [1],
147                       'url': 'git://github.com/wg-debs/common_msgs.git'}
148        gbprepo_to_rosdep_data({
149            'release-name': 'foorte',
150            'repositories': [bad_example],
151            'type': 'gbp'}, targets)
152        assert False, 'should have raised'
153    except InvalidData:
154        pass
155
156
157def test_gbprepo_to_rosdep_data_on_ok_input():
158    from rosdep2.gbpdistro_support import gbprepo_to_rosdep_data
159    simple_gbpdistro = {'release-name': 'foorte',
160                        'repositories': {},
161                        'type': 'gbp'}
162    targets = {'foorte': ['lucid', 'oneiric']}
163    # make sure our sample files work for the above checks before
164    # proceeding to real data
165    rosdep_data = gbprepo_to_rosdep_data(simple_gbpdistro, targets)
166    assert rosdep_data is not None
167    assert {} == rosdep_data
168
169    gbpdistro_data = {
170        'release-name': 'foorte',
171        'repositories': {
172            'common_msgs': dict(
173                target='all',
174                url='git://github.com/wg-debs/common_msgs.git',
175                packages={'foo': 'subdir/foo', 'bar': 'subdir/bar'}),
176            'gazebo': dict(
177                target=['lucid', 'natty'],
178                url='git://github.com/wg-debs/gazebo.git'),
179            'foo-bar': dict(
180                target=['precise'],
181                url='git://github.com/wg-debs/gazebo.git',
182                packages={'foo-bar': None}),
183        },
184        'type': 'gbp',
185    }
186
187    rosdep_data = gbprepo_to_rosdep_data(gbpdistro_data, targets)
188    for k in ['foo', 'bar', 'gazebo', 'foo-bar']:
189        assert k in rosdep_data, k
190
191    # all targets and name transform
192    # These are from the 'common_msgs' repo above.
193    pkgs = ['foo', 'bar']
194    v = 'ros-foorte-%s'
195    for pkg in pkgs:
196        for p in ['lucid', 'oneiric']:
197            rule = rosdep_data[pkg]['ubuntu'][p]
198            assert rule['apt']['packages'] == [v % pkg], rule['apt']['packages']
199        for p in ['maverick', 'natty']:
200            assert p not in rosdep_data[k]['ubuntu']
201
202    # target overrides
203    pkg = 'gazebo'
204    v = 'ros-foorte-gazebo'
205    for p in ['lucid', 'natty']:
206        rule = rosdep_data[pkg]['ubuntu'][p]
207        assert rule['apt']['packages'] == [v], rule['apt']['packages']
208    for p in ['oneiric', 'precise']:
209        assert p not in rosdep_data[pkg]['ubuntu']
210
211    # target overrides
212    # These are from the 'foo-bar' repo above.
213    v = 'ros-foorte-foo-bar'
214    for pkg in ['foo-bar']:
215        for p in ['precise']:
216            rule = rosdep_data[pkg]['ubuntu'][p]
217            assert rule['apt']['packages'] == [v], rule['apt']['packages']
218        for p in ['oneiric', 'natty', 'lucid']:
219            assert p not in rosdep_data[pkg]['ubuntu']
220
221
222def test_get_owner_name_homebrew():
223    from rosdep2.gbpdistro_support import get_owner_name
224    empty_url = ''
225    assert get_owner_name(empty_url) == 'ros', 'url: ' + empty_url
226    https_test_url = 'https://github.com/' \
227        'ros/rosdistro/raw/master/releases/fuerte.yaml'
228    assert get_owner_name(https_test_url) == 'ros', 'url: ' + https_test_url
229    user_test_url = 'https://github.com/' \
230        'zklapow/rosdistro/raw/master/releases/fuerte.yaml'
231    assert get_owner_name(user_test_url) == 'zklapow', 'url: ' + user_test_url
232    non_github_url = 'https://ros.org/files/releases/fuerte.yaml'
233    assert get_owner_name(non_github_url) == 'ros', 'url: ' + non_github_url
234