blob: b38488a30e2dc78655828d2cd7c4b18be04d490a [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
20class ExistsTest(cros_test_lib.TempDirTestCase):
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')
28 def impl(_input_proto):
29 self.fail('Incorrectly allowed method to execute.')
30
31 with self.assertRaises(cros_build_lib.DieSystemExit):
32 impl(common_pb2.Chroot(path=path))
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')
40 def impl(_input_proto):
41 pass
42
43 impl(common_pb2.Chroot(path=path))
44
45
Alex Klein231d2da2019-07-22 16:44:45 -060046class IsInTest(cros_test_lib.TestCase):
47 """Tests for the is_in validator."""
48
49 def test_in(self):
50 """Test a valid value."""
51 @validate.is_in('path', ['/chroot/path', '/other/chroot/path'])
52 def impl(*_args):
53 pass
54
55 # Make sure all of the values work.
56 impl(common_pb2.Chroot(path='/chroot/path'))
57 impl(common_pb2.Chroot(path='/other/chroot/path'))
58
59 def test_not_in(self):
60 """Test an invalid value."""
61 @validate.is_in('path', ['/chroot/path', '/other/chroot/path'])
62 def impl(*_args):
63 pass
64
65 # Should be failing on the invalid value.
66 with self.assertRaises(cros_build_lib.DieSystemExit):
67 impl(common_pb2.Chroot(path='/bad/value'))
68
69 def test_not_set(self):
70 """Test an unset value."""
71 @validate.is_in('path', ['/chroot/path', '/other/chroot/path'])
72 def impl(*_args):
73 pass
74
75 # Should be failing without a value set.
76 with self.assertRaises(cros_build_lib.DieSystemExit):
77 impl(common_pb2.Chroot())
78
79
Alex Klein2b236722019-06-19 15:44:26 -060080class RequiredTest(cros_test_lib.TestCase):
81 """Tests for the required validator."""
82
83 def test_invalid_field(self):
84 """Test validator fails when given an unset value."""
85
86 @validate.require('does.not.exist')
87 def impl(_input_proto):
88 self.fail('Incorrectly allowed method to execute.')
89
90 with self.assertRaises(cros_build_lib.DieSystemExit):
91 impl(common_pb2.Chroot())
92
93 def test_not_set(self):
94 """Test validator fails when given an unset value."""
95
96 @validate.require('env.use_flags')
97 def impl(_input_proto):
98 self.fail('Incorrectly allowed method to execute.')
99
100 with self.assertRaises(cros_build_lib.DieSystemExit):
101 impl(common_pb2.Chroot())
102
103 def test_set(self):
104 """Test validator passes when given set values."""
105
106 @validate.require('path', 'env.use_flags')
107 def impl(_input_proto):
108 pass
109
110 impl(common_pb2.Chroot(path='/chroot/path',
111 env={'use_flags': [{'flag': 'test'}]}))
112
113 def test_mixed(self):
114 """Test validator passes when given a set value."""
115
116 @validate.require('path', 'env.use_flags')
117 def impl(_input_proto):
118 pass
119
120 with self.assertRaises(cros_build_lib.DieSystemExit):
121 impl(common_pb2.Chroot(path='/chroot/path'))
Alex Klein69339cc2019-07-22 14:08:35 -0600122
123
124class ValidateOnlyTest(cros_test_lib.TestCase, api_config.ApiConfigMixin):
125 """validate_only decorator tests."""
126
127 def test_validate_only(self):
128 """Test validate only."""
129 @validate.require('path')
130 @validate.validation_complete
131 def impl(_input_proto, _output_proto, _config):
132 self.fail('Implementation was called.')
133 return 1
134
135 # Just using arbitrary messages, we just need the
136 # (request, response, config) arguments so it can check the config.
137 rc = impl(common_pb2.Chroot(path='/chroot/path'), common_pb2.Chroot(),
138 self.validate_only_config)
139
140 self.assertEqual(0, rc)
141
142 def test_no_validate_only(self):
143 """Test no use of validate only."""
144 @validate.validation_complete
145 def impl(_input_proto, _output_proto, _config):
146 assert False
147
148 # We will get an assertion error unless validate_only prevents the function
149 # from being called.
150 with self.assertRaises(AssertionError):
151 impl(common_pb2.Chroot(), common_pb2.Chroot(), self.api_config)