blob: c39da65aa86cf3fdb9003d09a1a7167a33f6b8dc [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
11
Alex Klein69339cc2019-07-22 14:08:35 -060012from chromite.api import api_config
Alex Klein2b236722019-06-19 15:44:26 -060013from chromite.api import validate
14from chromite.api.gen.chromiumos import common_pb2
15from chromite.lib import cros_build_lib
16from chromite.lib import cros_test_lib
17from chromite.lib import osutils
18
19
Alex Klein2008aee2019-08-20 16:25:27 -060020class ExistsTest(cros_test_lib.TempDirTestCase, api_config.ApiConfigMixin):
Alex Klein2b236722019-06-19 15:44:26 -060021 """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 Klein2008aee2019-08-20 16:25:27 -060028 def impl(_input_proto, _output_proto, _config):
Alex Klein2b236722019-06-19 15:44:26 -060029 self.fail('Incorrectly allowed method to execute.')
30
31 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein2008aee2019-08-20 16:25:27 -060032 impl(common_pb2.Chroot(path=path), None, self.api_config)
Alex Klein2b236722019-06-19 15:44:26 -060033
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 Klein2008aee2019-08-20 16:25:27 -060040 def impl(_input_proto, _output_proto, _config):
Alex Klein2b236722019-06-19 15:44:26 -060041 pass
42
Alex Klein2008aee2019-08-20 16:25:27 -060043 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 Klein2b236722019-06-19 15:44:26 -060053
54
Alex Klein2008aee2019-08-20 16:25:27 -060055class IsInTest(cros_test_lib.TestCase, api_config.ApiConfigMixin):
Alex Klein231d2da2019-07-22 16:44:45 -060056 """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 Klein2008aee2019-08-20 16:25:27 -060061 def impl(_input_proto, _output_proto, _config):
Alex Klein231d2da2019-07-22 16:44:45 -060062 pass
63
64 # Make sure all of the values work.
Alex Klein2008aee2019-08-20 16:25:27 -060065 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 Klein231d2da2019-07-22 16:44:45 -060067
68 def test_not_in(self):
69 """Test an invalid value."""
70 @validate.is_in('path', ['/chroot/path', '/other/chroot/path'])
Alex Klein2008aee2019-08-20 16:25:27 -060071 def impl(_input_proto, _output_proto, _config):
Alex Klein231d2da2019-07-22 16:44:45 -060072 pass
73
74 # Should be failing on the invalid value.
75 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein2008aee2019-08-20 16:25:27 -060076 impl(common_pb2.Chroot(path='/bad/value'), None, self.api_config)
Alex Klein231d2da2019-07-22 16:44:45 -060077
78 def test_not_set(self):
79 """Test an unset value."""
80 @validate.is_in('path', ['/chroot/path', '/other/chroot/path'])
Alex Klein2008aee2019-08-20 16:25:27 -060081 def impl(_input_proto, _output_proto, _config):
Alex Klein231d2da2019-07-22 16:44:45 -060082 pass
83
84 # Should be failing without a value set.
85 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein2008aee2019-08-20 16:25:27 -060086 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 Klein231d2da2019-07-22 16:44:45 -060096
97
Alex Klein2008aee2019-08-20 16:25:27 -060098class RequiredTest(cros_test_lib.TestCase, api_config.ApiConfigMixin):
Alex Klein2b236722019-06-19 15:44:26 -060099 """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 Klein2008aee2019-08-20 16:25:27 -0600105 def impl(_input_proto, _output_proto, _config):
Alex Klein2b236722019-06-19 15:44:26 -0600106 self.fail('Incorrectly allowed method to execute.')
107
108 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein2008aee2019-08-20 16:25:27 -0600109 impl(common_pb2.Chroot(), None, self.api_config)
Alex Klein2b236722019-06-19 15:44:26 -0600110
111 def test_not_set(self):
112 """Test validator fails when given an unset value."""
113
114 @validate.require('env.use_flags')
Alex Klein2008aee2019-08-20 16:25:27 -0600115 def impl(_input_proto, _output_proto, _config):
Alex Klein2b236722019-06-19 15:44:26 -0600116 self.fail('Incorrectly allowed method to execute.')
117
118 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein2008aee2019-08-20 16:25:27 -0600119 impl(common_pb2.Chroot(), None, self.api_config)
Alex Klein2b236722019-06-19 15:44:26 -0600120
121 def test_set(self):
122 """Test validator passes when given set values."""
123
124 @validate.require('path', 'env.use_flags')
Alex Klein2008aee2019-08-20 16:25:27 -0600125 def impl(_input_proto, _output_proto, _config):
Alex Klein2b236722019-06-19 15:44:26 -0600126 pass
127
Alex Klein2008aee2019-08-20 16:25:27 -0600128 in_proto = common_pb2.Chroot(path='/chroot/path',
129 env={'use_flags': [{'flag': 'test'}]})
130 impl(in_proto, None, self.api_config)
Alex Klein2b236722019-06-19 15:44:26 -0600131
132 def test_mixed(self):
133 """Test validator passes when given a set value."""
134
135 @validate.require('path', 'env.use_flags')
Alex Klein2008aee2019-08-20 16:25:27 -0600136 def impl(_input_proto, _output_proto, _config):
Alex Klein2b236722019-06-19 15:44:26 -0600137 pass
138
139 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein2008aee2019-08-20 16:25:27 -0600140 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 Klein69339cc2019-07-22 14:08:35 -0600150
151
152class 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 Klein2008aee2019-08-20 16:25:27 -0600174 self.fail('Incorrectly allowed method to execute.')
Alex Klein69339cc2019-07-22 14:08:35 -0600175
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)