def test_access_not_empty_traffic_targets(self): targets = NewTrafficTargets({'r0': 10, 'r1': 20, 'LATEST': 70}) r0 = traffic.NewTrafficTarget( MESSAGES, 'r0', 10) r1 = traffic.NewTrafficTarget( MESSAGES, 'r1', 20) latest = traffic.NewTrafficTarget( MESSAGES, traffic.LATEST_REVISION_KEY, 70) self.assertEqual(targets['r0'], [r0]) self.assertEqual(targets['r1'], [r1]) self.assertEqual(targets[traffic.LATEST_REVISION_KEY], [latest]) self.assertIn('r1', targets) self.assertIn(traffic.LATEST_REVISION_KEY, targets) self.assertEqual(len(targets), 3) self.assertEqual( self.GetTargetsKeys(targets), [u'r0', u'r1', traffic.LATEST_REVISION_KEY]) self.assertEqual(len(targets.MakeSerializable()), 3) self.assertIn(r0, targets.MakeSerializable()) self.assertIn(r1, targets.MakeSerializable()) self.assertIn(latest, targets.MakeSerializable()) self.assertIn('r0:', str(targets)) self.assertIn('r1:', str(targets)) self.assertIn('LATEST:', str(targets))
def testTrafficToLatest(self): self.service.spec_traffic['r1'] = [ traffic.NewTrafficTarget(self.serverless_messages, 'r1', 100) ] self.args.to_latest = True self.args.to_revisions = None self.StartObjectPatch(self.args, 'IsSpecified', return_value=True) self._GetAndApplyChanges() expect = { traffic.LATEST_REVISION_KEY: [ traffic.NewTrafficTarget(self.serverless_messages, traffic.LATEST_REVISION_KEY, 100) ] } self.assertEqual(self.service.spec_traffic, expect)
def test_modify_traffic_targets(self): targets = NewTrafficTargets({'r0': 10, 'LATEST': 70}) r0 = traffic.NewTrafficTarget( MESSAGES, 'r0', 10) r1 = traffic.NewTrafficTarget( MESSAGES, 'r1', 20) r1b = traffic.NewTrafficTarget( MESSAGES, 'r1', 30) latest = traffic.NewTrafficTarget( MESSAGES, traffic.LATEST_REVISION_KEY, 70) self.assertEqual(len(targets), 2) self.assertEqual(targets['r0'], [r0]) self.assertEqual(targets[traffic.LATEST_REVISION_KEY], [latest]) self.assertNotIn('r1', targets) targets['r1'] = [r1] self.assertIn('r1', targets) self.assertEqual(len(targets), 3) self.assertEqual(targets['r1'], [r1]) self.assertEqual( self.GetTargetsKeys(targets), [u'r0', u'r1', traffic.LATEST_REVISION_KEY]) targets['r1'] = [r1b] self.assertIn('r1', targets) self.assertEqual(len(targets), 3) self.assertEqual(targets['r1'], [r1b]) self.assertEqual( self.GetTargetsKeys(targets), [u'r0', u'r1', traffic.LATEST_REVISION_KEY]) del targets['r1'] self.assertNotIn('r1', targets) self.assertEqual(len(targets), 2) self.assertEqual( self.GetTargetsKeys(targets), [u'r0', traffic.LATEST_REVISION_KEY]) del targets[traffic.LATEST_REVISION_KEY] self.assertNotIn(traffic.LATEST_REVISION_KEY, targets) self.assertEqual(len(targets), 1) self.assertEqual(self.GetTargetsKeys(targets), [u'r0']) targets[traffic.LATEST_REVISION_KEY] = [latest] self.assertIn(traffic.LATEST_REVISION_KEY, targets) self.assertEqual(len(targets), 2) self.assertEqual( self.GetTargetsKeys(targets), [u'r0', traffic.LATEST_REVISION_KEY])
def TrafficTarget(key, percent=None, tag=None, populate_url=False): target = traffic.NewTrafficTarget(MESSAGES, key, percent, tag) if tag and populate_url: target.url = URL_FORMAT.format(tag) return target
def TrafficTarget(key, percent=None, tag=None): return traffic.NewTrafficTarget(MESSAGES, key, percent, tag)