blob: 9b9c0e4d52c19db54385115798accfe382309767 [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
12from chromite.api import validate
13from chromite.api.gen.chromiumos import common_pb2
14from chromite.lib import cros_build_lib
15from chromite.lib import cros_test_lib
16from chromite.lib import osutils
17
18
19class ExistsTest(cros_test_lib.TempDirTestCase):
20 """Tests for the exists validator."""
21
22 def test_not_exists(self):
23 """Test the validator fails when given a path that doesn't exist."""
24 path = os.path.join(self.tempdir, 'DOES_NOT_EXIST')
25
26 @validate.exists('path')
27 def impl(_input_proto):
28 self.fail('Incorrectly allowed method to execute.')
29
30 with self.assertRaises(cros_build_lib.DieSystemExit):
31 impl(common_pb2.Chroot(path=path))
32
33 def test_exists(self):
34 """Test the validator fails when given a path that doesn't exist."""
35 path = os.path.join(self.tempdir, 'chroot')
36 osutils.SafeMakedirs(path)
37
38 @validate.exists('path')
39 def impl(_input_proto):
40 pass
41
42 impl(common_pb2.Chroot(path=path))
43
44
45class RequiredTest(cros_test_lib.TestCase):
46 """Tests for the required validator."""
47
48 def test_invalid_field(self):
49 """Test validator fails when given an unset value."""
50
51 @validate.require('does.not.exist')
52 def impl(_input_proto):
53 self.fail('Incorrectly allowed method to execute.')
54
55 with self.assertRaises(cros_build_lib.DieSystemExit):
56 impl(common_pb2.Chroot())
57
58 def test_not_set(self):
59 """Test validator fails when given an unset value."""
60
61 @validate.require('env.use_flags')
62 def impl(_input_proto):
63 self.fail('Incorrectly allowed method to execute.')
64
65 with self.assertRaises(cros_build_lib.DieSystemExit):
66 impl(common_pb2.Chroot())
67
68 def test_set(self):
69 """Test validator passes when given set values."""
70
71 @validate.require('path', 'env.use_flags')
72 def impl(_input_proto):
73 pass
74
75 impl(common_pb2.Chroot(path='/chroot/path',
76 env={'use_flags': [{'flag': 'test'}]}))
77
78 def test_mixed(self):
79 """Test validator passes when given a set value."""
80
81 @validate.require('path', 'env.use_flags')
82 def impl(_input_proto):
83 pass
84
85 with self.assertRaises(cros_build_lib.DieSystemExit):
86 impl(common_pb2.Chroot(path='/chroot/path'))