Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | # Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Tests for the validate module.""" |
| 7 | |
| 8 | from __future__ import print_function |
| 9 | |
| 10 | import os |
Mike Frysinger | ef94e4c | 2020-02-10 23:59:54 -0500 | [diff] [blame] | 11 | import sys |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 12 | |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 13 | from chromite.api import api_config |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 14 | from chromite.api import validate |
| 15 | from chromite.api.gen.chromiumos import common_pb2 |
| 16 | from chromite.lib import cros_build_lib |
| 17 | from chromite.lib import cros_test_lib |
| 18 | from chromite.lib import osutils |
| 19 | |
| 20 | |
Mike Frysinger | ef94e4c | 2020-02-10 23:59:54 -0500 | [diff] [blame] | 21 | assert sys.version_info >= (3, 6), 'This module requires Python 3.6+' |
| 22 | |
| 23 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 24 | class ExistsTest(cros_test_lib.TempDirTestCase, api_config.ApiConfigMixin): |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 25 | """Tests for the exists validator.""" |
| 26 | |
| 27 | def test_not_exists(self): |
| 28 | """Test the validator fails when given a path that doesn't exist.""" |
| 29 | path = os.path.join(self.tempdir, 'DOES_NOT_EXIST') |
| 30 | |
| 31 | @validate.exists('path') |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 32 | def impl(_input_proto, _output_proto, _config): |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 33 | self.fail('Incorrectly allowed method to execute.') |
| 34 | |
| 35 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 36 | impl(common_pb2.Chroot(path=path), None, self.api_config) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 37 | |
| 38 | def test_exists(self): |
| 39 | """Test the validator fails when given a path that doesn't exist.""" |
| 40 | path = os.path.join(self.tempdir, 'chroot') |
| 41 | osutils.SafeMakedirs(path) |
| 42 | |
| 43 | @validate.exists('path') |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 44 | def impl(_input_proto, _output_proto, _config): |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 45 | pass |
| 46 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 47 | impl(common_pb2.Chroot(path=path), None, self.api_config) |
| 48 | |
| 49 | def test_skip_validation(self): |
| 50 | """Test skipping validation case.""" |
| 51 | @validate.exists('path') |
| 52 | def impl(_input_proto, _output_proto, _config): |
| 53 | pass |
| 54 | |
| 55 | # This would otherwise raise an error for an invalid path. |
| 56 | impl(common_pb2.Chroot(), None, self.no_validate_config) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 57 | |
| 58 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 59 | class IsInTest(cros_test_lib.TestCase, api_config.ApiConfigMixin): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 60 | """Tests for the is_in validator.""" |
| 61 | |
| 62 | def test_in(self): |
| 63 | """Test a valid value.""" |
| 64 | @validate.is_in('path', ['/chroot/path', '/other/chroot/path']) |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 65 | def impl(_input_proto, _output_proto, _config): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 66 | pass |
| 67 | |
| 68 | # Make sure all of the values work. |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 69 | impl(common_pb2.Chroot(path='/chroot/path'), None, self.api_config) |
| 70 | impl(common_pb2.Chroot(path='/other/chroot/path'), None, self.api_config) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 71 | |
| 72 | def test_not_in(self): |
| 73 | """Test an invalid value.""" |
| 74 | @validate.is_in('path', ['/chroot/path', '/other/chroot/path']) |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 75 | def impl(_input_proto, _output_proto, _config): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 76 | pass |
| 77 | |
| 78 | # Should be failing on the invalid value. |
| 79 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 80 | impl(common_pb2.Chroot(path='/bad/value'), None, self.api_config) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 81 | |
| 82 | def test_not_set(self): |
| 83 | """Test an unset value.""" |
| 84 | @validate.is_in('path', ['/chroot/path', '/other/chroot/path']) |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 85 | def impl(_input_proto, _output_proto, _config): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 86 | pass |
| 87 | |
| 88 | # Should be failing without a value set. |
| 89 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 90 | impl(common_pb2.Chroot(), None, self.api_config) |
| 91 | |
| 92 | def test_skip_validation(self): |
| 93 | """Test skipping validation case.""" |
| 94 | @validate.is_in('path', ['/chroot/path', '/other/chroot/path']) |
| 95 | def impl(_input_proto, _output_proto, _config): |
| 96 | pass |
| 97 | |
| 98 | # This would otherwise raise an error for an invalid path. |
| 99 | impl(common_pb2.Chroot(), None, self.no_validate_config) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 100 | |
| 101 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 102 | class RequiredTest(cros_test_lib.TestCase, api_config.ApiConfigMixin): |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 103 | """Tests for the required validator.""" |
| 104 | |
| 105 | def test_invalid_field(self): |
| 106 | """Test validator fails when given an unset value.""" |
| 107 | |
| 108 | @validate.require('does.not.exist') |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 109 | def impl(_input_proto, _output_proto, _config): |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 110 | self.fail('Incorrectly allowed method to execute.') |
| 111 | |
| 112 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 113 | impl(common_pb2.Chroot(), None, self.api_config) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 114 | |
| 115 | def test_not_set(self): |
| 116 | """Test validator fails when given an unset value.""" |
| 117 | |
| 118 | @validate.require('env.use_flags') |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 119 | def impl(_input_proto, _output_proto, _config): |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 120 | self.fail('Incorrectly allowed method to execute.') |
| 121 | |
| 122 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 123 | impl(common_pb2.Chroot(), None, self.api_config) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 124 | |
| 125 | def test_set(self): |
| 126 | """Test validator passes when given set values.""" |
| 127 | |
| 128 | @validate.require('path', 'env.use_flags') |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 129 | def impl(_input_proto, _output_proto, _config): |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 130 | pass |
| 131 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 132 | in_proto = common_pb2.Chroot(path='/chroot/path', |
| 133 | env={'use_flags': [{'flag': 'test'}]}) |
| 134 | impl(in_proto, None, self.api_config) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 135 | |
| 136 | def test_mixed(self): |
| 137 | """Test validator passes when given a set value.""" |
| 138 | |
| 139 | @validate.require('path', 'env.use_flags') |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 140 | def impl(_input_proto, _output_proto, _config): |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 141 | pass |
| 142 | |
| 143 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 144 | impl(common_pb2.Chroot(path='/chroot/path'), None, self.api_config) |
| 145 | |
| 146 | def test_skip_validation(self): |
| 147 | """Test skipping validation case.""" |
| 148 | @validate.require('path', 'env.use_flags') |
| 149 | def impl(_input_proto, _output_proto, _config): |
| 150 | pass |
| 151 | |
| 152 | # This would otherwise raise an error for an invalid path. |
| 153 | impl(common_pb2.Chroot(), None, self.no_validate_config) |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 154 | |
| 155 | |
| 156 | class ValidateOnlyTest(cros_test_lib.TestCase, api_config.ApiConfigMixin): |
| 157 | """validate_only decorator tests.""" |
| 158 | |
| 159 | def test_validate_only(self): |
| 160 | """Test validate only.""" |
| 161 | @validate.require('path') |
| 162 | @validate.validation_complete |
| 163 | def impl(_input_proto, _output_proto, _config): |
| 164 | self.fail('Implementation was called.') |
| 165 | return 1 |
| 166 | |
| 167 | # Just using arbitrary messages, we just need the |
| 168 | # (request, response, config) arguments so it can check the config. |
| 169 | rc = impl(common_pb2.Chroot(path='/chroot/path'), common_pb2.Chroot(), |
| 170 | self.validate_only_config) |
| 171 | |
| 172 | self.assertEqual(0, rc) |
| 173 | |
| 174 | def test_no_validate_only(self): |
| 175 | """Test no use of validate only.""" |
| 176 | @validate.validation_complete |
| 177 | def impl(_input_proto, _output_proto, _config): |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 178 | self.fail('Incorrectly allowed method to execute.') |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 179 | |
| 180 | # We will get an assertion error unless validate_only prevents the function |
| 181 | # from being called. |
| 182 | with self.assertRaises(AssertionError): |
| 183 | impl(common_pb2.Chroot(), common_pb2.Chroot(), self.api_config) |