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