1 # Black box tests for script/traffic_leaner
3 # Copyright (C) Catalyst IT Ltd. 2017
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.
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.
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/>.
18 """Blackbox tests for traffic_replay"""
20 from contextlib import contextmanager
24 from samba.tests import BlackboxTestCase
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")
38 def temp_file(temp_dir):
40 tf = tempfile.NamedTemporaryFile(dir=temp_dir)
45 if os.path.exists(name):
49 class TrafficLearnerTests(BlackboxTestCase):
52 options = "--clean-up"
53 command = "%s %s %s %s" % (SCRIPT, options, CREDS, SERVER)
54 self.check_run(command)
56 def test_generate_users_only(self):
57 """Ensure the generate users only option functions correctly
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)
68 def test_summary_generation(self):
69 """Ensure a summary file is generated and the contents are correct"""
71 for i, opts in enumerate((["--random-seed=3"],
74 "--conversation-persistence=0.5"],
76 with temp_file(self.tempdir) as output:
77 command = ([SCRIPT, MODEL,
78 "--traffic-summary", output,
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)
87 def test_summary_replay_no_fixed(self):
88 """Ensure a summary file with no fixed password fails
90 command = [SCRIPT, MODEL, CREDS, SERVER]
91 self.check_exit_code(command, 1)
93 def test_model_replay(self):
94 """Ensure a model can be replayed against a DC
96 command = [SCRIPT, MODEL,
100 self.check_run(command)
102 def test_generate_users_only_no_password(self):
103 """Ensure the generate users only fails if no fixed_password supplied"
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)