Beispiel #1
0
 def testGet_NoMatchFound(self):
     default = 'default-value'
     registry = runtime_registry.Registry(self._DEFAULT_REGISTRY,
                                          default=default)
     self.assertEqual(
         registry.Get('bad', env.FLEX), default,
         'A non-matching runtime should always result in the default.')
Beispiel #2
0
    def testGet_DefaultFalse(self):
        registry = runtime_registry.Registry(self._DEFAULT_REGISTRY,
                                             default=False)

        self.assertFalse(
            registry.Get('intercal', env.STANDARD),
            'A matching runtime with an incorrect environment should result in the '
            'default.')
Beispiel #3
0
    def testGet_RightRuntimeWrongEnv(self):
        default = 'default-value'
        registry = runtime_registry.Registry(self._DEFAULT_REGISTRY,
                                             default=default)

        self.assertEqual(
            registry.Get('intercal', env.STANDARD), default,
            'A matching runtime with an incorrect environment should result in the '
            'default.')
Beispiel #4
0
    def testGet_MultipleEnv(self):
        registry = runtime_registry.Registry({
            runtime_registry.RegistryEntry('intercal', {
                env.FLEX, env.STANDARD
            }):
            'intercal-value'
        })

        self.assertEqual(registry.Get('intercal', env.FLEX), 'intercal-value')
        self.assertEqual(registry.Get('intercal', env.STANDARD),
                         'intercal-value')
Beispiel #5
0
    def testGet_Override(self):
        registry = runtime_registry.Registry(
            {
                runtime_registry.RegistryEntry(re.compile(r'.*'), {
                                                   env.FLEX, env.STANDARD
                                               }):
                'dummy'
            },
            override='my-override')

        self.assertEqual(registry.Get('anything', env.FLEX), 'my-override')
        self.assertEqual(registry.Get('anything', env.STANDARD), 'my-override')
Beispiel #6
0
    def testGet_RegexpRuntime(self):
        default = 'default-value'
        registry = runtime_registry.Registry(
            {
                runtime_registry.RegistryEntry(re.compile('pattern[12]$'), {
                                                   env.FLEX
                                               }):
                'pattern-value'
            },
            default=default)

        self.assertEqual(registry.Get('pattern1', env.FLEX), 'pattern-value')
        self.assertEqual(registry.Get('pattern2', env.FLEX), 'pattern-value')
        self.assertEqual(registry.Get('pattern3', env.FLEX), default)
Beispiel #7
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)
Beispiel #8
0
def GetTiRuntimeRegistry():
    """A simple registry whose `Get()` method answers True if runtime is Ti."""
    return runtime_registry.Registry(_TI_RUNTIME_REGISTRY, default=False)
Beispiel #9
0
def _GetGcloudignoreRegistry():
    return runtime_registry.Registry(_GCLOUDIGNORE_REGISTRY, default=False)
Beispiel #10
0
def GetOverrideStager(command, staging_area):
    """Get a stager with a registry that always calls the given command."""
    return Stager(
        runtime_registry.Registry(None,
                                  override=command,
                                  default=NoopCommand()), staging_area)
Beispiel #11
0
def GetNoopStager(staging_area):
    """Get a stager with an empty registry."""
    return Stager(runtime_registry.Registry({}, default=NoopCommand()),
                  staging_area)
Beispiel #12
0
def GetBetaRegistry():
    mappings = _STAGING_REGISTRY.copy()
    mappings.update(_STAGING_REGISTRY_BETA)
    return runtime_registry.Registry(mappings, default=NoopCommand())
Beispiel #13
0
def GetRegistry():
    return runtime_registry.Registry(_STAGING_REGISTRY, default=NoopCommand())
Beispiel #14
0
    def testGet_MatchFound(self):
        registry = runtime_registry.Registry(self._DEFAULT_REGISTRY)

        self.assertEqual(registry.Get('intercal', env.FLEX), 'intercal-value')
Beispiel #15
0
 def testGet_NoMatchFoundNoDefault(self):
     registry = runtime_registry.Registry(self._DEFAULT_REGISTRY)
     self.assertEqual(
         registry.Get('bad', env.FLEX), None,
         'A non-matching runtime should return None if no default is provided.'
     )
Beispiel #16
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')