dsdb: Log the transaction duraton.
[nivanova/samba-autobuild/.git] / python / samba / tests / netlogonsvc.py
1 # Tests to check the netlogon service is only running when it's required
2 #
3 # Copyright (C) Catalyst IT Ltd. 2017
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 from samba.tests import TestCase
19 import os
20
21 import samba
22 from samba.credentials import Credentials
23 from samba.dcerpc import netlogon
24 from samba import NTSTATUSError, ntstatus
25 import ctypes
26
27 """
28 Tests whether the netlogon service is running
29 """
30
31 class NetlogonServiceTests(TestCase):
32
33     def setUp(self):
34         super(NetlogonServiceTests, self).setUp()
35
36         self.server      = os.environ["SERVER"]
37         self.lp          = self.get_loadparm()
38         self.creds = Credentials()
39
40         # prefer the DC user/password in environments that have it
41         if "DC_USERNAME" in os.environ and "DC_PASSWORD" in os.environ:
42             self.creds.set_username(os.environ["DC_USERNAME"])
43             self.creds.set_password(os.environ["DC_PASSWORD"])
44         else:
45             self.creds.set_username(os.environ["USERNAME"])
46             self.creds.set_password(os.environ["PASSWORD"])
47
48         self.creds.guess(self.lp)
49
50     def tearDown(self):
51         super(NetlogonServiceTests, self).tearDown()
52
53     def test_have_netlogon_connection(self):
54         try:
55             c = self.get_netlogon_connection()
56             self.assertIsNotNone(c)
57         except NTSTATUSError as e:
58             # On non-DC test environments, netlogon should not be running on
59             # the server, so we expect the test to fail here
60             enum = ctypes.c_uint32(e[0]).value
61             if enum == ntstatus.NT_STATUS_OBJECT_NAME_NOT_FOUND:
62                 self.fail("netlogon service is not running")
63             else:
64                 raise
65
66     # Establish netlogon connection over NP
67     def get_netlogon_connection(self):
68         return netlogon.netlogon("ncacn_np:%s[seal]" % self.server, self.lp,
69                                  self.creds)