2015-09-01 15:58:30 +12:00
# Blackbox tests for "samba_dnsupdate" command
# Copyright (C) Kamen Mazdrashki <kamenim@samba.org> 2011
# Copyright (C) Andrew Bartlett <abartlet@samba.org> 2015
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import samba . tests
2020-07-04 13:53:34 +12:00
from io import StringIO
2020-09-11 14:29:46 -06:00
from samba . common import get_string
2018-03-19 16:50:36 +13:00
from samba . netcmd . main import cmd_sambatool
from samba . credentials import Credentials
from samba . auth import system_session
from samba . samdb import SamDB
import ldb
2018-07-30 18:21:38 +12:00
import shutil
2015-09-01 15:58:30 +12:00
2018-07-30 18:20:39 +12:00
2015-09-01 15:58:30 +12:00
class SambaDnsUpdateTests ( samba . tests . BlackboxTestCase ) :
""" Blackbox test case for samba_dnsupdate. """
def setUp ( self ) :
self . server_ip = samba . tests . env_get_var_value ( " DNS_SERVER_IP " )
super ( SambaDnsUpdateTests , self ) . setUp ( )
try :
out = self . check_output ( " samba_dnsupdate --verbose " )
2018-10-30 13:25:59 +00:00
self . assertTrue ( b " Looking for DNS entry " in out , out )
2015-09-01 15:58:30 +12:00
except samba . tests . BlackboxProcessError :
pass
def test_samba_dnsupate_no_change ( self ) :
2018-07-10 13:14:18 +12:00
try :
out = self . check_output ( " samba_dnsupdate --verbose " )
except samba . tests . BlackboxProcessError as e :
self . fail ( " Error calling samba_dnsupdate: %s " % e )
2018-10-30 13:25:59 +00:00
self . assertTrue ( b " No DNS updates needed " in out , out )
2015-09-01 15:58:30 +12:00
def test_samba_dnsupate_set_ip ( self ) :
try :
out = self . check_output ( " samba_dnsupdate --verbose --current-ip=10.0.0.1 " )
2018-10-30 13:25:59 +00:00
self . assertTrue ( b " DNS updates and " in out , out )
self . assertTrue ( b " DNS deletes needed " in out , out )
2015-09-01 15:58:30 +12:00
except samba . tests . BlackboxProcessError :
pass
2016-06-15 16:32:23 +12:00
try :
out = self . check_output ( " samba_dnsupdate --verbose --use-nsupdate --current-ip=10.0.0.1 " )
2016-08-02 18:38:33 +02:00
except samba . tests . BlackboxProcessError as e :
self . fail ( " Error calling samba_dnsupdate: %s " % e )
2016-06-15 16:32:23 +12:00
2018-10-30 13:25:59 +00:00
self . assertTrue ( b " No DNS updates needed " in out , out )
2016-06-15 16:32:23 +12:00
try :
2017-02-27 11:39:51 +13:00
rpc_out = self . check_output ( " samba_dnsupdate --verbose --use-samba-tool --rpc-server-ip= %s " % self . server_ip )
2016-08-02 18:38:33 +02:00
except samba . tests . BlackboxProcessError as e :
self . fail ( " Error calling samba_dnsupdate: %s " % e )
2016-06-15 16:32:23 +12:00
2018-10-30 13:25:59 +00:00
self . assertTrue ( b " DNS updates and " in rpc_out , rpc_out )
self . assertTrue ( b " DNS deletes needed " in rpc_out , rpc_out )
2015-09-01 15:58:30 +12:00
out = self . check_output ( " samba_dnsupdate --verbose " )
2018-10-30 13:25:59 +00:00
self . assertTrue ( b " No DNS updates needed " in out , out + rpc_out )
2018-03-19 16:50:36 +13:00
def test_add_new_uncovered_site ( self ) :
name = ' sites '
cmd = cmd_sambatool . subcommands [ name ]
cmd . outf = StringIO ( )
cmd . errf = StringIO ( )
site_name = ' ABCDEFGHIJKLMNOPQRSTUVWXYZ '
# Clear out any existing site
cmd . _run ( " samba-tool %s " % name , ' remove ' , site_name )
result = cmd . _run ( " samba-tool %s " % name , ' create ' , site_name )
if result is not None :
self . fail ( " Error creating new site " )
self . lp = samba . tests . env_loadparm ( )
self . creds = Credentials ( )
self . creds . guess ( self . lp )
self . session = system_session ( )
2018-07-10 13:14:18 +12:00
uc_fn = self . lp . private_path ( ' dns_update_cache ' )
tmp_uc = uc_fn + ' _tmp '
shutil . copyfile ( uc_fn , tmp_uc )
2018-03-19 16:50:36 +13:00
self . samdb = SamDB ( session_info = self . session ,
credentials = self . creds ,
lp = self . lp )
m = ldb . Message ( )
m . dn = ldb . Dn ( self . samdb , ' CN=DEFAULTIPSITELINK,CN=IP, '
2018-09-21 13:22:56 +12:00
' CN=Inter-Site Transports,CN=Sites, {0} ' . format (
2018-03-19 16:50:36 +13:00
self . samdb . get_config_basedn ( ) ) )
2018-09-21 13:22:56 +12:00
m [ ' siteList ' ] = ldb . MessageElement ( " CN= {0} ,CN=Sites, {1} " . format (
2018-03-19 16:50:36 +13:00
site_name ,
self . samdb . get_config_basedn ( ) ) ,
ldb . FLAG_MOD_ADD , " siteList " )
2018-09-21 13:22:56 +12:00
dns_c = " samba_dnsupdate --verbose --use-file= {0} " . format ( tmp_uc )
2018-10-30 13:25:59 +00:00
out = get_string ( self . check_output ( dns_c ) )
2021-03-24 15:16:21 +13:00
self . assertNotIn ( site_name . lower ( ) , out )
2018-03-19 16:50:36 +13:00
self . samdb . modify ( m )
2018-07-10 13:14:18 +12:00
shutil . copyfile ( uc_fn , tmp_uc )
2018-10-30 13:25:59 +00:00
out = get_string ( self . check_output ( dns_c ) )
2018-03-19 16:50:36 +13:00
2021-03-24 15:16:21 +13:00
self . assertNotIn ( " No DNS updates needed " , out )
self . assertIn ( site_name . lower ( ) , out )
2018-03-19 16:50:36 +13:00
result = cmd . _run ( " samba-tool %s " % name , ' remove ' , site_name )
if result is not None :
self . fail ( " Error deleting site " )