Merge lp:~canonical-platform-qa/snappy-ecosystem-tests/store-rest-helpers into lp:snappy-ecosystem-tests
- store-rest-helpers
- Merge into trunk
Status: | Merged |
---|---|
Approved by: | Omer Akram |
Approved revision: | 20 |
Merged at revision: | 21 |
Proposed branch: | lp:~canonical-platform-qa/snappy-ecosystem-tests/store-rest-helpers |
Merge into: | lp:snappy-ecosystem-tests |
Prerequisite: | lp:~canonical-platform-qa/snappy-ecosystem-tests/adding-test-runner |
Diff against target: |
2361 lines (+2112/-101) 19 files modified
README.rst (+19/-0) pylint.cfg (+1/-1) requirements.txt (+3/-0) snappy_ecosystem_tests/helpers/fixture_setup.py (+5/-6) snappy_ecosystem_tests/helpers/snapcraft/__init__.py (+19/-0) snappy_ecosystem_tests/helpers/snapcraft/client.py (+74/-0) snappy_ecosystem_tests/helpers/snapcraft/config.py (+114/-0) snappy_ecosystem_tests/helpers/snapcraft/constants.py (+52/-0) snappy_ecosystem_tests/helpers/snapcraft/deprecations.py (+56/-0) snappy_ecosystem_tests/helpers/snapcraft/indicators.py (+118/-0) snappy_ecosystem_tests/helpers/snapcraft/options.py (+244/-0) snappy_ecosystem_tests/helpers/store_apis/__init__.py (+19/-0) snappy_ecosystem_tests/helpers/store_apis/errors.py (+371/-0) snappy_ecosystem_tests/helpers/store_apis/rest_apis.py (+880/-0) snappy_ecosystem_tests/helpers/store_apis/upload.py (+97/-0) snappy_ecosystem_tests/snapcraft/__init__.py (+0/-19) snappy_ecosystem_tests/snapcraft/snapcraft.py (+0/-74) snappy_ecosystem_tests/tests/test_store_apis_login.py (+39/-0) snappy_ecosystem_tests/tests/test_store_login.py (+1/-1) |
To merge this branch: | bzr merge lp:~canonical-platform-qa/snappy-ecosystem-tests/store-rest-helpers |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
platform-qa-bot | continuous-integration | Approve | |
Omer Akram (community) | Approve | ||
Santiago Baldassin (community) | Approve | ||
Review via email:
|
This proposal supersedes a proposal from 2017-02-03.
Commit message
Adding helpers for Store RESTful APIs.
Also includes a first version of ecosystem test base clase and fixture setup.
Description of the change
This change adds helpers for Store RESTful APIs, a first version of snappy ecosystem test base class and a fixture setup
A simple test was added for making sure we can authenticate against Store RESTful APIs. You can run it: ./run_system_tests snappy_
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
I Ahmad (iahmad) wrote : Posted in a previous version of this proposal | # |
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
Omer Akram (om26er) wrote : Posted in a previous version of this proposal | # |
Thanks for working on this, I was not able to test the code but wrote some inline comments and suggestions.
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
Omer Akram (om26er) wrote : Posted in a previous version of this proposal | # |
We are missing docstrings, so it would also be helpful if you could add docstrings for the methods.
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
platform-qa-bot (platform-qa-bot) wrote : Posted in a previous version of this proposal | # |
FAILED: Continuous integration, rev:6
https:/
Executed test runs:
None: https:/
Click here to trigger a rebuild:
https:/
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
Santiago Baldassin (sbaldassin) wrote : Posted in a previous version of this proposal | # |
Looks good in general. I just think that we should rethink the way the different clients are represented here. So far it is a little bit confussing
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
Heber Parrucci (heber013) wrote : Posted in a previous version of this proposal | # |
Reply inline. I will address other comments once we agree in this one, because it would make other comments as not valid depending on which approach we take: use snapcraft upstream code or not.
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
platform-qa-bot (platform-qa-bot) wrote : | # |
FAILED: Continuous integration, rev:6
https:/
Executed test runs:
None: https:/
Click here to trigger a rebuild:
https:/
- 7. By Heber Parrucci
-
Fixing comments on code review.
Fixing pylint issues.
Addind a simple test for login to Store REST API
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
platform-qa-bot (platform-qa-bot) wrote : | # |
FAILED: Continuous integration, rev:7
https:/
Executed test runs:
None: https:/
Click here to trigger a rebuild:
https:/
- 8. By Heber Parrucci
-
fixing error in requirements.txt
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
platform-qa-bot (platform-qa-bot) wrote : | # |
FAILED: Continuous integration, rev:8
https:/
Executed test runs:
None: https:/
Click here to trigger a rebuild:
https:/
- 9. By Heber Parrucci
-
removing duplicated dependencies in requirements.txt
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
platform-qa-bot (platform-qa-bot) wrote : | # |
FAILED: Continuous integration, rev:9
https:/
Executed test runs:
None: https:/
Click here to trigger a rebuild:
https:/
- 10. By Heber Parrucci
-
changing dict initialization sintax according to PEP 448
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
platform-qa-bot (platform-qa-bot) wrote : | # |
FAILED: Continuous integration, rev:10
https:/
Executed test runs:
None: https:/
Click here to trigger a rebuild:
https:/
- 11. By Heber Parrucci
-
Merge from parent branch and fixing pylint to met max-line-length=80
- 12. By Heber Parrucci
-
deleting file not needed
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
platform-qa-bot (platform-qa-bot) wrote : | # |
FAILED: Continuous integration, rev:11
https:/
Executed test runs:
None: https:/
Click here to trigger a rebuild:
https:/
- 13. By Heber Parrucci
-
changing dict initialization
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
platform-qa-bot (platform-qa-bot) wrote : | # |
FAILED: Continuous integration, rev:12
https:/
Executed test runs:
None: https:/
Click here to trigger a rebuild:
https:/
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
platform-qa-bot (platform-qa-bot) wrote : | # |
FAILED: Continuous integration, rev:13
https:/
Executed test runs:
None: https:/
Click here to trigger a rebuild:
https:/
- 14. By Heber Parrucci
-
chaning dict initialization until jenkins slave is updated
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
platform-qa-bot (platform-qa-bot) wrote : | # |
PASSED: Continuous integration, rev:14
https:/
Executed test runs:
None: https:/
Click here to trigger a rebuild:
https:/
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
Omer Akram (om26er) wrote : | # |
Can you tell how should the config file look like ? I ran the tests and got http://
- 15. By Heber Parrucci
-
Updating README.rst with user credentials instructions
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
Heber Parrucci (heber013) wrote : | # |
> Can you tell how should the config file look like ? I ran the tests and got
> http://
Thanks for reviewing!
I have added a section in README that is called: 'User Credentials' here it explains the alternatives for providing them.
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
platform-qa-bot (platform-qa-bot) wrote : | # |
PASSED: Continuous integration, rev:15
https:/
Executed test runs:
None: https:/
Click here to trigger a rebuild:
https:/
- 16. By Heber Parrucci
-
merge from trunk
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
platform-qa-bot (platform-qa-bot) wrote : | # |
PASSED: Continuous integration, rev:16
https:/
Executed test runs:
None: https:/
Click here to trigger a rebuild:
https:/
- 17. By Heber Parrucci
-
Updating README.rst to make storing credentials section more clear.
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
platform-qa-bot (platform-qa-bot) wrote : | # |
PASSED: Continuous integration, rev:17
https:/
Executed test runs:
None: https:/
Click here to trigger a rebuild:
https:/
- 18. By Heber Parrucci
-
renaming class Store and adding a better docstring
- 19. By Heber Parrucci
-
fixing pylint
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
platform-qa-bot (platform-qa-bot) wrote : | # |
PASSED: Continuous integration, rev:19
https:/
Executed test runs:
None: https:/
Click here to trigger a rebuild:
https:/
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
Santiago Baldassin (sbaldassin) wrote : | # |
Code looks good to me
- 20. By Heber Parrucci
-
updating license year in headers
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
Omer Akram (om26er) wrote : | # |
code looks good. test passes. Lets land it.
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
platform-qa-bot (platform-qa-bot) wrote : | # |
PASSED: Continuous integration, rev:20
https:/
Executed test runs:
None: https:/
Click here to trigger a rebuild:
https:/
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
platform-qa-bot (platform-qa-bot) : | # |
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
platform-qa-bot (platform-qa-bot) : | # |
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
platform-qa-bot (platform-qa-bot) : | # |
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
platform-qa-bot (platform-qa-bot) : | # |
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
platform-qa-bot (platform-qa-bot) : | # |
- 21. By Heber Parrucci
-
merge from trunk
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
platform-qa-bot (platform-qa-bot) : | # |
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
platform-qa-bot (platform-qa-bot) : | # |
Preview Diff
1 | === modified file 'README.rst' | |||
2 | --- README.rst 2017-02-13 16:00:04 +0000 | |||
3 | +++ README.rst 2017-02-20 17:48:06 +0000 | |||
4 | @@ -56,6 +56,25 @@ | |||
5 | 56 | 56 | ||
6 | 57 | pytest.cfg stores the config related to pytest | 57 | pytest.cfg stores the config related to pytest |
7 | 58 | 58 | ||
8 | 59 | User Credentials | ||
9 | 60 | ================ | ||
10 | 61 | For storing user credentials you have 2 options: | ||
11 | 62 | |||
12 | 63 | option 1 - You can create a config file in your local machine with path: | ||
13 | 64 | ~/.config/ecosystem_tests.cfg | ||
14 | 65 | You can change the dir location of that config file by setting the env variable: | ||
15 | 66 | XDG_USER_CONFIG_HOME=^DIR_PATH^ | ||
16 | 67 | |||
17 | 68 | The config file should look like: | ||
18 | 69 | [user] | ||
19 | 70 | user_email=^USER_NAME^ | ||
20 | 71 | user_password=^USER_PASSWORD^ | ||
21 | 72 | |||
22 | 73 | option 2 - Set the following environment variables: | ||
23 | 74 | user_email=^USER_NAME^ | ||
24 | 75 | user_password=^USER_PASSWORD^ | ||
25 | 76 | |||
26 | 77 | |||
27 | 59 | Changing store: | 78 | Changing store: |
28 | 60 | =============== | 79 | =============== |
29 | 61 | You can change the store to use in config file ecosystem_tests.cfg | 80 | You can change the store to use in config file ecosystem_tests.cfg |
30 | 62 | 81 | ||
31 | === modified file 'pylint.cfg' | |||
32 | --- pylint.cfg 2017-02-15 19:14:19 +0000 | |||
33 | +++ pylint.cfg 2017-02-20 17:48:06 +0000 | |||
34 | @@ -336,7 +336,7 @@ | |||
35 | 336 | [DESIGN] | 336 | [DESIGN] |
36 | 337 | 337 | ||
37 | 338 | # Maximum number of arguments for function / method | 338 | # Maximum number of arguments for function / method |
39 | 339 | max-args=5 | 339 | max-args=8 |
40 | 340 | 340 | ||
41 | 341 | # Argument names that match this expression will be ignored. Default to name | 341 | # Argument names that match this expression will be ignored. Default to name |
42 | 342 | # with leading underscore | 342 | # with leading underscore |
43 | 343 | 343 | ||
44 | === modified file 'requirements.txt' | |||
45 | --- requirements.txt 2017-02-13 17:43:02 +0000 | |||
46 | +++ requirements.txt 2017-02-20 17:48:06 +0000 | |||
47 | @@ -1,3 +1,4 @@ | |||
48 | 1 | libnacl | ||
49 | 1 | pytest | 2 | pytest |
50 | 2 | testtools | 3 | testtools |
51 | 3 | unittest2 | 4 | unittest2 |
52 | @@ -11,4 +12,6 @@ | |||
53 | 11 | pyxdg==0.25 | 12 | pyxdg==0.25 |
54 | 12 | jsonschema==2.5.1 | 13 | jsonschema==2.5.1 |
55 | 13 | pexpect | 14 | pexpect |
56 | 15 | pymacaroons==0.9.2 | ||
57 | 16 | requests-toolbelt==0.6.0 | ||
58 | 14 | chromedriver_installer | 17 | chromedriver_installer |
59 | 15 | 18 | ||
60 | === modified file 'snappy_ecosystem_tests/helpers/fixture_setup.py' | |||
61 | --- snappy_ecosystem_tests/helpers/fixture_setup.py 2017-02-15 19:14:19 +0000 | |||
62 | +++ snappy_ecosystem_tests/helpers/fixture_setup.py 2017-02-20 17:48:06 +0000 | |||
63 | @@ -18,10 +18,13 @@ | |||
64 | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
65 | 19 | # | 19 | # |
66 | 20 | 20 | ||
68 | 21 | """Manage general tests fixtures""" | 21 | |
69 | 22 | """General fixtures related to the store and the tests environment""" | ||
70 | 22 | 23 | ||
71 | 23 | import os | 24 | import os |
72 | 25 | |||
73 | 24 | import fixtures | 26 | import fixtures |
74 | 27 | |||
75 | 25 | from snappy_ecosystem_tests.commons.config import CONFIG_STACK | 28 | from snappy_ecosystem_tests.commons.config import CONFIG_STACK |
76 | 26 | from snappy_ecosystem_tests.utils.storeconfig import get_current_store | 29 | from snappy_ecosystem_tests.utils.storeconfig import get_current_store |
77 | 27 | 30 | ||
78 | @@ -30,10 +33,8 @@ | |||
79 | 30 | """Create a temporary directory an cd into it for the test duration.""" | 33 | """Create a temporary directory an cd into it for the test duration.""" |
80 | 31 | 34 | ||
81 | 32 | def setUp(self): | 35 | def setUp(self): |
82 | 33 | """Create a temporary directory an cd into it for the test duration.""" | ||
83 | 34 | super().setUp() | 36 | super().setUp() |
86 | 35 | current_dir = os.getcwd() | 37 | self.addCleanup(os.chdir, os.getcwd()) |
85 | 36 | self.addCleanup(os.chdir, current_dir) | ||
87 | 37 | os.chdir(self.path) | 38 | os.chdir(self.path) |
88 | 38 | 39 | ||
89 | 39 | 40 | ||
90 | @@ -43,10 +44,8 @@ | |||
91 | 43 | 44 | ||
92 | 44 | def setUp(self): | 45 | def setUp(self): |
93 | 45 | super().setUp() | 46 | super().setUp() |
94 | 46 | |||
95 | 47 | current_environment = os.environ.copy() | 47 | current_environment = os.environ.copy() |
96 | 48 | os.environ = {} | 48 | os.environ = {} |
97 | 49 | |||
98 | 50 | self.addCleanup(os.environ.update, current_environment) | 49 | self.addCleanup(os.environ.update, current_environment) |
99 | 51 | 50 | ||
100 | 52 | 51 | ||
101 | 53 | 52 | ||
102 | === added directory 'snappy_ecosystem_tests/helpers/snapcraft' | |||
103 | === added file 'snappy_ecosystem_tests/helpers/snapcraft/__init__.py' | |||
104 | --- snappy_ecosystem_tests/helpers/snapcraft/__init__.py 1970-01-01 00:00:00 +0000 | |||
105 | +++ snappy_ecosystem_tests/helpers/snapcraft/__init__.py 2017-02-20 17:48:06 +0000 | |||
106 | @@ -0,0 +1,19 @@ | |||
107 | 1 | # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*- | ||
108 | 2 | |||
109 | 3 | # | ||
110 | 4 | # Snappy Ecosystem Tests | ||
111 | 5 | # Copyright (C) 2017 Canonical | ||
112 | 6 | # | ||
113 | 7 | # This program is free software: you can redistribute it and/or modify | ||
114 | 8 | # it under the terms of the GNU General Public License as published by | ||
115 | 9 | # the Free Software Foundation, either version 3 of the License, or | ||
116 | 10 | # (at your option) any later version. | ||
117 | 11 | # | ||
118 | 12 | # This program is distributed in the hope that it will be useful, | ||
119 | 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
120 | 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
121 | 15 | # GNU General Public License for more details. | ||
122 | 16 | # | ||
123 | 17 | # You should have received a copy of the GNU General Public License | ||
124 | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
125 | 19 | # | ||
126 | 0 | 20 | ||
127 | === added file 'snappy_ecosystem_tests/helpers/snapcraft/client.py' | |||
128 | --- snappy_ecosystem_tests/helpers/snapcraft/client.py 1970-01-01 00:00:00 +0000 | |||
129 | +++ snappy_ecosystem_tests/helpers/snapcraft/client.py 2017-02-20 17:48:06 +0000 | |||
130 | @@ -0,0 +1,74 @@ | |||
131 | 1 | # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*- | ||
132 | 2 | |||
133 | 3 | # | ||
134 | 4 | # Snappy Ecosystem Tests | ||
135 | 5 | # Copyright (C) 2017 Canonical | ||
136 | 6 | # | ||
137 | 7 | # This program is free software: you can redistribute it and/or modify | ||
138 | 8 | # it under the terms of the GNU General Public License as published by | ||
139 | 9 | # the Free Software Foundation, either version 3 of the License, or | ||
140 | 10 | # (at your option) any later version. | ||
141 | 11 | # | ||
142 | 12 | # This program is distributed in the hope that it will be useful, | ||
143 | 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
144 | 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
145 | 15 | # GNU General Public License for more details. | ||
146 | 16 | # | ||
147 | 17 | # You should have received a copy of the GNU General Public License | ||
148 | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
149 | 19 | # | ||
150 | 20 | |||
151 | 21 | """Snapcraft client helpers""" | ||
152 | 22 | |||
153 | 23 | import pexpect | ||
154 | 24 | |||
155 | 25 | |||
156 | 26 | # login credentials exported by shell environment | ||
157 | 27 | from snappy_ecosystem_tests.utils import storeconfig | ||
158 | 28 | |||
159 | 29 | LOGIN_EMAIL, LOGIN_PASSWORD = storeconfig.get_store_credentials() | ||
160 | 30 | |||
161 | 31 | |||
162 | 32 | class Snapcraft(object): | ||
163 | 33 | """Contain Snapcraft specific functionality to use via command | ||
164 | 34 | line interface""" | ||
165 | 35 | def __init__(self): | ||
166 | 36 | """Create new snapcraft instance.""" | ||
167 | 37 | self._login = False | ||
168 | 38 | self._cleanup() | ||
169 | 39 | |||
170 | 40 | def _cleanup(self): | ||
171 | 41 | """Perform cleanup actions""" | ||
172 | 42 | self.logout() | ||
173 | 43 | |||
174 | 44 | def logout(self): | ||
175 | 45 | """logout of snapcraft store session""" | ||
176 | 46 | child = pexpect.spawn("snapcraft logout") | ||
177 | 47 | err = child.expect('Credentials cleared.') | ||
178 | 48 | child.terminate(True) | ||
179 | 49 | child.wait() | ||
180 | 50 | if err is not 0: | ||
181 | 51 | raise ValueError("Failed to logout") | ||
182 | 52 | |||
183 | 53 | self._login = False | ||
184 | 54 | |||
185 | 55 | def login(self): | ||
186 | 56 | """login to store using the credential environment variables""" | ||
187 | 57 | child = pexpect.spawn("snapcraft login") | ||
188 | 58 | child.expect('Email: ') | ||
189 | 59 | child.sendline(LOGIN_EMAIL) | ||
190 | 60 | child.expect('Password:') | ||
191 | 61 | child.sendline(LOGIN_PASSWORD) | ||
192 | 62 | err = child.expect('Login successful') | ||
193 | 63 | if err is not 0: | ||
194 | 64 | raise ValueError("Failed to login") | ||
195 | 65 | |||
196 | 66 | self._login = True | ||
197 | 67 | |||
198 | 68 | def list_registered(self): | ||
199 | 69 | """call snapcraft list-registered command, | ||
200 | 70 | raise exception if not logged in""" | ||
201 | 71 | if self._login is False: | ||
202 | 72 | raise ValueError("User is not logged in, " | ||
203 | 73 | "please login before using this command") | ||
204 | 74 | return pexpect.spawnu("snapcraft list-registered").read() | ||
205 | 0 | 75 | ||
206 | === added file 'snappy_ecosystem_tests/helpers/snapcraft/config.py' | |||
207 | --- snappy_ecosystem_tests/helpers/snapcraft/config.py 1970-01-01 00:00:00 +0000 | |||
208 | +++ snappy_ecosystem_tests/helpers/snapcraft/config.py 2017-02-20 17:48:06 +0000 | |||
209 | @@ -0,0 +1,114 @@ | |||
210 | 1 | # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*- | ||
211 | 2 | |||
212 | 3 | # | ||
213 | 4 | # Snappy Ecosystem Tests | ||
214 | 5 | # Copyright (C) 2017 Canonical | ||
215 | 6 | # | ||
216 | 7 | # This program is free software: you can redistribute it and/or modify | ||
217 | 8 | # it under the terms of the GNU General Public License as published by | ||
218 | 9 | # the Free Software Foundation, either version 3 of the License, or | ||
219 | 10 | # (at your option) any later version. | ||
220 | 11 | # | ||
221 | 12 | # This program is distributed in the hope that it will be useful, | ||
222 | 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
223 | 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
224 | 15 | # GNU General Public License for more details. | ||
225 | 16 | # | ||
226 | 17 | # You should have received a copy of the GNU General Public License | ||
227 | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
228 | 19 | # | ||
229 | 20 | |||
230 | 21 | """Helpers to store/manage/load snapcraft configuration in a config file""" | ||
231 | 22 | |||
232 | 23 | import configparser | ||
233 | 24 | import logging | ||
234 | 25 | import os | ||
235 | 26 | import urllib.parse | ||
236 | 27 | |||
237 | 28 | from snappy_ecosystem_tests.commons.config import CONFIG_STACK | ||
238 | 29 | from xdg import BaseDirectory | ||
239 | 30 | |||
240 | 31 | LOCAL_CONFIG_FILENAME = '.snapcraft/snapcraft.cfg' | ||
241 | 32 | |||
242 | 33 | LOGGER = logging.getLogger(__name__) | ||
243 | 34 | |||
244 | 35 | |||
245 | 36 | class SnapcraftConfig(object): | ||
246 | 37 | """Hold configuration options in sections. | ||
247 | 38 | There can be two sections for the sso related credentials: production and | ||
248 | 39 | staging. This is governed by the UBUNTU_SSO_API_ROOT_URL environment | ||
249 | 40 | variable. Other sections are ignored but preserved. | ||
250 | 41 | """ | ||
251 | 42 | |||
252 | 43 | def __init__(self): | ||
253 | 44 | self.parser = configparser.ConfigParser() | ||
254 | 45 | self.filename = None | ||
255 | 46 | self.load() | ||
256 | 47 | |||
257 | 48 | @staticmethod | ||
258 | 49 | def _section_name(): | ||
259 | 50 | """Return a section""" | ||
260 | 51 | # The only section we care about is the host from the SSO url | ||
261 | 52 | url = os.environ.get('UBUNTU_SSO_API_ROOT_URL', | ||
262 | 53 | CONFIG_STACK.config.get('production_urls', 'sso')) | ||
263 | 54 | return urllib.parse.urlparse(url).netloc | ||
264 | 55 | |||
265 | 56 | def get(self, option_name): | ||
266 | 57 | """Get an option name for a given section""" | ||
267 | 58 | try: | ||
268 | 59 | return self.parser.get(self._section_name(), option_name) | ||
269 | 60 | except (configparser.NoSectionError, | ||
270 | 61 | configparser.NoOptionError, | ||
271 | 62 | KeyError): | ||
272 | 63 | return None | ||
273 | 64 | |||
274 | 65 | def set(self, option_name, value): | ||
275 | 66 | """Set an option with the given value""" | ||
276 | 67 | section_name = self._section_name() | ||
277 | 68 | if not self.parser.has_section(section_name): | ||
278 | 69 | self.parser.add_section(section_name) | ||
279 | 70 | return self.parser.set(section_name, option_name, value) | ||
280 | 71 | |||
281 | 72 | def is_empty(self): | ||
282 | 73 | """Return True if snapcraft config does not have the default section | ||
283 | 74 | name""" | ||
284 | 75 | # Only check the current section | ||
285 | 76 | section_name = self._section_name() | ||
286 | 77 | if self.parser.has_section(section_name): | ||
287 | 78 | if self.parser.options(section_name): | ||
288 | 79 | return False | ||
289 | 80 | return True | ||
290 | 81 | |||
291 | 82 | def load(self): | ||
292 | 83 | """Load the snapcraft configuration""" | ||
293 | 84 | # Local configurations (per project) are supposed to be static. | ||
294 | 85 | # That's why it's only checked for 'loading' and never written to. | ||
295 | 86 | # Essentially, all authentication-related changes, like login/logout | ||
296 | 87 | # or macaroon-refresh, will not be persisted for the next runs. | ||
297 | 88 | if os.path.exists(LOCAL_CONFIG_FILENAME): | ||
298 | 89 | self.parser.read(LOCAL_CONFIG_FILENAME) | ||
299 | 90 | LOGGER.warning( | ||
300 | 91 | 'Using local configuration (`%s`), changes will ' | ||
301 | 92 | 'not be persisted.', LOCAL_CONFIG_FILENAME) | ||
302 | 93 | return | ||
303 | 94 | |||
304 | 95 | self.filename = BaseDirectory.load_first_config( | ||
305 | 96 | 'snapcraft', 'snapcraft.cfg') | ||
306 | 97 | if self.filename and os.path.exists(self.filename): | ||
307 | 98 | self.parser.read(self.filename) | ||
308 | 99 | |||
309 | 100 | @staticmethod | ||
310 | 101 | def save_path(): | ||
311 | 102 | """Return the save path for the snapcraft configuration""" | ||
312 | 103 | return os.path.join(BaseDirectory.save_config_path('snapcraft'), | ||
313 | 104 | 'snapcraft.cfg') | ||
314 | 105 | |||
315 | 106 | def save(self): | ||
316 | 107 | """Save the current configuration in the save path""" | ||
317 | 108 | self.filename = self.save_path() | ||
318 | 109 | with open(self.filename, 'w') as _f: | ||
319 | 110 | self.parser.write(_f) | ||
320 | 111 | |||
321 | 112 | def clear(self): | ||
322 | 113 | """Clear the current configuration""" | ||
323 | 114 | self.parser.remove_section(self._section_name()) | ||
324 | 0 | 115 | ||
325 | === added file 'snappy_ecosystem_tests/helpers/snapcraft/constants.py' | |||
326 | --- snappy_ecosystem_tests/helpers/snapcraft/constants.py 1970-01-01 00:00:00 +0000 | |||
327 | +++ snappy_ecosystem_tests/helpers/snapcraft/constants.py 2017-02-20 17:48:06 +0000 | |||
328 | @@ -0,0 +1,52 @@ | |||
329 | 1 | # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*- | ||
330 | 2 | |||
331 | 3 | # | ||
332 | 4 | # Snappy Ecosystem Tests | ||
333 | 5 | # Copyright (C) 2017 Canonical | ||
334 | 6 | # | ||
335 | 7 | # This program is free software: you can redistribute it and/or modify | ||
336 | 8 | # it under the terms of the GNU General Public License as published by | ||
337 | 9 | # the Free Software Foundation, either version 3 of the License, or | ||
338 | 10 | # (at your option) any later version. | ||
339 | 11 | # | ||
340 | 12 | # This program is distributed in the hope that it will be useful, | ||
341 | 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
342 | 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
343 | 15 | # GNU General Public License for more details. | ||
344 | 16 | # | ||
345 | 17 | # You should have received a copy of the GNU General Public License | ||
346 | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
347 | 19 | # | ||
348 | 20 | |||
349 | 21 | """Store snapcraft constants""" | ||
350 | 22 | |||
351 | 23 | from __future__ import absolute_import, unicode_literals | ||
352 | 24 | |||
353 | 25 | |||
354 | 26 | DEFAULT_SERIES = '16' | ||
355 | 27 | SCAN_STATUS_POLL_DELAY = 5 | ||
356 | 28 | SCAN_STATUS_POLL_RETRIES = 5 | ||
357 | 29 | |||
358 | 30 | # Messages and warnings. | ||
359 | 31 | MISSING_AGREEMENT = 'Developer has not signed agreement.' | ||
360 | 32 | MISSING_NAMESPACE = 'Developer profile is missing short namespace.' | ||
361 | 33 | AGREEMENT_ERROR = ( | ||
362 | 34 | 'You must agree to the developer terms and conditions to upload snaps.') | ||
363 | 35 | NAMESPACE_ERROR = ( | ||
364 | 36 | 'You need to set a username. It will appear in the developer field ' | ||
365 | 37 | 'alongside the other details for your snap. Please visit {} and login ' | ||
366 | 38 | 'again.') | ||
367 | 39 | AGREEMENT_INPUT_MSG = ( | ||
368 | 40 | 'Do you agree to the developer terms and conditions. ({})? [y/N] ') | ||
369 | 41 | AGREEMENT_SIGN_ERROR = ( | ||
370 | 42 | 'Unexpected error encountered during signing the developer terms and ' | ||
371 | 43 | 'conditions. Please visit {} and agree to the terms and conditions before ' | ||
372 | 44 | 'continuing.') | ||
373 | 45 | TWO_FACTOR_WARNING = ( | ||
374 | 46 | 'We strongly recommend enabling multi-factor authentication: ' | ||
375 | 47 | 'https://help.ubuntu.com/community/SSO/FAQs/2FA') | ||
376 | 48 | INVALID_CREDENTIALS = 'Invalid credentials supplied.' | ||
377 | 49 | AUTHENTICATION_ERROR = ('Problems encountered when authenticating your ' | ||
378 | 50 | 'credentials.') | ||
379 | 51 | ACCOUNT_INFORMATION_ERROR = ('Unexpected error when obtaining your account ' | ||
380 | 52 | 'information.') | ||
381 | 0 | 53 | ||
382 | === added file 'snappy_ecosystem_tests/helpers/snapcraft/deprecations.py' | |||
383 | --- snappy_ecosystem_tests/helpers/snapcraft/deprecations.py 1970-01-01 00:00:00 +0000 | |||
384 | +++ snappy_ecosystem_tests/helpers/snapcraft/deprecations.py 2017-02-20 17:48:06 +0000 | |||
385 | @@ -0,0 +1,56 @@ | |||
386 | 1 | # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*- | ||
387 | 2 | |||
388 | 3 | # | ||
389 | 4 | # Snappy Ecosystem Tests | ||
390 | 5 | # Copyright (C) 2017 Canonical | ||
391 | 6 | # | ||
392 | 7 | # This program is free software: you can redistribute it and/or modify | ||
393 | 8 | # it under the terms of the GNU General Public License as published by | ||
394 | 9 | # the Free Software Foundation, either version 3 of the License, or | ||
395 | 10 | # (at your option) any later version. | ||
396 | 11 | # | ||
397 | 12 | # This program is distributed in the hope that it will be useful, | ||
398 | 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
399 | 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
400 | 15 | # GNU General Public License for more details. | ||
401 | 16 | # | ||
402 | 17 | # You should have received a copy of the GNU General Public License | ||
403 | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
404 | 19 | # | ||
405 | 20 | |||
406 | 21 | """Handle surfacing deprecation notices. | ||
407 | 22 | |||
408 | 23 | When a new deprecation has occurred, write a Deprecation Notice for it on the | ||
409 | 24 | wiki, assigning it the next ID (DN1, DN2, etc.). Then add that ID along with a | ||
410 | 25 | brief message for surfacing to the user into the _DEPRECATION_MESSAGES in this | ||
411 | 26 | module. | ||
412 | 27 | """ | ||
413 | 28 | |||
414 | 29 | import logging | ||
415 | 30 | |||
416 | 31 | _DEPRECATION_MESSAGES = { | ||
417 | 32 | 'dn1': "The 'snap' keyword has been replaced by 'prime'.", | ||
418 | 33 | 'dn2': "Custom plugins should now be placed in 'snap/plugins'.", | ||
419 | 34 | 'dn3': "Assets in 'setup/gui' should now be placed in 'snap/gui'.", | ||
420 | 35 | } | ||
421 | 36 | |||
422 | 37 | _DEPRECATION_URL_FMT = 'http://snapcraft.io/docs/deprecation-notices/{id}' | ||
423 | 38 | |||
424 | 39 | LOGGER = logging.getLogger(__name__) | ||
425 | 40 | |||
426 | 41 | |||
427 | 42 | def _deprecation_message(_id): | ||
428 | 43 | """Return the deprecation message for the given id | ||
429 | 44 | :raise RuntimeError | ||
430 | 45 | """ | ||
431 | 46 | message = _DEPRECATION_MESSAGES.get(_id) | ||
432 | 47 | if not message: | ||
433 | 48 | raise RuntimeError('No deprecation notice with id {!r}'.format(id)) | ||
434 | 49 | return message | ||
435 | 50 | |||
436 | 51 | |||
437 | 52 | def handle_deprecation_notice(_id): | ||
438 | 53 | """Handle deprecation notice by logging a message""" | ||
439 | 54 | message = _deprecation_message(_id) | ||
440 | 55 | LOGGER.warning('DEPRECATED: %s\nSee %s for more information.', | ||
441 | 56 | message, _DEPRECATION_URL_FMT.format(id=_id)) | ||
442 | 0 | 57 | ||
443 | === added file 'snappy_ecosystem_tests/helpers/snapcraft/indicators.py' | |||
444 | --- snappy_ecosystem_tests/helpers/snapcraft/indicators.py 1970-01-01 00:00:00 +0000 | |||
445 | +++ snappy_ecosystem_tests/helpers/snapcraft/indicators.py 2017-02-20 17:48:06 +0000 | |||
446 | @@ -0,0 +1,118 @@ | |||
447 | 1 | # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*- | ||
448 | 2 | |||
449 | 3 | # | ||
450 | 4 | # Snappy Ecosystem Tests | ||
451 | 5 | # Copyright (C) 2017 Canonical | ||
452 | 6 | # | ||
453 | 7 | # This program is free software: you can redistribute it and/or modify | ||
454 | 8 | # it under the terms of the GNU General Public License as published by | ||
455 | 9 | # the Free Software Foundation, either version 3 of the License, or | ||
456 | 10 | # (at your option) any later version. | ||
457 | 11 | # | ||
458 | 12 | # This program is distributed in the hope that it will be useful, | ||
459 | 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
460 | 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
461 | 15 | # GNU General Public License for more details. | ||
462 | 16 | # | ||
463 | 17 | # You should have received a copy of the GNU General Public License | ||
464 | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
465 | 19 | # | ||
466 | 20 | |||
467 | 21 | """Helpers for snapcraft indicators with pretty progress bar""" | ||
468 | 22 | |||
469 | 23 | import os | ||
470 | 24 | import sys | ||
471 | 25 | |||
472 | 26 | from urllib.request import urlretrieve | ||
473 | 27 | from progressbar import ( | ||
474 | 28 | AnimatedMarker, | ||
475 | 29 | Bar, | ||
476 | 30 | Percentage, | ||
477 | 31 | ProgressBar, | ||
478 | 32 | UnknownLength, | ||
479 | 33 | ) | ||
480 | 34 | |||
481 | 35 | |||
482 | 36 | def _init_progress_bar(total_length, destination, message=None): | ||
483 | 37 | """Init the progress bar with the given length, destination and message""" | ||
484 | 38 | if not message: | ||
485 | 39 | message = 'Downloading {!r}'.format(os.path.basename(destination)) | ||
486 | 40 | |||
487 | 41 | valid_length = total_length and total_length > 0 | ||
488 | 42 | |||
489 | 43 | if valid_length and is_dumb_terminal(): | ||
490 | 44 | widgets = [message, ' ', Percentage()] | ||
491 | 45 | maxval = total_length | ||
492 | 46 | elif valid_length and not is_dumb_terminal(): | ||
493 | 47 | widgets = [message, | ||
494 | 48 | Bar(marker='=', left='[', right=']'), | ||
495 | 49 | ' ', Percentage()] | ||
496 | 50 | maxval = total_length | ||
497 | 51 | elif not valid_length and is_dumb_terminal(): | ||
498 | 52 | widgets = [message] | ||
499 | 53 | maxval = UnknownLength | ||
500 | 54 | else: | ||
501 | 55 | widgets = [message, AnimatedMarker()] | ||
502 | 56 | maxval = UnknownLength | ||
503 | 57 | |||
504 | 58 | return ProgressBar(widgets=widgets, maxval=maxval) | ||
505 | 59 | |||
506 | 60 | |||
507 | 61 | def download_requests_stream(request_stream, destination, message=None): | ||
508 | 62 | """This is a facility to download a request with nice progress bars.""" | ||
509 | 63 | |||
510 | 64 | # Doing len(request_stream.content) may defeat the purpose of a | ||
511 | 65 | # progress bar | ||
512 | 66 | total_length = 0 | ||
513 | 67 | if not request_stream.headers.get('Content-Encoding', ''): | ||
514 | 68 | total_length = int(request_stream.headers.get('Content-Length', '0')) | ||
515 | 69 | |||
516 | 70 | total_read = 0 | ||
517 | 71 | progress_bar = _init_progress_bar(total_length, destination, message) | ||
518 | 72 | progress_bar.start() | ||
519 | 73 | with open(destination, 'wb') as destination_file: | ||
520 | 74 | for buf in request_stream.iter_content(1024): | ||
521 | 75 | destination_file.write(buf) | ||
522 | 76 | total_read += len(buf) | ||
523 | 77 | progress_bar.update(total_read) | ||
524 | 78 | progress_bar.finish() | ||
525 | 79 | |||
526 | 80 | |||
527 | 81 | class UrllibDownloader(object): | ||
528 | 82 | """This is a facility to download an uri with nice progress bars.""" | ||
529 | 83 | |||
530 | 84 | def __init__(self, uri, destination, message=None): | ||
531 | 85 | self.uri = uri | ||
532 | 86 | self.destination = destination | ||
533 | 87 | self.message = message | ||
534 | 88 | self.progress_bar = None | ||
535 | 89 | |||
536 | 90 | def download(self): | ||
537 | 91 | """Download with a pretty progress bar""" | ||
538 | 92 | urlretrieve(self.uri, self.destination, self._progress_callback) | ||
539 | 93 | |||
540 | 94 | if self.progress_bar: | ||
541 | 95 | self.progress_bar.finish() | ||
542 | 96 | |||
543 | 97 | def _progress_callback(self, block_num, block_size, total_length): | ||
544 | 98 | """Callback to be used when downloading""" | ||
545 | 99 | if not self.progress_bar: | ||
546 | 100 | self.progress_bar = _init_progress_bar( | ||
547 | 101 | total_length, self.destination, self.message) | ||
548 | 102 | self.progress_bar.start() | ||
549 | 103 | |||
550 | 104 | total_read = block_num * block_size | ||
551 | 105 | self.progress_bar.update( | ||
552 | 106 | min(total_read, total_length) if total_length > 0 else total_read) | ||
553 | 107 | |||
554 | 108 | |||
555 | 109 | def download_urllib_source(uri, destination, message=None): | ||
556 | 110 | """Perform a download from the given uri to the given destination""" | ||
557 | 111 | UrllibDownloader(uri, destination, message).download() | ||
558 | 112 | |||
559 | 113 | |||
560 | 114 | def is_dumb_terminal(): | ||
561 | 115 | """Return True if on a dumb terminal.""" | ||
562 | 116 | is_stdout_tty = os.isatty(sys.stdout.fileno()) | ||
563 | 117 | is_term_dumb = os.environ.get('TERM', '') == 'dumb' | ||
564 | 118 | return not is_stdout_tty or is_term_dumb | ||
565 | 0 | 119 | ||
566 | === added file 'snappy_ecosystem_tests/helpers/snapcraft/options.py' | |||
567 | --- snappy_ecosystem_tests/helpers/snapcraft/options.py 1970-01-01 00:00:00 +0000 | |||
568 | +++ snappy_ecosystem_tests/helpers/snapcraft/options.py 2017-02-20 17:48:06 +0000 | |||
569 | @@ -0,0 +1,244 @@ | |||
570 | 1 | # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*- | ||
571 | 2 | |||
572 | 3 | # | ||
573 | 4 | # Snappy Ecosystem Tests | ||
574 | 5 | # Copyright (C) 2017 Canonical | ||
575 | 6 | # | ||
576 | 7 | # This program is free software: you can redistribute it and/or modify | ||
577 | 8 | # it under the terms of the GNU General Public License as published by | ||
578 | 9 | # the Free Software Foundation, either version 3 of the License, or | ||
579 | 10 | # (at your option) any later version. | ||
580 | 11 | # | ||
581 | 12 | # This program is distributed in the hope that it will be useful, | ||
582 | 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
583 | 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
584 | 15 | # GNU General Public License for more details. | ||
585 | 16 | # | ||
586 | 17 | # You should have received a copy of the GNU General Public License | ||
587 | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
588 | 19 | # | ||
589 | 20 | |||
590 | 21 | """Manage snapcraft project options""" | ||
591 | 22 | |||
592 | 23 | import logging | ||
593 | 24 | import multiprocessing | ||
594 | 25 | import os | ||
595 | 26 | import platform | ||
596 | 27 | |||
597 | 28 | from snappy_ecosystem_tests.helpers.snapcraft.deprecations import ( | ||
598 | 29 | handle_deprecation_notice | ||
599 | 30 | ) | ||
600 | 31 | |||
601 | 32 | LOGGER = logging.getLogger(__name__) | ||
602 | 33 | |||
603 | 34 | |||
604 | 35 | _ARCH_TRANSLATIONS = { | ||
605 | 36 | 'armv7l': { | ||
606 | 37 | 'kernel': 'arm', | ||
607 | 38 | 'deb': 'armhf', | ||
608 | 39 | 'cross-compiler-prefix': 'arm-linux-gnueabihf-', | ||
609 | 40 | 'cross-build-packages': ['gcc-arm-linux-gnueabihf'], | ||
610 | 41 | 'triplet': 'arm-linux-gnueabihf', | ||
611 | 42 | 'core-dynamic-linker': 'lib/ld-linux-armhf.so.3', | ||
612 | 43 | }, | ||
613 | 44 | 'aarch64': { | ||
614 | 45 | 'kernel': 'arm64', | ||
615 | 46 | 'deb': 'arm64', | ||
616 | 47 | 'cross-compiler-prefix': 'aarch64-linux-gnu-', | ||
617 | 48 | 'cross-build-packages': ['gcc-aarch64-linux-gnu'], | ||
618 | 49 | 'triplet': 'aarch64-linux-gnu', | ||
619 | 50 | 'core-dynamic-linker': 'lib/ld-linux-aarch64.so.1', | ||
620 | 51 | }, | ||
621 | 52 | 'i686': { | ||
622 | 53 | 'kernel': 'x86', | ||
623 | 54 | 'deb': 'i386', | ||
624 | 55 | 'triplet': 'i386-linux-gnu', | ||
625 | 56 | }, | ||
626 | 57 | 'ppc64le': { | ||
627 | 58 | 'kernel': 'powerpc', | ||
628 | 59 | 'deb': 'ppc64el', | ||
629 | 60 | 'cross-compiler-prefix': 'powerpc64le-linux-gnu-', | ||
630 | 61 | 'cross-build-packages': ['gcc-powerpc64le-linux-gnu'], | ||
631 | 62 | 'triplet': 'powerpc64le-linux-gnu', | ||
632 | 63 | 'core-dynamic-linker': '/lib64/ld64.so.2', | ||
633 | 64 | }, | ||
634 | 65 | 'ppc': { | ||
635 | 66 | 'kernel': 'powerpc', | ||
636 | 67 | 'deb': 'powerpc', | ||
637 | 68 | 'cross-compiler-prefix': 'powerpc-linux-gnu-', | ||
638 | 69 | 'cross-build-packages': ['gcc-powerpc-linux-gnu'], | ||
639 | 70 | 'triplet': 'powerpc-linux-gnu', | ||
640 | 71 | }, | ||
641 | 72 | 'x86_64': { | ||
642 | 73 | 'kernel': 'x86', | ||
643 | 74 | 'deb': 'amd64', | ||
644 | 75 | 'triplet': 'x86_64-linux-gnu', | ||
645 | 76 | 'core-dynamic-linker': 'lib64/ld-linux-x86-64.so.2', | ||
646 | 77 | }, | ||
647 | 78 | 's390x': { | ||
648 | 79 | 'kernel': 's390x', | ||
649 | 80 | 'deb': 's390x', | ||
650 | 81 | 'cross-compiler-prefix': 's390x-linux-gnu-', | ||
651 | 82 | 'cross-build-packages': ['gcc-s390x-linux-gnu'], | ||
652 | 83 | 'triplet': 's390x-linux-gnu', | ||
653 | 84 | 'core-dynamic-linker': '/lib/ld64.so.1', | ||
654 | 85 | } | ||
655 | 86 | } | ||
656 | 87 | |||
657 | 88 | |||
658 | 89 | _32BIT_USERSPACE_ARCHITECTURE = { | ||
659 | 90 | 'aarch64': 'armv7l', | ||
660 | 91 | 'armv8l': 'armv7l', | ||
661 | 92 | 'ppc64le': 'ppc', | ||
662 | 93 | 'x86_64': 'i686', | ||
663 | 94 | } | ||
664 | 95 | |||
665 | 96 | |||
666 | 97 | def _get_platform_architecture(): | ||
667 | 98 | """Return the current platform architecture of the machine""" | ||
668 | 99 | architecture = platform.machine() | ||
669 | 100 | if platform.architecture()[0] == '32bit': | ||
670 | 101 | userspace = _32BIT_USERSPACE_ARCHITECTURE.get(architecture) | ||
671 | 102 | if userspace: | ||
672 | 103 | architecture = userspace | ||
673 | 104 | return architecture | ||
674 | 105 | |||
675 | 106 | |||
676 | 107 | class ProjectOptions: | ||
677 | 108 | """Represents a snapcraft project options""" | ||
678 | 109 | def __init__(self, use_geoip=False, parallel_builds=True, | ||
679 | 110 | target_deb_arch=None, debug=False): | ||
680 | 111 | self.__project_dir = os.getcwd() | ||
681 | 112 | self.__use_geoip = use_geoip | ||
682 | 113 | self.__parallel_builds = parallel_builds | ||
683 | 114 | self._set_machine(target_deb_arch) | ||
684 | 115 | self.__debug = debug | ||
685 | 116 | |||
686 | 117 | @property | ||
687 | 118 | def use_geoip(self): | ||
688 | 119 | """Return True if the project uses geoip""" | ||
689 | 120 | return self.__use_geoip | ||
690 | 121 | |||
691 | 122 | @property | ||
692 | 123 | def parallel_builds(self): | ||
693 | 124 | """Return True if the project uses parallel build""" | ||
694 | 125 | return self.__parallel_builds | ||
695 | 126 | |||
696 | 127 | @property | ||
697 | 128 | def parallel_build_count(self): | ||
698 | 129 | """Return the amount of cpus used for parallel builds""" | ||
699 | 130 | build_count = 1 | ||
700 | 131 | if self.__parallel_builds: | ||
701 | 132 | try: | ||
702 | 133 | build_count = multiprocessing.cpu_count() | ||
703 | 134 | except NotImplementedError: | ||
704 | 135 | LOGGER.warning( | ||
705 | 136 | 'Unable to determine CPU count; disabling parallel builds') | ||
706 | 137 | return build_count | ||
707 | 138 | |||
708 | 139 | @property | ||
709 | 140 | def is_cross_compiling(self): | ||
710 | 141 | """Return True if the target machine does not match | ||
711 | 142 | the platform arch of the project""" | ||
712 | 143 | return self.__target_machine != self.__platform_arch | ||
713 | 144 | |||
714 | 145 | @property | ||
715 | 146 | def cross_compiler_prefix(self): | ||
716 | 147 | """Return the cross compiler prefix""" | ||
717 | 148 | try: | ||
718 | 149 | return self.__machine_info['cross-compiler-prefix'] | ||
719 | 150 | except KeyError: | ||
720 | 151 | raise EnvironmentError( | ||
721 | 152 | 'Cross compilation not support for target arch {}'.format( | ||
722 | 153 | self.__target_machine)) | ||
723 | 154 | |||
724 | 155 | @property | ||
725 | 156 | def additional_build_packages(self): | ||
726 | 157 | """Return the additional build packages from the machine info""" | ||
727 | 158 | packages = [] | ||
728 | 159 | if self.is_cross_compiling: | ||
729 | 160 | packages.extend(self.__machine_info.get( | ||
730 | 161 | 'cross-build-packages', [])) | ||
731 | 162 | return packages | ||
732 | 163 | |||
733 | 164 | @property | ||
734 | 165 | def arch_triplet(self): | ||
735 | 166 | """Return the arch triplet from the machine info""" | ||
736 | 167 | return self.__machine_info['triplet'] | ||
737 | 168 | |||
738 | 169 | @property | ||
739 | 170 | def deb_arch(self): | ||
740 | 171 | """Return the deb arch from the machine info""" | ||
741 | 172 | return self.__machine_info['deb'] | ||
742 | 173 | |||
743 | 174 | @property | ||
744 | 175 | def kernel_arch(self): | ||
745 | 176 | """Return the kernel arch from the machine info""" | ||
746 | 177 | return self.__machine_info['kernel'] | ||
747 | 178 | |||
748 | 179 | @property | ||
749 | 180 | def local_plugins_dir(self): | ||
750 | 181 | """Return the plugin directory""" | ||
751 | 182 | deprecated_plugins_dir = os.path.join(self.parts_dir, 'plugins') | ||
752 | 183 | if os.path.exists(deprecated_plugins_dir): | ||
753 | 184 | handle_deprecation_notice('dn2') | ||
754 | 185 | return deprecated_plugins_dir | ||
755 | 186 | return os.path.join(self.__project_dir, 'snap', 'plugins') | ||
756 | 187 | |||
757 | 188 | @property | ||
758 | 189 | def parts_dir(self): | ||
759 | 190 | """Return the parts directory""" | ||
760 | 191 | return os.path.join(self.__project_dir, 'parts') | ||
761 | 192 | |||
762 | 193 | @property | ||
763 | 194 | def stage_dir(self): | ||
764 | 195 | """Return the stage directory""" | ||
765 | 196 | return os.path.join(self.__project_dir, 'stage') | ||
766 | 197 | |||
767 | 198 | @property | ||
768 | 199 | def snap_dir(self): | ||
769 | 200 | """Return the snap directory""" | ||
770 | 201 | return os.path.join(self.__project_dir, 'prime') | ||
771 | 202 | |||
772 | 203 | @property | ||
773 | 204 | def debug(self): | ||
774 | 205 | """Return debug of the project options""" | ||
775 | 206 | return self.__debug | ||
776 | 207 | |||
777 | 208 | def get_core_dynamic_linker(self): | ||
778 | 209 | """Returns the dynamic linker used for the targetted core. | ||
779 | 210 | If not found realpath for `/lib/ld-linux.so.2` is returned. | ||
780 | 211 | However if core is not installed None will be returned. | ||
781 | 212 | """ | ||
782 | 213 | core_path = os.path.join('/snap', 'core', 'current') | ||
783 | 214 | core_dynamic_linker = self.__machine_info.get('core-dynamic-linker', | ||
784 | 215 | 'lib/ld-linux.so.2') | ||
785 | 216 | |||
786 | 217 | try: | ||
787 | 218 | dynamic_linker_resolved_path = os.readlink( | ||
788 | 219 | os.path.join(core_path, core_dynamic_linker)) | ||
789 | 220 | dynamic_linker_path = os.path.join( | ||
790 | 221 | core_path, dynamic_linker_resolved_path.lstrip('/')) | ||
791 | 222 | except FileNotFoundError: | ||
792 | 223 | dynamic_linker_path = None | ||
793 | 224 | |||
794 | 225 | return dynamic_linker_path | ||
795 | 226 | |||
796 | 227 | def _set_machine(self, target_deb_arch): | ||
797 | 228 | """Set the target machine with the given deb arch""" | ||
798 | 229 | self.__platform_arch = _get_platform_architecture() | ||
799 | 230 | if not target_deb_arch: | ||
800 | 231 | self.__target_machine = self.__platform_arch | ||
801 | 232 | else: | ||
802 | 233 | self.__target_machine = _find_machine(target_deb_arch) | ||
803 | 234 | LOGGER.info('Setting target machine to {%s}', target_deb_arch) | ||
804 | 235 | self.__machine_info = _ARCH_TRANSLATIONS[self.__target_machine] | ||
805 | 236 | |||
806 | 237 | |||
807 | 238 | def _find_machine(deb_arch): | ||
808 | 239 | """Return a machine for the given deb arch""" | ||
809 | 240 | for machine in _ARCH_TRANSLATIONS: | ||
810 | 241 | if _ARCH_TRANSLATIONS[machine].get('deb', '') == deb_arch: | ||
811 | 242 | return machine | ||
812 | 243 | raise EnvironmentError( | ||
813 | 244 | 'Cannot set machine from deb_arch {!r}'.format(deb_arch)) | ||
814 | 0 | 245 | ||
815 | === renamed directory 'snappy_ecosystem_tests/snapd' => 'snappy_ecosystem_tests/helpers/snapd' | |||
816 | === added directory 'snappy_ecosystem_tests/helpers/store_apis' | |||
817 | === added file 'snappy_ecosystem_tests/helpers/store_apis/__init__.py' | |||
818 | --- snappy_ecosystem_tests/helpers/store_apis/__init__.py 1970-01-01 00:00:00 +0000 | |||
819 | +++ snappy_ecosystem_tests/helpers/store_apis/__init__.py 2017-02-20 17:48:06 +0000 | |||
820 | @@ -0,0 +1,19 @@ | |||
821 | 1 | # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*- | ||
822 | 2 | |||
823 | 3 | # | ||
824 | 4 | # Snappy Ecosystem Tests | ||
825 | 5 | # Copyright (C) 2017 Canonical | ||
826 | 6 | # | ||
827 | 7 | # This program is free software: you can redistribute it and/or modify | ||
828 | 8 | # it under the terms of the GNU General Public License as published by | ||
829 | 9 | # the Free Software Foundation, either version 3 of the License, or | ||
830 | 10 | # (at your option) any later version. | ||
831 | 11 | # | ||
832 | 12 | # This program is distributed in the hope that it will be useful, | ||
833 | 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
834 | 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
835 | 15 | # GNU General Public License for more details. | ||
836 | 16 | # | ||
837 | 17 | # You should have received a copy of the GNU General Public License | ||
838 | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
839 | 19 | # | ||
840 | 0 | 20 | ||
841 | === added file 'snappy_ecosystem_tests/helpers/store_apis/errors.py' | |||
842 | --- snappy_ecosystem_tests/helpers/store_apis/errors.py 1970-01-01 00:00:00 +0000 | |||
843 | +++ snappy_ecosystem_tests/helpers/store_apis/errors.py 2017-02-20 17:48:06 +0000 | |||
844 | @@ -0,0 +1,371 @@ | |||
845 | 1 | # -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*- | ||
846 | 2 | # | ||
847 | 3 | # Copyright (C) 2017 Canonical Ltd | ||
848 | 4 | # | ||
849 | 5 | # This program is free software: you can redistribute it and/or modify | ||
850 | 6 | # it under the terms of the GNU General Public License version 3 as | ||
851 | 7 | # published by the Free Software Foundation. | ||
852 | 8 | # | ||
853 | 9 | # This program is distributed in the hope that it will be useful, | ||
854 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
855 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
856 | 12 | # GNU General Public License for more details. | ||
857 | 13 | # | ||
858 | 14 | # You should have received a copy of the GNU General Public License | ||
859 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
860 | 16 | |||
861 | 17 | """All the store exceptions""" | ||
862 | 18 | |||
863 | 19 | from simplejson.scanner import JSONDecodeError | ||
864 | 20 | |||
865 | 21 | |||
866 | 22 | class StoreException(Exception): | ||
867 | 23 | """Base class for all store exceptions. | ||
868 | 24 | :cvar fmt: A format string that daughter classes override | ||
869 | 25 | """ | ||
870 | 26 | fmt = 'Daughter classes should redefine this' | ||
871 | 27 | |||
872 | 28 | def __init__(self, **kwargs): | ||
873 | 29 | super().__init__() | ||
874 | 30 | for key, value in kwargs.items(): | ||
875 | 31 | setattr(self, key, value) | ||
876 | 32 | |||
877 | 33 | def __str__(self): | ||
878 | 34 | return self.fmt.format([], **self.__dict__) | ||
879 | 35 | |||
880 | 36 | |||
881 | 37 | class InvalidCredentialsError(StoreException): | ||
882 | 38 | """Exception to be raised when credentials are invalid""" | ||
883 | 39 | fmt = 'Invalid credentials: {message}.' | ||
884 | 40 | |||
885 | 41 | def __init__(self, message): | ||
886 | 42 | super().__init__(message=message) | ||
887 | 43 | |||
888 | 44 | |||
889 | 45 | class SnapNotFoundError(StoreException): | ||
890 | 46 | """Exception to be raised when a snap is not found in the store""" | ||
891 | 47 | __FMT_ARCH_CHANNEL = ( | ||
892 | 48 | 'Snap {name!r} for {arch!r} cannot be found in the {channel!r} ' | ||
893 | 49 | 'channel.') | ||
894 | 50 | __FMT_CHANNEL = 'Snap {name!r} was not found in the {channel!r} channel.' | ||
895 | 51 | __FMT_SERIES_ARCH = ( | ||
896 | 52 | 'Snap {name!r} for {arch!r} was not found in {series!r} series.') | ||
897 | 53 | __FMT_SERIES = 'Snap {name!r} was not found in {series!r} series.' | ||
898 | 54 | |||
899 | 55 | fmt = 'Snap {name!r} was not found.' | ||
900 | 56 | |||
901 | 57 | def __init__(self, name, channel=None, arch=None, series=None): | ||
902 | 58 | if channel and arch: | ||
903 | 59 | self.fmt = self.__FMT_ARCH_CHANNEL | ||
904 | 60 | elif channel: | ||
905 | 61 | self.fmt = self.__FMT_CHANNEL | ||
906 | 62 | elif series and arch: | ||
907 | 63 | self.fmt = self.__FMT_SERIES_ARCH | ||
908 | 64 | elif series: | ||
909 | 65 | self.fmt = self.__FMT_SERIES | ||
910 | 66 | |||
911 | 67 | super().__init__(name=name, channel=channel, arch=arch, series=series) | ||
912 | 68 | |||
913 | 69 | |||
914 | 70 | class SHAMismatchError(StoreException): | ||
915 | 71 | """Exception to be raised when there is a mismatch in sha512""" | ||
916 | 72 | fmt = 'SHA512 checksum for {path} is not {expected_sha}.' | ||
917 | 73 | |||
918 | 74 | def __init__(self, path, expected_sha): | ||
919 | 75 | super().__init__(path=path, expected_sha=expected_sha) | ||
920 | 76 | |||
921 | 77 | |||
922 | 78 | class StoreAuthenticationError(StoreException): | ||
923 | 79 | """Exception to be raised when there is an authentication error in the | ||
924 | 80 | store""" | ||
925 | 81 | fmt = 'Authentication error: {}.' | ||
926 | 82 | |||
927 | 83 | def __init__(self, message): | ||
928 | 84 | super().__init__(message=message) | ||
929 | 85 | |||
930 | 86 | |||
931 | 87 | class StoreTwoFactorAuthenticationRequired(StoreAuthenticationError): | ||
932 | 88 | """Exception to be raised when the credentials were provided but 2-factor | ||
933 | 89 | authentication was not and is required. | ||
934 | 90 | """ | ||
935 | 91 | def __init__(self): | ||
936 | 92 | super().__init__("Two-factor authentication required.") | ||
937 | 93 | |||
938 | 94 | |||
939 | 95 | class StoreMacaroonNeedsRefreshError(StoreException): | ||
940 | 96 | """Exception to be raised when a request needs to refresh | ||
941 | 97 | the macaroon to be authenticated properly""" | ||
942 | 98 | fmt = 'Authentication macaroon needs to be refreshed.' | ||
943 | 99 | |||
944 | 100 | |||
945 | 101 | class DeveloperAgreementSignError(StoreException): | ||
946 | 102 | """Exception to be raised when developer tries to do an operation | ||
947 | 103 | in the store but he did not sign the Agreement yet""" | ||
948 | 104 | fmt = ( | ||
949 | 105 | 'There was an error while signing developer agreement.\n' | ||
950 | 106 | 'Reason: {reason!r}\n' | ||
951 | 107 | 'Text: {text!r}') | ||
952 | 108 | |||
953 | 109 | def __init__(self, response): | ||
954 | 110 | super().__init__(reason=response.reason, text=response.text) | ||
955 | 111 | |||
956 | 112 | |||
957 | 113 | class NeedTermsSignedError(StoreException): | ||
958 | 114 | """Exception to be raised when an operation needs the Developer | ||
959 | 115 | Terms of Service to be signed before proceed""" | ||
960 | 116 | fmt = ( | ||
961 | 117 | 'Developer Terms of Service agreement must be signed ' | ||
962 | 118 | 'before continuing: {message}') | ||
963 | 119 | |||
964 | 120 | def __init__(self, message): | ||
965 | 121 | super().__init__(message=message) | ||
966 | 122 | |||
967 | 123 | |||
968 | 124 | class StoreAccountInformationError(StoreException): | ||
969 | 125 | """Exception to be raised when there was not possible | ||
970 | 126 | to fetch the user account information""" | ||
971 | 127 | fmt = 'Error fetching account information from store: {error}' | ||
972 | 128 | |||
973 | 129 | def __init__(self, response): | ||
974 | 130 | error = '{} {}'.format(response.status_code, response.reason) | ||
975 | 131 | extra = [] | ||
976 | 132 | try: | ||
977 | 133 | response_json = response.json() | ||
978 | 134 | if 'error_list' in response_json: | ||
979 | 135 | error = ' '.join( | ||
980 | 136 | error['message'] for error in response_json['error_list']) | ||
981 | 137 | extra = [ | ||
982 | 138 | error['extra'] for error in response_json[ | ||
983 | 139 | 'error_list'] if 'extra' in error] | ||
984 | 140 | except JSONDecodeError: | ||
985 | 141 | pass | ||
986 | 142 | super().__init__(error=error, extra=extra) | ||
987 | 143 | |||
988 | 144 | |||
989 | 145 | class StoreKeyRegistrationError(StoreException): | ||
990 | 146 | """Exception to be raised when there was a problem | ||
991 | 147 | registering a key in the store""" | ||
992 | 148 | fmt = 'Key registration failed: {error}' | ||
993 | 149 | |||
994 | 150 | def __init__(self, response): | ||
995 | 151 | error = '{} {}'.format(response.status_code, response.reason) | ||
996 | 152 | try: | ||
997 | 153 | response_json = response.json() | ||
998 | 154 | if 'error_list' in response_json: | ||
999 | 155 | error = ' '.join( | ||
1000 | 156 | error['message'] for error in response_json['error_list']) | ||
1001 | 157 | except JSONDecodeError: | ||
1002 | 158 | pass | ||
1003 | 159 | super().__init__(error=error) | ||
1004 | 160 | |||
1005 | 161 | |||
1006 | 162 | class StoreRegistrationError(StoreException): | ||
1007 | 163 | """Exception to be raised when there was a problem registering | ||
1008 | 164 | a snap name""" | ||
1009 | 165 | __FMT_ALREADY_REGISTERED = ( | ||
1010 | 166 | 'The name {snap_name!r} is already taken.\n\n' | ||
1011 | 167 | 'We can if needed rename snaps to ensure they match the expectations ' | ||
1012 | 168 | 'of most users. If you are the publisher most users expect for ' | ||
1013 | 169 | '{snap_name!r} then claim the name at {register_name_url!r}') | ||
1014 | 170 | |||
1015 | 171 | __FMT_ALREADY_OWNED = 'You already own the name {snap_name!r}.' | ||
1016 | 172 | |||
1017 | 173 | __FMT_RESERVED = ( | ||
1018 | 174 | 'The name {snap_name!r} is reserved.\n\n' | ||
1019 | 175 | 'If you are the publisher most users expect for ' | ||
1020 | 176 | '{snap_name!r} then please claim the name at {register_name_url!r}') | ||
1021 | 177 | |||
1022 | 178 | __FMT_RETRY_WAIT = ( | ||
1023 | 179 | 'You must wait {retry_after} seconds before trying to register ' | ||
1024 | 180 | 'your next snap.') | ||
1025 | 181 | |||
1026 | 182 | fmt = 'Registration failed.' | ||
1027 | 183 | |||
1028 | 184 | __error_messages = { | ||
1029 | 185 | 'already_registered': __FMT_ALREADY_REGISTERED, | ||
1030 | 186 | 'already_owned': __FMT_ALREADY_OWNED, | ||
1031 | 187 | 'reserved_name': __FMT_RESERVED, | ||
1032 | 188 | 'register_window': __FMT_RETRY_WAIT, | ||
1033 | 189 | } | ||
1034 | 190 | |||
1035 | 191 | def __init__(self, snap_name, response): | ||
1036 | 192 | try: | ||
1037 | 193 | response_json = response.json() | ||
1038 | 194 | except JSONDecodeError: | ||
1039 | 195 | response_json = {} | ||
1040 | 196 | |||
1041 | 197 | error_code = response_json.get('code') | ||
1042 | 198 | if error_code: | ||
1043 | 199 | # we default to self.fmt in case error_code is not mapped yet. | ||
1044 | 200 | self.fmt = self.__error_messages.get(error_code, self.fmt) | ||
1045 | 201 | |||
1046 | 202 | super().__init__(snap_name=snap_name, **response_json) | ||
1047 | 203 | |||
1048 | 204 | |||
1049 | 205 | class StoreUploadError(StoreException): | ||
1050 | 206 | """Exception to be raised when there was a problem | ||
1051 | 207 | uploading a snap to the store""" | ||
1052 | 208 | fmt = ( | ||
1053 | 209 | 'There was an error uploading the package.\n' | ||
1054 | 210 | 'Reason: {reason!r}\n' | ||
1055 | 211 | 'Text: {text!r}') | ||
1056 | 212 | |||
1057 | 213 | def __init__(self, response): | ||
1058 | 214 | super().__init__(reason=response.reason, text=response.text) | ||
1059 | 215 | |||
1060 | 216 | |||
1061 | 217 | class StorePushError(StoreException): | ||
1062 | 218 | """Exception to be raised when there was a problem | ||
1063 | 219 | pushing a snap to the store""" | ||
1064 | 220 | __FMT_NOT_REGISTERED = ( | ||
1065 | 221 | 'You are not the publisher or allowed to push revisions for this ' | ||
1066 | 222 | 'snap. To become the publisher, run `snapcraft register {snap_name}` ' | ||
1067 | 223 | 'and try to push again.') | ||
1068 | 224 | |||
1069 | 225 | fmt = 'Received {status_code!r}: {text!r}' | ||
1070 | 226 | |||
1071 | 227 | def __init__(self, snap_name, response): | ||
1072 | 228 | try: | ||
1073 | 229 | response_json = response.json() | ||
1074 | 230 | except (AttributeError, JSONDecodeError): | ||
1075 | 231 | response_json = {} | ||
1076 | 232 | |||
1077 | 233 | if response.status_code == 404: | ||
1078 | 234 | self.fmt = self.__FMT_NOT_REGISTERED | ||
1079 | 235 | elif response.status_code == 401 or response.status_code == 403: | ||
1080 | 236 | try: | ||
1081 | 237 | response_json['text'] = response.text | ||
1082 | 238 | except AttributeError: | ||
1083 | 239 | response_json['text'] = 'error while pushing' | ||
1084 | 240 | |||
1085 | 241 | super().__init__(snap_name=snap_name, status_code=response.status_code, | ||
1086 | 242 | **response_json) | ||
1087 | 243 | |||
1088 | 244 | |||
1089 | 245 | class StoreReviewError(StoreException): | ||
1090 | 246 | """Exception to be raised when there was a problem in the snap | ||
1091 | 247 | that prevents it to be published/updated""" | ||
1092 | 248 | __FMT_NEED_MANUAL_REVIEW = ( | ||
1093 | 249 | 'Publishing checks failed.\n' | ||
1094 | 250 | 'To release this to stable channel please request a review on ' | ||
1095 | 251 | 'the snapcraft list.\n' | ||
1096 | 252 | 'Use devmode in the edge or beta channels to disable confinement.') | ||
1097 | 253 | |||
1098 | 254 | __FMT_PROCESSING_ERROR = ( | ||
1099 | 255 | 'There has been a problem while analyzing the snap, check the snap ' | ||
1100 | 256 | 'and try to push again.') | ||
1101 | 257 | |||
1102 | 258 | __messages = { | ||
1103 | 259 | 'need_manual_review': __FMT_NEED_MANUAL_REVIEW, | ||
1104 | 260 | 'processing_error': __FMT_PROCESSING_ERROR, | ||
1105 | 261 | } | ||
1106 | 262 | |||
1107 | 263 | def __init__(self, result): | ||
1108 | 264 | self.fmt = self.__messages[result['code']] | ||
1109 | 265 | super().__init__() | ||
1110 | 266 | |||
1111 | 267 | |||
1112 | 268 | class StoreReleaseError(StoreException): | ||
1113 | 269 | """Exception to be raised when the snap could not be released""" | ||
1114 | 270 | __FMT_NOT_REGISTERED = ( | ||
1115 | 271 | 'Sorry, try `snapcraft register {snap_name}` before trying to ' | ||
1116 | 272 | 'release or choose an existing revision.') | ||
1117 | 273 | |||
1118 | 274 | fmt = 'Received {status_code!r}: {text!r}' | ||
1119 | 275 | |||
1120 | 276 | def __init__(self, snap_name, response): | ||
1121 | 277 | try: | ||
1122 | 278 | response_json = response.json() | ||
1123 | 279 | except (AttributeError, JSONDecodeError): | ||
1124 | 280 | response_json = {} | ||
1125 | 281 | |||
1126 | 282 | if response.status_code == 404: | ||
1127 | 283 | self.fmt = self.__FMT_NOT_REGISTERED | ||
1128 | 284 | elif response.status_code == 401 or response.status_code == 403: | ||
1129 | 285 | try: | ||
1130 | 286 | response_json['text'] = response.text | ||
1131 | 287 | except AttributeError: | ||
1132 | 288 | response_json['text'] = 'error while releasing' | ||
1133 | 289 | elif 'errors' in response_json: | ||
1134 | 290 | self.fmt = '{errors}' | ||
1135 | 291 | |||
1136 | 292 | super().__init__(snap_name=snap_name, status_code=response.status_code, | ||
1137 | 293 | **response_json) | ||
1138 | 294 | |||
1139 | 295 | |||
1140 | 296 | class StoreValidationError(StoreException): | ||
1141 | 297 | """Exception to be raised when there was a validation | ||
1142 | 298 | problem in the store""" | ||
1143 | 299 | fmt = 'Received error {status_code!r}: {text!r}' | ||
1144 | 300 | |||
1145 | 301 | def __init__(self, response, message=None): | ||
1146 | 302 | try: | ||
1147 | 303 | response_json = response.json() | ||
1148 | 304 | response_json['text'] = response.json()['error_list'][0]['message'] | ||
1149 | 305 | except (AttributeError, JSONDecodeError): | ||
1150 | 306 | response_json = {'text': message or response} | ||
1151 | 307 | |||
1152 | 308 | super().__init__(status_code=response.status_code, | ||
1153 | 309 | **response_json) | ||
1154 | 310 | |||
1155 | 311 | |||
1156 | 312 | class StoreSnapBuildError(StoreException): | ||
1157 | 313 | """Exception to be raised when a snap build assertion fails""" | ||
1158 | 314 | fmt = 'Could not assert build: {error}' | ||
1159 | 315 | |||
1160 | 316 | def __init__(self, response): | ||
1161 | 317 | error = '{} {}'.format(response.status_code, response.reason) | ||
1162 | 318 | try: | ||
1163 | 319 | response_json = response.json() | ||
1164 | 320 | if 'error_list' in response_json: | ||
1165 | 321 | error = ' '.join( | ||
1166 | 322 | error['message'] for error in response_json['error_list']) | ||
1167 | 323 | except JSONDecodeError: | ||
1168 | 324 | pass | ||
1169 | 325 | |||
1170 | 326 | super().__init__(error=error) | ||
1171 | 327 | |||
1172 | 328 | |||
1173 | 329 | class StoreSnapHistoryError(StoreException): | ||
1174 | 330 | """Exception to be raised when there was a problem fetching a snap | ||
1175 | 331 | history""" | ||
1176 | 332 | fmt = ( | ||
1177 | 333 | 'Error fetching history of snap id {snap_id!r} for {arch!r} ' | ||
1178 | 334 | 'in {series!r} series: {error}.') | ||
1179 | 335 | |||
1180 | 336 | def __init__(self, response, snap_id, series, arch): | ||
1181 | 337 | error = '{} {}'.format(response.status_code, response.reason) | ||
1182 | 338 | try: | ||
1183 | 339 | response_json = response.json() | ||
1184 | 340 | if 'error_list' in response_json: | ||
1185 | 341 | error = ' '.join( | ||
1186 | 342 | error['message'] for error in response_json['error_list']) | ||
1187 | 343 | except JSONDecodeError: | ||
1188 | 344 | pass | ||
1189 | 345 | |||
1190 | 346 | super().__init__( | ||
1191 | 347 | snap_id=snap_id, arch=arch or 'any arch', | ||
1192 | 348 | series=series or 'any', error=error) | ||
1193 | 349 | |||
1194 | 350 | |||
1195 | 351 | class StoreSnapStatusError(StoreSnapHistoryError): | ||
1196 | 352 | """Exception to be raised when there was a problem fetching a snap | ||
1197 | 353 | status""" | ||
1198 | 354 | fmt = ( | ||
1199 | 355 | 'Error fetching status of snap id {snap_id!r} for {arch!r} ' | ||
1200 | 356 | 'in {series!r} series: {error}.') | ||
1201 | 357 | |||
1202 | 358 | |||
1203 | 359 | class StoreChannelClosingError(StoreException): | ||
1204 | 360 | """Exception to be raised when there was a problem fetching a snap | ||
1205 | 361 | history""" | ||
1206 | 362 | fmt = 'Could not close channel: {error}' | ||
1207 | 363 | |||
1208 | 364 | def __init__(self, response): | ||
1209 | 365 | try: | ||
1210 | 366 | _e = response.json()['error_list'][0] | ||
1211 | 367 | error = '{}'.format(_e['message']) | ||
1212 | 368 | except (JSONDecodeError, KeyError, IndexError): | ||
1213 | 369 | error = '{} {}'.format( | ||
1214 | 370 | response.status_code, response.reason) | ||
1215 | 371 | super().__init__(error=error) | ||
1216 | 0 | 372 | ||
1217 | === added file 'snappy_ecosystem_tests/helpers/store_apis/rest_apis.py' | |||
1218 | --- snappy_ecosystem_tests/helpers/store_apis/rest_apis.py 1970-01-01 00:00:00 +0000 | |||
1219 | +++ snappy_ecosystem_tests/helpers/store_apis/rest_apis.py 2017-02-20 17:48:06 +0000 | |||
1220 | @@ -0,0 +1,880 @@ | |||
1221 | 1 | # -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*- | ||
1222 | 2 | # | ||
1223 | 3 | # Copyright (C) 2017 Canonical Ltd | ||
1224 | 4 | # | ||
1225 | 5 | # This program is free software: you can redistribute it and/or modify | ||
1226 | 6 | # it under the terms of the GNU General Public License version 3 as | ||
1227 | 7 | # published by the Free Software Foundation. | ||
1228 | 8 | # | ||
1229 | 9 | # This program is distributed in the hope that it will be useful, | ||
1230 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
1231 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
1232 | 12 | # GNU General Public License for more details. | ||
1233 | 13 | # | ||
1234 | 14 | # You should have received a copy of the GNU General Public License | ||
1235 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
1236 | 16 | |||
1237 | 17 | """Clients needed to interact with the Snap Store""" | ||
1238 | 18 | |||
1239 | 19 | import contextlib | ||
1240 | 20 | import hashlib | ||
1241 | 21 | import itertools | ||
1242 | 22 | import json | ||
1243 | 23 | import logging | ||
1244 | 24 | import os | ||
1245 | 25 | import urllib.parse | ||
1246 | 26 | from queue import Queue | ||
1247 | 27 | from threading import Thread | ||
1248 | 28 | from time import sleep | ||
1249 | 29 | |||
1250 | 30 | import pymacaroons | ||
1251 | 31 | import requests | ||
1252 | 32 | from progressbar import ( | ||
1253 | 33 | AnimatedMarker, | ||
1254 | 34 | ProgressBar, | ||
1255 | 35 | UnknownLength, | ||
1256 | 36 | ) | ||
1257 | 37 | from simplejson.scanner import JSONDecodeError | ||
1258 | 38 | from snappy_ecosystem_tests.commons.config import CONFIG_STACK | ||
1259 | 39 | |||
1260 | 40 | from snappy_ecosystem_tests.helpers.snapcraft import constants | ||
1261 | 41 | from snappy_ecosystem_tests.helpers.snapcraft.config import SnapcraftConfig | ||
1262 | 42 | from snappy_ecosystem_tests.helpers.snapcraft.indicators import ( | ||
1263 | 43 | download_requests_stream | ||
1264 | 44 | ) | ||
1265 | 45 | from snappy_ecosystem_tests.helpers.snapcraft.options import ProjectOptions | ||
1266 | 46 | from snappy_ecosystem_tests.helpers.store_apis import errors | ||
1267 | 47 | from snappy_ecosystem_tests.helpers.store_apis.upload import upload_files | ||
1268 | 48 | |||
1269 | 49 | LOGGER = logging.getLogger(__name__) | ||
1270 | 50 | |||
1271 | 51 | JSON_CONTENT_TYPE = {'Content-Type': 'application/json'} | ||
1272 | 52 | JSON_ACCEPT = {'Accept': 'application/json'} | ||
1273 | 53 | JSON_HEADERS = dict(JSON_CONTENT_TYPE, **JSON_ACCEPT) | ||
1274 | 54 | |||
1275 | 55 | |||
1276 | 56 | def _macaroon_auth(conf): | ||
1277 | 57 | """Format a macaroon and its associated discharge. | ||
1278 | 58 | :return: A string suitable to use in an Authorization header. | ||
1279 | 59 | """ | ||
1280 | 60 | root_macaroon_raw = conf.get('macaroon') | ||
1281 | 61 | if root_macaroon_raw is None: | ||
1282 | 62 | raise errors.InvalidCredentialsError( | ||
1283 | 63 | 'Root macaroon not in the config file') | ||
1284 | 64 | unbound_raw = conf.get('unbound_discharge') | ||
1285 | 65 | if unbound_raw is None: | ||
1286 | 66 | raise errors.InvalidCredentialsError( | ||
1287 | 67 | 'Unbound discharge not in the config file') | ||
1288 | 68 | |||
1289 | 69 | root_macaroon = _deserialize_macaroon(root_macaroon_raw) | ||
1290 | 70 | unbound = _deserialize_macaroon(unbound_raw) | ||
1291 | 71 | bound = root_macaroon.prepare_for_request(unbound) | ||
1292 | 72 | discharge_macaroon_raw = bound.serialize() | ||
1293 | 73 | auth = 'Macaroon root={}, discharge={}'.format( | ||
1294 | 74 | root_macaroon_raw, discharge_macaroon_raw) | ||
1295 | 75 | return auth | ||
1296 | 76 | |||
1297 | 77 | |||
1298 | 78 | def _deserialize_macaroon(value): | ||
1299 | 79 | """Deserialize the given macaroon and return it | ||
1300 | 80 | :raise InvalidCredentialsError: when macaroon cannot be deserialized | ||
1301 | 81 | """ | ||
1302 | 82 | try: | ||
1303 | 83 | return pymacaroons.Macaroon.deserialize(value) | ||
1304 | 84 | except: | ||
1305 | 85 | raise errors.InvalidCredentialsError('Failed to deserialize macaroon') | ||
1306 | 86 | |||
1307 | 87 | |||
1308 | 88 | class Client: | ||
1309 | 89 | """A base class to define clients for the ols servers. | ||
1310 | 90 | This is a simple wrapper around requests.Session so we inherit all good | ||
1311 | 91 | bits while providing a simple point for tests to override when needed. | ||
1312 | 92 | """ | ||
1313 | 93 | |||
1314 | 94 | def __init__(self, conf, root_url): | ||
1315 | 95 | self.conf = conf | ||
1316 | 96 | self.root_url = root_url | ||
1317 | 97 | self.session = requests.Session() | ||
1318 | 98 | |||
1319 | 99 | def request(self, method, url, params=None, headers=None, **kwargs): | ||
1320 | 100 | """Overriding base class to handle the root url.""" | ||
1321 | 101 | # Note that url may be absolute in which case 'root_url' is ignored by | ||
1322 | 102 | # urljoin. | ||
1323 | 103 | if headers is None: | ||
1324 | 104 | headers = JSON_HEADERS | ||
1325 | 105 | final_url = urllib.parse.urljoin(self.root_url, url) | ||
1326 | 106 | response = self.session.request( | ||
1327 | 107 | method, final_url, headers=headers, | ||
1328 | 108 | params=params, **kwargs) | ||
1329 | 109 | return response | ||
1330 | 110 | |||
1331 | 111 | def get(self, url, **kwargs): | ||
1332 | 112 | """Do a GET request and return the response""" | ||
1333 | 113 | return self.request('GET', url, **kwargs) | ||
1334 | 114 | |||
1335 | 115 | def post(self, url, **kwargs): | ||
1336 | 116 | """Do a POST request and return the response""" | ||
1337 | 117 | return self.request('POST', url, **kwargs) | ||
1338 | 118 | |||
1339 | 119 | def put(self, url, **kwargs): | ||
1340 | 120 | """Do a PUT request and return the response""" | ||
1341 | 121 | return self.request('PUT', url, **kwargs) | ||
1342 | 122 | |||
1343 | 123 | |||
1344 | 124 | class Store: | ||
1345 | 125 | """It is an entry point for all the REST operations in the store. | ||
1346 | 126 | It will call the corresponding client to resolve the request. | ||
1347 | 127 | It also takes care about refreshing the macaroon if necessary | ||
1348 | 128 | in each request. | ||
1349 | 129 | """ | ||
1350 | 130 | |||
1351 | 131 | ACLs = ('package_upload', 'package_access') | ||
1352 | 132 | |||
1353 | 133 | def __init__(self): | ||
1354 | 134 | self.conf = SnapcraftConfig() | ||
1355 | 135 | self.sso = SSOClient(self.conf) | ||
1356 | 136 | self.cpi = SnapIndexClient(self.conf) | ||
1357 | 137 | self.updown = UpDownClient(self.conf) | ||
1358 | 138 | self.sca = SCAClient(self.conf) | ||
1359 | 139 | |||
1360 | 140 | def login(self, email, password, one_time_password=None, acls=ACLs, | ||
1361 | 141 | packages=None, channels=None, save=True): | ||
1362 | 142 | """Log in via the Ubuntu One SSO API.""" | ||
1363 | 143 | # Ask the store for the needed capabilities to be associated with the | ||
1364 | 144 | # macaroon. | ||
1365 | 145 | macaroon = self.sca.get_macaroon(acls, packages, channels) | ||
1366 | 146 | caveat_id = self._extract_caveat_id(macaroon) | ||
1367 | 147 | unbound_discharge = self.sso.get_unbound_discharge( | ||
1368 | 148 | email, password, one_time_password, caveat_id) | ||
1369 | 149 | # The macaroon has been discharged, save it in the config | ||
1370 | 150 | self.conf.set('macaroon', macaroon) | ||
1371 | 151 | self.conf.set('unbound_discharge', unbound_discharge) | ||
1372 | 152 | if save: | ||
1373 | 153 | self.conf.save() | ||
1374 | 154 | |||
1375 | 155 | @property | ||
1376 | 156 | def config(self): | ||
1377 | 157 | """Get current config""" | ||
1378 | 158 | return self.conf | ||
1379 | 159 | |||
1380 | 160 | def load_config(self): | ||
1381 | 161 | """Load current config""" | ||
1382 | 162 | self.conf.load() | ||
1383 | 163 | |||
1384 | 164 | def _extract_caveat_id(self, root_macaroon): | ||
1385 | 165 | """Extract caveat id for the given macaroon | ||
1386 | 166 | :raise InvalidCredentialsError: when given macaroon is not valid | ||
1387 | 167 | """ | ||
1388 | 168 | macaroon = pymacaroons.Macaroon.deserialize(root_macaroon) | ||
1389 | 169 | # macaroons are all bytes, never strings | ||
1390 | 170 | sso_host = urllib.parse.urlparse(self.sso.root_url).netloc | ||
1391 | 171 | for caveat in macaroon.caveats: | ||
1392 | 172 | if caveat.location == sso_host: | ||
1393 | 173 | return caveat.caveat_id | ||
1394 | 174 | raise errors.InvalidCredentialsError('Invalid root macaroon') | ||
1395 | 175 | |||
1396 | 176 | def logout(self): | ||
1397 | 177 | """Logout from Store client""" | ||
1398 | 178 | self.conf.clear() | ||
1399 | 179 | self.conf.save() | ||
1400 | 180 | |||
1401 | 181 | def _refresh_if_necessary(self, func, *args, **kwargs): | ||
1402 | 182 | """Make a request, refreshing macaroons if necessary.""" | ||
1403 | 183 | try: | ||
1404 | 184 | return func(*args, **kwargs) | ||
1405 | 185 | except errors.StoreMacaroonNeedsRefreshError: | ||
1406 | 186 | unbound_discharge = self.sso.refresh_unbound_discharge( | ||
1407 | 187 | self.conf.get('unbound_discharge')) | ||
1408 | 188 | self.conf.set('unbound_discharge', unbound_discharge) | ||
1409 | 189 | self.conf.save() | ||
1410 | 190 | return func(*args, **kwargs) | ||
1411 | 191 | |||
1412 | 192 | def get_account_information(self): | ||
1413 | 193 | """Get current account information""" | ||
1414 | 194 | return self._refresh_if_necessary(self.sca.get_account_information) | ||
1415 | 195 | |||
1416 | 196 | def register_key(self, account_key_request): | ||
1417 | 197 | """Register a key for the current user""" | ||
1418 | 198 | return self._refresh_if_necessary( | ||
1419 | 199 | self.sca.register_key, account_key_request) | ||
1420 | 200 | |||
1421 | 201 | def register(self, snap_name, is_private=False): | ||
1422 | 202 | """Register a package name for the current user | ||
1423 | 203 | First refresh macaroon if necessary""" | ||
1424 | 204 | return self._refresh_if_necessary( | ||
1425 | 205 | self.sca.register, snap_name, is_private, constants.DEFAULT_SERIES) | ||
1426 | 206 | |||
1427 | 207 | def push_precheck(self, snap_name): | ||
1428 | 208 | """Do a snap push as a pre-check (dry_run: do not actually push) | ||
1429 | 209 | First refresh macaroon if necessary | ||
1430 | 210 | """ | ||
1431 | 211 | return self._refresh_if_necessary( | ||
1432 | 212 | self.sca.snap_push_precheck, snap_name) | ||
1433 | 213 | |||
1434 | 214 | def push_snap_build(self, snap_id, snap_build): | ||
1435 | 215 | """Do a snap push | ||
1436 | 216 | First refresh macaroon if necessary | ||
1437 | 217 | """ | ||
1438 | 218 | return self._refresh_if_necessary( | ||
1439 | 219 | self.sca.push_snap_build, snap_id, snap_build) | ||
1440 | 220 | |||
1441 | 221 | def upload(self, snap_name, snap_filename): | ||
1442 | 222 | """Upload a snap file. | ||
1443 | 223 | :raise InvalidCredentialsError when no unbound_discharge | ||
1444 | 224 | is found in current config | ||
1445 | 225 | """ | ||
1446 | 226 | if not self.conf.get('unbound_discharge'): | ||
1447 | 227 | raise errors.InvalidCredentialsError( | ||
1448 | 228 | 'Unbound discharge not in the config file') | ||
1449 | 229 | |||
1450 | 230 | updown_data = upload_files(snap_filename, self.updown) | ||
1451 | 231 | |||
1452 | 232 | return self._refresh_if_necessary( | ||
1453 | 233 | self.sca.snap_push_metadata, snap_name, updown_data) | ||
1454 | 234 | |||
1455 | 235 | def release(self, snap_name, revision, channels): | ||
1456 | 236 | """Release a snap revision to the given channels | ||
1457 | 237 | First refresh macaroon if necessary | ||
1458 | 238 | """ | ||
1459 | 239 | return self._refresh_if_necessary( | ||
1460 | 240 | self.sca.snap_release, snap_name, revision, channels) | ||
1461 | 241 | |||
1462 | 242 | def get_snap_history(self, snap_name, series=None, arch=None): | ||
1463 | 243 | """Get a snap history. First refresh macaroon if necessary | ||
1464 | 244 | :raise: SnapNotFoundError if the snap does not belong to | ||
1465 | 245 | the current user | ||
1466 | 246 | or if it is not found. | ||
1467 | 247 | """ | ||
1468 | 248 | if series is None: | ||
1469 | 249 | series = constants.DEFAULT_SERIES | ||
1470 | 250 | |||
1471 | 251 | account_info = self.get_account_information() | ||
1472 | 252 | try: | ||
1473 | 253 | snap_id = account_info['snaps'][series][snap_name]['snap-id'] | ||
1474 | 254 | except KeyError: | ||
1475 | 255 | raise errors.SnapNotFoundError(snap_name, series=series, arch=arch) | ||
1476 | 256 | |||
1477 | 257 | response = self._refresh_if_necessary( | ||
1478 | 258 | self.sca.snap_history, snap_id, series, arch) | ||
1479 | 259 | |||
1480 | 260 | if not response: | ||
1481 | 261 | raise errors.SnapNotFoundError(snap_name, series=series, arch=arch) | ||
1482 | 262 | |||
1483 | 263 | return response | ||
1484 | 264 | |||
1485 | 265 | def get_snap_status(self, snap_name, series=None, arch=None): | ||
1486 | 266 | """Get a snap status. First refresh macaroon if necessary | ||
1487 | 267 | :raise: SnapNotFoundError if the snap does not belong | ||
1488 | 268 | to the current user | ||
1489 | 269 | or if it is not found. | ||
1490 | 270 | """ | ||
1491 | 271 | if series is None: | ||
1492 | 272 | series = constants.DEFAULT_SERIES | ||
1493 | 273 | |||
1494 | 274 | account_info = self.get_account_information() | ||
1495 | 275 | try: | ||
1496 | 276 | snap_id = account_info['snaps'][series][snap_name]['snap-id'] | ||
1497 | 277 | except KeyError: | ||
1498 | 278 | raise errors.SnapNotFoundError(snap_name, series=series, arch=arch) | ||
1499 | 279 | |||
1500 | 280 | response = self._refresh_if_necessary( | ||
1501 | 281 | self.sca.snap_status, snap_id, series, arch) | ||
1502 | 282 | |||
1503 | 283 | if not response: | ||
1504 | 284 | raise errors.SnapNotFoundError(snap_name, series=series, arch=arch) | ||
1505 | 285 | |||
1506 | 286 | return response | ||
1507 | 287 | |||
1508 | 288 | def close_channels(self, snap_id, channel_names): | ||
1509 | 289 | """Close channels for the given snap_id. | ||
1510 | 290 | First refresh macaroon if necessary""" | ||
1511 | 291 | return self._refresh_if_necessary( | ||
1512 | 292 | self.sca.close_channels, snap_id, channel_names) | ||
1513 | 293 | |||
1514 | 294 | def download(self, snap_name, channel, download_path, arch=None): | ||
1515 | 295 | """Download a snap and store it locally | ||
1516 | 296 | :param snap_name: the snap name | ||
1517 | 297 | :param channel: the channel name | ||
1518 | 298 | :param download_path: the local path to download the snap | ||
1519 | 299 | :param arch: the snap architecture | ||
1520 | 300 | """ | ||
1521 | 301 | if arch is None: | ||
1522 | 302 | arch = ProjectOptions().deb_arch | ||
1523 | 303 | |||
1524 | 304 | package = self.cpi.get_package(snap_name, channel, arch) | ||
1525 | 305 | self._download_snap( | ||
1526 | 306 | snap_name, download_path, | ||
1527 | 307 | package['anon_download_url'], package['download_sha512']) | ||
1528 | 308 | |||
1529 | 309 | def _download_snap(self, name, download_path, download_url, | ||
1530 | 310 | expected_sha512): | ||
1531 | 311 | """Download a snap and store it locally | ||
1532 | 312 | :param name: the snap name | ||
1533 | 313 | :param download_path: the local path to download the snap | ||
1534 | 314 | :param download_url: the url to download the snap | ||
1535 | 315 | :param expected_sha512: the snap expected sha256 | ||
1536 | 316 | """ | ||
1537 | 317 | if self._is_downloaded(download_path, expected_sha512): | ||
1538 | 318 | LOGGER.info('Already downloaded %s at %s', name, download_path) | ||
1539 | 319 | return | ||
1540 | 320 | LOGGER.info('Downloading %s at %s', name, download_path) | ||
1541 | 321 | request = self.cpi.get(download_url, stream=True) | ||
1542 | 322 | request.raise_for_status() | ||
1543 | 323 | download_requests_stream(request, download_path) | ||
1544 | 324 | |||
1545 | 325 | if self._is_downloaded(download_path, expected_sha512): | ||
1546 | 326 | LOGGER.info('Successfully downloaded %s at %s', name, | ||
1547 | 327 | download_path) | ||
1548 | 328 | else: | ||
1549 | 329 | raise errors.SHAMismatchError(download_path, expected_sha512) | ||
1550 | 330 | |||
1551 | 331 | @staticmethod | ||
1552 | 332 | def _is_downloaded(path, expected_sha512): | ||
1553 | 333 | """Return True if the snap is already downloaded in the local machine | ||
1554 | 334 | :param path: the path to look for the snap | ||
1555 | 335 | :param expected_sha512: the expected snap sha512 | ||
1556 | 336 | """ | ||
1557 | 337 | if not os.path.exists(path): | ||
1558 | 338 | return False | ||
1559 | 339 | |||
1560 | 340 | file_sum = hashlib.sha512() | ||
1561 | 341 | with open(path, 'rb') as _f: | ||
1562 | 342 | for file_chunk in iter( | ||
1563 | 343 | lambda: _f.read(file_sum.block_size * 128), b''): | ||
1564 | 344 | file_sum.update(file_chunk) | ||
1565 | 345 | return expected_sha512 == file_sum.hexdigest() | ||
1566 | 346 | |||
1567 | 347 | def push_validation(self, snap_id, assertion): | ||
1568 | 348 | """Push a snap validation/assertion to the store | ||
1569 | 349 | :param snap_id: the snap id | ||
1570 | 350 | :param assertion: the snap assertion | ||
1571 | 351 | """ | ||
1572 | 352 | return self.sca.push_validation(snap_id, assertion) | ||
1573 | 353 | |||
1574 | 354 | def get_validations(self, snap_id): | ||
1575 | 355 | """Get current snap validations/assertions""" | ||
1576 | 356 | return self.sca.get_validations(snap_id) | ||
1577 | 357 | |||
1578 | 358 | def sign_developer_agreement(self, latest_tos_accepted=False): | ||
1579 | 359 | """Sign developer agreement to be able to do operations in the store""" | ||
1580 | 360 | return self.sca.sign_developer_agreement(latest_tos_accepted) | ||
1581 | 361 | |||
1582 | 362 | |||
1583 | 363 | class SSOClient(Client): | ||
1584 | 364 | """The Single Sign On server deals with authentication. | ||
1585 | 365 | It is used directly or indirectly by other servers. | ||
1586 | 366 | """ | ||
1587 | 367 | def __init__(self, conf): | ||
1588 | 368 | super().__init__(conf, os.environ.get( | ||
1589 | 369 | 'UBUNTU_SSO_API_ROOT_URL', | ||
1590 | 370 | CONFIG_STACK.config.get('production_urls', 'sso'))) | ||
1591 | 371 | |||
1592 | 372 | def get_unbound_discharge(self, email, password, one_time_password, | ||
1593 | 373 | caveat_id): | ||
1594 | 374 | """Get the unbound discharge for the caveat_id. | ||
1595 | 375 | :param email: user email | ||
1596 | 376 | :param password: user password | ||
1597 | 377 | :param one_time_password: the one time password | ||
1598 | 378 | :param caveat_id: the caveat id to get the unbound discharge | ||
1599 | 379 | :raise StoreTwoFactorAuthenticationRequired: | ||
1600 | 380 | if response is unauthorized and two-factor is required | ||
1601 | 381 | :raise StoreAuthenticationError: if failed to get unbound discharge | ||
1602 | 382 | """ | ||
1603 | 383 | data = dict(email=email, password=password, | ||
1604 | 384 | caveat_id=caveat_id) | ||
1605 | 385 | if one_time_password: | ||
1606 | 386 | data['otp'] = one_time_password | ||
1607 | 387 | response = self.post( | ||
1608 | 388 | 'tokens/discharge', data=json.dumps(data), | ||
1609 | 389 | headers=JSON_HEADERS) | ||
1610 | 390 | try: | ||
1611 | 391 | response_json = response.json() | ||
1612 | 392 | except JSONDecodeError: | ||
1613 | 393 | response_json = {} | ||
1614 | 394 | if response.ok: | ||
1615 | 395 | return response_json['discharge_macaroon'] | ||
1616 | 396 | else: | ||
1617 | 397 | if (response.status_code == requests.codes.get('unauthorized') and | ||
1618 | 398 | any(error.get('code') == 'twofactor-required' | ||
1619 | 399 | for error in response_json.get('error_list', []))): | ||
1620 | 400 | raise errors.StoreTwoFactorAuthenticationRequired() | ||
1621 | 401 | else: | ||
1622 | 402 | raise errors.StoreAuthenticationError( | ||
1623 | 403 | 'Failed to get unbound discharge: {}'.format( | ||
1624 | 404 | response.text)) | ||
1625 | 405 | |||
1626 | 406 | def refresh_unbound_discharge(self, unbound_discharge): | ||
1627 | 407 | """Refresh the given unbound discharge. | ||
1628 | 408 | :raise StoreAuthenticationError: if fails to refresh the | ||
1629 | 409 | unbound discharge. | ||
1630 | 410 | """ | ||
1631 | 411 | data = {'discharge_macaroon': unbound_discharge} | ||
1632 | 412 | response = self.post( | ||
1633 | 413 | 'tokens/refresh', data=json.dumps(data), | ||
1634 | 414 | headers=JSON_HEADERS) | ||
1635 | 415 | if response.ok: | ||
1636 | 416 | return response.json()['discharge_macaroon'] | ||
1637 | 417 | else: | ||
1638 | 418 | raise errors.StoreAuthenticationError( | ||
1639 | 419 | 'Failed to refresh unbound discharge: {}'.format( | ||
1640 | 420 | response.text)) | ||
1641 | 421 | |||
1642 | 422 | |||
1643 | 423 | class SnapIndexClient(Client): | ||
1644 | 424 | """The Click Package Index knows everything about existing snaps. | ||
1645 | 425 | https://wiki.ubuntu.com/AppStore/Interfaces/ClickPackageIndex is the | ||
1646 | 426 | canonical reference. | ||
1647 | 427 | """ | ||
1648 | 428 | def __init__(self, conf): | ||
1649 | 429 | super().__init__(conf, os.environ.get( | ||
1650 | 430 | 'UBUNTU_STORE_SEARCH_ROOT_URL', | ||
1651 | 431 | CONFIG_STACK.config.get('production_urls', 'search'))) | ||
1652 | 432 | |||
1653 | 433 | def get_default_headers(self): | ||
1654 | 434 | """Return default headers for CPI requests. | ||
1655 | 435 | Tries to build an 'Authorization' header with local credentials | ||
1656 | 436 | if they are available. | ||
1657 | 437 | Also pin specific branded store if `SNAPCRAFT_UBUNTU_STORE` | ||
1658 | 438 | environment is set. | ||
1659 | 439 | """ | ||
1660 | 440 | headers = {} | ||
1661 | 441 | |||
1662 | 442 | with contextlib.suppress(errors.InvalidCredentialsError): | ||
1663 | 443 | headers['Authorization'] = _macaroon_auth(self.conf) | ||
1664 | 444 | |||
1665 | 445 | branded_store = os.getenv('SNAPCRAFT_UBUNTU_STORE') | ||
1666 | 446 | if branded_store: | ||
1667 | 447 | headers['X-Ubuntu-Store'] = branded_store | ||
1668 | 448 | |||
1669 | 449 | return headers | ||
1670 | 450 | |||
1671 | 451 | def get_package(self, snap_name, channel, arch=None): | ||
1672 | 452 | """Get snap details. | ||
1673 | 453 | :param snap_name: the snap name | ||
1674 | 454 | :param channel: the snap channel | ||
1675 | 455 | :param arch: the snap architecture | ||
1676 | 456 | """ | ||
1677 | 457 | headers = self.get_default_headers() | ||
1678 | 458 | headers.update({ | ||
1679 | 459 | 'Accept': 'application/hal+json', | ||
1680 | 460 | 'X-Ubuntu-Release': constants.DEFAULT_SERIES, | ||
1681 | 461 | }) | ||
1682 | 462 | if arch: | ||
1683 | 463 | headers['X-Ubuntu-Architecture'] = arch | ||
1684 | 464 | |||
1685 | 465 | params = { | ||
1686 | 466 | 'channel': channel, | ||
1687 | 467 | 'fields': 'status,anon_download_url,download_url,' | ||
1688 | 468 | 'download_sha512,snap_id,release', | ||
1689 | 469 | } | ||
1690 | 470 | LOGGER.info('Getting details for %s', snap_name) | ||
1691 | 471 | url = 'api/v1/snaps/details/{}'.format(snap_name) | ||
1692 | 472 | resp = super().get(url, headers=headers, params=params) | ||
1693 | 473 | if resp.status_code != 200: | ||
1694 | 474 | raise errors.SnapNotFoundError(snap_name, channel, arch) | ||
1695 | 475 | return resp.json() | ||
1696 | 476 | |||
1697 | 477 | |||
1698 | 478 | class UpDownClient(Client): | ||
1699 | 479 | """The Up/Down server provide upload/download snap capabilities.""" | ||
1700 | 480 | |||
1701 | 481 | def __init__(self, conf): | ||
1702 | 482 | super().__init__(conf, os.environ.get( | ||
1703 | 483 | 'UBUNTU_STORE_UPLOAD_ROOT_URL', | ||
1704 | 484 | CONFIG_STACK.config.get('production_urls', 'upload'))) | ||
1705 | 485 | |||
1706 | 486 | def upload(self, monitor): | ||
1707 | 487 | """Upload a snap capability""" | ||
1708 | 488 | return self.post( | ||
1709 | 489 | urllib.parse.urljoin(self.root_url, 'unscanned-upload/'), | ||
1710 | 490 | data=monitor, | ||
1711 | 491 | headers=dict({'Content-Type': monitor.content_type}, | ||
1712 | 492 | **JSON_ACCEPT)) | ||
1713 | 493 | |||
1714 | 494 | |||
1715 | 495 | class SCAClient(Client): | ||
1716 | 496 | """The software center agent deals with managing snaps.""" | ||
1717 | 497 | |||
1718 | 498 | def __init__(self, conf): | ||
1719 | 499 | super().__init__(conf, os.environ.get( | ||
1720 | 500 | 'UBUNTU_STORE_API_ROOT_URL', | ||
1721 | 501 | CONFIG_STACK.config.get('production_urls', 'root_api'))) | ||
1722 | 502 | |||
1723 | 503 | def get_macaroon(self, acls, packages=None, channels=None): | ||
1724 | 504 | """Get a macaroon that is used to authenticate | ||
1725 | 505 | request against the store | ||
1726 | 506 | :param acls: list of permissions to request the macaroon for. | ||
1727 | 507 | :param packages: list of packages to request the macaroon for. | ||
1728 | 508 | :param channels: list of channels to request the macaroon for. | ||
1729 | 509 | """ | ||
1730 | 510 | data = {'permissions': acls} | ||
1731 | 511 | if packages is not None: | ||
1732 | 512 | data.update({'packages': packages}) | ||
1733 | 513 | if channels is not None: | ||
1734 | 514 | data.update({'channels': channels}) | ||
1735 | 515 | headers = JSON_ACCEPT | ||
1736 | 516 | response = self.post( | ||
1737 | 517 | 'acl/', json=data, headers=headers) | ||
1738 | 518 | if response.ok: | ||
1739 | 519 | return response.json()['macaroon'] | ||
1740 | 520 | else: | ||
1741 | 521 | raise errors.StoreAuthenticationError('Failed to get macaroon') | ||
1742 | 522 | |||
1743 | 523 | @staticmethod | ||
1744 | 524 | def _needs_refreshed_response(response): | ||
1745 | 525 | """Return True if the given response needs to refresh the macaroon. | ||
1746 | 526 | :param response: the response to check if the | ||
1747 | 527 | macaroon refresh is needed. | ||
1748 | 528 | """ | ||
1749 | 529 | return ( | ||
1750 | 530 | response.status_code == requests.codes.get('unauthorized') and | ||
1751 | 531 | response.headers.get('WWW-Authenticate') == ( | ||
1752 | 532 | 'Macaroon needs_refresh=1')) | ||
1753 | 533 | |||
1754 | 534 | def request(self, *args, **kwargs): | ||
1755 | 535 | """Wrapper of client request that raises an exception for | ||
1756 | 536 | the cases that the response macaroon needs to be refreshed | ||
1757 | 537 | :raise StoreMacaroonNeedsRefreshError: | ||
1758 | 538 | if the response needs the macaroon to be refreshed. | ||
1759 | 539 | """ | ||
1760 | 540 | response = super().request(*args, **kwargs) | ||
1761 | 541 | if self._needs_refreshed_response(response): | ||
1762 | 542 | raise errors.StoreMacaroonNeedsRefreshError() | ||
1763 | 543 | return response | ||
1764 | 544 | |||
1765 | 545 | def get_account_information(self): | ||
1766 | 546 | """Get the current account information | ||
1767 | 547 | :raise StoreAccountInformationError: if response is not ok status | ||
1768 | 548 | """ | ||
1769 | 549 | auth = _macaroon_auth(self.conf) | ||
1770 | 550 | response = self.get( | ||
1771 | 551 | 'account', | ||
1772 | 552 | headers=dict({'Authorization': auth}, **JSON_ACCEPT)) | ||
1773 | 553 | if response.ok: | ||
1774 | 554 | return response.json() | ||
1775 | 555 | else: | ||
1776 | 556 | raise errors.StoreAccountInformationError(response) | ||
1777 | 557 | |||
1778 | 558 | def register_key(self, account_key_request): | ||
1779 | 559 | """Register a key for a user with the given request | ||
1780 | 560 | :param account_key_request: the key to register | ||
1781 | 561 | :raise StoreKeyRegistrationError | ||
1782 | 562 | """ | ||
1783 | 563 | data = {'account_key_request': account_key_request} | ||
1784 | 564 | auth = _macaroon_auth(self.conf) | ||
1785 | 565 | response = self.post( | ||
1786 | 566 | 'account/account-key', data=json.dumps(data), | ||
1787 | 567 | headers=dict({'Authorization': auth}, **JSON_HEADERS)) | ||
1788 | 568 | if not response.ok: | ||
1789 | 569 | raise errors.StoreKeyRegistrationError(response) | ||
1790 | 570 | |||
1791 | 571 | def register(self, snap_name, is_private, series): | ||
1792 | 572 | """Register a snap name in the store | ||
1793 | 573 | :param snap_name: the name to be registered | ||
1794 | 574 | :param is_private: whether the snap name is private or not | ||
1795 | 575 | :param series: the snap's series to be registered | ||
1796 | 576 | """ | ||
1797 | 577 | auth = _macaroon_auth(self.conf) | ||
1798 | 578 | data = dict(snap_name=snap_name, is_private=is_private, | ||
1799 | 579 | series=series) | ||
1800 | 580 | response = self.post( | ||
1801 | 581 | 'register-name/', data=json.dumps(data), | ||
1802 | 582 | headers=dict({'Authorization': auth}, **JSON_ACCEPT)) | ||
1803 | 583 | if not response.ok: | ||
1804 | 584 | raise errors.StoreRegistrationError(snap_name, response) | ||
1805 | 585 | |||
1806 | 586 | def snap_push_precheck(self, snap_name): | ||
1807 | 587 | """Do a snap push pre-check (dry_run: do not actually push) | ||
1808 | 588 | :raise StorePushError | ||
1809 | 589 | """ | ||
1810 | 590 | data = { | ||
1811 | 591 | 'name': snap_name, | ||
1812 | 592 | 'dry_run': True, | ||
1813 | 593 | } | ||
1814 | 594 | auth = _macaroon_auth(self.conf) | ||
1815 | 595 | response = self.post( | ||
1816 | 596 | 'snap-push/', data=json.dumps(data), | ||
1817 | 597 | headers=dict({'Authorization': auth}, **JSON_HEADERS)) | ||
1818 | 598 | if not response.ok: | ||
1819 | 599 | raise errors.StorePushError(data['name'], response) | ||
1820 | 600 | |||
1821 | 601 | def snap_push_metadata(self, snap_name, updown_data): | ||
1822 | 602 | """Push metadata fot the given snap | ||
1823 | 603 | :param snap_name: the snap name which metadata want to be updated | ||
1824 | 604 | :param updown_data: a dictionary that contains the metadata: upload_id, | ||
1825 | 605 | binary_filesize, source_uploaded | ||
1826 | 606 | :raise: StorePushError | ||
1827 | 607 | """ | ||
1828 | 608 | data = { | ||
1829 | 609 | 'name': snap_name, | ||
1830 | 610 | 'series': constants.DEFAULT_SERIES, | ||
1831 | 611 | 'updown_id': updown_data['upload_id'], | ||
1832 | 612 | 'binary_filesize': updown_data['binary_filesize'], | ||
1833 | 613 | 'source_uploaded': updown_data['source_uploaded'], | ||
1834 | 614 | } | ||
1835 | 615 | auth = _macaroon_auth(self.conf) | ||
1836 | 616 | response = self.post( | ||
1837 | 617 | 'snap-push/', data=json.dumps(data), | ||
1838 | 618 | headers=dict({'Authorization': auth}, **JSON_HEADERS)) | ||
1839 | 619 | if not response.ok: | ||
1840 | 620 | raise errors.StorePushError(data['name'], response) | ||
1841 | 621 | |||
1842 | 622 | return StatusTracker(response.json()['status_details_url']) | ||
1843 | 623 | |||
1844 | 624 | def snap_release(self, snap_name, revision, channels): | ||
1845 | 625 | """ | ||
1846 | 626 | Release a snap revision to the given channel | ||
1847 | 627 | :param snap_name: the snap name to be released | ||
1848 | 628 | :param revision: the revision number to be released | ||
1849 | 629 | :param channels: the channels in which release the snap | ||
1850 | 630 | :raise: StoreReleaseError | ||
1851 | 631 | """ | ||
1852 | 632 | data = { | ||
1853 | 633 | 'name': snap_name, | ||
1854 | 634 | 'revision': str(revision), | ||
1855 | 635 | 'channels': channels, | ||
1856 | 636 | } | ||
1857 | 637 | auth = _macaroon_auth(self.conf) | ||
1858 | 638 | response = self.post( | ||
1859 | 639 | 'snap-release/', data=json.dumps(data), | ||
1860 | 640 | headers=dict({'Authorization': auth}, **JSON_HEADERS)) | ||
1861 | 641 | if not response.ok: | ||
1862 | 642 | raise errors.StoreReleaseError(data['name'], response) | ||
1863 | 643 | |||
1864 | 644 | response_json = response.json() | ||
1865 | 645 | |||
1866 | 646 | return response_json | ||
1867 | 647 | |||
1868 | 648 | def push_validation(self, snap_id, assertion): | ||
1869 | 649 | """ | ||
1870 | 650 | Push a validation/assertion to the store | ||
1871 | 651 | :param snap_id: the snap_id of the snap | ||
1872 | 652 | :param assertion: the validation/assertion to be pushed | ||
1873 | 653 | :raise: JSONDecodeError, StoreValidationError | ||
1874 | 654 | """ | ||
1875 | 655 | data = { | ||
1876 | 656 | 'assertion': assertion.decode('utf-8'), | ||
1877 | 657 | } | ||
1878 | 658 | auth = _macaroon_auth(self.conf) | ||
1879 | 659 | response = self.put( | ||
1880 | 660 | 'snaps/{}/validations'.format(snap_id), data=json.dumps(data), | ||
1881 | 661 | headers=dict({'Authorization': auth}, **JSON_HEADERS)) | ||
1882 | 662 | if not response.ok: | ||
1883 | 663 | raise errors.StoreValidationError(snap_id, response) | ||
1884 | 664 | try: | ||
1885 | 665 | response_json = response.json() | ||
1886 | 666 | except JSONDecodeError: | ||
1887 | 667 | message = ('Invalid response from the server when pushing ' | ||
1888 | 668 | 'validations: {} {}').format( | ||
1889 | 669 | response.status_code, response) | ||
1890 | 670 | LOGGER.debug(message) | ||
1891 | 671 | raise errors.StoreValidationError( | ||
1892 | 672 | response, message='Invalid response from the server') | ||
1893 | 673 | |||
1894 | 674 | return response_json | ||
1895 | 675 | |||
1896 | 676 | def get_validations(self, snap_id): | ||
1897 | 677 | """Get validations/assertion of a given snap | ||
1898 | 678 | :param snap_id: the id of the snap to get the validations for. | ||
1899 | 679 | :raise: JSONDecodeError, StoreValidationError | ||
1900 | 680 | """ | ||
1901 | 681 | auth = _macaroon_auth(self.conf) | ||
1902 | 682 | response = self.get( | ||
1903 | 683 | 'snaps/{}/validations'.format(snap_id), | ||
1904 | 684 | headers=dict({'Authorization': auth}, **JSON_HEADERS)) | ||
1905 | 685 | if not response.ok: | ||
1906 | 686 | raise errors.StoreValidationError(snap_id, response) | ||
1907 | 687 | try: | ||
1908 | 688 | response_json = response.json() | ||
1909 | 689 | except JSONDecodeError: | ||
1910 | 690 | message = ('Invalid response from the server when getting ' | ||
1911 | 691 | 'validations: {} {}').format( | ||
1912 | 692 | response.status_code, response) | ||
1913 | 693 | LOGGER.debug(message) | ||
1914 | 694 | raise errors.StoreValidationError( | ||
1915 | 695 | response, message='Invalid response from the server') | ||
1916 | 696 | |||
1917 | 697 | return response_json | ||
1918 | 698 | |||
1919 | 699 | def push_snap_build(self, snap_id, snap_build): | ||
1920 | 700 | """ | ||
1921 | 701 | Push a snap build to the store | ||
1922 | 702 | :param snap_id: the snap id | ||
1923 | 703 | :param snap_build: the snap build to push | ||
1924 | 704 | :raise: StoreSnapBuildError | ||
1925 | 705 | """ | ||
1926 | 706 | url = 'snaps/{}/builds'.format(snap_id) | ||
1927 | 707 | data = json.dumps({"assertion": snap_build}) | ||
1928 | 708 | |||
1929 | 709 | headers = dict({'Authorization': _macaroon_auth(self.conf)}, | ||
1930 | 710 | **JSON_CONTENT_TYPE) | ||
1931 | 711 | response = self.post(url, data=data, headers=headers) | ||
1932 | 712 | if not response.ok: | ||
1933 | 713 | raise errors.StoreSnapBuildError(response) | ||
1934 | 714 | |||
1935 | 715 | def snap_history(self, snap_id, series=None, arch=None): | ||
1936 | 716 | """ | ||
1937 | 717 | Get a snap history | ||
1938 | 718 | :param snap_id: the id of the snap to get the history for. | ||
1939 | 719 | :param series: the series of the snap | ||
1940 | 720 | :param arch: the architecture of the snap | ||
1941 | 721 | :raise: StoreSnapHistoryError | ||
1942 | 722 | """ | ||
1943 | 723 | qs = {} | ||
1944 | 724 | if series: | ||
1945 | 725 | qs['series'] = series | ||
1946 | 726 | if arch: | ||
1947 | 727 | qs['arch'] = arch | ||
1948 | 728 | url = 'snaps/' + snap_id + '/history' | ||
1949 | 729 | if qs: | ||
1950 | 730 | url += '?' + urllib.parse.urlencode(qs) | ||
1951 | 731 | auth = _macaroon_auth(self.conf) | ||
1952 | 732 | response = self.get( | ||
1953 | 733 | url, | ||
1954 | 734 | headers=dict({'Authorization': auth}, **JSON_HEADERS)) | ||
1955 | 735 | if not response.ok: | ||
1956 | 736 | raise errors.StoreSnapHistoryError(response, snap_id, series, arch) | ||
1957 | 737 | |||
1958 | 738 | response_json = response.json() | ||
1959 | 739 | |||
1960 | 740 | return response_json | ||
1961 | 741 | |||
1962 | 742 | def snap_status(self, snap_id, series=None, arch=None): | ||
1963 | 743 | """ | ||
1964 | 744 | Get a snap status | ||
1965 | 745 | :param snap_id: the id of the snap to get the history for. | ||
1966 | 746 | :param series: the series of the snap | ||
1967 | 747 | :param arch: the architecture of the snap | ||
1968 | 748 | :raise: StoreSnapStatusError | ||
1969 | 749 | """ | ||
1970 | 750 | qs = {} | ||
1971 | 751 | if series: | ||
1972 | 752 | qs['series'] = series | ||
1973 | 753 | if arch: | ||
1974 | 754 | qs['arch'] = arch | ||
1975 | 755 | url = 'snaps/' + snap_id + '/status' | ||
1976 | 756 | if qs: | ||
1977 | 757 | url += '?' + urllib.parse.urlencode(qs) | ||
1978 | 758 | auth = _macaroon_auth(self.conf) | ||
1979 | 759 | response = self.get( | ||
1980 | 760 | url, | ||
1981 | 761 | headers=dict({'Authorization': auth}, **JSON_HEADERS)) | ||
1982 | 762 | if not response.ok: | ||
1983 | 763 | raise errors.StoreSnapStatusError(response, snap_id, series, arch) | ||
1984 | 764 | |||
1985 | 765 | response_json = response.json() | ||
1986 | 766 | |||
1987 | 767 | return response_json | ||
1988 | 768 | |||
1989 | 769 | def close_channels(self, snap_id, channel_names): | ||
1990 | 770 | """ | ||
1991 | 771 | Close channel for a given snap | ||
1992 | 772 | :param snap_id: the id of the snap | ||
1993 | 773 | :param channel_names: the channels to be closed | ||
1994 | 774 | :raise: StoreChannelClosingError | ||
1995 | 775 | """ | ||
1996 | 776 | url = 'snaps/{}/close'.format(snap_id) | ||
1997 | 777 | data = { | ||
1998 | 778 | 'channels': channel_names | ||
1999 | 779 | } | ||
2000 | 780 | headers = { | ||
2001 | 781 | 'Authorization': _macaroon_auth(self.conf), | ||
2002 | 782 | } | ||
2003 | 783 | response = self.post(url, json=data, headers=headers) | ||
2004 | 784 | if not response.ok: | ||
2005 | 785 | raise errors.StoreChannelClosingError(response) | ||
2006 | 786 | |||
2007 | 787 | try: | ||
2008 | 788 | results = response.json() | ||
2009 | 789 | return results['closed_channels'], results['channel_maps'] | ||
2010 | 790 | except (JSONDecodeError, KeyError): | ||
2011 | 791 | LOGGER.debug( | ||
2012 | 792 | 'Invalid response from the server on channel closing:\n' | ||
2013 | 793 | '%s %s\n%s', response.status_code, response.reason, | ||
2014 | 794 | response.content) | ||
2015 | 795 | raise errors.StoreChannelClosingError(response) | ||
2016 | 796 | |||
2017 | 797 | def sign_developer_agreement(self, latest_tos_accepted=False): | ||
2018 | 798 | """Sign developer agreement to be able to do operations in the store""" | ||
2019 | 799 | auth = _macaroon_auth(self.conf) | ||
2020 | 800 | data = {'latest_tos_accepted': latest_tos_accepted} | ||
2021 | 801 | response = self.post( | ||
2022 | 802 | 'agreement/', data=json.dumps(data), | ||
2023 | 803 | headers=dict({'Authorization': auth}, **JSON_HEADERS)) | ||
2024 | 804 | if not response.ok: | ||
2025 | 805 | raise errors.DeveloperAgreementSignError(response) | ||
2026 | 806 | return response.json() | ||
2027 | 807 | |||
2028 | 808 | |||
2029 | 809 | class StatusTracker: | ||
2030 | 810 | |||
2031 | 811 | """Class to track the status of an operation""" | ||
2032 | 812 | |||
2033 | 813 | __messages = { | ||
2034 | 814 | 'being_processed': 'Processing...', | ||
2035 | 815 | 'ready_to_release': 'Ready to release!', | ||
2036 | 816 | 'need_manual_review': 'Will need manual review...', | ||
2037 | 817 | 'processing_error': 'Error while processing...', | ||
2038 | 818 | } | ||
2039 | 819 | |||
2040 | 820 | __error_codes = ( | ||
2041 | 821 | 'processing_error', | ||
2042 | 822 | 'need_manual_review', | ||
2043 | 823 | ) | ||
2044 | 824 | |||
2045 | 825 | def __init__(self, status_details_url): | ||
2046 | 826 | self.__content = None | ||
2047 | 827 | self.__status_details_url = status_details_url | ||
2048 | 828 | |||
2049 | 829 | def track(self): | ||
2050 | 830 | """Track the status""" | ||
2051 | 831 | queue = Queue() | ||
2052 | 832 | thread = Thread(target=self._update_status, args=(queue,)) | ||
2053 | 833 | thread.start() | ||
2054 | 834 | widgets = ['Processing...', AnimatedMarker()] | ||
2055 | 835 | progress_indicator = ProgressBar(widgets=widgets, maxval=UnknownLength) | ||
2056 | 836 | progress_indicator.start() | ||
2057 | 837 | content = {} | ||
2058 | 838 | for indicator_count in itertools.count(): | ||
2059 | 839 | if not queue.empty(): | ||
2060 | 840 | content = queue.get() | ||
2061 | 841 | if isinstance(content, Exception): | ||
2062 | 842 | raise Exception(content) | ||
2063 | 843 | widgets[0] = self._get_message(content) | ||
2064 | 844 | progress_indicator.update(indicator_count) | ||
2065 | 845 | if content.get('processed'): | ||
2066 | 846 | break | ||
2067 | 847 | sleep(0.1) | ||
2068 | 848 | progress_indicator.finish() | ||
2069 | 849 | self.__content = content | ||
2070 | 850 | return content | ||
2071 | 851 | |||
2072 | 852 | def raise_for_code(self): | ||
2073 | 853 | """Raise an exception if content has particular error codes""" | ||
2074 | 854 | if any(self.__content['code'] == _k for _k in self.__error_codes): | ||
2075 | 855 | raise errors.StoreReviewError(self.__content) | ||
2076 | 856 | |||
2077 | 857 | def _get_message(self, content): | ||
2078 | 858 | """Get a message from content""" | ||
2079 | 859 | return self.__messages.get(content['code'], content['code']) | ||
2080 | 860 | |||
2081 | 861 | def _update_status(self, queue): | ||
2082 | 862 | """Update an operation status""" | ||
2083 | 863 | for content in self._get_status(): | ||
2084 | 864 | queue.put(content) | ||
2085 | 865 | if content['processed']: | ||
2086 | 866 | break | ||
2087 | 867 | sleep(constants.SCAN_STATUS_POLL_DELAY) | ||
2088 | 868 | |||
2089 | 869 | def _get_status(self): | ||
2090 | 870 | """Get an operation status""" | ||
2091 | 871 | connection_errors_allowed = 10 | ||
2092 | 872 | while True: | ||
2093 | 873 | try: | ||
2094 | 874 | content = requests.get(self.__status_details_url).json() | ||
2095 | 875 | except (requests.ConnectionError, requests.HTTPError) as _e: | ||
2096 | 876 | if not connection_errors_allowed: | ||
2097 | 877 | yield _e | ||
2098 | 878 | content = {'processed': False, 'code': 'being_processed'} | ||
2099 | 879 | connection_errors_allowed -= 1 | ||
2100 | 880 | yield content | ||
2101 | 0 | 881 | ||
2102 | === added file 'snappy_ecosystem_tests/helpers/store_apis/upload.py' | |||
2103 | --- snappy_ecosystem_tests/helpers/store_apis/upload.py 1970-01-01 00:00:00 +0000 | |||
2104 | +++ snappy_ecosystem_tests/helpers/store_apis/upload.py 2017-02-20 17:48:06 +0000 | |||
2105 | @@ -0,0 +1,97 @@ | |||
2106 | 1 | # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*- | ||
2107 | 2 | |||
2108 | 3 | # | ||
2109 | 4 | # Snappy Ecosystem Tests | ||
2110 | 5 | # Copyright (C) 2017 Canonical | ||
2111 | 6 | # | ||
2112 | 7 | # This program is free software: you can redistribute it and/or modify | ||
2113 | 8 | # it under the terms of the GNU General Public License as published by | ||
2114 | 9 | # the Free Software Foundation, either version 3 of the License, or | ||
2115 | 10 | # (at your option) any later version. | ||
2116 | 11 | # | ||
2117 | 12 | # This program is distributed in the hope that it will be useful, | ||
2118 | 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
2119 | 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
2120 | 15 | # GNU General Public License for more details. | ||
2121 | 16 | # | ||
2122 | 17 | # You should have received a copy of the GNU General Public License | ||
2123 | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
2124 | 19 | # | ||
2125 | 20 | |||
2126 | 21 | """Helpers to upload binary files to the store""" | ||
2127 | 22 | |||
2128 | 23 | import logging | ||
2129 | 24 | import functools | ||
2130 | 25 | import os | ||
2131 | 26 | |||
2132 | 27 | from progressbar import ( | ||
2133 | 28 | Bar, | ||
2134 | 29 | Percentage, | ||
2135 | 30 | ProgressBar, | ||
2136 | 31 | ) | ||
2137 | 32 | from requests_toolbelt import (MultipartEncoder, MultipartEncoderMonitor) | ||
2138 | 33 | |||
2139 | 34 | from snappy_ecosystem_tests.helpers.store_apis.errors import StoreUploadError | ||
2140 | 35 | |||
2141 | 36 | LOGGER = logging.getLogger(__name__) | ||
2142 | 37 | |||
2143 | 38 | |||
2144 | 39 | def _update_progress_bar(progress_bar, maximum_value, monitor): | ||
2145 | 40 | """Update the progress bar status""" | ||
2146 | 41 | if monitor.bytes_read <= maximum_value: | ||
2147 | 42 | progress_bar.update(monitor.bytes_read) | ||
2148 | 43 | |||
2149 | 44 | |||
2150 | 45 | def upload_files(binary_filename, updown_client): | ||
2151 | 46 | """Upload a binary file to the Store. | ||
2152 | 47 | Submit a file to the Store upload service and return the | ||
2153 | 48 | corresponding upload_id. | ||
2154 | 49 | """ | ||
2155 | 50 | binary_file = None | ||
2156 | 51 | try: | ||
2157 | 52 | binary_file_size = os.path.getsize(binary_filename) | ||
2158 | 53 | binary_file = open(binary_filename, 'rb') | ||
2159 | 54 | encoder = MultipartEncoder( | ||
2160 | 55 | fields={ | ||
2161 | 56 | 'binary': ('filename', binary_file, 'application/octet-stream') | ||
2162 | 57 | } | ||
2163 | 58 | ) | ||
2164 | 59 | |||
2165 | 60 | # Create a progress bar that looks like: Uploading foo [== ] 50% | ||
2166 | 61 | progress_bar = ProgressBar( | ||
2167 | 62 | widgets=['Uploading {} '.format(binary_filename), | ||
2168 | 63 | Bar(marker='=', left='[', right=']'), ' ', Percentage()], | ||
2169 | 64 | maxval=os.path.getsize(binary_filename)) | ||
2170 | 65 | progress_bar.start() | ||
2171 | 66 | # Print a newline so the progress bar has some breathing room. | ||
2172 | 67 | LOGGER.info('') | ||
2173 | 68 | |||
2174 | 69 | # Create a monitor for this upload, so that progress can be displayed | ||
2175 | 70 | monitor = MultipartEncoderMonitor( | ||
2176 | 71 | encoder, functools.partial(_update_progress_bar, progress_bar, | ||
2177 | 72 | binary_file_size)) | ||
2178 | 73 | |||
2179 | 74 | # Begin upload | ||
2180 | 75 | response = updown_client.upload(monitor) | ||
2181 | 76 | |||
2182 | 77 | # Make sure progress bar shows 100% complete | ||
2183 | 78 | progress_bar.finish() | ||
2184 | 79 | |||
2185 | 80 | except Exception as err: | ||
2186 | 81 | raise RuntimeError( | ||
2187 | 82 | 'An unexpected error was found while uploading ' | ||
2188 | 83 | 'files: {!r}.'.format(err)) | ||
2189 | 84 | finally: | ||
2190 | 85 | # Close the open file | ||
2191 | 86 | if binary_file: | ||
2192 | 87 | binary_file.close() | ||
2193 | 88 | |||
2194 | 89 | if not response.ok: | ||
2195 | 90 | raise StoreUploadError(response) | ||
2196 | 91 | |||
2197 | 92 | response_data = response.json() | ||
2198 | 93 | return { | ||
2199 | 94 | 'upload_id': response_data['upload_id'], | ||
2200 | 95 | 'binary_filesize': binary_file_size, | ||
2201 | 96 | 'source_uploaded': False, | ||
2202 | 97 | } | ||
2203 | 0 | 98 | ||
2204 | === removed directory 'snappy_ecosystem_tests/snapcraft' | |||
2205 | === removed file 'snappy_ecosystem_tests/snapcraft/__init__.py' | |||
2206 | --- snappy_ecosystem_tests/snapcraft/__init__.py 2017-02-09 20:09:40 +0000 | |||
2207 | +++ snappy_ecosystem_tests/snapcraft/__init__.py 1970-01-01 00:00:00 +0000 | |||
2208 | @@ -1,19 +0,0 @@ | |||
2209 | 1 | # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*- | ||
2210 | 2 | |||
2211 | 3 | # | ||
2212 | 4 | # Snappy Ecosystem Tests | ||
2213 | 5 | # Copyright (C) 2017 Canonical | ||
2214 | 6 | # | ||
2215 | 7 | # This program is free software: you can redistribute it and/or modify | ||
2216 | 8 | # it under the terms of the GNU General Public License as published by | ||
2217 | 9 | # the Free Software Foundation, either version 3 of the License, or | ||
2218 | 10 | # (at your option) any later version. | ||
2219 | 11 | # | ||
2220 | 12 | # This program is distributed in the hope that it will be useful, | ||
2221 | 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
2222 | 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
2223 | 15 | # GNU General Public License for more details. | ||
2224 | 16 | # | ||
2225 | 17 | # You should have received a copy of the GNU General Public License | ||
2226 | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
2227 | 19 | # | ||
2228 | 20 | 0 | ||
2229 | === removed file 'snappy_ecosystem_tests/snapcraft/snapcraft.py' | |||
2230 | --- snappy_ecosystem_tests/snapcraft/snapcraft.py 2017-02-15 19:14:19 +0000 | |||
2231 | +++ snappy_ecosystem_tests/snapcraft/snapcraft.py 1970-01-01 00:00:00 +0000 | |||
2232 | @@ -1,74 +0,0 @@ | |||
2233 | 1 | # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*- | ||
2234 | 2 | |||
2235 | 3 | # | ||
2236 | 4 | # Snappy Ecosystem Tests | ||
2237 | 5 | # Copyright (C) 2017 Canonical | ||
2238 | 6 | # | ||
2239 | 7 | # This program is free software: you can redistribute it and/or modify | ||
2240 | 8 | # it under the terms of the GNU General Public License as published by | ||
2241 | 9 | # the Free Software Foundation, either version 3 of the License, or | ||
2242 | 10 | # (at your option) any later version. | ||
2243 | 11 | # | ||
2244 | 12 | # This program is distributed in the hope that it will be useful, | ||
2245 | 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
2246 | 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
2247 | 15 | # GNU General Public License for more details. | ||
2248 | 16 | # | ||
2249 | 17 | # You should have received a copy of the GNU General Public License | ||
2250 | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
2251 | 19 | # | ||
2252 | 20 | |||
2253 | 21 | """Snapcraft client helpers""" | ||
2254 | 22 | |||
2255 | 23 | import pexpect | ||
2256 | 24 | |||
2257 | 25 | |||
2258 | 26 | # login credentials exported by shell environment | ||
2259 | 27 | from snappy_ecosystem_tests.utils import storeconfig | ||
2260 | 28 | |||
2261 | 29 | LOGIN_EMAIL, LOGIN_PASSWORD = storeconfig.get_store_credentials() | ||
2262 | 30 | |||
2263 | 31 | |||
2264 | 32 | class Snapcraft(object): | ||
2265 | 33 | """Contain Snapcraft specific functionality to use via command | ||
2266 | 34 | line interface""" | ||
2267 | 35 | def __init__(self): | ||
2268 | 36 | """Create new snapcraft instance.""" | ||
2269 | 37 | self._login = False | ||
2270 | 38 | self._cleanup() | ||
2271 | 39 | |||
2272 | 40 | def _cleanup(self): | ||
2273 | 41 | """Perform cleanup actions""" | ||
2274 | 42 | self.logout() | ||
2275 | 43 | |||
2276 | 44 | def logout(self): | ||
2277 | 45 | """logout of snapcraft store session""" | ||
2278 | 46 | child = pexpect.spawn("snapcraft logout") | ||
2279 | 47 | err = child.expect('Credentials cleared.') | ||
2280 | 48 | child.terminate(True) | ||
2281 | 49 | child.wait() | ||
2282 | 50 | if err is not 0: | ||
2283 | 51 | raise ValueError("Failed to logout") | ||
2284 | 52 | |||
2285 | 53 | self._login = False | ||
2286 | 54 | |||
2287 | 55 | def login(self): | ||
2288 | 56 | """login to store using the credential environment variables""" | ||
2289 | 57 | child = pexpect.spawn("snapcraft login") | ||
2290 | 58 | child.expect('Email: ') | ||
2291 | 59 | child.sendline(LOGIN_EMAIL) | ||
2292 | 60 | child.expect('Password:') | ||
2293 | 61 | child.sendline(LOGIN_PASSWORD) | ||
2294 | 62 | err = child.expect('Login successful') | ||
2295 | 63 | if err is not 0: | ||
2296 | 64 | raise ValueError("Failed to login") | ||
2297 | 65 | |||
2298 | 66 | self._login = True | ||
2299 | 67 | |||
2300 | 68 | def list_registered(self): | ||
2301 | 69 | """call snapcraft list-registered command, | ||
2302 | 70 | raise exception if not logged in""" | ||
2303 | 71 | if self._login is False: | ||
2304 | 72 | raise ValueError("User is not logged in, " | ||
2305 | 73 | "please login before using this command") | ||
2306 | 74 | return pexpect.spawnu("snapcraft list-registered").read() | ||
2307 | 75 | 0 | ||
2308 | === added file 'snappy_ecosystem_tests/tests/test_store_apis_login.py' | |||
2309 | --- snappy_ecosystem_tests/tests/test_store_apis_login.py 1970-01-01 00:00:00 +0000 | |||
2310 | +++ snappy_ecosystem_tests/tests/test_store_apis_login.py 2017-02-20 17:48:06 +0000 | |||
2311 | @@ -0,0 +1,39 @@ | |||
2312 | 1 | # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*- | ||
2313 | 2 | |||
2314 | 3 | # | ||
2315 | 4 | # Snappy Ecosystem Tests | ||
2316 | 5 | # Copyright (C) 2017 Canonical | ||
2317 | 6 | # | ||
2318 | 7 | # This program is free software: you can redistribute it and/or modify | ||
2319 | 8 | # it under the terms of the GNU General Public License as published by | ||
2320 | 9 | # the Free Software Foundation, either version 3 of the License, or | ||
2321 | 10 | # (at your option) any later version. | ||
2322 | 11 | # | ||
2323 | 12 | # This program is distributed in the hope that it will be useful, | ||
2324 | 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
2325 | 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
2326 | 15 | # GNU General Public License for more details. | ||
2327 | 16 | # | ||
2328 | 17 | # You should have received a copy of the GNU General Public License | ||
2329 | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
2330 | 19 | # | ||
2331 | 20 | |||
2332 | 21 | """Test for Login to the Store via RESTful APIs""" | ||
2333 | 22 | |||
2334 | 23 | from snappy_ecosystem_tests.helpers.store_apis.rest_apis import Store | ||
2335 | 24 | from snappy_ecosystem_tests.helpers.test_base import SnappyEcosystemTestCase | ||
2336 | 25 | from snappy_ecosystem_tests.utils.storeconfig import get_store_credentials | ||
2337 | 26 | |||
2338 | 27 | |||
2339 | 28 | class StoreAPILoginTestCase(SnappyEcosystemTestCase): | ||
2340 | 29 | |||
2341 | 30 | def setUp(self): | ||
2342 | 31 | super().setUp() | ||
2343 | 32 | self.client = Store() | ||
2344 | 33 | |||
2345 | 34 | def test_api_login_logout(self): | ||
2346 | 35 | """Test login and logout functionality in store REST APIs""" | ||
2347 | 36 | self.client.login(*get_store_credentials()) | ||
2348 | 37 | self.client.load_config() | ||
2349 | 38 | self.assertIsNotNone(self.client.config.get('macaroon')) | ||
2350 | 39 | self.assertIsNotNone(self.client.config.get('unbound_discharge')) | ||
2351 | 0 | 40 | ||
2352 | === modified file 'snappy_ecosystem_tests/tests/test_store_login.py' | |||
2353 | --- snappy_ecosystem_tests/tests/test_store_login.py 2017-02-15 19:14:19 +0000 | |||
2354 | +++ snappy_ecosystem_tests/tests/test_store_login.py 2017-02-20 17:48:06 +0000 | |||
2355 | @@ -20,7 +20,7 @@ | |||
2356 | 20 | 20 | ||
2357 | 21 | """Test for Login to the Store via Web Interface, snapcraft and snapd""" | 21 | """Test for Login to the Store via Web Interface, snapcraft and snapd""" |
2358 | 22 | 22 | ||
2360 | 23 | from snappy_ecosystem_tests.snapcraft.snapcraft import Snapcraft | 23 | from snappy_ecosystem_tests.helpers.snapcraft.client import Snapcraft |
2361 | 24 | from snappy_ecosystem_tests.helpers.web_test_base import UbuntuStoreWebTestsBase | 24 | from snappy_ecosystem_tests.helpers.web_test_base import UbuntuStoreWebTestsBase |
2362 | 25 | 25 | ||
2363 | 26 | 26 |
How about using it as it is from snapcraft/storeapi as it is, instead of making a copy? that way we won't have to catchup the changes upstream.
Also if we have an example test case along with the merge then it will become convenient to test the base and other utility classes.
Otherwise LGTM