Skip to content

Commit

Permalink
fix functional test cache
Browse files Browse the repository at this point in the history
  • Loading branch information
ricolin committed Nov 27, 2024
1 parent 275f88c commit da763a5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 55 deletions.
17 changes: 17 additions & 0 deletions magnum_cluster_api/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ def mock_osc(session_mocker, image):
)
)

# Nova
def _gen_m_server_group():
return openstack.compute.v2.server_group.ServerGroup(
id=uuidutils.generate_uuid()
)
mock_nova_client = mock_clients.nova.return_value
sgs = [_gen_m_server_group() for i in range(1, 100)]
mock_nova_client.server_groups.create.side_effect = sgs

# Glance
mock_glance_client = mock_clients.glance.return_value
mock_glance_client.images.get.return_value = image
Expand Down Expand Up @@ -273,3 +282,11 @@ def server_side_apply_matcher():
"force": "True",
}
)


@pytest.fixture(scope="session")
def mock_cache(session_mocker):
session_mocker.patch(
"magnum_cluster_api.cache.ServerGroupCache.get", return_value=None
)
session_mocker.patch("magnum_cluster_api.cache.ServerGroupCache.set")
1 change: 1 addition & 0 deletions magnum_cluster_api/tests/functional/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def cluster(
mock_validate_cluster,
mock_osc,
mock_certificates,
mock_cache,
):
try:
ubuntu_driver.create_cluster(context, cluster_obj, 60)
Expand Down
81 changes: 26 additions & 55 deletions magnum_cluster_api/tests/functional/test_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,47 +29,18 @@ def setup(self, cluster):
self.api = clients.get_pykube_api()
self.cluster = cluster

def _assert_machine_deployment_config_matches_node_group(self, md, node_group):
assert md is not None
# TODO: more?

@tenacity.retry(
stop=tenacity.stop_after_attempt(10),
wait=tenacity.wait_fixed(1),
)
def _assert_machine_deployments_for_node_groups(
def _assert_server_groups_for_node_groups(
self, *node_groups: magnum_objects.NodeGroup
):
mds = objects.MachineDeployment.objects(
self.api, namespace="magnum-system"
).filter(
selector={
"cluster.x-k8s.io/cluster-name": self.cluster.stack_id,
},
)

worker_ngs = [ng for ng in node_groups if ng.role != "master"]

assert len(mds) == len(worker_ngs), "Expected %d MachineDeployments, got %d" % (
len(worker_ngs),
len(mds),
)

# NOTE(mnaser): We need to loop over all the node groups and make sure
# that the machine deployments are created for them.
for ng in worker_ngs:
md = objects.MachineDeployment.for_node_group(self.api, self.cluster, ng)
self._assert_machine_deployment_config_matches_node_group(md, ng)

# NOTE(mnaser): We also need to make sure there are no extra machine
# deployments created.
for md in mds:
assert md.labels["topology.cluster.x-k8s.io/deployment-name"] in [
ng.name for ng in node_groups
]
pass
# We need to add server group asserts here

def test_default_node_group(self):
self._assert_machine_deployments_for_node_groups(*self.cluster.nodegroups)
self._assert_server_groups_for_node_groups(*self.cluster.nodegroups)

def _create_node_group(self, context, driver, node_group_name, cluster_template):
new_node_group = utils.get_test_nodegroup(
Expand Down Expand Up @@ -140,7 +111,7 @@ def test_upgrade_cluster_with_multiple_node_groups(
cluster_resource = objects.Cluster.for_magnum_cluster(self.api, self.cluster)
assert cluster_resource.observed_generation != current_observed_generation

self._assert_machine_deployments_for_node_groups(
self._assert_server_groups_for_node_groups(
*self.cluster.nodegroups,
)

Expand All @@ -149,99 +120,99 @@ def test_upgrade_cluster_with_multiple_node_groups(
def test_create_node_group(
self, mock_validate_nodegroup, context, ubuntu_driver, cluster_template
):
self._assert_machine_deployments_for_node_groups(*self.cluster.nodegroups)
self._assert_server_groups_for_node_groups(*self.cluster.nodegroups)
new_node_group = self._create_node_group(
context, ubuntu_driver, "high-cpu", cluster_template
)
self._assert_machine_deployments_for_node_groups(
self._assert_server_groups_for_node_groups(
*self.cluster.nodegroups,
new_node_group,
)

def test_create_and_delete_node_group(
self, mock_validate_nodegroup, context, ubuntu_driver, cluster_template
):
self._assert_machine_deployments_for_node_groups(*self.cluster.nodegroups)
self._assert_server_groups_for_node_groups(*self.cluster.nodegroups)
new_node_group = self._create_node_group(
context, ubuntu_driver, "high-cpu", cluster_template
)
self._assert_machine_deployments_for_node_groups(
self._assert_server_groups_for_node_groups(
*self.cluster.nodegroups,
new_node_group,
)
ubuntu_driver.delete_nodegroup(context, self.cluster, new_node_group)
self._assert_machine_deployments_for_node_groups(*self.cluster.nodegroups)
self._assert_server_groups_for_node_groups(*self.cluster.nodegroups)

def test_create_two_node_groups(
self, mock_validate_nodegroup, context, ubuntu_driver, cluster_template
self, mock_validate_nodegroup, mock_osc, context, ubuntu_driver, cluster_template
):
self._assert_machine_deployments_for_node_groups(*self.cluster.nodegroups)
self._assert_server_groups_for_node_groups(*self.cluster.nodegroups)
first_new_node_group = self._create_node_group(
context, ubuntu_driver, "high-cpu", cluster_template
)
self._assert_machine_deployments_for_node_groups(
self._assert_server_groups_for_node_groups(
*self.cluster.nodegroups,
first_new_node_group,
)
second_new_node_group = self._create_node_group(
context, ubuntu_driver, "high-memory", cluster_template
)
self._assert_machine_deployments_for_node_groups(
self._assert_server_groups_for_node_groups(
*self.cluster.nodegroups,
first_new_node_group,
second_new_node_group,
)

def test_create_and_delete_two_node_groups_deleting_newest_first(
self, mock_validate_nodegroup, context, ubuntu_driver, cluster_template
self, mock_validate_nodegroup, mock_osc, context, ubuntu_driver, cluster_template
):
self._assert_machine_deployments_for_node_groups(*self.cluster.nodegroups)
self._assert_server_groups_for_node_groups(*self.cluster.nodegroups)
first_new_node_group = self._create_node_group(
context, ubuntu_driver, "high-cpu", cluster_template
)
self._assert_machine_deployments_for_node_groups(
self._assert_server_groups_for_node_groups(
*self.cluster.nodegroups,
first_new_node_group,
)
second_new_node_group = self._create_node_group(
context, ubuntu_driver, "high-memory", cluster_template
)
self._assert_machine_deployments_for_node_groups(
self._assert_server_groups_for_node_groups(
*self.cluster.nodegroups,
first_new_node_group,
second_new_node_group,
)
ubuntu_driver.delete_nodegroup(context, self.cluster, second_new_node_group)
self._assert_machine_deployments_for_node_groups(
self._assert_server_groups_for_node_groups(
*self.cluster.nodegroups,
first_new_node_group,
)
ubuntu_driver.delete_nodegroup(context, self.cluster, first_new_node_group)
self._assert_machine_deployments_for_node_groups(*self.cluster.nodegroups)
self._assert_server_groups_for_node_groups(*self.cluster.nodegroups)

def test_create_and_delete_two_node_groups_deleting_oldest_first(
self, mock_validate_nodegroup, context, ubuntu_driver, cluster_template
self, mock_validate_nodegroup, mock_osc, context, ubuntu_driver, cluster_template
):
self._assert_machine_deployments_for_node_groups(*self.cluster.nodegroups)
self._assert_server_groups_for_node_groups(*self.cluster.nodegroups)
first_new_node_group = self._create_node_group(
context, ubuntu_driver, "high-cpu", cluster_template
)
self._assert_machine_deployments_for_node_groups(
self._assert_server_groups_for_node_groups(
*self.cluster.nodegroups,
first_new_node_group,
)
second_new_node_group = self._create_node_group(
context, ubuntu_driver, "high-memory", cluster_template
)
self._assert_machine_deployments_for_node_groups(
self._assert_server_groups_for_node_groups(
*self.cluster.nodegroups,
first_new_node_group,
second_new_node_group,
)
ubuntu_driver.delete_nodegroup(context, self.cluster, first_new_node_group)
self._assert_machine_deployments_for_node_groups(
self._assert_server_groups_for_node_groups(
*self.cluster.nodegroups,
second_new_node_group,
)
ubuntu_driver.delete_nodegroup(context, self.cluster, second_new_node_group)
self._assert_machine_deployments_for_node_groups(*self.cluster.nodegroups)
self._assert_server_groups_for_node_groups(*self.cluster.nodegroups)

0 comments on commit da763a5

Please sign in to comment.