File: conf.py

package info (click to toggle)
python-stem 1.2.2-1.1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,568 kB
  • ctags: 2,036
  • sloc: python: 20,108; makefile: 127; sh: 3
file content (138 lines) | stat: -rw-r--r-- 3,774 bytes parent folder | download | duplicates (2)
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
"""
Integration tests for the stem.util.conf class and functions.
"""

import os
import unittest

import stem.util.conf
import test.runner

CONF_HEADER = """# Demo configuration for integration tests to run against. Nothing to see here,
# move along, move along.
"""

EXAMPLE_CONF = """
destination.ip 1.2.3.4
destination.port blarg

startup.run export PATH=$PATH:~/bin
startup.run alias l=ls
"""

MULTILINE_CONF = """
multiline.entry.simple
|la de da
|and a ho hum

multiline.entry.leading_whitespace
 |la de da
     |and a ho hum

multiline.entry.empty

multiline.entry.squashed_top
|la de da
|and a ho hum
multiline.entry.squashed_bottom
|la de da
|and a ho hum
"""

HERALD_POEM = """
What a beautiful morning,
what a beautiful day.
Why are those arrows",
coming my way?!?"""


def _get_test_config_path():
  return test.runner.get_runner().get_test_dir('integ_test_cfg')


def _make_config(contents):
  """
  Writes a test configuration to disk, returning the path where it is located.
  """

  test_config_path = _get_test_config_path()

  test_conf_file = open(test_config_path, 'w')
  test_conf_file.write(CONF_HEADER)
  test_conf_file.write(contents)
  test_conf_file.close()

  return test_config_path


class TestConf(unittest.TestCase):
  def tearDown(self):
    # clears the config contents
    test_config = stem.util.conf.get_config('integ_testing')
    test_config.clear()
    test_config.clear_listeners()

    # cleans up test configurations we made
    test_config_path = _get_test_config_path()

    if os.path.exists(test_config_path):
      os.remove(test_config_path)

  def test_example(self):
    """
    Checks that the pydoc example is correct.
    """

    ssh_config = stem.util.conf.config_dict('integ_testing', {
      'login.user': 'atagar',
      'login.password': 'pepperjack_is_awesome!',
      'destination.ip': '127.0.0.1',
      'destination.port': 22,
      'startup.run': [],
    })

    test_config_path = _make_config(EXAMPLE_CONF)
    user_config = stem.util.conf.get_config('integ_testing')
    user_config.load(test_config_path)

    self.assertEquals('atagar', ssh_config['login.user'])
    self.assertEquals('pepperjack_is_awesome!', ssh_config['login.password'])
    self.assertEquals('1.2.3.4', ssh_config['destination.ip'])
    self.assertEquals(22, ssh_config['destination.port'])
    self.assertEquals(['export PATH=$PATH:~/bin', 'alias l=ls'], ssh_config['startup.run'])

  def test_load_multiline(self):
    """
    Tests the load method with multi-line configuration files.
    """

    test_config_path = _make_config(MULTILINE_CONF)
    test_config = stem.util.conf.get_config('integ_testing')
    test_config.load(test_config_path)

    for entry in ('simple', 'leading_whitespace', 'squashed_top', 'squashed_bottom'):
      self.assertEquals('la de da\nand a ho hum', test_config.get('multiline.entry.%s' % entry))

    self.assertEquals('', test_config.get('multiline.entry.empty'))

  def test_save(self):
    """
    Saves then reloads a configuration with several types of values.
    """

    # makes a configuration with a variety of types
    test_config = stem.util.conf.get_config('integ_testing')

    test_config.set('single_value', "yup, I'm there")
    test_config.set('multiple_values', 'a', False)
    test_config.set('multiple_values', 'b', False)
    test_config.set('multiple_values', 'c', False)
    test_config.set('multiline_value', HERALD_POEM)

    test_config.save(_get_test_config_path())
    test_config.clear()
    test_config.load()

    self.assertEquals("yup, I'm there", test_config.get_value('single_value'))
    self.assertEquals(['a', 'b', 'c'], test_config.get_value('multiple_values', multiple = True))
    self.assertEquals(HERALD_POEM, test_config.get_value('multiline_value'))