def test_logging_code(self):
    compute = virtualenv.Compute()
    service = MockVirtualEnvService()

    # Test logging code
    with self.assertLogs(virtualenv.__name__, level="DEBUG4") as cm:
      os.environ['BAR'] = 'FOO'
      env = os.environ.copy()
      env.pop('BAR')
      env['FOO'] = 'BAR'
      service.env = env

      self.return_value = 0
      compute.run(service)

    env_lines = [x for x in cm.output if "Environment Modification:" in x][0]
    env_lines = env_lines.split('\n')
    self.assertEqual(len(env_lines), 5)

    self.assertTrue(any(o.startswith('- BAR:') for o in env_lines))
    self.assertTrue(any(o.startswith('+ FOO:') for o in env_lines))
    # Added by Terra
    self.assertTrue(any(o.startswith('+ TERRA_SETTINGS_FILE:')
                        for o in env_lines))
    # Added by TestSettingsConfigureCase
    self.assertTrue(any(o.startswith('- TERRA_SETTINGS_FILE:')
                        for o in env_lines))
    def test_run_failed(self):
        compute = virtualenv.Compute()
        service = MockVirtualEnvService()

        self.return_value = 1
        with self.assertRaises(base.ServiceRunFailed):
            compute.run(service)
  def test_run_simple(self):
    compute = virtualenv.Compute()
    service = MockVirtualEnvService()

    self.return_value = 0
    compute.run(service)

    self.assertEqual(self.popen_args, (['ls'],))
    # Only kwarg is env
    self.assertEqual(set(self.popen_kwargs.keys()), {'env', 'executable'})
    self.assertEqual(self.popen_kwargs['env']['BAR'], 'FOO')
  def test_run_failed(self):
    compute = virtualenv.Compute()
    service = MockVirtualEnvService()

    self.return_value = 1

    import warnings
    with warnings.catch_warnings():
      with self.assertRaises(base.ServiceRunFailed):
        warnings.simplefilter('ignore')
        compute.run(service)
      # Delete the service now, so that the temp directory is cleaned up, so
      # the warning is captured now, in the context
      del service
    def test_run_virtualenv(self):
        compute = virtualenv.Compute()
        service = MockVirtualEnvService()

        with settings:
            settings.compute.virtualenv_dir = "/bar/foo"

            self.return_value = 0
            compute.run(service)

        self.assertEqual(self.popen_args, (['ls'], ))
        self.assertEqual(set(self.popen_kwargs.keys()), {'env', 'executable'})
        self.assertEqual(self.popen_kwargs['env']['BAR'], 'FOO')
        self.assertTrue(
            self.popen_kwargs['env']['PATH'].startswith('/bar/foo'))
  def test_run_virtualenv(self):
    compute = virtualenv.Compute()
    service = MockVirtualEnvService()

    with settings:
      settings.compute.virtualenv_dir = "/bar/foo"

      self.return_value = 0
      with self.assertLogs(virtualenv.__name__, level="WARNING") as cm:
        compute.run(service)
      self.assertTrue(any("Couldn't find command ls in virtualenv_dir" in x
                          for x in cm.output))

    self.assertEqual(self.popen_args, (['ls'],))
    self.assertEqual(set(self.popen_kwargs.keys()), {'env', 'executable'})
    self.assertEqual(self.popen_kwargs['env']['BAR'], 'FOO')
    self.assertTrue(self.popen_kwargs['env']['PATH'].startswith('/bar/foo'))
  def setUp(self):
    # self.run trigger Executor
    self.patches.append(mock.patch.dict(Executor.__dict__))
    # This will resets the _connection to an uninitialized state
    self.patches.append(
        mock.patch.object(
            terra.compute.utils.ComputeHandler,
            '_connection',
            mock.PropertyMock(return_value=virtualenv.Compute())))

    # Use mock_popen to check the arguments
    self.patches.append(mock.patch.object(virtualenv,
                                          'Popen', self.mock_popen))

    # patches.append(mock.patch.dict(base.services, clear=True))
    self.config.compute = {'arch': 'virtualenv', 'virtualenv_dir': None}
    super().setUp()
    def setUp(self):
        # Use settings
        self.patches.append(mock.patch.object(settings, '_wrapped', None))
        # This will resets the _connection to an uninitialized state
        self.patches.append(
            mock.patch.object(
                terra.compute.utils.ComputeHandler, '_connection',
                mock.PropertyMock(return_value=virtualenv.Compute())))

        # Use mock_popen to check the arguments
        self.patches.append(
            mock.patch.object(virtualenv, 'Popen', self.mock_popen))

        # patches.append(mock.patch.dict(base.services, clear=True))
        super().setUp()
        settings.configure({
            'compute': {
                'arch': 'virtualenv'
            },
            'processing_dir':
            self.temp_dir.name,
            'test_dir':
            '/opt/projects/terra/terra_dsm/external/terra/foo'
        })