示例#1
0
    def testGetPath(self):
        """Tests that GetPath does nothing."""
        command = staging.NoopCommand()

        result = command.GetPath()

        self.assertIsNone(result, 'NoopCommand has no associated path.')
示例#2
0
    def testEnsureInstalled(self):
        """Tests that EnsureInstalled does nothing."""
        command = staging.NoopCommand()

        command.EnsureInstalled()

        self.assertEqual(os.listdir(self.staging_area), [],
                         'NoopCommand should not modify the staging area.')
        self.assertEqual(os.listdir(self.app_dir), [],
                         'NoopCommand should not modify the app directory.')
示例#3
0
 def testRegistry_DefaultMappings(self):
     """Ensures the default registry works is what it was originally set to."""
     self._MockDefaultRegistries()
     expected = {
         ('intercal', env.FLEX): 'fake-intercal-command',
         ('x86-asm', env.STANDARD): 'fake-x86-asm-command',
         ('chicken', env.STANDARD): staging.NoopCommand(),
     }
     registry = staging.GetRegistry()
     for key, value in expected.items():
         self.assertEqual(registry.Get(*key), value)
示例#4
0
    def testRun(self):
        """Tests that Run does nothing."""
        command = staging.NoopCommand()

        result = command.Run(self.staging_area, self.descriptor, self.app_dir)

        self.assertIsNone(result)
        self.assertEqual(os.listdir(self.staging_area), [],
                         'NoopCommand should not modify the staging area.')
        self.assertEqual(os.listdir(self.app_dir), [],
                         'NoopCommand should not modify the app directory.')
示例#5
0
class StagerRealExecutableTest(sdk_test_base.WithLogCapture):
    """Tests the staging code using real executables."""

    _REGISTRY = runtime_registry.Registry(
        {
            runtime_registry.RegistryEntry('success', {env.FLEX}):
            staging._BundledCommand('success.sh', 'success.cmd'),
            runtime_registry.RegistryEntry('failure', {env.FLEX}):
            staging._BundledCommand('failure.sh', 'failure.cmd')
        },
        default=staging.NoopCommand())

    _SUCCESS_OUTPUT_PATTERN = (r'-+ STDOUT -+\n'
                               r'out\n'
                               r'-+ STDERR -+\n'
                               r'service-yaml path: app.yaml\n'
                               r'app-dir path: .\n'
                               r'-+')

    _FAILURE_PATTERN = (r'Staging command '
                        r'\[\S+failure.(?:sh|cmd) app.yaml . \S+\] '
                        r'failed with return code \[1\].\n\n'
                        r'-+ STDOUT -+\n'
                        r'out\n'
                        r'-+ STDERR -+\n'
                        r'service.yaml path: app.yaml\n'
                        r'app-dir path: .\n'
                        r'-+')

    def SetUp(self):
        scripts_dir = self.Resource('tests', 'unit', 'command_lib', 'app',
                                    'testdata', 'scripts')
        self.StartObjectPatch(config.Paths,
                              'sdk_root',
                              new_callable=mock.PropertyMock,
                              return_value=scripts_dir)
        self.staging_area = tempfile.mkdtemp()
        self.stager = staging.Stager(self._REGISTRY, self.staging_area)

    def testStage_Success(self):
        app_dir = self.stager.Stage('app.yaml', '.', 'success', env.FLEX)
        self.AssertFileExistsWithContents('app.yaml contents\n',
                                          os.path.join(app_dir, 'app.yaml'))
        self.AssertLogMatches(self._SUCCESS_OUTPUT_PATTERN)

    def testStage_Failure(self):
        with self.assertRaisesRegex(staging.StagingCommandFailedError,
                                    self._FAILURE_PATTERN):
            self.stager.Stage('app.yaml', '.', 'failure', env.FLEX)
示例#6
0
class StagerMockExecTest(sdk_test_base.WithLogCapture):
    """Tests the staging code by mocking executables."""

    _REGISTRY = runtime_registry.Registry(
        {
            runtime_registry.RegistryEntry('intercal', {env.FLEX}):
            staging._BundledCommand('intercal-flex', 'intercal-flex.exe'),
            runtime_registry.RegistryEntry('x86-asm', {env.STANDARD}):
            staging._BundledCommand('x86-asm-standard', 'x86-asm-standard.exe',
                                    'app-engine-x86-asm'),
            runtime_registry.RegistryEntry('german', {env.STANDARD}):
            staging._BundledCommand('german-standard',
                                    'german-standard.exe',
                                    mapper=_GermanMapper),
        },
        default=staging.NoopCommand())

    _OUTPUT_PATTERN = (r'-+ STDOUT -+\n'
                       r'out\n'
                       r'-+ STDERR -+\n'
                       r'err\n'
                       r'-+')

    _SUCCESS_MESSAGE = 'Executing staging command: [{command}]'

    _ERROR_MESSAGE = (
        'Staging command [{command}] failed with return code [{code}].')

    def SetUp(self):
        self.staging_area = '/staging-area'
        self.stager = staging.Stager(self._REGISTRY, self.staging_area)
        self.exec_mock = self.StartObjectPatch(execution_utils,
                                               'Exec',
                                               side_effect=_FakeExec())
        self.sdk_root_mock = self.StartPropertyPatch(
            config.Paths, 'sdk_root', return_value='sdk_root_dir')
        self.mkdtemp_mock = self.StartObjectPatch(tempfile,
                                                  'mkdtemp',
                                                  autospec=True,
                                                  return_value='tmp_dir')
        self.StartObjectPatch(platforms.OperatingSystem,
                              'Current',
                              return_value=platforms.OperatingSystem.LINUX)

    def testStage_StagingDir(self):
        """Ensures that the staging dir is created properly."""
        app_dir = self.stager.Stage('app.yaml', 'dir', 'intercal', env.FLEX)
        self.assertEqual(app_dir, 'tmp_dir')
        self.mkdtemp_mock.assert_called_once_with(dir='/staging-area')

    def testStage_MismatchedRuntime(self):
        self.assertIsNone(
            self.stager.Stage('app.yaml', 'dir', 'intercal', env.STANDARD))
        self.AssertOutputEquals('')
        self.AssertLogNotContains('err')  # Log will have debug messages
        self.exec_mock.assert_not_called()
        self.mkdtemp_mock.assert_not_called()

    def testStage_MismatchedEnvironment(self):
        self.assertIsNone(
            self.stager.Stage('app.yaml', 'dir', 'intercal', env.STANDARD))
        self.AssertOutputEquals('')
        self.AssertLogNotContains('err')  # Log will have debug messages
        self.exec_mock.assert_not_called()
        self.mkdtemp_mock.assert_not_called()

    def testStage_NoSdkRoot(self):
        self.sdk_root_mock.return_value = None
        with self.assertRaises(staging.NoSdkRootError):
            self.stager.Stage('app.yaml', 'dir', 'intercal', env.FLEX)
        self.exec_mock.assert_not_called()

    def testStage_StagingCommandFailed(self):
        self.exec_mock.side_effect = _FakeExec(return_code=1)
        args = [
            os.path.join('sdk_root_dir', 'intercal-flex'), 'app.yaml', 'dir',
            'tmp_dir'
        ]
        command = ' '.join(args)
        expected_pattern = (
            re.escape(self._ERROR_MESSAGE.format(command=command, code=1)) +
            '\n\n' + self._OUTPUT_PATTERN)
        with self.assertRaisesRegex(staging.StagingCommandFailedError,
                                    expected_pattern):
            self.stager.Stage('app.yaml', 'dir', 'intercal', env.FLEX)
        self.exec_mock.assert_called_once_with(args,
                                               no_exit=True,
                                               out_func=mock.ANY,
                                               err_func=mock.ANY)

    def testStage_Success(self):
        args = [
            os.path.join('sdk_root_dir', 'intercal-flex'), 'app.yaml', 'dir',
            'tmp_dir'
        ]
        self.stager.Stage('app.yaml', 'dir', 'intercal', env.FLEX)
        self.exec_mock.assert_called_once_with(args,
                                               no_exit=True,
                                               out_func=mock.ANY,
                                               err_func=mock.ANY)
        command = ' '.join(args)
        self.AssertLogMatches(
            re.escape(self._SUCCESS_MESSAGE.format(command=command)))
        self.AssertLogMatches(self._OUTPUT_PATTERN)
        self.mkdtemp_mock.assert_called_once_with(dir='/staging-area')

    def testStage_SuccessWindows(self):
        self.StartObjectPatch(platforms.OperatingSystem,
                              'Current',
                              return_value=platforms.OperatingSystem.WINDOWS)
        args = [
            os.path.join('sdk_root_dir', 'intercal-flex.exe'), 'app.yaml',
            'dir', 'tmp_dir'
        ]
        self.stager.Stage('app.yaml', 'dir', 'intercal', env.FLEX)
        self.exec_mock.assert_called_once_with(args,
                                               no_exit=True,
                                               out_func=mock.ANY,
                                               err_func=mock.ANY)
        command = ' '.join(args)
        self.AssertLogMatches(
            re.escape(self._SUCCESS_MESSAGE.format(command=command)))
        self.AssertLogMatches(self._OUTPUT_PATTERN)
        self.mkdtemp_mock.assert_called_once_with(dir='/staging-area')

    def testStage_SuccessInstalledComponent(self):
        ensure_installed_mock = self.StartObjectPatch(
            update_manager.UpdateManager, 'EnsureInstalledAndRestart')
        args = [
            os.path.join('sdk_root_dir', 'x86-asm-standard'), 'app.yaml',
            'dir', 'tmp_dir'
        ]
        self.stager.Stage('app.yaml', 'dir', 'x86-asm', env.STANDARD)
        self.exec_mock.assert_called_once_with(args,
                                               no_exit=True,
                                               out_func=mock.ANY,
                                               err_func=mock.ANY)
        command = ' '.join(args)
        self.AssertLogMatches(
            re.escape(self._SUCCESS_MESSAGE.format(command=command)))
        self.AssertLogMatches(self._OUTPUT_PATTERN)
        ensure_installed_mock.assert_called_once_with(['app-engine-x86-asm'],
                                                      msg=mock.ANY)
        self.mkdtemp_mock.assert_called_once_with(dir='/staging-area')

    def testStage_CustomMapper(self):
        """Test that a custom mapper function can be invoked."""
        args = [
            os.path.join('sdk_root_dir', 'german-standard'), '-dir', 'tmp_dir',
            '-yaml', 'app.yaml', 'dir'
        ]
        self.stager.Stage('app.yaml', 'dir', 'german', env.STANDARD)
        self.exec_mock.assert_called_once_with(args,
                                               no_exit=True,
                                               out_func=mock.ANY,
                                               err_func=mock.ANY)
        command = ' '.join(args)
        self.AssertLogMatches(
            re.escape(self._SUCCESS_MESSAGE.format(command=command)))
        self.AssertLogMatches(self._OUTPUT_PATTERN)
        self.mkdtemp_mock.assert_called_once_with(dir='/staging-area')