Ejemplo n.º 1
0
    def test_parallel_invocation_inject_snap(self, mock_is_snap):
        mock_is_snap.side_effect = lambda: True

        fake_snapd = fixture_setup.FakeSnapd()
        self.useFixture(fake_snapd)
        fake_snapd.snaps_result = [
            {
                'name': 'core',
                'confinement': 'strict',
                'id': '2kkitQurgOkL3foImG4wDwn9CIANuHlt',
                'channel': 'stable',
                'revision': '123'
            },
            {
                'name': 'snapcraft',
                'confinement': 'classic',
                'id': '3lljuRvshPlM4gpJnH5xExo0DJBOvImu',
                'channel': 'edge',
                'revision': '345'
            },
        ]

        builder1 = self.make_containerbuild()
        builder2 = self.make_containerbuild()
        builder1.execute()
        builder2.execute()
Ejemplo n.º 2
0
    def test_inject_snap(self,
                         mock_is_snap,
                         mock_container_run):
        mock_is_snap.side_effect = lambda: True
        mock_container_run.side_effect = lambda cmd, **kwargs: cmd

        fake_snapd = fixture_setup.FakeSnapd()
        self.useFixture(fake_snapd)
        fake_snapd.snaps_result = [
            {'name': 'core',
             'confinement': 'strict',
             'id': '2kkitQurgOkL3foImG4wDwn9CIANuHlt',
             'channel': 'stable',
             'revision': '123'},
            {'name': 'snapcraft',
             'confinement': 'classic',
             'id': '3lljuRvshPlM4gpJnH5xExo0DJBOvImu',
             'channel': 'edge',
             'revision': '345'},
        ]

        builder = self.make_containerbuild()

        builder.execute()
        if hasattr(self, 'cross') and self.cross:
            mock_container_run.assert_has_calls([
                call(['snap', 'install', 'core', '--channel', 'stable']),
                call(['snap', 'refresh', 'core', '--channel', 'stable']),
                call(['snap', 'install', 'snapcraft', '--channel', 'edge',
                      '--classic']),
                call(['snap', 'refresh', 'snapcraft', '--channel', 'edge',
                      '--classic']),
            ])
            return

        tmp_dir = self.fake_filesystem.tmp_dir
        self.fake_lxd.check_call_mock.assert_has_calls([
            call(['lxc', 'file', 'push',
                  os.path.join(tmp_dir, 'core_123.assert'),
                  '{}/run/core_123.assert'.format(self.fake_lxd.name)]),
            call(['lxc', 'file', 'push',
                  os.path.join(tmp_dir, 'core_123.snap'),
                  '{}/run/core_123.snap'.format(self.fake_lxd.name)]),
            call(['lxc', 'file', 'push',
                  os.path.join(tmp_dir, 'snapcraft_345.assert'),
                  '{}/run/snapcraft_345.assert'.format(self.fake_lxd.name)]),
            call(['lxc', 'file', 'push',
                  os.path.join(tmp_dir, 'snapcraft_345.snap'),
                  '{}/run/snapcraft_345.snap'.format(self.fake_lxd.name)]),
        ])
        mock_container_run.assert_has_calls([
            call(['apt-get', 'install', 'squashfuse', '-y']),
            call(['snap', 'ack', '/run/core_123.assert']),
            call(['snap', 'install', '/run/core_123.snap']),
            call(['snap', 'ack', '/run/snapcraft_345.assert']),
            call(['snap', 'install', '/run/snapcraft_345.snap', '--classic']),
        ])
Ejemplo n.º 3
0
    def test_inject_snap_api_error(self, mock_is_snap):
        mock_is_snap.side_effect = lambda: True

        fake_snapd = fixture_setup.FakeSnapd()
        fake_snapd.snaps_result = []
        self.useFixture(fake_snapd)

        builder = self.make_containerbuild()
        self.assertIn('Error querying \'core\' snap: not found',
                      str(self.assertRaises(SnapdError, builder.execute)))
Ejemplo n.º 4
0
    def test_snap_containerized_inject_snap_dangerous(self,
                                                      mock_makedirs,
                                                      mock_copyfile,
                                                      mock_rmtree,
                                                      mock_is_snap,
                                                      mock_container_run,
                                                      mock_getuid):
        # Create open mock here for context manager to work correctly
        patcher = mock.patch('snapcraft.internal.lxd.open',
                             mock.mock_open())
        patcher.start()
        self.addCleanup(patcher.stop)

        mock_is_snap.side_effect = lambda: True
        mock_container_run.side_effect = lambda cmd, **kwargs: cmd
        mock_getuid.return_value = 1234
        fake_snapd = fixture_setup.FakeSnapd()
        self.useFixture(fake_snapd)
        fake_snapd.snaps['snapcraft']['revision'] = 'x1'
        fake_snapd.snaps['snapcraft']['id'] = ''
        fake_lxd = fixture_setup.FakeLXD()
        self.useFixture(fake_lxd)
        self.useFixture(fixtures.EnvironmentVariable(
                'SNAPCRAFT_CONTAINER_BUILDS', '1'))
        self.make_snapcraft_yaml()

        self.run_command(['snap'])
        tmpdir = os.path.expanduser(
            os.path.join('~', 'snap', 'lxd', 'common', 'snapcraft.tmp'))
        fake_lxd.check_call_mock.assert_has_calls([
            call(['lxc', 'file', 'push',
                  os.path.join(tmpdir, 'core_123.assert'),
                  '{}/run/core_123.assert'.format(fake_lxd.name)]),
            call(['lxc', 'file', 'push',
                  os.path.join(tmpdir, 'core_123.snap'),
                  '{}/run/core_123.snap'.format(fake_lxd.name)]),
            call(['sudo', 'cp', '/var/lib/snapd/snaps/snapcraft_x1.snap',
                  os.path.join(tmpdir, 'snapcraft_x1.snap')]),
            call(['sudo', 'chown', str(os.getuid()),
                  os.path.join(tmpdir, 'snapcraft_x1.snap')]),
            call(['lxc', 'file', 'push',
                  os.path.join(tmpdir, 'snapcraft_x1.snap'),
                  '{}/run/snapcraft_x1.snap'.format(fake_lxd.name)]),
        ])
        mock_container_run.assert_has_calls([
            call(['apt-get', 'install', 'squashfuse', '-y']),
            call(['snap', 'ack', '/run/core_123.assert']),
            call(['snap', 'install', '/run/core_123.snap']),
            call(['snap', 'install', '/run/snapcraft_x1.snap',
                  '--dangerous', '--classic']),
        ])
        # Temporary folder should be removed in the end
        mock_rmtree.assert_has_calls([call(tmpdir)])
Ejemplo n.º 5
0
    def test_inject_apt(self, mock_is_snap, mock_container_run):
        mock_is_snap.side_effect = lambda: False

        fake_snapd = fixture_setup.FakeSnapd()
        self.useFixture(fake_snapd)

        builder = self.make_containerbuild()
        builder.execute()

        mock_container_run.assert_has_calls([
            call(['apt-get', 'install', 'snapcraft', '-y']),
        ])
Ejemplo n.º 6
0
    def test_inject_snap_dangerous(self,
                                   mock_is_snap,
                                   mock_container_run,
                                   mock_getuid):
        mock_is_snap.side_effect = lambda: True
        mock_container_run.side_effect = lambda cmd, **kwargs: cmd
        mock_getuid.return_value = 1234

        fake_snapd = fixture_setup.FakeSnapd()
        self.useFixture(fake_snapd)
        fake_snapd.snaps_result = [
            {'name': 'core',
             'confinement': 'strict',
             'id': '2kkitQurgOkL3foImG4wDwn9CIANuHlt',
             'channel': 'stable',
             'revision': '123'},
            {'name': 'snapcraft',
             'confinement': 'classic',
             'id': '',
             'channel': 'edge',
             'revision': 'x1'},
        ]

        builder = self.make_containerbuild()

        builder.execute()
        if hasattr(self, 'cross') and self.cross:
            mock_container_run.assert_has_calls([
                call(['snap', 'install', 'core', '--channel', 'stable']),
                call(['snap', 'refresh', 'core', '--channel', 'stable']),
                call(['snap', 'install', 'snapcraft', '--channel', 'edge',
                      '--classic']),
                call(['snap', 'refresh', 'snapcraft', '--channel', 'edge',
                      '--classic']),
            ])
            return

        tmp_dir = self.fake_filesystem.tmp_dir
        self.fake_lxd.check_call_mock.assert_has_calls([
            call(['sudo', 'cp', '/var/lib/snapd/snaps/snapcraft_x1.snap',
                  os.path.join(tmp_dir, 'snapcraft_x1.snap')]),
            call(['sudo', 'chown', str(os.getuid()),
                  os.path.join(tmp_dir, 'snapcraft_x1.snap')]),
            call(['lxc', 'file', 'push',
                  os.path.join(tmp_dir, 'snapcraft_x1.snap'),
                  '{}/run/snapcraft_x1.snap'.format(self.fake_lxd.name)]),
        ])
        mock_container_run.assert_has_calls([
            call(['snap', 'install', '/run/snapcraft_x1.snap',
                  '--dangerous', '--classic']),
        ])
Ejemplo n.º 7
0
    def test_inject_snap_existing_container(self, mock_is_snap,
                                            mock_container_run):
        mock_is_snap.return_value = True

        fake_snapd = fixture_setup.FakeSnapd()
        self.useFixture(fake_snapd)
        fake_snapd.snaps_result = [
            {
                'name': 'core',
                'confinement': 'strict',
                'id': '2kkitQurgOkL3foImG4wDwn9CIANuHlt',
                'channel': 'stable',
                'revision': '123'
            },
            {
                'name': 'snapcraft',
                'confinement': 'classic',
                'id': '3lljuRvshPlM4gpJnH5xExo0DJBOvImu',
                'channel': 'edge',
                'revision': '345'
            },
        ]
        # Container was created before, and isn't running
        self.fake_lxd.name = 'myremote:snapcraft-project'
        self.fake_lxd.status = 'Stopped'

        self.make_containerbuild().execute()

        if hasattr(self, 'cross') and self.cross:
            mock_container_run.assert_has_calls([
                call(['snap', 'install', 'core', '--channel', 'stable']),
                call(['snap', 'refresh', 'core', '--channel', 'stable']),
                call([
                    'snap', 'install', 'snapcraft', '--channel', 'edge',
                    '--classic'
                ]),
                call([
                    'snap', 'refresh', 'snapcraft', '--channel', 'edge',
                    '--classic'
                ]),
            ])
        else:
            mock_container_run.assert_has_calls([
                call(['snap', 'ack', '/run/core_123.assert']),
                call(['snap', 'install', '/run/core_123.snap']),
                call(['snap', 'ack', '/run/snapcraft_345.assert']),
                call([
                    'snap', 'install', '/run/snapcraft_345.snap', '--classic'
                ]),
            ])
Ejemplo n.º 8
0
    def test_inject_socket_error(self, mock_is_snap):
        mock_is_snap.side_effect = lambda: True

        def snap_details(handler_instalce, snap_name):
            raise requests.exceptions.ConnectionError(
                'Connection aborted.',
                FileNotFoundError(2, 'No such file or directory'))

        fake_snapd = fixture_setup.FakeSnapd()
        fake_snapd.snap_details_func = snap_details
        self.useFixture(fake_snapd)

        builder = self.make_containerbuild()
        self.assertIn('Error connecting to',
                      str(self.assertRaises(SnapdError, builder.execute)))
Ejemplo n.º 9
0
    def test_snap_containerized_inject_snap_api_error(self, mock_makedirs,
                                                      mock_copyfile,
                                                      mock_rmtree,
                                                      mock_is_snap):
        mock_is_snap.side_effect = lambda: True
        fake_snapd = fixture_setup.FakeSnapd()
        self.useFixture(fake_snapd)
        fake_snapd.snaps = {}
        self.useFixture(fixture_setup.FakeLXD())
        self.useFixture(
            fixtures.EnvironmentVariable('SNAPCRAFT_CONTAINER_BUILDS', '1'))
        self.make_snapcraft_yaml()

        self.assertIn(
            'Error querying \'core\' snap: not found',
            str(self.assertRaises(SnapdError, self.run_command, ['snap'])))
        # Temporary folder should remain in case of failure
        mock_rmtree.assert_not_called()
Ejemplo n.º 10
0
    def test_snap_containerized_inject_snap_socket_error(
            self, mock_makedirs, mock_rmtree, mock_is_snap):
        mock_is_snap.side_effect = lambda: True
        fake_snapd = fixture_setup.FakeSnapd()
        self.useFixture(fake_snapd)
        fake_snapd.session_request_mock.side_effect = (
            requests.exceptions.ConnectionError(
                'Connection aborted.',
                FileNotFoundError(2, 'No such file or directory')))
        self.useFixture(fixture_setup.FakeLXD())
        self.useFixture(
            fixtures.EnvironmentVariable('SNAPCRAFT_CONTAINER_BUILDS', '1'))
        self.make_snapcraft_yaml()

        self.assertIn(
            'Error connecting to ',
            str(self.assertRaises(SnapdError, self.run_command, ['snap'])))
        # Temporary folder should remain in case of failure
        mock_rmtree.assert_not_called()
Ejemplo n.º 11
0
    def setUp(self):
        super().setUp()
        original_check_output = subprocess.check_output

        def fake_uname(cmd, *args, **kwargs):
            if 'uname' in cmd:
                return 'Linux test uname 4.10 x86_64'.encode(
                    sys.getfilesystemencoding())
            else:
                return original_check_output(cmd, *args, **kwargs)
        check_output_patcher = mock.patch(
            'subprocess.check_output', side_effect=fake_uname)
        check_output_patcher.start()
        self.addCleanup(check_output_patcher.stop)

        self.fake_apt_cache = fixture_setup.FakeAptCache()
        self.useFixture(self.fake_apt_cache)

        self.fake_snapd = fixture_setup.FakeSnapd()
        self.useFixture(self.fake_snapd)
        self.fake_snapd.snaps_result = []
Ejemplo n.º 12
0
    def test_inject_snap_already_installed(self,
                                           mock_is_snap,
                                           mock_container_run):
        mock_is_snap.side_effect = lambda: True
        mock_container_run.side_effect = lambda cmd, **kwargs: cmd

        def call_effect(*args, **kwargs):
            if args[0][:2] == ['lxc', 'exec']:
                if 'readlink' in args[0]:
                    if args[0][-1].endswith('/current'):
                        return '123\n'.encode('utf-8')
                if 'sha384sum' in args[0]:
                    if args[0][-1].endswith('core_123.snap'):
                        return 'deadbeef {}'.format(args[0][1]).encode('utf-8')
                    return 'abcdef {}'.format(args[0][1]).encode('utf-8')
            return default_side_effect(*args, **kwargs)

        default_side_effect = self.fake_lxd.check_output_mock.side_effect
        self.fake_lxd.check_output_mock.side_effect = call_effect

        fake_snapd = fixture_setup.FakeSnapd()
        self.useFixture(fake_snapd)
        fake_snapd.snaps_result = [
            {'name': 'core',
             'confinement': 'strict',
             'id': '2kkitQurgOkL3foImG4wDwn9CIANuHlt',
             'channel': 'stable',
             'revision': '123'},
            {'name': 'snapcraft',
             'confinement': 'classic',
             'id': '',
             'channel': 'edge',
             'revision': '123'},
        ]

        builder = self.make_containerbuild()

        builder.execute()
        if hasattr(self, 'cross') and self.cross:
            mock_container_run.assert_has_calls([
                call(['snap', 'install', 'core', '--channel', 'stable']),
                call(['snap', 'refresh', 'core', '--channel', 'stable']),
                call(['snap', 'install', 'snapcraft', '--channel', 'edge',
                      '--classic']),
                call(['snap', 'refresh', 'snapcraft', '--channel', 'edge',
                      '--classic']),
            ])
            return

        tmp_dir = self.fake_filesystem.tmp_dir
        self.fake_lxd.check_call_mock.assert_has_calls([
            call(['lxc', 'file', 'push',
                  os.path.join(tmp_dir, 'snapcraft_123.assert'),
                  '{}/run/snapcraft_123.assert'.format(self.fake_lxd.name)]),
            call(['lxc', 'file', 'push',
                  os.path.join(tmp_dir, 'snapcraft_123.snap'),
                  '{}/run/snapcraft_123.snap'.format(self.fake_lxd.name)]),
        ])
        mock_container_run.assert_has_calls([
            call(['apt-get', 'install', 'squashfuse', '-y']),
            call(['snap', 'ack', '/run/snapcraft_123.assert']),
            call(['snap', 'install', '/run/snapcraft_123.snap', '--classic']),
        ])
Ejemplo n.º 13
0
    def setUp(self):
        super().setUp()

        self.fake_snapd = fixture_setup.FakeSnapd()
        self.useFixture(self.fake_snapd)