blob: d5c49b595488438b8e83a9c60b83cc239c1a1190 [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
46class RequiredTest(cros_test_lib.TestCase):
47 """Tests for the required validator."""
48
49 def test_invalid_field(self):
50 """Test validator fails when given an unset value."""
51
52 @validate.require('does.not.exist')
53 def impl(_input_proto):
54 self.fail('Incorrectly allowed method to execute.')
55
56 with self.assertRaises(cros_build_lib.DieSystemExit):
57 impl(common_pb2.Chroot())
58
59 def test_not_set(self):
60 """Test validator fails when given an unset value."""
61
62 @validate.require('env.use_flags')
63 def impl(_input_proto):
64 self.fail('Incorrectly allowed method to execute.')
65
66 with self.assertRaises(cros_build_lib.DieSystemExit):
67 impl(common_pb2.Chroot())
68
69 def test_set(self):
70 """Test validator passes when given set values."""
71
72 @validate.require('path', 'env.use_flags')
73 def impl(_input_proto):
74 pass
75
76 impl(common_pb2.Chroot(path='/chroot/path',
77 env={'use_flags': [{'flag': 'test'}]}))
78
79 def test_mixed(self):
80 """Test validator passes when given a set value."""
81
82 @validate.require('path', 'env.use_flags')
83 def impl(_input_proto):
84 pass
85
86 with self.assertRaises(cros_build_lib.DieSystemExit):
87 impl(common_pb2.Chroot(path='/chroot/path'))
Alex Klein69339cc2019-07-22 14:08:35 -060088
89
90class ValidateOnlyTest(cros_test_lib.TestCase, api_config.ApiConfigMixin):
91 """validate_only decorator tests."""
92
93 def test_validate_only(self):
94 """Test validate only."""
95 @validate.require('path')
96 @validate.validation_complete
97 def impl(_input_proto, _output_proto, _config):
98 self.fail('Implementation was called.')
99 return 1
100
101 # Just using arbitrary messages, we just need the
102 # (request, response, config) arguments so it can check the config.
103 rc = impl(common_pb2.Chroot(path='/chroot/path'), common_pb2.Chroot(),
104 self.validate_only_config)
105
106 self.assertEqual(0, rc)
107
108 def test_no_validate_only(self):
109 """Test no use of validate only."""
110 @validate.validation_complete
111 def impl(_input_proto, _output_proto, _config):
112 assert False
113
114 # We will get an assertion error unless validate_only prevents the function
115 # from being called.
116 with self.assertRaises(AssertionError):
117 impl(common_pb2.Chroot(), common_pb2.Chroot(), self.api_config)