Skip to content

Commit

Permalink
Add test case for componse-node-name
Browse files Browse the repository at this point in the history
This commit is based on the test case introduced in PR kapicorp#1138.

We move the compose-node-name test implementation to `tests/` so it's
stored in the same place as the other tests and make the test class
inheritable so that we can reuse the test for reclass-rs.

Additionally we extend the test to check that all targets found by
`search_targets()` are rendered by `render_targets()`. This is needed
since we don't use reclass(-rs)'s target discovery logic in
`search_targets()` but instead use a simplified version that's
implemented directly in Kapitan's inventory backend base class.

However, `render_targets()` then renders whatever targets reclass(-rs)'s
target discovery finds, so if there's a mismatch we'd want to be
informed.

Co-authored-by: Matteo Voges <matteo.voges@nexenio.com>
Co-authored-by: Simon Gerber <simon.gerber@vshn.ch>
  • Loading branch information
simu and MatteoVoges committed Mar 7, 2024
1 parent 112b492 commit 251e82b
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions tests/test_compose_node_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import glob
import logging
import os
import shutil
import tempfile
import unittest

from kapitan.inventory import ReclassInventory
from kapitan.inventory import ReclassRsInventory
from kapitan.inventory.inventory import InventoryError
from kapitan import setup_logging


class ReclassComposeNodeNameTest(unittest.TestCase):
def setUp(self):
self.inventory = ReclassInventory

def test_compose_target_name(self):
inventory_path = "examples/kubernetes/inventory"
example_target_names = [
os.path.splitext(f)[0] for f in os.listdir(os.path.join(inventory_path, "targets"))
]

temp_inventory_dir = tempfile.mkdtemp()
shutil.copytree(inventory_path, temp_inventory_dir, dirs_exist_ok=True)

# ensure normal rendering works
compose_target_name = True
inv = self.inventory(temp_inventory_dir, compose_target_name)
found_targets = inv.search_targets()
self.assertEqual(example_target_names, list(found_targets.keys()))
# ensure that actual rendering finds the same nodes as `search_targets()`
for t in example_target_names:
nodeinfo = inv.get_target(t)
self.assertTrue(nodeinfo is not None)

# create compose_target_name setup
targets_path = os.path.join(inventory_path, "targets")
shutil.copytree(targets_path, os.path.join(temp_inventory_dir, "targets", "env1"))
shutil.copytree(targets_path, os.path.join(temp_inventory_dir, "targets", "env2"))

composed_target_names = []
for name in example_target_names:
composed_target_names.extend([name, f"env1.{name}", f"env2.{name}"])

# ensure inventory detects name collision
compose_target_name = False
inv = self.inventory(temp_inventory_dir, compose_target_name)
with self.assertRaises(InventoryError):
inv.search_targets()

# ensure compose_target_name works as intended
compose_target_name = True
inv = self.inventory(temp_inventory_dir, compose_target_name)
found_targets = inv.search_targets()

self.assertEqual(set(composed_target_names), set(found_targets.keys()))
# ensure that actual rendering finds the same nodes as `search_targets()`
for t in composed_target_names:
nodeinfo = inv.get_target(t)
self.assertTrue(nodeinfo is not None)

shutil.rmtree(temp_inventory_dir)


class ReclassRsComposeNodeNameTest(ReclassComposeNodeNameTest):
def setUp(self):
self.inventory = ReclassRsInventory

0 comments on commit 251e82b

Please sign in to comment.