|
| 1 | +# |
| 2 | +# sambacc: a samba container configuration tool |
| 3 | +# Copyright (C) 2022 John Mulligan |
| 4 | +# |
| 5 | +# This program is free software: you can redistribute it and/or modify |
| 6 | +# it under the terms of the GNU General Public License as published by |
| 7 | +# the Free Software Foundation, either version 3 of the License, or |
| 8 | +# (at your option) any later version. |
| 9 | +# |
| 10 | +# This program is distributed in the hope that it will be useful, |
| 11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +# GNU General Public License for more details. |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU General Public License |
| 16 | +# along with this program. If not, see <http://www.gnu.org/licenses/> |
| 17 | +# |
| 18 | + |
| 19 | +import os |
| 20 | + |
| 21 | +import pytest |
| 22 | + |
| 23 | + |
| 24 | +import sambacc.addc |
| 25 | + |
| 26 | + |
| 27 | +def _fake_samba_tool(path): |
| 28 | + fake_samba_tool = path / "fake_samba_tool.sh" |
| 29 | + with open(fake_samba_tool, "w") as fh: |
| 30 | + fh.write("#!/bin/sh\n") |
| 31 | + fh.write(f"[ -e {path}/fail ] && exit 1\n") |
| 32 | + fh.write(f'echo "$@" > {path}/args.out\n') |
| 33 | + fh.write("exit 0") |
| 34 | + os.chmod(fake_samba_tool, 0o700) |
| 35 | + return fake_samba_tool |
| 36 | + |
| 37 | + |
| 38 | +def test_provision(tmp_path, monkeypatch): |
| 39 | + monkeypatch.setattr( |
| 40 | + sambacc.samba_cmds, "_GLOBAL_PREFIX", [_fake_samba_tool(tmp_path)] |
| 41 | + ) |
| 42 | + |
| 43 | + sambacc.addc.provision("FOOBAR.TEST", "quux", "h4ckm3") |
| 44 | + with open(tmp_path / "args.out") as fh: |
| 45 | + result = fh.read() |
| 46 | + assert "--realm=FOOBAR.TEST" in result |
| 47 | + assert "--option=netbios name=quux" in result |
| 48 | + assert "--dns-backend=SAMBA_INTERNAL" in result |
| 49 | + |
| 50 | + sambacc.addc.provision( |
| 51 | + "BARFOO.TEST", |
| 52 | + "quux", |
| 53 | + "h4ckm3", |
| 54 | + options=[ |
| 55 | + ("ldap server require strong auth", "no"), |
| 56 | + ("dns zone scavenging", "yes"), |
| 57 | + ("ldap machine suffix", "ou=Machines"), |
| 58 | + ("netbios name", "flipper"), |
| 59 | + ], |
| 60 | + ) |
| 61 | + with open(tmp_path / "args.out") as fh: |
| 62 | + result = fh.read() |
| 63 | + assert "--realm=BARFOO.TEST" in result |
| 64 | + assert "--option=netbios name=quux" in result |
| 65 | + assert "--dns-backend=SAMBA_INTERNAL" in result |
| 66 | + assert "--option=ldap server require strong auth=no" in result |
| 67 | + assert "--option=dns zone scavenging=yes" in result |
| 68 | + assert "--option=ldap machine suffix=ou=Machines" in result |
| 69 | + assert "--option=netbios name=flipper" not in result |
| 70 | + |
| 71 | + open(tmp_path / "fail", "w").close() |
| 72 | + with pytest.raises(Exception): |
| 73 | + sambacc.addc.provision("FOOBAR.TEST", "quux", "h4ckm3") |
| 74 | + |
| 75 | + |
| 76 | +def test_join(tmp_path, monkeypatch): |
| 77 | + monkeypatch.setattr( |
| 78 | + sambacc.samba_cmds, "_GLOBAL_PREFIX", [_fake_samba_tool(tmp_path)] |
| 79 | + ) |
| 80 | + |
| 81 | + sambacc.addc.join("FOOBAR.TEST", "quux", "h4ckm3") |
| 82 | + with open(tmp_path / "args.out") as fh: |
| 83 | + result = fh.read() |
| 84 | + assert "FOOBAR.TEST" in result |
| 85 | + assert "--option=netbios name=quux" in result |
| 86 | + assert "--dns-backend=SAMBA_INTERNAL" in result |
| 87 | + |
| 88 | + sambacc.addc.join( |
| 89 | + "BARFOO.TEST", |
| 90 | + "quux", |
| 91 | + "h4ckm3", |
| 92 | + options=[ |
| 93 | + ("ldap server require strong auth", "no"), |
| 94 | + ("dns zone scavenging", "yes"), |
| 95 | + ("ldap machine suffix", "ou=Machines"), |
| 96 | + ("netbios name", "flipper"), |
| 97 | + ], |
| 98 | + ) |
| 99 | + with open(tmp_path / "args.out") as fh: |
| 100 | + result = fh.read() |
| 101 | + with open(tmp_path / "args.out") as fh: |
| 102 | + result = fh.read() |
| 103 | + assert "BARFOO.TEST" in result |
| 104 | + assert "--option=netbios name=quux" in result |
| 105 | + assert "--dns-backend=SAMBA_INTERNAL" in result |
| 106 | + assert "--option=ldap server require strong auth=no" in result |
| 107 | + assert "--option=dns zone scavenging=yes" in result |
| 108 | + assert "--option=ldap machine suffix=ou=Machines" in result |
| 109 | + assert "--option=netbios name=flipper" not in result |
| 110 | + |
| 111 | + |
| 112 | +def test_create_user(tmp_path, monkeypatch): |
| 113 | + monkeypatch.setattr( |
| 114 | + sambacc.samba_cmds, "_GLOBAL_PREFIX", [_fake_samba_tool(tmp_path)] |
| 115 | + ) |
| 116 | + |
| 117 | + sambacc.addc.create_user("fflintstone", "b3dr0ck", "Flintstone", "Fred") |
| 118 | + with open(tmp_path / "args.out") as fh: |
| 119 | + result = fh.read() |
| 120 | + assert "user create fflintstone" in result |
| 121 | + assert "--surname=Flintstone" in result |
| 122 | + assert "--given-name=Fred" in result |
| 123 | + |
| 124 | + |
| 125 | +def test_create_group(tmp_path, monkeypatch): |
| 126 | + monkeypatch.setattr( |
| 127 | + sambacc.samba_cmds, "_GLOBAL_PREFIX", [_fake_samba_tool(tmp_path)] |
| 128 | + ) |
| 129 | + |
| 130 | + sambacc.addc.create_group("quarry_workers") |
| 131 | + with open(tmp_path / "args.out") as fh: |
| 132 | + result = fh.read() |
| 133 | + assert "group add quarry_workers" in result |
| 134 | + |
| 135 | + |
| 136 | +def test_add_group_members(tmp_path, monkeypatch): |
| 137 | + monkeypatch.setattr( |
| 138 | + sambacc.samba_cmds, "_GLOBAL_PREFIX", [_fake_samba_tool(tmp_path)] |
| 139 | + ) |
| 140 | + |
| 141 | + sambacc.addc.add_group_members( |
| 142 | + "quarry_workers", ["fflintstone", "brubble"] |
| 143 | + ) |
| 144 | + with open(tmp_path / "args.out") as fh: |
| 145 | + result = fh.read() |
| 146 | + assert "group addmembers quarry_workers" in result |
| 147 | + assert "fflintstone,brubble" in result |
0 commit comments