|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Copyright 2024 Flant JSC Licensed under Apache License 2.0 |
| 4 | +# |
| 5 | + |
| 6 | +import unittest |
| 7 | +import typing |
| 8 | + |
| 9 | +from .hook import Output |
| 10 | + |
| 11 | + |
| 12 | +# msg: typing.Tuple[str, ...] | str | None |
| 13 | +def __assert_validation(t: unittest.TestCase, o: Output, allowed: bool, msg: typing.Union[typing.Tuple[str, ...], str, None]): |
| 14 | + v = o.validations |
| 15 | + |
| 16 | + t.assertEqual(len(v.data), 1) |
| 17 | + |
| 18 | + if allowed: |
| 19 | + t.assertTrue(v.data[0]["allowed"]) |
| 20 | + |
| 21 | + if not msg: |
| 22 | + return |
| 23 | + |
| 24 | + if isinstance(msg, str): |
| 25 | + t.assertEqual(len(v.data[0]["warnings"]), 1) |
| 26 | + t.assertEqual(v.data[0]["warnings"][0], msg) |
| 27 | + elif isinstance(msg, tuple): |
| 28 | + t.assertEqual(v.data[0]["warnings"], msg) |
| 29 | + else: |
| 30 | + t.fail("Incorrect msg type") |
| 31 | + else: |
| 32 | + if not isinstance(msg, str): |
| 33 | + t.fail("Incorrect msg type") |
| 34 | + |
| 35 | + t.assertIsNotNone(msg) |
| 36 | + t.assertIsInstance(msg, str) |
| 37 | + t.assertFalse(v.data[0]["allowed"]) |
| 38 | + t.assertEqual(v.data[0]["message"], msg) |
| 39 | + |
| 40 | + |
| 41 | +# msg: typing.Tuple[str, ...] | str | None |
| 42 | +def assert_validation_allowed(t: unittest.TestCase, o: Output, msg: typing.Union[typing.Tuple[str, ...], str, None]): |
| 43 | + """ |
| 44 | + Assert that validation webhook returns "allowed" result |
| 45 | +
|
| 46 | + Args: |
| 47 | + t (unittest.TestCase): unit test context (self in Test class method) |
| 48 | + o (hook.Output): output from hook.testrun |
| 49 | + msg (any): tuple or str or None, warnings for output, tuple for multiple warnings, str for one warning, None without warnings |
| 50 | + """ |
| 51 | + __assert_validation(t, o, True, msg) |
| 52 | + |
| 53 | + |
| 54 | +def assert_validation_deny(t: unittest.TestCase, o: Output, msg: str): |
| 55 | + """ |
| 56 | + Assert that validation webhook returns "deny" result |
| 57 | +
|
| 58 | + Args: |
| 59 | + t (unittest.TestCase): unit test context (self in Test class method) |
| 60 | + o (hook.Output): output from hook.testrun |
| 61 | + msg (str): failed message |
| 62 | + """ |
| 63 | + __assert_validation(t, o, False, msg) |
| 64 | + |
| 65 | + |
| 66 | +def assert_common_resource_fields(t: unittest.TestCase, obj: dict, api_version: str, name: str, namespace: str = ""): |
| 67 | + """ |
| 68 | + Assert for object represented as dict api version name and namespace |
| 69 | + This fixture may be useful for conversion webhook tests for checking |
| 70 | + that conversion webhook did not change name and namespace and set valid api version |
| 71 | +
|
| 72 | + Args: |
| 73 | + t (unittest.TestCase): unit test context (self in Test class method) |
| 74 | + obj (hook.Output): output from hook.testrun |
| 75 | + api_version (str): API version for expected object |
| 76 | + name (str): name of expected object |
| 77 | + namespace (str): namespace of expected object |
| 78 | + """ |
| 79 | + |
| 80 | + t.assertIn("apiVersion", obj) |
| 81 | + t.assertEqual(obj["apiVersion"], api_version) |
| 82 | + |
| 83 | + t.assertIn("metadata", obj) |
| 84 | + |
| 85 | + t.assertIn("name", obj["metadata"]) |
| 86 | + t.assertEqual(obj["metadata"]["name"], name) |
| 87 | + |
| 88 | + if namespace: |
| 89 | + t.assertIn("namespace", obj["metadata"]) |
| 90 | + t.assertEqual(obj["metadata"]["namespace"], namespace) |
| 91 | + |
| 92 | +# res: dict | typing.List[dict] | typing.Callable[[unittest.TestCase, typing.List[dict]], None] |
| 93 | +def assert_conversion(t: unittest.TestCase, o: Output, res: typing.Union[dict, typing.List[dict], typing.Callable[[unittest.TestCase, typing.List[dict]], None]], failed_msg: str): |
| 94 | + """ |
| 95 | + Assert result of conversion webhook |
| 96 | +
|
| 97 | + Args: |
| 98 | + t (unittest.TestCase): unit test context (self in Test class method) |
| 99 | + o (hook.Output): output from hook.testrun |
| 100 | + res (any): Can be: dict - for one resource convertion, list of dicts for conversion multiple objects per request |
| 101 | + or function callable[ (unittest.TestCase, typing.List[dict]) -> None ] for assert objects for your manner |
| 102 | + failed_msg (str | None): should present for asserting failed result of webhook |
| 103 | + """ |
| 104 | + |
| 105 | + d = o.conversions.data |
| 106 | + |
| 107 | + t.assertEqual(len(d), 1) |
| 108 | + |
| 109 | + if not failed_msg is None: |
| 110 | + t.assertEqual(len(d[0]), 1) |
| 111 | + t.assertEqual(d[0]["failedMessage"], failed_msg) |
| 112 | + return |
| 113 | + |
| 114 | + if callable(res): |
| 115 | + res(t, d[0]["convertedObjects"]) |
| 116 | + return |
| 117 | + |
| 118 | + expected = res |
| 119 | + if isinstance(res, dict): |
| 120 | + expected = [res] |
| 121 | + |
| 122 | + |
| 123 | + t.assertEqual(d[0]["convertedObjects"], expected) |
0 commit comments