8370939d243db5a25949117bbc554ae0c96aa76f
[garming/samba-autobuild/.git] / python / samba / tests / blackbox / traffic_replay.py
1 # Black box tests for script/traffic_leaner
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 """Blackbox tests for traffic_replay"""
19
20 from contextlib import contextmanager
21 import os
22 import tempfile
23
24 from samba.tests import BlackboxTestCase
25
26 DATA_DIR      = "python/samba/tests/blackbox/testdata"
27 SCRIPT        = "script/traffic_replay"
28 FIXED         = "--fixed-password=trafficreplay01%"
29 SERVER        = os.environ["SERVER"]
30 PASSWORD      = os.environ["PASSWORD"]
31 USER          = os.environ["USERNAME"]
32 CREDS         = "-U%s%%%s" % (USER, PASSWORD)
33 MODEL         = os.path.join(DATA_DIR, "traffic-sample-very-short.model")
34 EXPECTED_OUTPUT = os.path.join(DATA_DIR, "traffic_replay-%s.expected")
35
36
37 @contextmanager
38 def temp_file(temp_dir):
39     try:
40         tf   = tempfile.NamedTemporaryFile(dir=temp_dir)
41         name = tf.name
42         tf.close()
43         yield name
44     finally:
45         if os.path.exists(name):
46             os.remove(name)
47
48
49 class TrafficLearnerTests(BlackboxTestCase):
50
51     def tearDown(self):
52         options = "--clean-up"
53         command = "%s %s %s %s" % (SCRIPT, options, CREDS, SERVER)
54         self.check_run(command)
55
56     def test_generate_users_only(self):
57         """Ensure the generate users only option functions correctly
58            """
59         options = ("--generate-users-only --number-of-users 20 "
60                    "--number-of-groups 5 --average-groups-per-user 2")
61         command = "%s %s %s %s %s" % (
62             SCRIPT, options, FIXED, CREDS, SERVER)
63         self.check_run(command)
64         command = "%s %s %s %s %s %s" % (
65             SCRIPT, MODEL, options, FIXED, CREDS, SERVER)
66         self.check_run(command)
67
68     def test_summary_generation(self):
69         """Ensure a summary file is generated and the contents are correct"""
70
71         for i, opts in enumerate((["--random-seed=3"],
72                                   ["--random-seed=4"],
73                                   ["--random-seed=3",
74                                    "--conversation-persistence=0.5"],
75                                   )):
76             with temp_file(self.tempdir) as output:
77                 command = ([SCRIPT, MODEL,
78                             "--traffic-summary", output,
79                             "-D1", "-S0.1"] +
80                            opts +
81                            [FIXED, CREDS, SERVER])
82                 self.check_run(command)
83                 expected = open(EXPECTED_OUTPUT % i).read()
84                 actual = open(output).read()
85                 self.assertStringsEqual(expected, actual)
86
87     def test_summary_replay_no_fixed(self):
88         """Ensure a summary file with no fixed password fails
89            """
90         command = [SCRIPT, MODEL, CREDS, SERVER]
91         self.check_exit_code(command, 1)
92
93     def test_model_replay(self):
94         """Ensure a model can be replayed against a DC
95            """
96         command = [SCRIPT, MODEL,
97                    FIXED,
98                    '-D2', '-S0.1',
99                    CREDS, SERVER]
100         self.check_run(command)
101
102     def test_generate_users_only_no_password(self):
103         """Ensure the generate users only fails if no fixed_password supplied"
104            """
105         options = ("--generate-users-only --number-of-users 20 "
106                    "--number-of-groups 5 --average-groups-per-user 2")
107         command  = "%s %s %s %s" % (SCRIPT, options, CREDS, SERVER)
108         self.check_exit_code(command, 1)
109         command = "%s %s %s %s %s" % (SCRIPT, MODEL, options, CREDS, SERVER)
110         self.check_exit_code(command, 1)