blob: cce8674c9cddae16388e29a3fba0a0c81d81c767 [file] [log] [blame]
Alex Klein2b236722019-06-19 15:44:26 -06001# -*- 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
8from __future__ import print_function
9
10import os
Mike Frysingeref94e4c2020-02-10 23:59:54 -050011import sys
Alex Klein2b236722019-06-19 15:44:26 -060012
Alex Klein69339cc2019-07-22 14:08:35 -060013from chromite.api import api_config
Alex Klein2b236722019-06-19 15:44:26 -060014from chromite.api import validate
15from chromite.api.gen.chromiumos import common_pb2
16from chromite.lib import cros_build_lib
17from chromite.lib import cros_test_lib
18from chromite.lib import osutils
19
20
Mike Frysingeref94e4c2020-02-10 23:59:54 -050021assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
22
23
Alex Klein2008aee2019-08-20 16:25:27 -060024class ExistsTest(cros_test_lib.TempDirTestCase, api_config.ApiConfigMixin):
Alex Klein2b236722019-06-19 15:44:26 -060025 """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 Klein2008aee2019-08-20 16:25:27 -060032 def impl(_input_proto, _output_proto, _config):
Alex Klein2b236722019-06-19 15:44:26 -060033 self.fail('Incorrectly allowed method to execute.')
34
35 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein2008aee2019-08-20 16:25:27 -060036 impl(common_pb2.Chroot(path=path), None, self.api_config)
Alex Klein2b236722019-06-19 15:44:26 -060037
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 Klein2008aee2019-08-20 16:25:27 -060044 def impl(_input_proto, _output_proto, _config):
Alex Klein2b236722019-06-19 15:44:26 -060045 pass
46
Alex Klein2008aee2019-08-20 16:25:27 -060047 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 Klein2b236722019-06-19 15:44:26 -060057
58
Alex Klein2008aee2019-08-20 16:25:27 -060059class IsInTest(cros_test_lib.TestCase, api_config.ApiConfigMixin):
Alex Klein231d2da2019-07-22 16:44:45 -060060 """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 Klein2008aee2019-08-20 16:25:27 -060065 def impl(_input_proto, _output_proto, _config):
Alex Klein231d2da2019-07-22 16:44:45 -060066 pass
67
68 # Make sure all of the values work.
Alex Klein2008aee2019-08-20 16:25:27 -060069 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 Klein231d2da2019-07-22 16:44:45 -060071
72 def test_not_in(self):
73 """Test an invalid value."""
74 @validate.is_in('path', ['/chroot/path', '/other/chroot/path'])
Alex Klein2008aee2019-08-20 16:25:27 -060075 def impl(_input_proto, _output_proto, _config):
Alex Klein231d2da2019-07-22 16:44:45 -060076 pass
77
78 # Should be failing on the invalid value.
79 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein2008aee2019-08-20 16:25:27 -060080 impl(common_pb2.Chroot(path='/bad/value'), None, self.api_config)
Alex Klein231d2da2019-07-22 16:44:45 -060081
82 def test_not_set(self):
83 """Test an unset value."""
84 @validate.is_in('path', ['/chroot/path', '/other/chroot/path'])
Alex Klein2008aee2019-08-20 16:25:27 -060085 def impl(_input_proto, _output_proto, _config):
Alex Klein231d2da2019-07-22 16:44:45 -060086 pass
87
88 # Should be failing without a value set.
89 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein2008aee2019-08-20 16:25:27 -060090 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 Klein231d2da2019-07-22 16:44:45 -0600100
101
Alex Klein2008aee2019-08-20 16:25:27 -0600102class RequiredTest(cros_test_lib.TestCase, api_config.ApiConfigMixin):
Alex Klein2b236722019-06-19 15:44:26 -0600103 """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 Klein2008aee2019-08-20 16:25:27 -0600109 def impl(_input_proto, _output_proto, _config):
Alex Klein2b236722019-06-19 15:44:26 -0600110 self.fail('Incorrectly allowed method to execute.')
111
112 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein2008aee2019-08-20 16:25:27 -0600113 impl(common_pb2.Chroot(), None, self.api_config)
Alex Klein2b236722019-06-19 15:44:26 -0600114
115 def test_not_set(self):
116 """Test validator fails when given an unset value."""
117
118 @validate.require('env.use_flags')
Alex Klein2008aee2019-08-20 16:25:27 -0600119 def impl(_input_proto, _output_proto, _config):
Alex Klein2b236722019-06-19 15:44:26 -0600120 self.fail('Incorrectly allowed method to execute.')
121
122 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein2008aee2019-08-20 16:25:27 -0600123 impl(common_pb2.Chroot(), None, self.api_config)
Alex Klein2b236722019-06-19 15:44:26 -0600124
125 def test_set(self):
126 """Test validator passes when given set values."""
127
128 @validate.require('path', 'env.use_flags')
Alex Klein2008aee2019-08-20 16:25:27 -0600129 def impl(_input_proto, _output_proto, _config):
Alex Klein2b236722019-06-19 15:44:26 -0600130 pass
131
Alex Klein2008aee2019-08-20 16:25:27 -0600132 in_proto = common_pb2.Chroot(path='/chroot/path',
133 env={'use_flags': [{'flag': 'test'}]})
134 impl(in_proto, None, self.api_config)
Alex Klein2b236722019-06-19 15:44:26 -0600135
136 def test_mixed(self):
137 """Test validator passes when given a set value."""
138
139 @validate.require('path', 'env.use_flags')
Alex Klein2008aee2019-08-20 16:25:27 -0600140 def impl(_input_proto, _output_proto, _config):
Alex Klein2b236722019-06-19 15:44:26 -0600141 pass
142
143 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein2008aee2019-08-20 16:25:27 -0600144 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 Klein69339cc2019-07-22 14:08:35 -0600154
155
156class 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 Klein2008aee2019-08-20 16:25:27 -0600178 self.fail('Incorrectly allowed method to execute.')
Alex Klein69339cc2019-07-22 14:08:35 -0600179
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)