summaryrefslogtreecommitdiffstats
path: root/testsuite/Testlib/TestOptions.py
blob: 4c87b2f42c33320fe9e78332cda552f8af30818f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import os
import sys
import unittest
from mock import Mock, patch
import Bcfg2.Options
from ..common import *

class TestOption(Bcfg2TestCase):
    def test__init(self):
        self.assertRaises(Bcfg2.Options.OptionFailure,
                          Bcfg2.Options.Option,
                          'foo', False, cmd='f')
        self.assertRaises(Bcfg2.Options.OptionFailure,
                          Bcfg2.Options.Option,
                          'foo', False, cmd='--f')
        self.assertRaises(Bcfg2.Options.OptionFailure,
                          Bcfg2.Options.Option,
                          'foo', False, cmd='-foo')
        self.assertRaises(Bcfg2.Options.OptionFailure,
                          Bcfg2.Options.Option,
                          'foo', False, cmd='-foo', long_arg=True)

    @patch('Bcfg2.Options.DefaultConfigParser')
    @patch('__builtin__.open')
    def test_get(self, mock_open, mock_cp):
        mock_cp.return_value = Mock()
        o = Bcfg2.Options.Option('foo', False, cmd='-f')
        self.assertFalse(o.cf)
        c = Bcfg2.Options.DefaultConfigParser()
        c.get('foo', False, cmd='-f')
        mock_cp.assert_any_call()
        mock_open.assert_any_call(Bcfg2.Options.DEFAULT_CONFIG_LOCATION)
        print(mock_cp.return_value.get.called)
        self.assertTrue(mock_cp.return_value.get.called)

    @patch('Bcfg2.Options.DefaultConfigParser')
    def test_parse(self, mock_cfp):
        cf = ('communication', 'password')
        o = Bcfg2.Options.Option('foo', default='test4', cmd='-F', env='TEST2',
                                 odesc='bar', cf=cf)
        o.parse([], ['-F', 'test'])
        self.assertEqual(o.value, 'test')
        o.parse([('-F', 'test2')], [])
        self.assertEqual(o.value, 'test2')

        os.environ['TEST2'] = 'test3'
        o.parse([], [])
        self.assertEqual(o.value, 'test3')
        del os.environ['TEST2']

        mock_cfp.get = Mock()
        mock_cfp.get.return_value = 'test5'
        o.parse([], [], configparser=mock_cfp)
        mock_cfp.get.assert_any_call(*cf)
        self.assertEqual(o.value, 'test5')

        o.cf = False
        o.parse([], [])
        assert o.value == 'test4'

    def test_cook(self):
        # check that default value isn't cooked
        o1 = Bcfg2.Options.Option('foo', 'test4', cook=Bcfg2.Options.get_bool)
        o1.parse([], [])
        assert o1.value == 'test4'
        o2 = Bcfg2.Options.Option('foo', False, cmd='-F')
        o2.parse([('-F', '')], [])
        assert o2.value == True


class TestOptionSet(Bcfg2TestCase):
    def test_buildGetopt(self):
        opts = [('foo', Bcfg2.Options.Option('foo', 'test1', cmd='-G')),
                ('bar', Bcfg2.Options.Option('foo', 'test2')),
                ('baz', Bcfg2.Options.Option('foo', 'test1', cmd='-H',
                                             odesc='1'))]
        oset = Bcfg2.Options.OptionSet(opts)
        res = oset.buildGetopt()
        self.assertIn('H:', res)
        self.assertIn('G', res)
        self.assertEqual(len(res), 3)

    def test_buildLongGetopt(self):
        opts = [('foo', Bcfg2.Options.Option('foo', 'test1', cmd='-G')),
                ('bar', Bcfg2.Options.Option('foo', 'test2')),
                ('baz', Bcfg2.Options.Option('foo', 'test1', cmd='--H',
                                             odesc='1', long_arg=True))]
        oset = Bcfg2.Options.OptionSet(opts)
        res = oset.buildLongGetopt()
        self.assertIn('H=', res)
        self.assertEqual(len(res), 1)

    def test_parse(self):
        opts = [('foo', Bcfg2.Options.Option('foo', 'test1', cmd='-G')),
                ('bar', Bcfg2.Options.Option('foo', 'test2')),
                ('baz', Bcfg2.Options.Option('foo', 'test1', cmd='-H',
                                             odesc='1'))]
        oset = Bcfg2.Options.OptionSet(opts)
        self.assertRaises(SystemExit,
                          oset.parse,
                          ['-G', '-H'])
        oset2 = Bcfg2.Options.OptionSet(opts)
        self.assertRaises(SystemExit,
                          oset2.parse,
                          ['-h'])
        oset3 = Bcfg2.Options.OptionSet(opts)
        oset3.parse(['-G'])
        self.assertTrue(oset3['foo'])


class TestOptionParser(Bcfg2TestCase):
    def test__init(self):
        opts = [('foo', Bcfg2.Options.Option('foo', 'test1', cmd='-h')),
                ('bar', Bcfg2.Options.Option('foo', 'test2')),
                ('baz', Bcfg2.Options.Option('foo', 'test1', cmd='-H',
                                             odesc='1'))]
        oset1 = Bcfg2.Options.OptionParser(opts)
        self.assertEqual(oset1.cfile,
                         Bcfg2.Options.DEFAULT_CONFIG_LOCATION)
        sys.argv = ['foo', '-C', '/usr/local/etc/bcfg2.conf']
        oset2 = Bcfg2.Options.OptionParser(opts)
        self.assertEqual(oset2.cfile,
                         '/usr/local/etc/bcfg2.conf')
        sys.argv = []
        oset3 = Bcfg2.Options.OptionParser(opts)
        self.assertEqual(oset3.cfile,
                         Bcfg2.Options.DEFAULT_CONFIG_LOCATION)