1
# Copyright 2012-2014 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
"""Test the snippets-related support routines for commissioning user data."""
6
from __future__ import (
19
from maastesting.factory import factory
20
from maastesting.testcase import MAASTestCase
21
from metadataserver.commissioning.snippets import (
30
class TestSnippets(MAASTestCase):
32
def test_read_snippet_reads_snippet_file(self):
33
contents = factory.make_string()
34
snippet = self.make_file(contents=contents)
37
read_snippet(os.path.dirname(snippet), os.path.basename(snippet)))
39
def test_strip_name_leaves_simple_names_intact(self):
40
simple_name = factory.make_string()
41
self.assertEqual(simple_name, strip_name(simple_name))
43
def test_strip_name_replaces_dots(self):
44
self.assertEqual('_x_y_', strip_name('.x.y.'))
46
def test_is_snippet(self):
54
'__init__.pyc': False,
59
{name: is_snippet(name) for name in are_snippets})
61
def test_list_snippets(self):
62
snippets_dir = self.make_dir()
63
factory.make_file(snippets_dir, 'snippet')
64
factory.make_file(snippets_dir, '.backup.pyc')
65
self.assertItemsEqual(['snippet'], list_snippets(snippets_dir))
67
def test_get_snippet_context(self):
68
contents = factory.make_string()
69
snippets_dir = self.make_dir()
70
factory.make_file(snippets_dir, 'snippet.py', contents=contents)
71
self.assertItemsEqual(
72
{'snippet_py': contents},
73
get_snippet_context(snippets_dir=snippets_dir))
75
def test_get_snippet_context_empty_if_no_snippets(self):
76
snippets_dir = self.make_dir()
79
context, get_snippet_context(snippets_dir=snippets_dir))