forked from mozilla-releng/pushsnapscript
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add push on top of snapcraft 2.39.2 (git submodule)
- Loading branch information
1 parent
b7a79a1
commit 44fcf6c
Showing
11 changed files
with
153 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "snapcraft"] | ||
path = snapcraft | ||
url = https://github.com/snapcore/snapcraft.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import base64 | ||
import os | ||
|
||
from scriptworker.utils import makedirs | ||
|
||
from pushsnapscript.utils import cwd | ||
|
||
# XXX Hack to only import a subset of snapcraft. Otherwise snapcraft can't be built on any other | ||
# distribution than Ubuntu. The prod instance runs CentOS 6. There isn't a package version of | ||
# snapcraft on that platform either. | ||
import sys | ||
dir_path = os.path.dirname(os.path.realpath(__file__)) | ||
sys.path.append(os.path.join(dir_path, '..', 'snapcraft')) | ||
from snapcraft import _store as snapcraft_store_client # noqa | ||
|
||
|
||
def push(context, snap_file_path, channel): | ||
# Snapcraft requires credentials to be stored at $CWD/.snapcraft/snapcraft.cfg. Let's store them | ||
# in a folder that gets purged at the end of the run. | ||
_craft_credentials_file(context, channel) | ||
with cwd(context.config['work_dir']): | ||
snapcraft_store_client.push(snap_file_path, channel) | ||
|
||
|
||
def _craft_credentials_file(context, channel): | ||
base64_creds = context.config['base64_macaroons_configs'][channel] | ||
decoded_creds_bytes = base64.b64decode(base64_creds) | ||
decoded_creds = decoded_creds_bytes.decode() | ||
|
||
snapcraft_dir = os.path.join(context.config['work_dir'], '.snapcraft') | ||
makedirs(snapcraft_dir) | ||
snapcraft_config_file = os.path.join(snapcraft_dir, 'snapcraft.cfg') | ||
with open(snapcraft_config_file, 'w') as f: | ||
f.write(decoded_creds) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import pytest | ||
import os | ||
import tempfile | ||
|
||
from unittest.mock import MagicMock | ||
|
||
from pushsnapscript.snap_store import snapcraft_store_client, push, _craft_credentials_file | ||
|
||
SNAPCRAFT_SAMPLE_CONFIG = '''[login.ubuntu.com] | ||
macaroon = SomeBase64 | ||
unbound_discharge = SomeOtherBase64 | ||
email = release@m.c | ||
''' | ||
|
||
SNAPCRAFT_SAMPLE_CONFIG_BASE64 = 'W2xvZ2luLnVidW50dS5jb21dCm1hY2Fyb29uID0gU29tZUJhc2U2NAp1bmJvdW5kX\ | ||
2Rpc2NoYXJnZSA9IFNvbWVPdGhlckJhc2U2NAplbWFpbCA9IHJlbGVhc2VAbS5jCg==' | ||
|
||
|
||
@pytest.mark.parametrize('channel', ('edge', 'candidate')) | ||
def test_push(monkeypatch, channel): | ||
generator = (n for n in range(0, 2)) | ||
|
||
context = MagicMock() | ||
context.config = {'base64_macaroons_configs': {channel: SNAPCRAFT_SAMPLE_CONFIG_BASE64}} | ||
|
||
with tempfile.TemporaryDirectory() as d: | ||
def snapcraft_store_client_push_fake(snap_file_path, channel): | ||
# This function can't be a regular mock because of the following check: | ||
assert os.getcwd() == d # Push must be done from the work_dir | ||
|
||
assert snap_file_path == '/some/file.snap' | ||
assert channel == channel | ||
next(generator) | ||
|
||
context.config['work_dir'] = d | ||
monkeypatch.setattr(snapcraft_store_client, 'push', snapcraft_store_client_push_fake) | ||
push(context, '/some/file.snap', channel) | ||
|
||
assert os.getcwd() != d | ||
|
||
assert next(generator) == 1 # Check fake function was called once | ||
|
||
|
||
@pytest.mark.parametrize('channel', ('edge', 'candidate')) | ||
def test_craft_credentials_file(channel): | ||
context = MagicMock() | ||
context.config = {'base64_macaroons_configs': {channel: SNAPCRAFT_SAMPLE_CONFIG_BASE64}} | ||
|
||
with tempfile.TemporaryDirectory() as d: | ||
context.config['work_dir'] = d | ||
_craft_credentials_file(context, channel) | ||
with open(os.path.join(d, '.snapcraft', 'snapcraft.cfg')) as f: | ||
assert f.read() == SNAPCRAFT_SAMPLE_CONFIG |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import os | ||
import tempfile | ||
|
||
from pushsnapscript.utils import cwd | ||
|
||
|
||
def test_cwd(): | ||
old_working_dir = os.getcwd() | ||
with tempfile.TemporaryDirectory() as d: | ||
with cwd(d): | ||
current_working_dir = os.getcwd() | ||
assert current_working_dir == d | ||
assert old_working_dir != current_working_dir | ||
|
||
assert old_working_dir == os.getcwd() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import contextlib | ||
import os | ||
|
||
|
||
@contextlib.contextmanager | ||
def cwd(new_cwd): | ||
current_dir = os.getcwd() | ||
try: | ||
os.chdir(new_cwd) | ||
yield | ||
finally: | ||
os.chdir(current_dir) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,16 @@ | ||
aiohttp | ||
scriptworker | ||
|
||
# XXX Dependencies required by snapcraft._store.push | ||
click | ||
progressbar33 | ||
pyelftools | ||
pymacaroons | ||
pysha3 | ||
python-debian | ||
pyxdg | ||
PyYAML | ||
requests | ||
requests-toolbelt | ||
requests-unixsocket | ||
simplejson | ||
tabulate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters