Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

# Copyright (C) 2010  Internet Systems Consortium. 

# 

# Permission to use, copy, modify, and distribute this software for any 

# purpose with or without fee is hereby granted, provided that the above 

# copyright notice and this permission notice appear in all copies. 

# 

# THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM 

# DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL 

# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL 

# INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, 

# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING 

# FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 

# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 

# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 

 

# 

# Tests for the configuration manager run script 

# 

 

import unittest 

import os 

import sys 

import bind10_config 

from isc.testutils.parse_args import OptsError, TestOptParser 

 

class MyConfigManager: 

    def __init__(self, path, filename, session=None, rename_config_file=False): 

        self._path = path 

        self.read_config_called = False 

        self.notify_boss_called = False 

        self.run_called = False 

        self.write_config_called = False 

        self.rename_config_called = False 

        self.running = True 

        self.virtual_modules = [] 

 

    def read_config(self): 

        self.read_config_called = True 

 

    def notify_boss(self): 

        self.notify_boss_called = True 

 

    def run(self): 

        self.run_called = True 

 

    def write_config(self): 

        self.write_config_called = True 

 

    def rename_config_file(self, ofile, nfile): 

        self.rename_config_called = True 

 

    def set_virtual_module(self, spec, function): 

        self.virtual_modules.append((spec, function)) 

 

class TestPlugins(unittest.TestCase): 

    def test_load_plugins(self): 

        """Test we can successfully find and load the mock plugin.""" 

        # Let it load the plugin 

        b = __import__("b10-cfgmgr") 

        # The parameters aren't important for this test 

        cm = MyConfigManager(None, None) 

        b.load_plugins(os.environ['TESTDATA_PATH'] + os.sep + 'plugins', cm) 

        # Check exactly one plugin was loaded and the right data were fed into 

        # the cm 

        self.assertEqual(len(cm.virtual_modules), 1) 

        (spec, check) = cm.virtual_modules[0] 

        self.assertEqual(spec.get_module_name(), "mock_config_plugin") 

        self.assertEqual(check(None), "Mock config plugin") 

 

class TestConfigManagerStartup(unittest.TestCase): 

    def test_cfgmgr(self): 

        # some creative module use; 

        # b10-cfgmgr has a hypen, so we use __import__ 

        # this also gives us the chance to override the imported 

        # module ConfigManager in it. 

        b = __import__("b10-cfgmgr") 

        orig_load = b.load_plugins 

        b.PLUGIN_PATHS = ["/plugin/path"] 

        self.loaded_plugins = False 

        def load_plugins(path, cm): 

            # Check it's called with proper arguments 

            self.assertEqual(path, "/plugin/path") 

            self.assertTrue(isinstance(cm, MyConfigManager)) 

            self.loaded_plugins = True 

        b.load_plugins = load_plugins 

        b.ConfigManager = MyConfigManager 

 

        b.main() 

        b.load_plugins = orig_load 

 

        self.assertTrue(b.cm.read_config_called) 

        self.assertTrue(b.cm.notify_boss_called) 

        self.assertTrue(b.cm.run_called) 

        self.assertTrue(self.loaded_plugins) 

        # if there are no changes, config is not written 

        self.assertFalse(b.cm.write_config_called) 

        self.assertFalse(b.cm.rename_config_called) 

 

        self.assertTrue(b.cm.running) 

        b.signal_handler(None, None) 

        self.assertFalse(b.cm.running) 

 

        # TODO: take value from the 'global config module' 

        # (and rename the .in away from this file again) 

        data_path = "${prefix}/var/bind10-devel".replace("${prefix}", "/home/jelte/opt/bind10") 

        self.assertEqual(data_path, b.DATA_PATH) 

 

        # remove the 'module' again, or later tests may fail 

        # (if it is already present it won't be loaded again) 

        sys.modules.pop("b10-cfgmgr") 

 

    def test_cfgmgr_from_source(self): 

        tmp_env_var = "/just/some/dir" 

        env_var = None 

116        if "B10_FROM_SOURCE" in os.environ: 

            env_var = os.environ["B10_FROM_SOURCE"] 

 

        os.environ["B10_FROM_SOURCE"] = tmp_env_var 

        bind10_config.reload() 

        b = __import__("b10-cfgmgr", globals(), locals()) 

        b.PLUGIN_PATH = [] # It's enough to test plugins in one test 

        b.ConfigManager = MyConfigManager 

        self.assertEqual(tmp_env_var, b.DATA_PATH) 

 

126        if env_var != None: 

            os.environ["B10_FROM_SOURCE"] = env_var 

        bind10_config.reload() 

 

        sys.modules.pop("b10-cfgmgr") 

 

class TestParseArgs(unittest.TestCase): 

    """ 

    Test for the parsing of command line arguments. We provide a different 

    array to parse instead. 

    """ 

 

    def test_defaults(self): 

        """ 

        Test the default values when no options are provided. 

        """ 

        # Pass it empty array, not our arguments 

        b = __import__("b10-cfgmgr") 

        parsed = b.parse_options([], TestOptParser) 

        self.assertEqual(b.DATA_PATH, parsed.data_path) 

        self.assertEqual(b.DEFAULT_CONFIG_FILE, parsed.config_file) 

 

    def test_wrong_args(self): 

        """ 

        Test it fails when we pass invalid option. 

        """ 

        b = __import__("b10-cfgmgr") 

        self.assertRaises(OptsError, b.parse_options, ['--wrong-option'], 

                          TestOptParser) 

 

    def test_not_arg(self): 

        """ 

        Test it fails when there's an argument that's not option 

        (eg. without -- at the beginning). 

        """ 

        b = __import__("b10-cfgmgr") 

        self.assertRaises(OptsError, b.parse_options, ['not-option'], 

                          TestOptParser) 

 

    def test_datapath(self): 

        """ 

        Test overwriting the data path. 

        """ 

        b = __import__("b10-cfgmgr") 

        parsed = b.parse_options(['--data-path=/path'], TestOptParser) 

        self.assertEqual('/path', parsed.data_path) 

        self.assertEqual(b.DEFAULT_CONFIG_FILE, parsed.config_file) 

        parsed = b.parse_options(['-p', '/path'], TestOptParser) 

        self.assertEqual('/path', parsed.data_path) 

        self.assertEqual(b.DEFAULT_CONFIG_FILE, parsed.config_file) 

        self.assertRaises(OptsError, b.parse_options, ['-p'], TestOptParser) 

        self.assertRaises(OptsError, b.parse_options, ['--data-path'], 

                          TestOptParser) 

 

    def test_db_filename(self): 

        """ 

        Test setting the configuration database file. 

        """ 

        b = __import__("b10-cfgmgr") 

        parsed = b.parse_options(['--config-filename=filename'], 

                                 TestOptParser) 

        self.assertEqual(b.DATA_PATH, parsed.data_path) 

        self.assertEqual("filename", parsed.config_file) 

        parsed = b.parse_options(['-c', 'filename'], TestOptParser) 

        self.assertEqual(b.DATA_PATH, parsed.data_path) 

        self.assertEqual("filename", parsed.config_file) 

        self.assertRaises(OptsError, b.parse_options, ['-c'], TestOptParser) 

        self.assertRaises(OptsError, b.parse_options, ['--config-filename'], 

                          TestOptParser) 

 

    def test_clear_config(self): 

        b = __import__("b10-cfgmgr") 

        parsed = b.parse_options([], TestOptParser) 

        self.assertFalse(parsed.clear_config) 

        parsed = b.parse_options(['--clear-config'], TestOptParser) 

        self.assertTrue(parsed.clear_config) 

 

 

exitif __name__ == '__main__': 

    unittest.main()