File: test_local.py

package info (click to toggle)
python-os-collect-config 14.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 360 kB
  • sloc: python: 3,089; makefile: 19
file content (153 lines) | stat: -rw-r--r-- 5,556 bytes parent folder | download
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
# Copyright (c) 2014 Hewlett-Packard Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import json
import locale
import os
import tempfile

import fixtures
from oslo_config import cfg
import testtools
from testtools import matchers

from os_collect_config import collect
from os_collect_config import exc
from os_collect_config import local


META_DATA = {'localstrA': 'A',
             'localint9': 9,
             'localmap_xy': {
                 'x': 42,
                 'y': 'foo',
             }}
META_DATA2 = {'localstrA': 'Z',
              'localint9': 9}


class TestLocal(testtools.TestCase):
    def setUp(self):
        super().setUp()
        self.log = self.useFixture(fixtures.FakeLogger())
        self.useFixture(fixtures.NestedTempfile())
        self.tdir = tempfile.mkdtemp()
        collect.setup_conf()
        self.addCleanup(cfg.CONF.reset)
        cfg.CONF.register_cli_opts(local.opts, group='local')
        cfg.CONF.set_override(name='path',
                              override=[self.tdir],
                              group='local')

    def _call_collect(self):
        md = local.Collector().collect()
        return md

    def _setup_test_json(self, data, md_base='test.json'):
        md_name = os.path.join(self.tdir, md_base)
        with open(md_name, 'w') as md:
            md.write(json.dumps(data))
        return md_name

    def test_collect_local(self):
        self._setup_test_json(META_DATA)
        local_md = self._call_collect()

        self.assertThat(local_md, matchers.IsInstance(list))
        self.assertEqual(1, len(local_md))
        self.assertThat(local_md[0], matchers.IsInstance(tuple))
        self.assertEqual(2, len(local_md[0]))
        self.assertEqual('test.json', local_md[0][0])

        only_md = local_md[0][1]
        self.assertThat(only_md, matchers.IsInstance(dict))

        for k in ('localstrA', 'localint9', 'localmap_xy'):
            self.assertIn(k, only_md)
            self.assertEqual(only_md[k], META_DATA[k])

        self.assertEqual('', self.log.output)

    def test_collect_local_world_writable(self):
        md_name = self._setup_test_json(META_DATA)
        os.chmod(md_name, 0o666)
        self.assertRaises(exc.LocalMetadataNotAvailable, self._call_collect)
        self.assertIn('%s is world writable. This is a security risk.' %
                      md_name, self.log.output)

    def test_collect_local_world_writable_dir(self):
        self._setup_test_json(META_DATA)
        os.chmod(self.tdir, 0o666)
        self.assertRaises(exc.LocalMetadataNotAvailable, self._call_collect)
        self.assertIn('%s is world writable. This is a security risk.' %
                      self.tdir, self.log.output)

    def test_collect_local_owner_not_uid(self):
        self._setup_test_json(META_DATA)
        real_getuid = os.getuid

        def fake_getuid():
            return real_getuid() + 1
        self.useFixture(fixtures.MonkeyPatch('os.getuid', fake_getuid))
        self.assertRaises(exc.LocalMetadataNotAvailable, self._call_collect)
        self.assertIn('%s is owned by another user. This is a security risk.' %
                      self.tdir, self.log.output)

    def test_collect_local_orders_multiple(self):
        self._setup_test_json(META_DATA, '00test.json')
        self._setup_test_json(META_DATA2, '99test.json')

        # Monkey Patch os.listdir so it _always_ returns the wrong sort
        unpatched_listdir = os.listdir

        def wrong_sort_listdir(path):
            ret = unpatched_listdir(path)
            save_locale = locale.getlocale()
            try:
                locale.setlocale(locale.LC_ALL, 'C')
                bad_sort = sorted(ret, reverse=True)
            finally:
                locale.setlocale(locale.LC_ALL, save_locale)
            return bad_sort

        self.useFixture(fixtures.MonkeyPatch('os.listdir', wrong_sort_listdir))
        local_md = self._call_collect()

        self.assertThat(local_md, matchers.IsInstance(list))
        self.assertEqual(2, len(local_md))
        self.assertThat(local_md[0], matchers.IsInstance(tuple))

        self.assertEqual('00test.json', local_md[0][0])
        md1 = local_md[0][1]
        self.assertEqual(META_DATA, md1)

        self.assertEqual('99test.json', local_md[1][0])
        md2 = local_md[1][1]
        self.assertEqual(META_DATA2, md2)

    def test_collect_invalid_json_fail(self):
        self._setup_test_json(META_DATA)
        with open(os.path.join(self.tdir, 'bad.json'), 'w') as badjson:
            badjson.write('{')
        self.assertRaises(exc.LocalMetadataNotAvailable, self._call_collect)
        self.assertIn('is not valid JSON', self.log.output)

    def test_collect_local_path_nonexist(self):
        cfg.CONF.set_override(name='path',
                              override=['/this/doesnt/exist'],
                              group='local')
        local_md = self._call_collect()
        self.assertThat(local_md, matchers.IsInstance(list))
        self.assertEqual(0, len(local_md))