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