def testReadAutoscalingPolicy_invalid(self): self.WriteInput('foo: bar') policy_read = util.ReadAutoscalingPolicy( dataproc=self.dataproc_mock, policy_id='cool-policy', ) # Invalid fields are ignored, so you end up with a mostly empty message self.AssertMessagesEqual( self.messages.AutoscalingPolicy(id='cool-policy'), policy_read)
def testReadAutoscalingPolicy_stdin(self): policy = self.MakeAutoscalingPolicy('cool-project', 'cool-region', 'cool-policy') self.WriteInput(export_util.Export(message=policy)) expected_policy = copy.deepcopy(policy) expected_policy.name = None policy_read = util.ReadAutoscalingPolicy( dataproc=self.dataproc_mock, policy_id='cool-policy', ) self.AssertMessagesEqual(expected_policy, policy_read)
def testReadAutoscalingPolicy_gracefulTimeoutFormat_illegal( self, illegal_duration): policy = self.MakeAutoscalingPolicy('cool-project', 'cool-region', 'cool-policy') policy.id = None policy.name = None policy.basicAlgorithm.yarnConfig.gracefulDecommissionTimeout = illegal_duration self.WriteInput(export_util.Export(message=policy)) with self.assertRaises(argparse.ArgumentTypeError): util.ReadAutoscalingPolicy( dataproc=self.dataproc_mock, policy_id='cool-policy', )
def testReadAutoscalingPolicy_cooldownFormat_illegal( self, illegal_duration): policy = self.MakeAutoscalingPolicy('cool-project', 'cool-region', 'cool-policy') policy.id = None policy.name = None policy.basicAlgorithm.cooldownPeriod = illegal_duration self.WriteInput(export_util.Export(message=policy)) with self.assertRaises(argparse.ArgumentTypeError): util.ReadAutoscalingPolicy( dataproc=self.dataproc_mock, policy_id='cool-policy', )
def testReadAutoscalingPolicy_file(self): policy = self.MakeAutoscalingPolicy('cool-project', 'cool-region', 'cool-policy') file_name = os.path.join(self.temp_path, 'template.yaml') with files.FileWriter(file_name) as stream: export_util.Export(message=policy, stream=stream) expected_policy = copy.deepcopy(policy) expected_policy.name = None policy_read = util.ReadAutoscalingPolicy(dataproc=self.dataproc_mock, policy_id='cool-policy', policy_file_name=file_name) self.AssertMessagesEqual(expected_policy, policy_read)
def testReadAutoscalingPolicy_handlesUnsetIdAndName(self): policy = self.MakeAutoscalingPolicy('cool-project', 'cool-region', 'cool-policy') # id and name are unset policy.id = None policy.name = None self.WriteInput(export_util.Export(message=policy)) expected_policy = copy.deepcopy(policy) expected_policy.id = 'cool-policy' # gets set to correct value expected_policy.name = None # stays unset policy_read = util.ReadAutoscalingPolicy( dataproc=self.dataproc_mock, policy_id='cool-policy', ) self.AssertMessagesEqual(expected_policy, policy_read)
def testReadAutoscalingPolicy_durationFormats(self, duration, converted): policy = self.MakeAutoscalingPolicy('cool-project', 'cool-region', 'cool-policy') policy.id = None policy.name = None policy.basicAlgorithm.cooldownPeriod = duration policy.basicAlgorithm.yarnConfig.gracefulDecommissionTimeout = duration self.WriteInput(export_util.Export(message=policy)) expected_policy = copy.deepcopy(policy) expected_policy.id = 'cool-policy' expected_policy.name = None expected_policy.basicAlgorithm.cooldownPeriod = converted expected_policy.basicAlgorithm.yarnConfig.gracefulDecommissionTimeout = converted policy_read = util.ReadAutoscalingPolicy( dataproc=self.dataproc_mock, policy_id='cool-policy', ) self.AssertMessagesEqual(expected_policy, policy_read)
def _Run(dataproc, args): """Runs import command.""" policy_ref = args.CONCEPTS.autoscaling_policy.Parse() policy = util.ReadAutoscalingPolicy(dataproc=dataproc, policy_id=policy_ref.Name(), policy_file_name=args.source) try: return util.CreateAutoscalingPolicy(dataproc, policy_ref.RelativeName(), policy) except apitools_exceptions.HttpError as error: # Catch ALREADY_EXISTS if error.status_code != 409: raise error # Warn the user that they're going to overwrite an existing policy console_io.PromptContinue( message=('Autoscaling policy [{0}] will be overwritten.').format( policy.id), cancel_on_no=True) return util.UpdateAutoscalingPolicy(dataproc, policy_ref.RelativeName(), policy)
def testReadAutoscalingPolicy_omitCooldownPeriod(self): policy = self.MakeAutoscalingPolicy('cool-project', 'cool-region', 'cool-policy') policy.id = None policy.name = None policy.basicAlgorithm.cooldownPeriod = None policy.basicAlgorithm.yarnConfig.gracefulDecommissionTimeout = '1h' self.WriteInput(export_util.Export(message=policy)) expected_policy = copy.deepcopy(policy) expected_policy.id = 'cool-policy' expected_policy.name = None # cooldownPeriod remains None, and graceful timeout still gets converted. expected_policy.basicAlgorithm.yarnConfig.gracefulDecommissionTimeout = '3600s' policy_read = util.ReadAutoscalingPolicy( dataproc=self.dataproc_mock, policy_id='cool-policy', ) # Cooldown period remains none self.AssertMessagesEqual(expected_policy, policy_read)