Felipe Monteiro | eb197db | 2018-06-10 12:21:49 -0400 | [diff] [blame] | 1 | Patrole Coding Guide |
| 2 | ==================== |
DavidPurcell | 663aedf | 2017-01-03 10:01:14 -0500 | [diff] [blame] | 3 | |
gaozx | 6f663e0 | 2017-08-10 10:24:16 +0800 | [diff] [blame] | 4 | - Step 1: Read the OpenStack Style Commandments: `<https://docs.openstack.org/hacking/latest/>`__ |
| 5 | - Step 2: Review Tempest's Style Commandments: `<https://docs.openstack.org/tempest/latest/HACKING.html>`__ |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 6 | - Step 3: Read on |
Felipe Monteiro | 7bc35dc | 2017-04-19 21:11:46 +0100 | [diff] [blame] | 7 | |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 8 | Patrole Specific Commandments |
| 9 | ------------------------------ |
| 10 | |
| 11 | Patrole borrows the following commandments from Tempest; refer to |
gaozx | 6f663e0 | 2017-08-10 10:24:16 +0800 | [diff] [blame] | 12 | `Tempest's Commandments <https://docs.openstack.org/tempest/latest/HACKING.html>`__ |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 13 | for more information: |
| 14 | |
| 15 | .. note:: |
| 16 | |
| 17 | The original Tempest Commandments do not include Patrole-specific paths. |
| 18 | Patrole-specific paths replace the Tempest-specific paths within Patrole's |
| 19 | hacking checks. |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 20 | |
Felipe Monteiro | fdc4514 | 2018-07-18 20:54:20 +0100 | [diff] [blame] | 21 | - [T102] Cannot import OpenStack python clients in |
| 22 | ``patrole_tempest_plugin/tests/api`` |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 23 | - [T105] Tests cannot use setUpClass/tearDownClass |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 24 | - [T107] Check that a service tag isn't in the module path |
| 25 | - [T108] Check no hyphen at the end of rand_name() argument |
| 26 | - [T109] Cannot use testtools.skip decorator; instead use |
Felipe Monteiro | fdc4514 | 2018-07-18 20:54:20 +0100 | [diff] [blame] | 27 | ``decorators.skip_because`` from ``tempest.lib`` |
| 28 | - [T113] Check that tests use ``data_utils.rand_uuid()`` instead of |
| 29 | ``uuid.uuid4()`` |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 30 | - [N322] Method's default argument shouldn't be mutable |
| 31 | |
| 32 | The following are Patrole's specific Commandments: |
| 33 | |
| 34 | - [P100] The ``rbac_rule_validation.action`` decorator must be applied to |
Felipe Monteiro | 904a02b | 2018-10-21 12:54:46 -0400 | [diff] [blame] | 35 | all RBAC tests |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 36 | - [P101] RBAC test filenames must end with "_rbac.py"; for example, |
Felipe Monteiro | fdc4514 | 2018-07-18 20:54:20 +0100 | [diff] [blame] | 37 | test_servers_rbac.py, not test_servers.py |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 38 | - [P102] RBAC test class names must end in 'RbacTest' |
Samantha Blanco | cd87077 | 2017-05-22 14:23:17 -0400 | [diff] [blame] | 39 | - [P103] ``self.client`` must not be used as a client alias; this allows for |
Felipe Monteiro | fdc4514 | 2018-07-18 20:54:20 +0100 | [diff] [blame] | 40 | code that is more maintainable and easier to read |
Felipe Monteiro | bbbdd93 | 2018-10-31 23:28:39 -0400 | [diff] [blame] | 41 | - [P104] RBAC `extension test class`_ names must end in 'ExtRbacTest' |
Felipe Monteiro | 904a02b | 2018-10-21 12:54:46 -0400 | [diff] [blame] | 42 | |
Luigi Toscano | 6da06ed | 2019-01-07 17:50:41 +0100 | [diff] [blame] | 43 | .. _extension test class: https://git.openstack.org/cgit/openstack/patrole/plain/patrole_tempest_plugin/tests/api/network/README.rst |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 44 | |
Felipe Monteiro | e36a973 | 2018-11-16 17:28:52 +0000 | [diff] [blame] | 45 | Supported OpenStack Components |
| 46 | ------------------------------ |
| 47 | |
| 48 | Patrole only offers **in-tree** integration testing coverage for the following |
| 49 | components: |
| 50 | |
| 51 | * Cinder |
| 52 | * Glance |
| 53 | * Keystone |
| 54 | * Neutron |
| 55 | * Nova |
| 56 | |
| 57 | Patrole currently has no stable library, so reliance upon Patrole's framework |
| 58 | for external RBAC testing should be done with caution. Nonetheless, even when |
| 59 | Patrole has a stable library, it will only offer in-tree RBAC testing for |
| 60 | the components listed above. |
| 61 | |
Felipe Monteiro | 1c8620a | 2018-02-25 18:52:22 +0000 | [diff] [blame] | 62 | Role Overriding |
| 63 | --------------- |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 64 | |
Felipe Monteiro | 1c8620a | 2018-02-25 18:52:22 +0000 | [diff] [blame] | 65 | Correct role overriding is vital to correct RBAC testing within Patrole. If a |
Sergey Vilgelm | 78e7f57 | 2019-02-03 10:35:01 -0600 | [diff] [blame] | 66 | test does not call ``self.override_role()`` within the RBAC test, followed |
Felipe Monteiro | 1c8620a | 2018-02-25 18:52:22 +0000 | [diff] [blame] | 67 | by the API endpoint that enforces the expected policy action, then the test is |
| 68 | **not** a valid Patrole test: The API endpoint under test will be performed |
| 69 | with admin role, which is always wrong unless ``CONF.patrole.rbac_test_role`` |
| 70 | is also admin. |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 71 | |
Felipe Monteiro | 1c8620a | 2018-02-25 18:52:22 +0000 | [diff] [blame] | 72 | .. todo:: |
Felipe Monteiro | 0854ded | 2017-05-05 16:30:55 +0100 | [diff] [blame] | 73 | |
Felipe Monteiro | 1c8620a | 2018-02-25 18:52:22 +0000 | [diff] [blame] | 74 | Patrole does not have a hacking check for role overriding, but one may be |
| 75 | added in the future. |
Felipe Monteiro | 9ae705d | 2018-03-26 22:14:44 -0400 | [diff] [blame] | 76 | |
| 77 | Branchless Patrole Considerations |
| 78 | --------------------------------- |
| 79 | |
| 80 | Like Tempest, Patrole is branchless. This is to better ensure API and RBAC |
| 81 | consistency between releases because API and RBAC behavior should not change |
| 82 | between releases. This means that the stable branches are also gated by the |
| 83 | Patrole master branch, which also means that proposed commits to Patrole must |
| 84 | work against both the master and all the currently supported stable branches |
| 85 | of the projects. As such there are a few special considerations that have to |
| 86 | be accounted for when pushing new changes to Patrole. |
| 87 | |
| 88 | 1. New Tests for new features |
| 89 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 90 | |
| 91 | Patrole, like Tempest, *implicitly* tests new features because new policies |
| 92 | oftentimes accompany new features. The same `Tempest philosophy`_ regarding |
| 93 | feature flags and new features also applies to Patrole. |
| 94 | |
| 95 | .. _Tempest philosophy: https://docs.openstack.org/tempest/latest/HACKING.html#new-tests-for-new-features |
| 96 | |
| 97 | 2. New Tests for new policies |
| 98 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 99 | |
| 100 | When adding tests for new policies that were not in previous releases of the |
| 101 | projects, the new test must be properly skipped with a feature flag. This |
| 102 | involves using the ``testtools.skip(Unless|If)`` decorator above the test |
| 103 | to check if the required policy is enabled. Similarly, a feature flag must |
| 104 | be used whenever an OpenStack service covered by Patrole changes one of its |
| 105 | policies in a backwards-incompatible way. If there isn't a method of selecting |
| 106 | the new policy from the config file then there won't be a mechanism to disable |
| 107 | the test with older stable releases and the new test won't be able to merge. |
| 108 | |
| 109 | Introduction of a new feature flag requires specifying a default value for the |
| 110 | corresponding config option that is appropriate in the latest OpenStack |
| 111 | release. Because Patrole is branchless, the feature flag's default value will |
| 112 | need to be overridden to a value that is appropriate in earlier releases in |
| 113 | which the feature isn't available. In DevStack, this can be accomplished by |
| 114 | modifying Patrole's lib installation script for previous branches (because |
| 115 | DevStack is branched). |
| 116 | |
| 117 | 3. Bug fix on core project needing Patrole changes |
| 118 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 119 | |
| 120 | When trying to land a bug fix which changes a tested API you'll have to use the |
| 121 | following procedure: |
| 122 | |
| 123 | #. Propose change to the project, get a +2 on the change even with the |
| 124 | test failing Patrole side. |
| 125 | #. Propose skip to the relevant Patrole test which will only be approved |
| 126 | after the corresponding change in the project has a +2. |
| 127 | #. Land project change in master and all open stable branches |
| 128 | (if required). |
| 129 | #. Land changed test in Patrole. |
| 130 | |
| 131 | Otherwise the bug fix won't be able to land in the project. |
| 132 | |
| 133 | 4. New Tests for existing features or policies |
| 134 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 135 | |
| 136 | The same `Tempest logic`_ regarding new tests for existing features or |
| 137 | policies also applies to Patrole. |
| 138 | |
| 139 | .. _Tempest logic: https://docs.openstack.org/tempest/latest/HACKING.html#new-tests-for-existing-features |
Felipe Monteiro | 4d4cb1e | 2018-07-29 13:44:10 -0400 | [diff] [blame] | 140 | |
| 141 | |
| 142 | Black Box vs. White Box Testing |
| 143 | ------------------------------- |
| 144 | |
| 145 | Tempest is a `black box testing framework`_, meaning that it is concerned with |
| 146 | testing public API endpoints and doesn't concern itself with testing internal |
| 147 | implementation details. Patrole, as a Tempest plugin, also falls underneath |
| 148 | the category of black box testing. However, even with policy in code |
| 149 | documentation, some degree of white box testing is required in order to |
| 150 | correctly write RBAC tests. |
| 151 | |
| 152 | This is because :ref:`policy-in-code` documentation, while useful in many |
| 153 | respects, is usually quite brief and its main purpose is to help operators |
| 154 | understand how to customize policy configuration rather than to help |
| 155 | developers understand complex policy authorization work flows. For example, |
| 156 | policy in code documentation doesn't make deriving |
| 157 | :ref:`multiple policies <multiple-policies>` easy. Such documentation also |
| 158 | doesn't usually mention that a specific parameter needs to be set, or that a |
| 159 | particular microversion must be enabled, or that a particular set of |
| 160 | prerequisite API or policy actions must be executed, in order for the policy |
| 161 | under test to be enforced by the server. This means that test writers must |
| 162 | account for the internal RBAC implementation in API code in order to correctly |
| 163 | understand the complete RBAC work flow within an API. |
| 164 | |
| 165 | Besides, as mentioned :ref:`elsewhere <design-principles>` in this |
| 166 | documentation, not all services currently implement policy in code, making |
| 167 | some degree of white box testing a "necessary evil" for writing robust RBAC |
| 168 | tests. |
| 169 | |
| 170 | .. _black box testing framework: https://docs.openstack.org/tempest/latest/HACKING.html#negative-tests |