Skip to content

Commit

Permalink
Merge pull request #3245 from eemperor/name-computer-grain
Browse files Browse the repository at this point in the history
Fixes clobbering of name-computer grain
  • Loading branch information
eemperor authored Oct 3, 2023
2 parents f2fd564 + 693dcdd commit c18afb6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 53 deletions.
10 changes: 5 additions & 5 deletions src/watchmaker/workers/salt.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,13 +592,13 @@ def process_grains(self):
if grain:
self._set_grain('join-domain', grain)

grain = {}
if self.computer_name:
name = {'computername': str(self.computer_name)}
self._set_grain('name-computer', name)

grain['computername'] = self.computer_name
if self.computer_name_pattern:
pattern = {'pattern': str(self.computer_name_pattern)}
self._set_grain('name-computer', pattern)
grain['pattern'] = self.computer_name_pattern
if grain:
self._set_grain('name-computer', grain)

self.log.info('Syncing custom salt modules...')
self.run_salt('saltutil.sync_all')
Expand Down
49 changes: 1 addition & 48 deletions tests/test_saltworker.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@

import pytest

from watchmaker.exceptions import (
InvalidComputerNameError,
InvalidValue,
OuPathRequired,
)
from watchmaker.exceptions import InvalidValue, OuPathRequired
from watchmaker.workers.salt import SaltBase, SaltLinux, SaltWindows

# Supports Python2 and Python3 test mocks
Expand Down Expand Up @@ -991,46 +987,3 @@ def test_linux_computer_name_patt_none():

# assertions ===================
assert saltworker_lx._set_grain.call_count == 2


def test_linux_computer_name_patt():
"""Test that valid names match pattern and invalid do not."""
# setup ========================
system_params = {}
salt_config = {}
system_params["prepdir"] = "662f1bdb-5992-5f8f-87d6-15c4de958b7b"
system_params["logdir"] = "76b74ceb-e81d-5fac-b293-0d7d45901ef7"
system_params["workingdir"] = "4e6a1827-1d3b-5612-a7fd-f6fed00b5a2f"

# try "normal" first, with a valid computer. try with invalid below.
salt_config["computer_name"] = "xyz654abcdefghe"
salt_config["computer_name_pattern"] = r"(?i)xyz[\d]{3}[a-z]{8}[ex]"

# execution ====================
saltworker_lx = SaltLinux(system_params, **salt_config)

saltworker_lx._set_grain = MagicMock(return_value=None)
saltworker_lx.run_salt = MagicMock(
return_value={"retcode": 0, "stdout": b"", "stderr": b""}
)

saltworker_lx.process_grains()

# assertions ===================
assert saltworker_lx._set_grain.call_count == 4
saltworker_lx._set_grain.assert_called_with(
'name-computer', {'pattern': salt_config["computer_name_pattern"]})

# tried "normal" first, with a value, above. now, trying with none.
salt_config["computer_name"] = "123654abcdefghlmdone"

# execution ====================
saltworker_lx = SaltLinux(system_params, **salt_config)

saltworker_lx._set_grain = MagicMock(return_value=None)
saltworker_lx.run_salt = MagicMock(
return_value={"retcode": 0, "stdout": b"", "stderr": b""}
)

with pytest.raises(InvalidComputerNameError):
saltworker_lx.before_install()

0 comments on commit c18afb6

Please sign in to comment.