blob: 5fb3faad0c3c76ec4da1f423caff0341bd7d59d7 [file] [log] [blame]
Felipe Monteiroeb197db2018-06-10 12:21:49 -04001Patrole Coding Guide
2====================
DavidPurcell663aedf2017-01-03 10:01:14 -05003
gaozx6f663e02017-08-10 10:24:16 +08004- 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 Monteiro0854ded2017-05-05 16:30:55 +01006- Step 3: Read on
Felipe Monteiro7bc35dc2017-04-19 21:11:46 +01007
Felipe Monteiro0854ded2017-05-05 16:30:55 +01008Patrole Specific Commandments
9------------------------------
10
11Patrole borrows the following commandments from Tempest; refer to
gaozx6f663e02017-08-10 10:24:16 +080012`Tempest's Commandments <https://docs.openstack.org/tempest/latest/HACKING.html>`__
Felipe Monteiro0854ded2017-05-05 16:30:55 +010013for 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 Monteiro0854ded2017-05-05 16:30:55 +010020
Felipe Monteirofdc45142018-07-18 20:54:20 +010021- [T102] Cannot import OpenStack python clients in
22 ``patrole_tempest_plugin/tests/api``
Felipe Monteiro0854ded2017-05-05 16:30:55 +010023- [T105] Tests cannot use setUpClass/tearDownClass
Felipe Monteiro0854ded2017-05-05 16:30:55 +010024- [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 Monteirofdc45142018-07-18 20:54:20 +010027 ``decorators.skip_because`` from ``tempest.lib``
28- [T113] Check that tests use ``data_utils.rand_uuid()`` instead of
29 ``uuid.uuid4()``
Felipe Monteiro0854ded2017-05-05 16:30:55 +010030- [N322] Method's default argument shouldn't be mutable
31
32The following are Patrole's specific Commandments:
33
34- [P100] The ``rbac_rule_validation.action`` decorator must be applied to
Felipe Monteiro904a02b2018-10-21 12:54:46 -040035 all RBAC tests
Felipe Monteiro0854ded2017-05-05 16:30:55 +010036- [P101] RBAC test filenames must end with "_rbac.py"; for example,
Felipe Monteirofdc45142018-07-18 20:54:20 +010037 test_servers_rbac.py, not test_servers.py
Felipe Monteiro0854ded2017-05-05 16:30:55 +010038- [P102] RBAC test class names must end in 'RbacTest'
Samantha Blancocd870772017-05-22 14:23:17 -040039- [P103] ``self.client`` must not be used as a client alias; this allows for
Felipe Monteirofdc45142018-07-18 20:54:20 +010040 code that is more maintainable and easier to read
Felipe Monteirobbbdd932018-10-31 23:28:39 -040041- [P104] RBAC `extension test class`_ names must end in 'ExtRbacTest'
Felipe Monteiro904a02b2018-10-21 12:54:46 -040042
Luigi Toscano6da06ed2019-01-07 17:50:41 +010043.. _extension test class: https://git.openstack.org/cgit/openstack/patrole/plain/patrole_tempest_plugin/tests/api/network/README.rst
Felipe Monteiro0854ded2017-05-05 16:30:55 +010044
Felipe Monteiroe36a9732018-11-16 17:28:52 +000045Supported OpenStack Components
46------------------------------
47
48Patrole only offers **in-tree** integration testing coverage for the following
49components:
50
51* Cinder
52* Glance
53* Keystone
54* Neutron
55* Nova
56
57Patrole currently has no stable library, so reliance upon Patrole's framework
58for external RBAC testing should be done with caution. Nonetheless, even when
59Patrole has a stable library, it will only offer in-tree RBAC testing for
60the components listed above.
61
Felipe Monteiro1c8620a2018-02-25 18:52:22 +000062Role Overriding
63---------------
Felipe Monteiro0854ded2017-05-05 16:30:55 +010064
Felipe Monteiro1c8620a2018-02-25 18:52:22 +000065Correct role overriding is vital to correct RBAC testing within Patrole. If a
Sergey Vilgelm78e7f572019-02-03 10:35:01 -060066test does not call ``self.override_role()`` within the RBAC test, followed
Felipe Monteiro1c8620a2018-02-25 18:52:22 +000067by 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
69with admin role, which is always wrong unless ``CONF.patrole.rbac_test_role``
70is also admin.
Felipe Monteiro0854ded2017-05-05 16:30:55 +010071
Felipe Monteiro1c8620a2018-02-25 18:52:22 +000072.. todo::
Felipe Monteiro0854ded2017-05-05 16:30:55 +010073
Felipe Monteiro1c8620a2018-02-25 18:52:22 +000074 Patrole does not have a hacking check for role overriding, but one may be
75 added in the future.
Felipe Monteiro9ae705d2018-03-26 22:14:44 -040076
77Branchless Patrole Considerations
78---------------------------------
79
80Like Tempest, Patrole is branchless. This is to better ensure API and RBAC
81consistency between releases because API and RBAC behavior should not change
82between releases. This means that the stable branches are also gated by the
83Patrole master branch, which also means that proposed commits to Patrole must
84work against both the master and all the currently supported stable branches
85of the projects. As such there are a few special considerations that have to
86be accounted for when pushing new changes to Patrole.
87
881. New Tests for new features
89^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
90
91Patrole, like Tempest, *implicitly* tests new features because new policies
92oftentimes accompany new features. The same `Tempest philosophy`_ regarding
93feature 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
972. New Tests for new policies
98^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
99
100When adding tests for new policies that were not in previous releases of the
101projects, the new test must be properly skipped with a feature flag. This
102involves using the ``testtools.skip(Unless|If)`` decorator above the test
103to check if the required policy is enabled. Similarly, a feature flag must
104be used whenever an OpenStack service covered by Patrole changes one of its
105policies in a backwards-incompatible way. If there isn't a method of selecting
106the new policy from the config file then there won't be a mechanism to disable
107the test with older stable releases and the new test won't be able to merge.
108
109Introduction of a new feature flag requires specifying a default value for the
110corresponding config option that is appropriate in the latest OpenStack
111release. Because Patrole is branchless, the feature flag's default value will
112need to be overridden to a value that is appropriate in earlier releases in
113which the feature isn't available. In DevStack, this can be accomplished by
114modifying Patrole's lib installation script for previous branches (because
115DevStack is branched).
116
1173. Bug fix on core project needing Patrole changes
118^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
119
120When trying to land a bug fix which changes a tested API you'll have to use the
121following 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
131Otherwise the bug fix won't be able to land in the project.
132
1334. New Tests for existing features or policies
134^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
135
136The same `Tempest logic`_ regarding new tests for existing features or
137policies also applies to Patrole.
138
139.. _Tempest logic: https://docs.openstack.org/tempest/latest/HACKING.html#new-tests-for-existing-features
Felipe Monteiro4d4cb1e2018-07-29 13:44:10 -0400140
141
142Black Box vs. White Box Testing
143-------------------------------
144
145Tempest is a `black box testing framework`_, meaning that it is concerned with
146testing public API endpoints and doesn't concern itself with testing internal
147implementation details. Patrole, as a Tempest plugin, also falls underneath
148the category of black box testing. However, even with policy in code
149documentation, some degree of white box testing is required in order to
150correctly write RBAC tests.
151
152This is because :ref:`policy-in-code` documentation, while useful in many
153respects, is usually quite brief and its main purpose is to help operators
154understand how to customize policy configuration rather than to help
155developers understand complex policy authorization work flows. For example,
156policy in code documentation doesn't make deriving
157:ref:`multiple policies <multiple-policies>` easy. Such documentation also
158doesn't usually mention that a specific parameter needs to be set, or that a
159particular microversion must be enabled, or that a particular set of
160prerequisite API or policy actions must be executed, in order for the policy
161under test to be enforced by the server. This means that test writers must
162account for the internal RBAC implementation in API code in order to correctly
163understand the complete RBAC work flow within an API.
164
165Besides, as mentioned :ref:`elsewhere <design-principles>` in this
166documentation, not all services currently implement policy in code, making
167some degree of white box testing a "necessary evil" for writing robust RBAC
168tests.
169
170.. _black box testing framework: https://docs.openstack.org/tempest/latest/HACKING.html#negative-tests