32
class TestParseNetworkSpec(MAASServerTestCase):
33
"""Tests for `parse_network_spec`."""
38
class TestNameSpecifier(MAASServerTestCase):
35
40
def test_accepts_network_name(self):
36
network = factory.make_network()
38
('name', network.name),
39
parse_network_spec(network.name))
41
name = factory.make_name('net')
42
self.assertEqual(name, NameSpecifier(name).name)
41
44
def test_rejects_invalid_name(self):
42
self.assertRaises(ValidationError, parse_network_spec, '#.!')
44
def test_rejects_unknown_type_tag(self):
45
self.assertRaises(ValidationError, parse_network_spec, 'foo:bar')
45
self.assertRaises(ValidationError, NameSpecifier, '#.!')
47
def test_fails_if_wrong_type(self):
48
self.assertRaises(ValidationError, NameSpecifier, 'vlan:8')
51
class TestIPSpecifier(MAASServerTestCase):
47
53
def test_accepts_ip_address(self):
49
('ip', IPAddress('10.9.8.7')),
50
parse_network_spec('ip:10.9.8.7'))
52
def test_rejects_untagged_ip_address(self):
53
# If this becomes a stumbling block, it would be possible to accept
54
# plain IP addresses as network specifiers.
55
network = factory.make_network()
56
ip = unicode(network.ip)
57
self.assertRaises(ValidationError, parse_network_spec, ip)
54
self.assertEqual(IPAddress('10.9.8.7'), IPSpecifier('ip:10.9.8.7').ip)
59
56
def test_rejects_empty_ip_address(self):
60
self.assertRaises(ValidationError, parse_network_spec, 'ip:')
57
self.assertRaises(ValidationError, IPSpecifier, 'ip:')
62
59
def test_rejects_malformed_ip_address(self):
63
self.assertRaises(ValidationError, parse_network_spec, 'ip:abc')
60
self.assertRaises(ValidationError, IPSpecifier, 'ip:abc')
62
def test_fails_if_wrong_type(self):
63
self.assertRaises(AssertionError, IPSpecifier, 'vlan:8')
66
class TestVLANSpecifier(MAASServerTestCase):
65
68
def test_accepts_vlan_tag_in_range(self):
66
network = factory.make_network(vlan_tag=1)
68
('vlan', network.vlan_tag),
69
parse_network_spec('vlan:%d' % network.vlan_tag))
71
network = factory.make_network(vlan_tag=4094)
73
('vlan', network.vlan_tag),
74
parse_network_spec('vlan:%d' % network.vlan_tag))
69
self.assertEqual(12, VLANSpecifier('vlan:12').vlan_tag)
70
self.assertEqual(1, VLANSpecifier('vlan:1').vlan_tag)
71
self.assertEqual(4094, VLANSpecifier('vlan:4094').vlan_tag)
76
73
def test_rejects_empty_vlan_tag(self):
77
self.assertRaises(ValidationError, parse_network_spec, 'vlan:')
74
self.assertRaises(ValidationError, VLANSpecifier, 'vlan:')
79
76
def test_rejects_nonnumerical_vlan_tag(self):
80
self.assertRaises(ValidationError, parse_network_spec, 'vlan:B')
81
self.assertRaises(ValidationError, parse_network_spec, 'vlan:0x1g')
77
self.assertRaises(ValidationError, VLANSpecifier, 'vlan:B')
78
self.assertRaises(ValidationError, VLANSpecifier, 'vlan:0x1g')
83
80
def test_accepts_hexadecimal_vlan_tag(self):
84
self.assertEqual(('vlan', 0xf0f), parse_network_spec('vlan:0xf0f'))
85
self.assertEqual(('vlan', 0x1ac), parse_network_spec('vlan:0x1AC'))
81
self.assertEqual(0xf0f, VLANSpecifier('vlan:0xf0f').vlan_tag)
82
self.assertEqual(0x1ac, VLANSpecifier('vlan:0x1AC').vlan_tag)
87
84
def test_rejects_reserved_vlan_tags(self):
88
self.assertRaises(ValidationError, parse_network_spec, 'vlan:0')
89
self.assertRaises(ValidationError, parse_network_spec, 'vlan:4095')
85
self.assertRaises(ValidationError, VLANSpecifier, 'vlan:0')
86
self.assertRaises(ValidationError, VLANSpecifier, 'vlan:4095')
91
88
def test_rejects_vlan_tag_out_of_range(self):
92
self.assertRaises(ValidationError, parse_network_spec, 'vlan:-1')
93
self.assertRaises(ValidationError, parse_network_spec, 'vlan:4096')
89
self.assertRaises(ValidationError, VLANSpecifier, 'vlan:-1')
90
self.assertRaises(ValidationError, VLANSpecifier, 'vlan:4096')
92
def test_fails_if_wrong_type(self):
93
self.assertRaises(AssertionError, VLANSpecifier, 'ip:10.1.1.1')
96
class TestGetSpecifierType(MAASServerTestCase):
97
"""Tests for `get_specifier_type`."""
99
def test_returns_type_identified_by_type_tag(self):
100
self.assertEqual(IPSpecifier, get_specifier_type('ip:10.0.0.0'))
101
self.assertEqual(VLANSpecifier, get_specifier_type('vlan:99'))
103
def test_defaults_to_name_if_no_type_tag_found(self):
104
self.assertEqual(NameSpecifier, get_specifier_type('hello'))
106
def test_rejects_unsupported_tag(self):
107
self.assertRaises(ValidationError, get_specifier_type, 'foo:bar')
110
class TestParseNetworkSpec(MAASServerTestCase):
111
"""Tests for `parse_network_spec`."""
113
def test_rejects_unknown_type_tag(self):
114
self.assertRaises(ValidationError, parse_network_spec, 'foo:bar')
116
def test_accepts_valid_specifier(self):
117
spec = parse_network_spec('ip:10.8.8.8')
118
self.assertIsInstance(spec, IPSpecifier)
119
self.assertEqual(IPAddress('10.8.8.8'), spec.ip)
121
def test_rejects_untagged_ip_address(self):
122
# If this becomes a stumbling block, it would be possible to accept
123
# plain IP addresses as network specifiers.
124
self.assertRaises(ValidationError, parse_network_spec, '10.4.4.4')
96
127
class TestNetworkManager(MAASServerTestCase):