Exemplo n.º 1
0
    def test_snap_containerized_remote(self,
                                       mock_pipe,
                                       mock_container_run):
        mock_container_run.side_effect = lambda cmd, **kwargs: cmd
        mock_pipe.return_value = (9, 9)
        fake_lxd = fixture_setup.FakeLXD()
        self.useFixture(fake_lxd)
        fake_filesystem = fixture_setup.FakeFilesystem()
        self.useFixture(fake_filesystem)
        fake_logger = fixtures.FakeLogger(level=logging.INFO)
        self.useFixture(fake_logger)
        self.useFixture(fixtures.EnvironmentVariable(
            'SNAPCRAFT_CONTAINER_BUILDS', 'myremote'))
        self.make_snapcraft_yaml()

        result = self.run_command(['--debug', 'snap'])

        self.assertThat(result.exit_code, Equals(0))

        source = os.path.realpath(os.path.curdir)
        self.assertThat(fake_logger.output, Contains(
            "Using LXD remote 'myremote' from SNAPCRAFT_CONTAINER_BUILDS"))

        project_folder = '/root/build_snap-test'
        mock_container_run.assert_has_calls([
            call(['apt-get', 'install', '-y', 'sshfs']),
        ])
        fake_lxd.popen_mock.assert_has_calls([
            call(['/usr/lib/sftp-server'],
                 stdin=9, stdout=9),
            call(['lxc', 'exec', fake_lxd.name, '--',
                  'sshfs', '-o', 'slave', '-o', 'nonempty',
                  ':{}'.format(source), project_folder],
                 stdin=9, stdout=9),
        ])
Exemplo n.º 2
0
    def test_snap_containerized_exists_running(self,
                                               mock_getuid,
                                               mock_container_run):
        self.useFixture(
            fixtures.EnvironmentVariable('SUDO_UID', self.SUDO_UID))
        mock_getuid.return_value = self.getuid
        fake_lxd = fixture_setup.FakeLXD()
        self.useFixture(fake_lxd)
        # Container was created before and is running
        fake_lxd.name = 'local:snapcraft-snap-test'
        fake_lxd.status = 'Running'
        self.useFixture(fixtures.EnvironmentVariable(
                'SNAPCRAFT_CONTAINER_BUILDS', '1'))
        self.make_snapcraft_yaml()

        self.run_command(['snap'])

        source = os.path.realpath(os.path.curdir)
        project_folder = '/root/build_snap-test'
        fake_lxd.check_call_mock.assert_has_calls([
            call(['lxc', 'config', 'device', 'add', fake_lxd.name,
                  project_folder, 'disk', 'source={}'.format(source),
                  'path={}'.format(project_folder)]),
            call(['lxc', 'stop', '-f', fake_lxd.name]),
        ])
        mock_container_run.assert_has_calls([
            call(['python3', '-c', 'import urllib.request; ' +
                  'urllib.request.urlopen(' +
                  '"http://start.ubuntu.com/connectivity-check.html"' +
                  ', timeout=5)']),
            call(['snapcraft', 'snap', '--output',
                  'snap-test_1.0_amd64.snap'],
                 cwd=project_folder),
        ])
Exemplo n.º 3
0
    def test_cleanbuild(self):
        self.useFixture(fixture_setup.FakeLXD())

        result = self.run_command(['cleanbuild'])

        self.assertThat(result.exit_code, Equals(0))
        self.assertIn(
            'Setting up container with project assets\n'
            'Retrieved snap-test_1.0_amd64.snap\n',
            self.fake_logger.output)

        with tarfile.open('snap-test_1.0_source.tar.bz2') as tar:
            tar_members = tar.getnames()

        for f in self.files_no_tar:
            f = os.path.relpath(f)
            self.assertFalse('./{}'.format(f) in tar_members,
                             '{} should not be in {}'.format(f, tar_members))
        for f in self.files_tar:
            f = os.path.relpath(f)
            self.assertTrue('./{}'.format(f) in tar_members,
                            '{} should be in {}'.format(f, tar_members))

        # Also assert that the snapcraft.yaml made it into the cleanbuild tar
        self.assertThat(
            tar_members,
            Contains(os.path.join('.', 'snap', 'snapcraft.yaml')),
            'snap/snapcraft unexpectedly excluded from tarball')
Exemplo n.º 4
0
    def test_invalid_remote(self):
        fake_lxd = fixture_setup.FakeLXD()
        self.useFixture(fake_lxd)

        exception = self.assertRaises(InvalidContainerRemoteError,
                                      self.run_command,
                                      ['cleanbuild', '--remote', 'foo/bar'])
        self.assertThat(exception.remote, Equals('foo/bar'))
Exemplo n.º 5
0
    def test_snap_containerized(self, mock_getuid):
        mock_getuid.return_value = 1234
        fake_lxd = fixture_setup.FakeLXD()
        self.useFixture(fake_lxd)
        fake_logger = fixtures.FakeLogger(level=logging.INFO)
        self.useFixture(fake_logger)
        self.useFixture(
            fixtures.EnvironmentVariable('SNAPCRAFT_CONTAINER_BUILDS', '1'))
        self.make_snapcraft_yaml()

        result = self.run_command(['snap'])

        self.assertThat(result.exit_code, Equals(0))

        source = os.path.realpath(os.path.curdir)
        self.assertIn(
            'Mounting {} into container\n'
            'Waiting for a network connection...\n'
            'Network connection established\n'.format(source),
            fake_logger.output)

        container_name = 'local:snapcraft-snap-test'
        project_folder = '/root/build_snap-test'
        fake_lxd.check_call_mock.assert_has_calls([
            call(['lxc', 'init', 'ubuntu:xenial/amd64', container_name]),
            call([
                'lxc', 'config', 'set', container_name,
                'environment.SNAPCRAFT_SETUP_CORE', '1'
            ]),
            call([
                'lxc', 'config', 'set', container_name, 'raw.idmap',
                'both {} 0'.format(mock_getuid.return_value)
            ]),
            call(['lxc', 'start', container_name]),
            call([
                'lxc', 'config', 'device', 'add', container_name,
                project_folder, 'disk', 'source={}'.format(source),
                'path=/{}'.format(project_folder)
            ]),
            call([
                'lxc', 'exec', container_name, '--', 'python3', '-c',
                'import urllib.request; '
                'urllib.request.urlopen('
                '"http://start.ubuntu.com/connectivity-check.html", '
                'timeout=5)'
            ]),
            call(['lxc', 'exec', container_name, '--', 'apt-get', 'update']),
            call([
                'lxc', 'exec', container_name, '--', 'apt-get', 'install',
                'snapcraft', '-y'
            ]),
            call([
                'lxc', 'exec', container_name, '--', 'bash', '-c',
                'cd {}; snapcraft snap --output {}'.format(
                    project_folder, 'snap-test_1.0_amd64.snap')
            ]),
            call(['lxc', 'stop', '-f', container_name]),
        ])
Exemplo n.º 6
0
    def test_snap_containerized_exists_stopped(self, mock_inject,
                                               mock_container_run,
                                               mock_getuid):
        mock_container_run.side_effect = lambda cmd, **kwargs: cmd
        mock_getuid.return_value = 1234
        fake_lxd = fixture_setup.FakeLXD()
        self.useFixture(fake_lxd)
        # Container was created before, and isn't running
        fake_lxd.devices = '{"/root/build_snap-test":[]}'
        fake_lxd.name = 'local:snapcraft-snap-test'
        fake_lxd.status = 'Stopped'
        fake_logger = fixtures.FakeLogger(level=logging.INFO)
        self.useFixture(fake_logger)
        self.useFixture(
            fixtures.EnvironmentVariable('SNAPCRAFT_CONTAINER_BUILDS', '1'))
        self.make_snapcraft_yaml()

        result = self.run_command(['snap'])

        self.assertThat(result.exit_code, Equals(0))

        source = os.path.realpath(os.path.curdir)
        self.assertIn(
            'Mounting {} into container\n'
            'Waiting for a network connection...\n'
            'Network connection established\n'.format(source),
            fake_logger.output)

        container_name = 'local:snapcraft-snap-test'
        project_folder = '/root/build_snap-test'
        fake_lxd.check_call_mock.assert_has_calls([
            call([
                'lxc', 'config', 'set', container_name,
                'environment.SNAPCRAFT_SETUP_CORE', '1'
            ]),
            call([
                'lxc', 'config', 'set', container_name, 'raw.idmap',
                'both {} 0'.format(mock_getuid.return_value)
            ]),
            call([
                'lxc', 'config', 'device', 'remove', container_name,
                project_folder
            ]),
            call(['lxc', 'start', container_name]),
            call(['lxc', 'stop', '-f', container_name]),
        ])
        mock_container_run.assert_has_calls([
            call([
                'python3', '-c',
                'import urllib.request; ' + 'urllib.request.urlopen(' +
                '"http://start.ubuntu.com/connectivity-check.html"' +
                ', timeout=5)'
            ]),
            call(['apt-get', 'update']),
            call(['snapcraft', 'snap', '--output', 'snap-test_1.0_amd64.snap'],
                 cwd=project_folder),
        ])
Exemplo n.º 7
0
    def test_invalid_remote(self):
        fake_lxd = fixture_setup.FakeLXD()
        self.useFixture(fake_lxd)

        self.assertIn(
            "'foo/bar' is not a valid LXD remote name",
            str(self.assertRaises(
                InvalidContainerRemoteError,
                self.run_command, ['cleanbuild', '--remote', 'foo/bar'])))
Exemplo n.º 8
0
    def test_refresh_fails_without_env_var(self, mock_container_run):
        mock_container_run.side_effect = lambda cmd, **kwargs: cmd
        fake_lxd = fixture_setup.FakeLXD()
        self.useFixture(fake_lxd)
        fake_filesystem = fixture_setup.FakeFilesystem()
        self.useFixture(fake_filesystem)
        self.make_snapcraft_yaml()

        self.assertRaises(SnapcraftEnvironmentError, self.run_command,
                          ['refresh'])
        mock_container_run.assert_not_called()
Exemplo n.º 9
0
    def setUp(self):
        super().setUp()
        self.fake_lxd = fixture_setup.FakeLXD()
        self.useFixture(self.fake_lxd)
        self.fake_lxd.kernel_arch = self.server
        self.fake_filesystem = fixture_setup.FakeFilesystem()
        self.useFixture(self.fake_filesystem)

        self.fake_logger = fixtures.FakeLogger(level=logging.INFO)
        self.useFixture(self.fake_logger)
        self.project_options = ProjectOptions(target_deb_arch=self.target_arch)
Exemplo n.º 10
0
    def test_clean_containerized_noop(self):
        fake_lxd = fixture_setup.FakeLXD()
        self.useFixture(fake_lxd)
        self.useFixture(fixtures.EnvironmentVariable(
                'SNAPCRAFT_CONTAINER_BUILDS', '1'))
        self.make_snapcraft_yaml(n=3)

        result = self.run_command(['clean'])

        self.assertThat(result.exit_code, Equals(0))
        # clean should be a noop if no container exists yet/ anymore
        fake_lxd.check_call_mock.assert_not_called()
Exemplo n.º 11
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)])
Exemplo n.º 12
0
    def test_clean_containerized_with_part(self):
        fake_lxd = fixture_setup.FakeLXD()
        self.useFixture(fake_lxd)
        self.useFixture(fixtures.EnvironmentVariable(
                'SNAPCRAFT_CONTAINER_BUILDS', '1'))
        self.make_snapcraft_yaml(n=3)

        result = self.run_command(['clean', 'clean1'])

        self.assertThat(result.exit_code, Equals(0))
        # clean with parts should NOT delete the container
        self.assertNotEqual(fake_lxd.check_call_mock.call_args,
                            call(['lxc', 'delete', '-f', fake_lxd.name]))
Exemplo n.º 13
0
    def test_snap_containerized_inject_apt(self,
                                           mock_is_snap, mock_container_run):
        mock_is_snap.side_effect = lambda: False
        mock_container_run.side_effect = lambda cmd, **kwargs: cmd
        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'])
        mock_container_run.assert_has_calls([
            call(['apt-get', 'install', 'snapcraft', '-y']),
        ])
Exemplo n.º 14
0
    def test_clean_containerized_with_part(self):
        fake_lxd = fixture_setup.FakeLXD()
        fake_lxd.name = 'local:snapcraft-clean-test'
        fake_lxd.status = 'Stopped'
        self.useFixture(fake_lxd)
        self.useFixture(
            fixtures.EnvironmentVariable('SNAPCRAFT_CONTAINER_BUILDS',
                                         self.snapcraft_container_builds))
        self.make_snapcraft_yaml(n=3)

        result = self.run_command(['clean', 'clean1'])

        self.assertThat(result.exit_code, Equals(0))
        # clean with parts should NOT delete the container
        fake_lxd.check_call_mock.assert_not_called()
Exemplo n.º 15
0
    def test_clean_containerized(self):
        fake_lxd = fixture_setup.FakeLXD()
        self.useFixture(fake_lxd)
        self.useFixture(
            fixtures.EnvironmentVariable('SNAPCRAFT_CONTAINER_BUILDS', '1'))
        self.make_snapcraft_yaml(n=3)

        result = self.run_command(['clean'])

        self.assertThat(result.exit_code, Equals(0))
        container_name = 'local:snapcraft-clean-test'
        # clean with no parts should delete the container
        fake_lxd.check_call_mock.assert_has_calls([
            call(['lxc', 'delete', '-f', container_name]),
        ])
Exemplo n.º 16
0
    def test_cleanbuild_debug_prepended_goes_to_shell_on_errors(self):
        fake_lxd = fixture_setup.FakeLXD()
        self.useFixture(fake_lxd)

        def call_effect(*args, **kwargs):
            # Fail on an actual snapcraft command and not the command
            # for the installation of it.
            if 'snapcraft snap' in ' '.join(args[0]):
                raise subprocess.CalledProcessError(
                    returncode=255, cmd=args[0])

        fake_lxd.check_call_mock.side_effect = call_effect

        result = self.run_command(['--debug', 'cleanbuild'])
        self.assertThat(result.exit_code, Equals(0))
        self.assertThat(self.fake_logger.output, Contains(
            'Debug mode enabled, dropping into a shell'))
Exemplo n.º 17
0
    def test_refresh(self, mock_container_run):
        mock_container_run.side_effect = lambda cmd, **kwargs: cmd
        fake_lxd = fixture_setup.FakeLXD()
        self.useFixture(fake_lxd)
        fake_filesystem = fixture_setup.FakeFilesystem()
        self.useFixture(fake_filesystem)
        self.useFixture(
            fixtures.EnvironmentVariable('SNAPCRAFT_CONTAINER_BUILDS', '1'))
        self.make_snapcraft_yaml()

        self.run_command(['refresh'])

        mock_container_run.assert_has_calls([
            call(['apt-get', 'update']),
            call(['apt-get', 'upgrade', '-y']),
            call(['snap', 'refresh']),
        ])
Exemplo n.º 18
0
    def test_clean_containerized_pull_retains_container(
            self, mock_lifecycle_clean):
        fake_lxd = fixture_setup.FakeLXD()
        self.useFixture(fake_lxd)
        # Container was created before, and isn't running
        fake_lxd.name = 'local:snapcraft-clean-test'
        fake_lxd.status = 'Stopped'
        self.useFixture(
            fixtures.EnvironmentVariable('SNAPCRAFT_CONTAINER_BUILDS', '1'))
        self.make_snapcraft_yaml(n=3)

        result = self.run_command(['clean', '-s', 'pull'])

        self.assertThat(result.exit_code, Equals(0))
        # clean pull should NOT delete the container
        fake_lxd.check_call_mock.assert_not_called()
        # clean should be called normally, outside of the container
        mock_lifecycle_clean.assert_has_calls([call(ANY, (), 'pull')])
Exemplo n.º 19
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()
Exemplo n.º 20
0
    def test_snap_containerized_invalid_remote(self, mock_open, mock_makedirs,
                                               mock_rmtree,
                                               mock_container_run):
        mock_container_run.side_effect = lambda cmd, **kwargs: cmd
        mock_open.return_value = mock.MagicMock(spec=open)
        fake_lxd = fixture_setup.FakeLXD()
        self.useFixture(fake_lxd)

        fake_logger = fixtures.FakeLogger(level=logging.INFO)
        self.useFixture(fake_logger)
        self.useFixture(
            fixtures.EnvironmentVariable('SNAPCRAFT_CONTAINER_BUILDS',
                                         'foo/bar'))
        self.make_snapcraft_yaml()

        exception = self.assertRaises(
            snapcraft.internal.errors.InvalidContainerRemoteError,
            self.run_command, ['--debug', 'snap'])
        self.assertThat(exception.remote, Equals('foo/bar'))
Exemplo n.º 21
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()
Exemplo n.º 22
0
    def test_clean_containerized_exists_stopped(self):
        fake_lxd = fixture_setup.FakeLXD()
        self.useFixture(fake_lxd)
        # Container was created before, and isn't running
        fake_lxd.name = 'local:snapcraft-clean-test'
        fake_lxd.status = 'Stopped'
        self.useFixture(fixtures.EnvironmentVariable(
                'SNAPCRAFT_CONTAINER_BUILDS', '1'))
        self.make_snapcraft_yaml(n=3)

        result = self.run_command(['clean'])

        self.assertThat(result.exit_code, Equals(0))
        # clean with no parts should delete the container
        fake_lxd.check_call_mock.assert_has_calls([
            call(['lxc', 'delete', '-f', fake_lxd.name]),
        ])
        # no other commands should be run in the container
        self.assertEquals(fake_lxd.check_call_mock.call_count, 1)
Exemplo n.º 23
0
    def test_update_containerized_exists_running(self, mock_getuid,
                                                 mock_container_run):
        mock_container_run.side_effect = lambda cmd, **kwargs: cmd
        mock_getuid.return_value = 1234
        fake_lxd = fixture_setup.FakeLXD()
        self.useFixture(fake_lxd)
        # Container was created before and is running
        fake_lxd.name = 'local:snapcraft-snap-test'
        fake_lxd.status = 'Running'
        self.useFixture(
            fixtures.EnvironmentVariable('SNAPCRAFT_CONTAINER_BUILDS', '1'))
        self.make_snapcraft_yaml(self.yaml_template)

        result = self.run_command(['update'])
        self.assertThat(result.exit_code, Equals(0))

        project_folder = '/root/build_snap-test'
        mock_container_run.assert_has_calls([
            call(['snapcraft', 'update'], cwd=project_folder),
        ])
Exemplo n.º 24
0
    def test_clean_containerized_exists_stopped(self, mock_lifecycle_clean):
        fake_lxd = fixture_setup.FakeLXD()
        self.useFixture(fake_lxd)
        # Container was created before, and isn't running
        fake_lxd.name = '{}:snapcraft-clean-test'.format(self.remote)
        fake_lxd.status = 'Stopped'
        self.useFixture(
            fixtures.EnvironmentVariable('SNAPCRAFT_CONTAINER_BUILDS',
                                         self.snapcraft_container_builds))
        self.make_snapcraft_yaml(n=3)

        result = self.run_command(['clean'])

        self.assertThat(result.exit_code, Equals(0))
        # clean with no parts should delete the container
        fake_lxd.check_call_mock.assert_has_calls([
            call(['lxc', 'delete', '-f', fake_lxd.name]),
        ])
        # no other commands should be run in the container
        self.assertThat(fake_lxd.check_call_mock.call_count, Equals(1))
        # clean should be called normally, outside of the container
        mock_lifecycle_clean.assert_has_calls([call(ANY, (), 'pull')])
Exemplo n.º 25
0
    def test_snap_containerized(self, mock_inject, mock_container_run,
                                mock_getuid):
        self.useFixture(fixtures.EnvironmentVariable('SUDO_UID',
                                                     self.SUDO_UID))
        mock_getuid.return_value = self.getuid
        mock_container_run.side_effect = lambda cmd, **kwargs: cmd
        fake_lxd = fixture_setup.FakeLXD()
        self.useFixture(fake_lxd)
        fake_logger = fixtures.FakeLogger(level=logging.INFO)
        self.useFixture(fake_logger)
        self.useFixture(
            fixtures.EnvironmentVariable('SNAPCRAFT_CONTAINER_BUILDS', '1'))
        self.make_snapcraft_yaml()

        result = self.run_command(['snap'])

        self.assertThat(result.exit_code, Equals(0))

        source = os.path.realpath(os.path.curdir)
        self.assertIn(
            'Using default LXD remote because '
            'SNAPCRAFT_CONTAINER_BUILDS is set to 1\n'
            'Waiting for a network connection...\n'
            'Network connection established\n'
            'Mounting {} into container\n'.format(source), fake_logger.output)

        container_name = 'local:snapcraft-snap-test'
        project_folder = '/root/build_snap-test'
        fake_lxd.check_call_mock.assert_has_calls([
            call(['lxc', 'init', 'ubuntu:xenial', container_name]),
            call([
                'lxc', 'config', 'set', container_name,
                'environment.SNAPCRAFT_SETUP_CORE', '1'
            ]),
            call([
                'lxc', 'config', 'set', container_name, 'environment.LC_ALL',
                'C.UTF-8'
            ]),
            call([
                'lxc', 'config', 'set', container_name,
                'environment.SNAPCRAFT_IMAGE_INFO',
                '{"fingerprint": "test-fingerprint", '
                '"architecture": "test-architecture", '
                '"created_at": "test-created-at"}'
            ]),
            call([
                'lxc', 'config', 'set', container_name, 'raw.idmap',
                'both {} {}'.format(self.expected_idmap, self.getuid)
            ]),
            call([
                'lxc', 'config', 'device', 'add', container_name, 'fuse',
                'unix-char', 'path=/dev/fuse'
            ]),
            call(['lxc', 'start', container_name]),
            call([
                'lxc', 'config', 'device', 'add', container_name,
                project_folder, 'disk', 'source={}'.format(source),
                'path={}'.format(project_folder)
            ]),
            call(['lxc', 'stop', '-f', container_name]),
        ])
        mock_container_run.assert_has_calls([
            call([
                'python3', '-c',
                'import urllib.request; ' + 'urllib.request.urlopen(' +
                '"http://start.ubuntu.com/connectivity-check.html"' +
                ', timeout=5)'
            ]),
            call(['apt-get', 'update']),
            call(['apt-get', 'install', 'squashfuse', '-y']),
            call(['snapcraft', 'snap', '--output', 'snap-test_1.0_amd64.snap'],
                 cwd=project_folder,
                 user='******'),
        ])
Exemplo n.º 26
0
    def test_snap_containerized_exists_stopped(self, mock_inject,
                                               mock_container_run,
                                               mock_getuid):

        self.useFixture(fixtures.EnvironmentVariable('SUDO_UID',
                                                     self.SUDO_UID))
        mock_getuid.return_value = self.getuid
        mock_container_run.side_effect = lambda cmd, **kwargs: cmd
        fake_lxd = fixture_setup.FakeLXD()
        self.useFixture(fake_lxd)
        # Container was created before, and isn't running
        fake_lxd.devices = '{"/root/build_snap-test":[]}'
        fake_lxd.name = 'local:snapcraft-snap-test'
        fake_lxd.status = 'Stopped'
        fake_logger = fixtures.FakeLogger(level=logging.INFO)
        self.useFixture(fake_logger)
        self.useFixture(
            fixtures.EnvironmentVariable('SNAPCRAFT_CONTAINER_BUILDS', '1'))
        self.make_snapcraft_yaml()

        result = self.run_command(['snap'])

        self.assertThat(result.exit_code, Equals(0))

        self.assertIn(
            'Waiting for a network connection...\n'
            'Network connection established\n', fake_logger.output)

        container_name = 'local:snapcraft-snap-test'
        project_folder = '/root/build_snap-test'
        fake_lxd.check_call_mock.assert_has_calls([
            call([
                'lxc', 'config', 'set', container_name,
                'environment.SNAPCRAFT_SETUP_CORE', '1'
            ]),
            call([
                'lxc', 'config', 'set', container_name, 'environment.LC_ALL',
                'C.UTF-8'
            ]),
            call([
                'lxc', 'config', 'set', container_name,
                'environment.SNAPCRAFT_IMAGE_INFO',
                '{"fingerprint": "test-fingerprint", '
                '"architecture": "test-architecture", '
                '"created_at": "test-created-at"}'
            ]),
            call([
                'lxc', 'config', 'set', container_name, 'raw.idmap',
                'both {} {}'.format(self.expected_idmap, self.getuid)
            ]),
            call([
                'lxc', 'config', 'device', 'remove', container_name,
                project_folder
            ]),
            call([
                'lxc', 'config', 'device', 'add', container_name, 'fuse',
                'unix-char', 'path=/dev/fuse'
            ]),
            call(['lxc', 'start', container_name]),
            call(['lxc', 'stop', '-f', container_name]),
        ])
        mock_container_run.assert_has_calls([
            call([
                'python3', '-c',
                'import urllib.request; ' + 'urllib.request.urlopen(' +
                '"http://start.ubuntu.com/connectivity-check.html"' +
                ', timeout=5)'
            ]),
            call(['snapcraft', 'snap', '--output', 'snap-test_1.0_amd64.snap'],
                 cwd=project_folder,
                 user='******'),
        ])
        # Ensure there's no unexpected calls eg. two network checks
        self.assertThat(mock_container_run.call_count, Equals(2))
Exemplo n.º 27
0
 def test_cleanbuild(self):
     fake_logger = fixtures.FakeLogger(level=logging.INFO)
     self.useFixture(fake_logger)
     self.useFixture(fixture_setup.FakeLXD())