Skip to content

Commit

Permalink
tests: fix package version pinning tests. (#1269)
Browse files Browse the repository at this point in the history
The integration tests locked down on a version of "hello" which is error prone. We now get the version using rmadison like logic.

Signed-off-by: Sergio Schvezov <[email protected]>
  • Loading branch information
sergiusens authored Apr 20, 2017
1 parent 88ebc1f commit 9e46e66
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 28 deletions.
24 changes: 22 additions & 2 deletions integration_tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import fileinput
import os
import platform
import re
import subprocess
import time
Expand All @@ -25,11 +26,12 @@

import fixtures
import pexpect
from unittest import mock
import requests
import testtools
from unittest import mock
from testtools import content
from testtools.matchers import MatchesRegex

from snapcraft import ProjectOptions as _ProjectOptions
from snapcraft.tests import fixture_setup


Expand Down Expand Up @@ -94,6 +96,9 @@ def setUp(self):
self.stage_dir = 'stage'
self.prime_dir = 'prime'

self.deb_arch = _ProjectOptions().deb_arch
self.distro_series = platform.linux_distribution()[2]

def run_snapcraft(
self, command, project_dir=None, debug=True,
pre_func=lambda: None):
Expand Down Expand Up @@ -388,3 +393,18 @@ def push(self, snap, release=None, expected=None):
process.expect(pexpect.EOF)
process.close()
return process.exitstatus


def get_package_version(package_name, series, deb_arch):
# http://people.canonical.com/~ubuntu-archive/madison.cgi?package=hello&a=amd64&c=&s=zesty&text=on
params = {
'package': package_name,
's': series,
'a': deb_arch,
'text': 'on',
}
query = requests.get('http://people.canonical.com/~ubuntu-archive/'
'madison.cgi', params)
query.raise_for_status()
package_status = [i.strip() for i in query.text.strip().split('|')]
return package_status[1]
55 changes: 38 additions & 17 deletions integration_tests/test_build_package_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,54 @@
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import os
import subprocess

import apt
import testscenarios
import yaml
from testtools.matchers import Equals

import integration_tests


class BuildPackageVersionTestCase(integration_tests.TestCase):
class BuildPackageVersionTestCase(testscenarios.WithScenarios,
integration_tests.TestCase):

scenarios = (
('global', dict(project='build-package-version')),
('local', dict(project='build-package-version-global')),
)

def setUp(self):
super().setUp()
self.pkg_name = 'hello'
self.pkg_version = integration_tests.get_package_version(
self.pkg_name, self.distro_series, self.deb_arch)
self.hello_package = '{}={}'.format(self.pkg_name, self.pkg_version)

def _set_hello_package_version(self, snapcraft_yaml_file):
with open(snapcraft_yaml_file) as f:
snapcraft_yaml = yaml.load(f)
if 'build-packages' in snapcraft_yaml:
snapcraft_yaml['build-packages'] = [self.hello_package]
else:
snapcraft_yaml['parts']['hello']['build-packages'] = \
[self.hello_package]
with open(snapcraft_yaml_file, 'w') as f:
yaml.dump(snapcraft_yaml, f)

def test_build_package_gets_version(self):
self.run_snapcraft('pull', 'build-package-version')
pkg = 'hello'
expected_version = '2.10-1'
with apt.Cache() as apt_cache:
installed_version = apt_cache[pkg].candidate.version
self.assertEqual(expected_version,
installed_version)

def test_global_build_package_gets_version(self):
self.run_snapcraft('pull', 'build-package-version-global')
pkg = 'hello'
expected_version = '2.10-1'
self.copy_project_to_cwd(self.project)
self._set_hello_package_version(os.path.join('snap', 'snapcraft.yaml'))
self.run_snapcraft('pull')

with apt.Cache() as apt_cache:
installed_version = apt_cache[pkg].candidate.version
self.assertEqual(expected_version,
installed_version)
installed_version = apt_cache[self.pkg_name].candidate.version
self.assertThat(installed_version, Equals(self.pkg_version))


class BuildPackageVersionErrorsTestCase(integration_tests.TestCase):

def test_build_package_bad_version(self):
error = self.assertRaises(
Expand Down
39 changes: 30 additions & 9 deletions integration_tests/test_pull_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,39 +60,60 @@ def assert_expected_pull_state(self, project_dir):

class AssetTrackingTestCase(integration_tests.TestCase):

def setUp(self):
super().setUp()
hello_version = integration_tests.get_package_version(
'hello', self.distro_series, self.deb_arch)
self.hello_package = 'hello={}'.format(hello_version)

def _set_hello_package_version(self, snapcraft_yaml_file):
with open(snapcraft_yaml_file) as f:
snapcraft_yaml = yaml.load(f)
if 'build-packages' in snapcraft_yaml:
snapcraft_yaml['build-packages'] = [self.hello_package]
else:
snapcraft_yaml['parts']['asset-tracking']['stage-packages'] = \
[self.hello_package]
snapcraft_yaml['parts']['asset-tracking']['build-packages'] = \
[self.hello_package]
with open(snapcraft_yaml_file, 'w') as f:
yaml.dump(snapcraft_yaml, f)

def test_pull(self):
project_dir = 'asset-tracking'
self.run_snapcraft(['pull', 'asset-tracking'], project_dir)
self.copy_project_to_cwd('asset-tracking')
self._set_hello_package_version('snapcraft.yaml')
self.run_snapcraft('pull')

state_file = os.path.join(
self.parts_dir, project_dir, 'state', 'pull')
self.parts_dir, 'asset-tracking', 'state', 'pull')
self.assertThat(state_file, FileExists())
with open(state_file) as f:
state = yaml.load(f)

# Verify that the correct version of 'hello' is installed
self.assertTrue(len(state.assets['stage-packages']) > 0)
self.assertTrue(len(state.assets['build-packages']) > 0)
self.assertIn('hello=2.10-1', state.assets['stage-packages'])
self.assertIn('hello=2.10-1', state.assets['build-packages'])
self.assertIn(self.hello_package, state.assets['stage-packages'])
self.assertIn(self.hello_package, state.assets['build-packages'])
self.assertIn('source-details', state.assets)

def test_pull_global_build_packages_are_excluded(self):
"""
Ensure global build-packages are not included in each part's
build-packages data.
"""
project_dir = 'build-package-version-global'
self.run_snapcraft('pull', project_dir)
self.copy_project_to_cwd('build-package-version-global')
self._set_hello_package_version(os.path.join('snap', 'snapcraft.yaml'))
self.run_snapcraft('pull')

state_file = os.path.join(
self.parts_dir, project_dir, 'state', 'pull')
self.parts_dir, 'build-package-version-global', 'state', 'pull')
self.assertThat(state_file, FileExists())
with open(state_file) as f:
state = yaml.load(f)

self.assertTrue(len(state.assets['build-packages']) == 0)
self.assertNotIn('hello=2.10-1', state.assets['build-packages'])
self.assertNotIn(self.hello_package, state.assets['build-packages'])


TestDetail = namedtuple('TestDetail', ['field', 'value'])
Expand Down

0 comments on commit 9e46e66

Please sign in to comment.