]> git.samba.org - samba.git/blob - python/samba/tests/blackbox/samba_dnsupdate.py
PEP8: fix E401: multiple imports on one line
[samba.git] / python / samba / tests / blackbox / samba_dnsupdate.py
1 # Blackbox tests for "samba_dnsupdate" command
2 # Copyright (C) Kamen Mazdrashki <kamenim@samba.org> 2011
3 # Copyright (C) Andrew Bartlett <abartlet@samba.org> 2015
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 samba.tests
20 from StringIO import StringIO
21 from samba.netcmd.main import cmd_sambatool
22 from samba.credentials import Credentials
23 from samba.auth import system_session
24 from samba.samdb import SamDB
25 import ldb
26 import shutil
27 import os
28
29
30 class SambaDnsUpdateTests(samba.tests.BlackboxTestCase):
31     """Blackbox test case for samba_dnsupdate."""
32
33     def setUp(self):
34         self.server_ip = samba.tests.env_get_var_value("DNS_SERVER_IP")
35         super(SambaDnsUpdateTests, self).setUp()
36         try:
37             out = self.check_output("samba_dnsupdate --verbose")
38             self.assertTrue("Looking for DNS entry" in out, out)
39         except samba.tests.BlackboxProcessError:
40             pass
41
42     def test_samba_dnsupate_no_change(self):
43         try:
44             out = self.check_output("samba_dnsupdate --verbose")
45         except samba.tests.BlackboxProcessError as e:
46             self.fail("Error calling samba_dnsupdate: %s" % e)
47         self.assertTrue("No DNS updates needed" in out, out)
48
49     def test_samba_dnsupate_set_ip(self):
50         try:
51             out = self.check_output("samba_dnsupdate --verbose --current-ip=10.0.0.1")
52             self.assertTrue(" DNS updates and" in out, out)
53             self.assertTrue(" DNS deletes needed" in out, out)
54         except samba.tests.BlackboxProcessError:
55             pass
56
57         try:
58             out = self.check_output("samba_dnsupdate --verbose --use-nsupdate --current-ip=10.0.0.1")
59         except samba.tests.BlackboxProcessError as e:
60             self.fail("Error calling samba_dnsupdate: %s" % e)
61
62         self.assertTrue("No DNS updates needed" in out, out)
63         try:
64             rpc_out = self.check_output("samba_dnsupdate --verbose --use-samba-tool --rpc-server-ip=%s" % self.server_ip)
65         except samba.tests.BlackboxProcessError as e:
66             self.fail("Error calling samba_dnsupdate: %s" % e)
67
68         self.assertTrue(" DNS updates and" in rpc_out, rpc_out)
69         self.assertTrue(" DNS deletes needed" in rpc_out, rpc_out)
70         out = self.check_output("samba_dnsupdate --verbose")
71         self.assertTrue("No DNS updates needed" in out, out + rpc_out)
72
73     def test_add_new_uncovered_site(self):
74         name = 'sites'
75         cmd = cmd_sambatool.subcommands[name]
76         cmd.outf = StringIO()
77         cmd.errf = StringIO()
78
79         site_name = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
80
81         # Clear out any existing site
82         cmd._run("samba-tool %s" % name, 'remove', site_name)
83
84         result = cmd._run("samba-tool %s" % name, 'create', site_name)
85         if result is not None:
86             self.fail("Error creating new site")
87
88         self.lp = samba.tests.env_loadparm()
89         self.creds = Credentials()
90         self.creds.guess(self.lp)
91         self.session = system_session()
92         uc_fn = self.lp.private_path('dns_update_cache')
93         tmp_uc = uc_fn + '_tmp'
94         shutil.copyfile(uc_fn, tmp_uc)
95
96         self.samdb = SamDB(session_info=self.session,
97                            credentials=self.creds,
98                            lp=self.lp)
99
100         m = ldb.Message()
101         m.dn = ldb.Dn(self.samdb, 'CN=DEFAULTIPSITELINK,CN=IP,'
102                       'CN=Inter-Site Transports,CN=Sites,{}'.format(
103                           self.samdb.get_config_basedn()))
104         m['siteList'] = ldb.MessageElement("CN={},CN=Sites,{}".format(
105             site_name,
106             self.samdb.get_config_basedn()),
107             ldb.FLAG_MOD_ADD, "siteList")
108
109         dns_c = "samba_dnsupdate --verbose --use-file={}".format(tmp_uc)
110         out = self.check_output(dns_c)
111         self.assertFalse(site_name.lower() in out, out)
112
113         self.samdb.modify(m)
114
115         shutil.copyfile(uc_fn, tmp_uc)
116         out = self.check_output(dns_c)
117
118         self.assertFalse("No DNS updates needed" in out, out)
119         self.assertTrue(site_name.lower() in out, out)
120
121         result = cmd._run("samba-tool %s" % name, 'remove', site_name)
122         if result is not None:
123             self.fail("Error deleting site")