def test_new_stores_dhcp_key_on_nodegroup(self): key = generate_omapi_key() nodegroup = NodeGroup.objects.new( factory.make_name('nodegroup'), factory.make_name('uuid'), factory.getRandomIPAddress(), dhcp_key=key) self.assertEqual(key, nodegroup.dhcp_key)
def test_new_stores_dhcp_key_on_nodegroup(self): key = generate_omapi_key() nodegroup = NodeGroup.objects.new(factory.make_name('nodegroup'), factory.make_name('uuid'), factory.getRandomIPAddress(), dhcp_key=key) self.assertEqual(key, nodegroup.dhcp_key)
def ensure_dhcp_key(self): """Ensure that this nodegroup has a dhcp key. This method persists the dhcp key without triggering the model signals (pre_save/post_save/etc) because it's called from dhcp.configure_dhcp which, in turn, it called from the post_save signal of NodeGroup.""" if self.dhcp_key == '': dhcp_key = generate_omapi_key() self.dhcp_key = dhcp_key # Persist the dhcp_key without triggering the signals. NodeGroup.objects.filter(id=self.id).update(dhcp_key=dhcp_key)
def test_run_repeated_keygen(self): bad_patterns = { "+no", "/no", "no+", "no/", "+NO", "/NO", "NO+", "NO/", } bad_patterns_templates = { "foo%sbar", "one\ntwo\n%s\nthree\n", "%s", } # Test that a known bad key is ignored and we generate a new one # to replace it. bad_keys = { # This key is known to fail with omshell. "YXY5pr+No/8NZeodSd27wWbI8N6kIjMF/nrnFIlPwVLuByJKkQcBRtfDrD" "LLG2U9/ND7/bIlJxEGTUnyipffHQ==", } # Fabricate a range of keys containing the known-bad pattern. bad_keys.update(template % pattern for template, pattern in product( bad_patterns_templates, bad_patterns)) # An iterator that we can exhaust without mutating bad_keys. iter_bad_keys = iter(bad_keys) # Reference to the original parse_key_value_file, before we patch. parse_key_value_file = provisioningserver.omshell.parse_key_value_file # Patch parse_key_value_file to return each of the known-bad keys # we've created, followed by reverting to its usual behaviour. def side_effect(*args, **kwargs): try: return {'Key': next(iter_bad_keys)} except StopIteration: return parse_key_value_file(*args, **kwargs) mock = self.patch(provisioningserver.omshell, 'parse_key_value_file') mock.side_effect = side_effect # generate_omapi_key() does not return a key known to be bad. self.assertNotIn(generate_omapi_key(), bad_keys)
def ensure_master(self): """Obtain the master node group, creating it first if needed.""" # Avoid circular imports. from maasserver.models import Node try: # Get the first created nodegroup if it exists. master = self.all().order_by('id')[0:1].get() except NodeGroup.DoesNotExist: # The master did not exist yet; create it on demand. master = self.new( 'master', 'master', '127.0.0.1', dhcp_key=generate_omapi_key(), status=NODEGROUP_STATUS.ACCEPTED) # If any legacy nodes were still not associated with a node # group, enroll them in the master node group. Node.objects.filter(nodegroup=None).update(nodegroup=master) return master
def test_run_repeated_keygen(self): bad_patterns = { "+no", "/no", "no+", "no/", "+NO", "/NO", "NO+", "NO/", } bad_patterns_templates = { "foo%sbar", "one\ntwo\n%s\nthree\n", "%s", } # Test that a known bad key is ignored and we generate a new one # to replace it. bad_keys = { # This key is known to fail with omshell. "YXY5pr+No/8NZeodSd27wWbI8N6kIjMF/nrnFIlPwVLuByJKkQcBRtfDrD" "LLG2U9/ND7/bIlJxEGTUnyipffHQ==", } # Fabricate a range of keys containing the known-bad pattern. bad_keys.update( template % pattern for template, pattern in product( bad_patterns_templates, bad_patterns)) # An iterator that we can exhaust without mutating bad_keys. iter_bad_keys = iter(bad_keys) # Reference to the original parse_key_value_file, before we patch. parse_key_value_file = provisioningserver.omshell.parse_key_value_file # Patch parse_key_value_file to return each of the known-bad keys # we've created, followed by reverting to its usual behaviour. def side_effect(*args, **kwargs): try: return {'Key': next(iter_bad_keys)} except StopIteration: return parse_key_value_file(*args, **kwargs) mock = self.patch(provisioningserver.omshell, 'parse_key_value_file') mock.side_effect = side_effect # generate_omapi_key() does not return a key known to be bad. self.assertNotIn(generate_omapi_key(), bad_keys)
def test_generate_omapi_key_leaves_no_temp_files(self): tmpdir = self.useFixture(TempDir()).path # Make mkdtemp() in omshell nest all directories within tmpdir. self.patch(tempfile, 'tempdir', tmpdir) generate_omapi_key() self.assertEqual([], os.listdir(tmpdir))
def test_generate_omapi_key_returns_a_key(self): key = generate_omapi_key() # Could test for != None here, but the keys end in == for a 512 # bit length key, so that's a better check that the script was # actually run and produced output. self.assertThat(key, EndsWith("=="))
def test_generate_omapi_key_leaves_no_temp_files(self): tmpdir = self.useFixture(TempDirectory()).path # Make mkdtemp() in omshell nest all directories within tmpdir. self.patch(tempfile, 'tempdir', tmpdir) generate_omapi_key() self.assertEqual([], os.listdir(tmpdir))