Skip to content

Commit

Permalink
Fix enrichment for do and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ilia1243 committed May 29, 2024
1 parent f3b00e6 commit b7b44e3
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 15 deletions.
6 changes: 5 additions & 1 deletion kubemarine/kubernetes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ def enrich_reconfigure_inventory(cluster: KubernetesCluster) -> None:

@enrichment(EnrichmentStage.ALL)
def verify_roles(cluster: KubernetesCluster) -> None:
if cluster.make_group_from_roles(['control-plane']).is_empty():
control_plane_roles = ['control-plane']
if cluster.context['initial_procedure'] == 'do':
control_plane_roles = ['control-plane', 'master']

if cluster.make_group_from_roles(control_plane_roles).is_empty():
raise KME("KME0004")


Expand Down
11 changes: 0 additions & 11 deletions test/unit/core/test_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,17 +246,6 @@ def test_connection_enrichment(self):
host = cluster.nodes['all'].get_host()
self.assertEqual('10.101.1.100', cluster.connection_pool.get_connection(host).gateway.host)

def test_legacy_role(self):
inventory = demo.generate_inventory(**demo.ALLINONE)
del inventory['nodes'][0]['name']
# pylint: disable-next=implicit-str-concat
inventory['nodes'][0]['roles'] = ['balancer', 'm''a''s''t''e''r', 'worker']

cluster = demo.new_resources(inventory).cluster(EnrichmentStage.LIGHT)

self.assertIn('master', cluster.inventory['nodes'][0]['roles'])
self.assertEqual('control-plane-1', cluster.nodes['master'].get_node_name())

def test_recursive_compile_inventory(self):
inventory = demo.generate_inventory(**demo.ALLINONE)
inventory['values'] = {
Expand Down
38 changes: 38 additions & 0 deletions test/unit/core/test_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import invoke

from kubemarine.core import flow, static, utils
from kubemarine.core.cluster import EnrichmentStage
from kubemarine.procedures import do
from kubemarine import demo


Expand Down Expand Up @@ -485,6 +487,42 @@ def test_detect_nodes_context_removed_node_online(self):
self.assertEqual({'name': 'centos', 'version': '7.6', 'family': 'rhel'}, node_context["os"])
self.assertEqual('eth0', node_context["active_interface"])

def test_detect_nodes_context_do_procedure(self):
inventory = {
'nodes': [{'roles': ['master'], 'internal_address': '1.1.1.1', 'address': '1.1.1.1', 'keyfile': '/dev/null'}]
}
hosts = ['1.1.1.1']
self._stub_detect_nodes_context(inventory, hosts, hosts)

results = demo.create_hosts_result(hosts, stdout='root\n', hide=False)
self.light_fake_shell.add(results, 'sudo', ['whoami'])

context = do.create_context(['--', 'whoami'])
res = demo.FakeResources(context, inventory, fake_shell=self.light_fake_shell)

flow.ActionsFlow([do.CLIAction(context)]).run_flow(res, print_summary=False)

cluster = res.cluster(EnrichmentStage.LIGHT)
self.assertEqual("rhel", cluster.get_os_family())
self.assertEqual(1, len(cluster.nodes_context))
for node_context in cluster.nodes_context.values():
self.assertEqual({'online': True, 'accessible': True, 'sudo': 'Root'}, node_context["access"])
self.assertEqual({'name': 'centos', 'version': '7.6', 'family': 'rhel'}, node_context["os"])
self.assertEqual('eth0', node_context["active_interface"])

def test_do_master_offline(self):
inventory = {
'nodes': [{'roles': ['master'], 'internal_address': '1.1.1.1', 'address': '1.1.1.1', 'keyfile': '/dev/null'}]
}
self._stub_detect_nodes_context(inventory, [], [])

context = do.create_context(['--', 'whoami'])
res = demo.FakeResources(context, inventory, fake_shell=self.light_fake_shell)

with test_utils.assert_raises_kme(self, 'KME0006', escape=True,
offline=['1.1.1.1'], inaccessible=[]):
res.cluster(EnrichmentStage.LIGHT)

def test_kubernetes_version_not_allowed(self):
k8s_versions = list(sorted(static.KUBERNETES_VERSIONS["compatibility_map"], key=utils.version_key))
k8s_latest = k8s_versions[-1]
Expand Down
6 changes: 3 additions & 3 deletions test/unit/test_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,18 +243,18 @@ def test_controlplain_skip_vrrp_ips_assigned_to_removed_balancer(self):
self.assertEqual(inventory['control_plain']['external'], inventory['vrrp_ips'][1]['floating_ip'])

def test_single_control_plane(self):
inventory = demo.generate_inventory(master=['node-1'], worker=['node-1'], balancer=0)
inventory = demo.generate_inventory(control_plane=['node-1'], worker=['node-1'], balancer=0)
inventory['nodes'][0]['roles'].remove('worker')
cluster = demo.new_cluster(inventory)
self.assertTrue(cluster.make_group_from_roles(['worker']).is_empty())

def test_error_no_control_planes_balancers(self):
inventory = demo.generate_inventory(master=0, worker=1, balancer=0)
inventory = demo.generate_inventory(control_plane=0, worker=1, balancer=0)
with test_utils.assert_raises_kme(self, 'KME0004'):
demo.new_cluster(inventory)

def test_error_no_control_planes(self):
inventory = demo.generate_inventory(master=0, worker=1, balancer=1)
inventory = demo.generate_inventory(control_plane=0, worker=1, balancer=1)
with test_utils.assert_raises_kme(self, 'KME0004'):
demo.new_cluster(inventory)

Expand Down
5 changes: 5 additions & 0 deletions test/unit/test_do.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from kubemarine import demo
from kubemarine.core import flow
from kubemarine.core.cluster import EnrichmentStage
from kubemarine.procedures import do


Expand All @@ -45,6 +46,10 @@ def test_command_single_master(self):

self.assertEqual('v0.28.0\n', buf.getvalue(), "Unexpected stdout output")

cluster = resources.cluster(EnrichmentStage.LIGHT)
self.assertEqual(['master'], cluster.inventory['nodes'][0]['roles'])
self.assertEqual('control-plane-1', cluster.make_group_from_roles(['control-plane', 'master']).get_node_name())

def test_command_run_any_node(self):
inventory = demo.generate_inventory(**demo.MINIHA_KEEPALIVED)
hosts = [node['address'] for node in inventory['nodes']]
Expand Down
7 changes: 7 additions & 0 deletions test/unit/test_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ def test_not_supported_role(self):
re.escape("Value should be one of ['worker', 'control-plane', 'master', 'balancer']")):
demo.new_cluster(inventory)

def test_not_supported_master_role(self):
inventory = demo.generate_inventory(control_plane=2, worker=0, balancer=0)
inventory['nodes'][0]['roles'] = ['master']
with self.assertRaisesRegex(errors.FailException,
re.escape("Value should be one of ['worker', 'control-plane', 'balancer']")):
demo.new_cluster(inventory)

def test_explicitly_added_service_role(self):
error_regex = r"Value should be one of \['worker', 'control-plane', 'master', 'balancer']"
inventory = demo.generate_inventory(**demo.MINIHA_KEEPALIVED)
Expand Down

0 comments on commit b7b44e3

Please sign in to comment.