14
14
__metaclass__ = type
17
from base64 import b64encode
20
19
from django.core.exceptions import ValidationError
20
from maasserver.bootsources import _cache_boot_sources
21
21
from maasserver.models import BootSource
22
22
from maasserver.testing.factory import factory
23
23
from maasserver.testing.testcase import MAASServerTestCase
24
from maastesting.matchers import MockCalledOnceWith
25
from twisted.internet import reactor
26
from twisted.internet.threads import deferToThread
26
29
class TestBootSource(MAASServerTestCase):
35
38
BootSource.objects.filter(id=boot_source.id).exists())
37
40
def test_url_is_unqiue(self):
38
boot_source = factory.make_boot_source()
41
boot_source = factory.make_BootSource()
40
ValidationError, factory.make_boot_source, url=boot_source.url)
43
ValidationError, factory.make_BootSource, url=boot_source.url)
42
45
def test_cannot_set_keyring_data_and_filename(self):
43
46
# A BootSource cannot have both a keyring filename and keyring
55
58
self.assertRaises(ValidationError, boot_source.clean)
57
60
def test_to_dict_returns_dict(self):
58
boot_source = factory.make_boot_source(
61
boot_source = factory.make_BootSource(
59
62
keyring_data=b"123445", keyring_filename='')
60
boot_source_selection = factory.make_boot_source_selection(
63
boot_source_selection = factory.make_BootSourceSelection(
61
64
boot_source=boot_source)
62
65
boot_source_dict = boot_source.to_dict()
63
66
self.assertEqual(boot_source.url, boot_source_dict['url'])
70
73
keyring_file = self.make_file(contents=keyring_data)
71
74
self.addCleanup(os.remove, keyring_file)
73
boot_source = factory.make_boot_source(
76
boot_source = factory.make_BootSource(
74
77
keyring_data=b"", keyring_filename=keyring_file)
75
78
source = boot_source.to_dict()
77
80
source['keyring_data'],
78
b64encode(keyring_data))
80
83
def test_to_dict_handles_keyring_data(self):
81
84
keyring_data = b"Some Keyring Data"
82
boot_source = factory.make_boot_source(
85
boot_source = factory.make_BootSource(
83
86
keyring_data=keyring_data, keyring_filename="")
84
87
source = boot_source.to_dict()
86
89
source['keyring_data'],
87
b64encode(keyring_data))
92
def test_to_dict_with_selections_returns_dict_without_selections(self):
93
boot_source = factory.make_BootSource(
94
keyring_data=b"123445", keyring_filename='')
95
factory.make_BootSourceSelection(boot_source=boot_source)
96
boot_source_dict = boot_source.to_dict_without_selections()
99
boot_source_dict['selections'])
101
def test_calls_cache_boot_sources_on_create(self):
102
mock_callLater = self.patch(reactor, 'callLater')
103
BootSource.objects.create(
104
url="http://test.test/", keyring_data=b"1234")
107
MockCalledOnceWith(1, deferToThread, _cache_boot_sources))