예제 #1
0
  def test_already_active(self):
    """Ensures that the active instance template revision remains active."""
    template_cfg = config_pb2.InstanceTemplateConfig.InstanceTemplate(
        base_name='base-name',
    )
    expected_active_key = parse.get_instance_template_revision_key(template_cfg)
    instance_template = models.InstanceTemplate(
        active=parse.get_instance_template_revision_key(template_cfg),
        drained=[
            ndb.Key(models.InstanceTemplateRevision, 'fake-key'),
        ],
    )

    self.failIf(parse.ensure_instance_template_revision_active(
        template_cfg, instance_template))
    self.assertEqual(instance_template.active, expected_active_key)
    self.assertEqual(len(instance_template.drained), 1)
    self.assertEqual(instance_template.drained[0].id(), 'fake-key')
예제 #2
0
  def test_drains_and_reactivates(self):
    """Ensures that the active is drained and the drained is reactivated."""
    template_cfg = config_pb2.InstanceTemplateConfig.InstanceTemplate(
        base_name='base-name',
    )
    expected_active_key = parse.get_instance_template_revision_key(template_cfg)
    instance_template = models.InstanceTemplate(
        active=ndb.Key(models.InstanceTemplateRevision, 'fake-key'),
        drained=[
            parse.get_instance_template_revision_key(template_cfg),
        ],
    )

    self.failUnless(parse.ensure_instance_template_revision_active(
        template_cfg, instance_template))
    self.assertEqual(instance_template.active, expected_active_key)
    self.assertEqual(len(instance_template.drained), 1)
    self.assertEqual(instance_template.drained[0].id(), 'fake-key')
예제 #3
0
    def test_already_active(self):
        """Ensures that the active instance template revision remains active."""
        template_cfg = config_pb2.InstanceTemplateConfig.InstanceTemplate(
            base_name='base-name', )
        expected_active_key = parse.get_instance_template_revision_key(
            template_cfg)
        instance_template = models.InstanceTemplate(
            active=parse.get_instance_template_revision_key(template_cfg),
            drained=[
                ndb.Key(models.InstanceTemplateRevision, 'fake-key'),
            ],
        )

        self.failIf(
            parse.ensure_instance_template_revision_active(
                template_cfg, instance_template))
        self.assertEqual(instance_template.active, expected_active_key)
        self.assertEqual(len(instance_template.drained), 1)
        self.assertEqual(instance_template.drained[0].id(), 'fake-key')
예제 #4
0
    def test_drains_and_reactivates(self):
        """Ensures that the active is drained and the drained is reactivated."""
        template_cfg = config_pb2.InstanceTemplateConfig.InstanceTemplate(
            base_name='base-name', )
        expected_active_key = parse.get_instance_template_revision_key(
            template_cfg)
        instance_template = models.InstanceTemplate(
            active=ndb.Key(models.InstanceTemplateRevision, 'fake-key'),
            drained=[
                parse.get_instance_template_revision_key(template_cfg),
            ],
        )

        self.failUnless(
            parse.ensure_instance_template_revision_active(
                template_cfg, instance_template))
        self.assertEqual(instance_template.active, expected_active_key)
        self.assertEqual(len(instance_template.drained), 1)
        self.assertEqual(instance_template.drained[0].id(), 'fake-key')
예제 #5
0
  def test_activates(self):
    """Ensures that the instance template revision is activated."""
    template_cfg = config_pb2.InstanceTemplateConfig.InstanceTemplate(
        base_name='base-name',
    )
    expected_active_key = parse.get_instance_template_revision_key(template_cfg)
    instance_template = models.InstanceTemplate()

    self.failUnless(parse.ensure_instance_template_revision_active(
        template_cfg, instance_template))
    self.assertEqual(instance_template.active, expected_active_key)
예제 #6
0
    def test_activates(self):
        """Ensures that the instance template revision is activated."""
        template_cfg = config_pb2.InstanceTemplateConfig.InstanceTemplate(
            base_name='base-name', )
        expected_active_key = parse.get_instance_template_revision_key(
            template_cfg)
        instance_template = models.InstanceTemplate()

        self.failUnless(
            parse.ensure_instance_template_revision_active(
                template_cfg, instance_template))
        self.assertEqual(instance_template.active, expected_active_key)
예제 #7
0
    def test_creates_new_entity(self):
        """Ensures that a new entity is created when one doesn't exist."""
        template_cfg = config_pb2.InstanceTemplateConfig.InstanceTemplate(
            base_name='base-name',
            dimensions=[
                'os_family:LINUX',
            ],
            disk_size_gb=100,
            machine_type='n1-standard-8',
            metadata=[
                'key:value',
            ],
            service_accounts=[
                config_pb2.InstanceTemplateConfig.InstanceTemplate.
                ServiceAccount(
                    name='service-account',
                    scopes=[
                        'scope',
                    ],
                ),
            ],
            tags=[
                'tag',
            ])
        manager_cfgs = [
            config_pb2.InstanceGroupManagerConfig.InstanceGroupManager(
                maximum_size=2,
                minimum_size=1,
                template_base_name='base-name',
                zone='us-central-1a',
            ),
            config_pb2.InstanceGroupManagerConfig.InstanceGroupManager(
                maximum_size=3,
                minimum_size=2,
                template_base_name='base-name',
                zone='us-central-1b',
            ),
            config_pb2.InstanceGroupManagerConfig.InstanceGroupManager(
                maximum_size=4,
                minimum_size=3,
                template_base_name='base-name',
                zone='us-central-1c',
            ),
        ]
        expected_instance_template_key = parse.get_instance_template_key(
            template_cfg)
        expected_instance_template_revision_key = (
            parse.get_instance_template_revision_key(template_cfg))
        expected_dimensions = parse._load_machine_provider_dimensions(
            template_cfg.dimensions)
        expected_metadata = parse._load_dict(template_cfg.metadata)
        expected_service_accounts = [
            models.ServiceAccount(
                name=template_cfg.service_accounts[0].name,
                scopes=list(template_cfg.service_accounts[0].scopes),
            ),
        ]
        expected_active_keys = [
            parse.get_instance_group_manager_key(template_cfg, manager_cfg)
            for manager_cfg in manager_cfgs
        ]

        future = parse.ensure_entities_exist(template_cfg, manager_cfgs)
        future.wait()
        instance_template_key = future.get_result()
        instance_template = instance_template_key.get()
        instance_template_revision = instance_template.active.get()
        instance_group_managers = sorted(
            [
                instance_group_manager.get()
                for instance_group_manager in instance_template_revision.active
            ],
            key=lambda instance_group_manager: instance_group_manager.key.id(),
        )

        self.assertEqual(instance_template_key, expected_instance_template_key)
        self.assertEqual(instance_template.active,
                         expected_instance_template_revision_key)
        self.assertEqual(instance_template_revision.dimensions,
                         expected_dimensions)
        self.assertEqual(instance_template_revision.disk_size_gb,
                         template_cfg.disk_size_gb)
        self.assertEqual(instance_template_revision.machine_type,
                         template_cfg.machine_type)
        self.assertEqual(instance_template_revision.metadata,
                         expected_metadata)
        self.assertItemsEqual(instance_template_revision.service_accounts,
                              expected_service_accounts)
        self.assertItemsEqual(instance_template_revision.tags,
                              template_cfg.tags)
        self.assertItemsEqual(instance_template_revision.active,
                              expected_active_keys)
        self.assertEqual(instance_group_managers[0].maximum_size,
                         manager_cfgs[0].maximum_size)
        self.assertEqual(instance_group_managers[0].minimum_size,
                         manager_cfgs[0].minimum_size)
        self.assertEqual(instance_group_managers[1].maximum_size,
                         manager_cfgs[1].maximum_size)
        self.assertEqual(instance_group_managers[1].minimum_size,
                         manager_cfgs[1].minimum_size)
        self.assertEqual(instance_group_managers[2].maximum_size,
                         manager_cfgs[2].maximum_size)
        self.assertEqual(instance_group_managers[2].minimum_size,
                         manager_cfgs[2].minimum_size)
예제 #8
0
  def test_creates_new_entity(self):
    """Ensures that a new entity is created when one doesn't exist."""
    template_cfg = config_pb2.InstanceTemplateConfig.InstanceTemplate(
        base_name='base-name',
        dimensions=[
            'os_family:LINUX',
        ],
        disk_size_gb=100,
        machine_type='n1-standard-8',
        metadata=[
            'key:value',
        ],
        service_accounts=[
            config_pb2.InstanceTemplateConfig.InstanceTemplate.ServiceAccount(
                name='service-account',
                scopes=[
                  'scope',
                ],
            ),
        ],
        tags=[
          'tag',
        ]
    )
    manager_cfgs = [
        config_pb2.InstanceGroupManagerConfig.InstanceGroupManager(
            maximum_size=2,
            minimum_size=1,
            template_base_name='base-name',
            zone='us-central-1a',
        ),
        config_pb2.InstanceGroupManagerConfig.InstanceGroupManager(
            maximum_size=3,
            minimum_size=2,
            template_base_name='base-name',
            zone='us-central-1b',
        ),
        config_pb2.InstanceGroupManagerConfig.InstanceGroupManager(
            maximum_size=4,
            minimum_size=3,
            template_base_name='base-name',
            zone='us-central-1c',
        ),
    ]
    expected_instance_template_key = parse.get_instance_template_key(
        template_cfg)
    expected_instance_template_revision_key = (
        parse.get_instance_template_revision_key(template_cfg))
    expected_dimensions = parse._load_machine_provider_dimensions(
        template_cfg.dimensions)
    expected_metadata = parse._load_dict(template_cfg.metadata)
    expected_service_accounts = [
        models.ServiceAccount(
            name=template_cfg.service_accounts[0].name,
            scopes=list(template_cfg.service_accounts[0].scopes),
        ),
    ]
    expected_active_keys = [
        parse.get_instance_group_manager_key(template_cfg, manager_cfg)
        for manager_cfg in manager_cfgs
    ]

    future = parse.ensure_entities_exist(
        template_cfg, manager_cfgs)
    future.wait()
    instance_template_key = future.get_result()
    instance_template = instance_template_key.get()
    instance_template_revision = instance_template.active.get()
    instance_group_managers = sorted(
        [
            instance_group_manager.get()
            for instance_group_manager in instance_template_revision.active
        ],
        key=lambda instance_group_manager: instance_group_manager.key.id(),
    )

    self.assertEqual(instance_template_key, expected_instance_template_key)
    self.assertEqual(
        instance_template.active, expected_instance_template_revision_key)
    self.assertEqual(instance_template_revision.dimensions, expected_dimensions)
    self.assertEqual(
        instance_template_revision.disk_size_gb, template_cfg.disk_size_gb)
    self.assertEqual(
        instance_template_revision.machine_type, template_cfg.machine_type)
    self.assertEqual(instance_template_revision.metadata, expected_metadata)
    self.assertItemsEqual(
        instance_template_revision.service_accounts, expected_service_accounts)
    self.assertItemsEqual(instance_template_revision.tags, template_cfg.tags)
    self.assertItemsEqual(
        instance_template_revision.active, expected_active_keys)
    self.assertEqual(
        instance_group_managers[0].maximum_size, manager_cfgs[0].maximum_size)
    self.assertEqual(
        instance_group_managers[0].minimum_size, manager_cfgs[0].minimum_size)
    self.assertEqual(
        instance_group_managers[1].maximum_size, manager_cfgs[1].maximum_size)
    self.assertEqual(
        instance_group_managers[1].minimum_size, manager_cfgs[1].minimum_size)
    self.assertEqual(
        instance_group_managers[2].maximum_size, manager_cfgs[2].maximum_size)
    self.assertEqual(
        instance_group_managers[2].minimum_size, manager_cfgs[2].minimum_size)