def test_no_validate_only(self): """Test no use of validate only.""" @validate.validation_complete def impl(_input_proto, _output_proto, _config): self.fail('Incorrectly allowed method to execute.') # We will get an assertion error unless validate_only prevents the function # from being called. with self.assertRaises(AssertionError): impl(common_pb2.Chroot(), common_pb2.Chroot(), self.api_config)
def test_in(self): """Test a valid value.""" @validate.is_in('path', ['/chroot/path', '/other/chroot/path']) def impl(_input_proto, _output_proto, _config): pass # Make sure all of the values work. impl(common_pb2.Chroot(path='/chroot/path'), None, self.api_config) impl(common_pb2.Chroot(path='/other/chroot/path'), None, self.api_config)
def test_validate_only(self): """Test validate only.""" @validate.require('path') @validate.validation_complete def impl(_input_proto, _output_proto, _config): self.fail('Implementation was called.') return 1 # Just using arbitrary messages, we just need the # (request, response, config) arguments so it can check the config. rc = impl(common_pb2.Chroot(path='/chroot/path'), common_pb2.Chroot(), self.validate_only_config) self.assertEqual(0, rc)
def testSuccess(self): """Test successful handling case.""" path = '/chroot/path' cache_dir = '/cache/dir' chrome_root = '/chrome/root' use_flags = [{'flag': 'useflag1'}, {'flag': 'useflag2'}] features = [{'feature': 'feature1'}, {'feature': 'feature2'}] expected_env = { 'USE': 'useflag1 useflag2', 'FEATURES': 'feature1 feature2', 'CHROME_ORIGIN': 'LOCAL_SOURCE' } chroot_message = common_pb2.Chroot(path=path, cache_dir=cache_dir, chrome_dir=chrome_root, env={ 'use_flags': use_flags, 'features': features }) expected = Chroot(path=path, cache_dir=cache_dir, chrome_root=chrome_root, env=expected_env) result = controller_util.ParseChroot(chroot_message) self.assertEqual(expected, result)
def test_skip_validation(self): """Test skipping validation case.""" @validate.require('path', 'env.use_flags') def impl(_input_proto, _output_proto, _config): pass # This would otherwise raise an error for an invalid path. impl(common_pb2.Chroot(), None, self.no_validate_config)
def test_mixed(self): """Test validator passes when given a set value.""" @validate.require('path', 'env.use_flags') def impl(_input_proto, _output_proto, _config): pass with self.assertRaises(cros_build_lib.DieSystemExit): impl(common_pb2.Chroot(path='/chroot/path'), None, self.api_config)
def testWrongMessage(self): """Wrong message type handling.""" message = common_pb2.Chroot() message.env.use_flags.add().flag = 'foo' message.env.use_flags.add().flag = 'bar' with self.assertRaises(AssertionError): controller_util.ParseBuildTargets(message.env.use_flags)
def test_skip_validation(self): """Test skipping validation case.""" @validate.is_in('path', ['/chroot/path', '/other/chroot/path']) def impl(_input_proto, _output_proto, _config): pass # This would otherwise raise an error for an invalid path. impl(common_pb2.Chroot(), None, self.no_validate_config)
def test_invalid_field(self): """Test validator fails when given an unset value.""" @validate.require('does.not.exist') def impl(_input_proto, _output_proto, _config): self.fail('Incorrectly allowed method to execute.') with self.assertRaises(cros_build_lib.DieSystemExit): impl(common_pb2.Chroot(), None, self.api_config)
def test_not_set(self): """Test an unset value.""" @validate.is_in('path', ['/chroot/path', '/other/chroot/path']) def impl(_input_proto, _output_proto, _config): pass # Should be failing without a value set. with self.assertRaises(cros_build_lib.DieSystemExit): impl(common_pb2.Chroot(), None, self.api_config)
def test_not_in(self): """Test an invalid value.""" @validate.is_in('path', ['/chroot/path', '/other/chroot/path']) def impl(_input_proto, _output_proto, _config): pass # Should be failing on the invalid value. with self.assertRaises(cros_build_lib.DieSystemExit): impl(common_pb2.Chroot(path='/bad/value'), None, self.api_config)
def test_exists(self): """Test the validator fails when given a path that doesn't exist.""" path = os.path.join(self.tempdir, 'chroot') osutils.SafeMakedirs(path) @validate.exists('path') def impl(_input_proto, _output_proto, _config): pass impl(common_pb2.Chroot(path=path), None, self.api_config)
def test_not_exists(self): """Test the validator fails when given a path that doesn't exist.""" path = os.path.join(self.tempdir, 'DOES_NOT_EXIST') @validate.exists('path') def impl(_input_proto, _output_proto, _config): self.fail('Incorrectly allowed method to execute.') with self.assertRaises(cros_build_lib.DieSystemExit): impl(common_pb2.Chroot(path=path), None, self.api_config)
def _GetRequest(self, artifact_types=None): chroot = common_pb2.Chroot(path=self.tempdir) sysroot = sysroot_pb2.Sysroot( path='/build/board', build_target=common_pb2.BuildTarget(name='board')) return toolchain_pb2.BundleToolchainRequest( chroot=chroot, sysroot=sysroot, output_dir=self.tempdir, artifact_types=artifact_types, )
def test_set(self): """Test validator passes when given set values.""" @validate.require('path', 'env.use_flags') def impl(_input_proto, _output_proto, _config): pass in_proto = common_pb2.Chroot(path='/chroot/path', env={'use_flags': [{ 'flag': 'test' }]}) impl(in_proto, None, self.api_config)
def test_parse_chroot_success(self): """Test successful Chroot message parse.""" chroot_msg = common_pb2.Chroot() chroot_msg.path = self.path chroot_msg.cache_dir = self.cache_dir chroot_msg.chrome_dir = self.chrome_dir chroot_msg.env.features.add().feature = 'thing' chroot_handler = field_handler.ChrootHandler(clear_field=False) parsed_chroot = chroot_handler.parse_chroot(chroot_msg) self.assertEqual(self.expected_chroot, parsed_chroot)
def handle_chroot(message, clear_field=True): """Find and parse the chroot field, returning the Chroot instance. Returns: chroot_lib.Chroot """ handler = ChrootHandler(clear_field) chroot = handler.handle(message) if chroot: return chroot logging.warning('No chroot message found, falling back to defaults.') return handler.parse_chroot(common_pb2.Chroot())
def _GetRequest(self, artifact_types=None, input_artifacts=None, additional_args=None): chroot = common_pb2.Chroot(path=self.tempdir) sysroot = sysroot_pb2.Sysroot( path='/build/board', build_target=common_pb2.BuildTarget(name='board')) return toolchain_pb2.PrepareForToolchainBuildRequest( artifact_types=artifact_types, chroot=chroot, sysroot=sysroot, input_artifacts=input_artifacts, additional_args=additional_args)
def _Input(self): return test_pb2.MoblabVmTestRequest( chroot=common_pb2.Chroot(path=self.chroot_dir), image_payload=self._Payload(self.image_payload_dir), cache_payloads=[self._Payload(self.autotest_payload_dir)])
def testWrongMessage(self): """Test invalid message type given.""" with self.assertRaises(AssertionError): controller_util.ParseBuildTarget(common_pb2.Chroot())
def testChrootCallToGoma(self): """Test calls to goma.""" path = '/chroot/path' cache_dir = '/cache/dir' chrome_root = '/chrome/root' use_flags = [{'flag': 'useflag1'}, {'flag': 'useflag2'}] features = [{'feature': 'feature1'}, {'feature': 'feature2'}] goma_test_dir = '/goma/test/dir' goma_test_json_string = 'goma_json' chromeos_goma_test_dir = '/chromeos/goma/test/dir' # Patch goma constructor to avoid creating misc dirs. patch = self.PatchObject(goma_util, 'Goma') goma_config = common_pb2.GomaConfig( goma_dir=goma_test_dir, goma_client_json=goma_test_json_string) chroot_message = common_pb2.Chroot(path=path, cache_dir=cache_dir, chrome_dir=chrome_root, env={ 'use_flags': use_flags, 'features': features }, goma=goma_config) controller_util.ParseChroot(chroot_message) patch.assert_called_with(goma_test_dir, goma_test_json_string, stage_name='BuildAPI', chromeos_goma_dir=None, chroot_dir=path, goma_approach=None) goma_config.chromeos_goma_dir = chromeos_goma_test_dir chroot_message = common_pb2.Chroot(path=path, cache_dir=cache_dir, chrome_dir=chrome_root, env={ 'use_flags': use_flags, 'features': features }, goma=goma_config) controller_util.ParseChroot(chroot_message) patch.assert_called_with(goma_test_dir, goma_test_json_string, stage_name='BuildAPI', chromeos_goma_dir=chromeos_goma_test_dir, chroot_dir=path, goma_approach=None) goma_config.goma_approach = common_pb2.GomaConfig.RBE_PROD chroot_message = common_pb2.Chroot(path=path, cache_dir=cache_dir, chrome_dir=chrome_root, env={ 'use_flags': use_flags, 'features': features }, goma=goma_config) controller_util.ParseChroot(chroot_message) patch.assert_called_with(goma_test_dir, goma_test_json_string, stage_name='BuildAPI', chromeos_goma_dir=chromeos_goma_test_dir, chroot_dir=path, goma_approach=goma_util.GomaApproach( '?prod', 'goma.chromium.org', True)) goma_config.goma_approach = common_pb2.GomaConfig.RBE_STAGING chroot_message = common_pb2.Chroot(path=path, cache_dir=cache_dir, chrome_dir=chrome_root, env={ 'use_flags': use_flags, 'features': features }, goma=goma_config) controller_util.ParseChroot(chroot_message) patch.assert_called_with(goma_test_dir, goma_test_json_string, stage_name='BuildAPI', chromeos_goma_dir=chromeos_goma_test_dir, chroot_dir=path, goma_approach=goma_util.GomaApproach( '?staging', 'staging-goma.chromium.org', True))