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 STD_OPTIONS
= "-U%s%%%s %s" % (USER
, PASSWORD
, SERVER
)
33 SUMMARY
= os
.path
.join(DATA_DIR
, "traffic-sample-very-short.txt")
34 EXPECTED_NAME
= os
.path
.join(DATA_DIR
, "traffic_replay.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" % (SCRIPT
, options
, STD_OPTIONS
)
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
, SUMMARY
, options
, FIXED
, STD_OPTIONS
)
63 self
.check_run(command
)
65 def test_summary_generation(self
):
66 """Ensure a summary file is generated and the contents are correct"""
68 with
temp_file(self
.tempdir
) as output
:
69 options
= "--traffic-summary %s " % (output
)
70 command
= "%s %s %s %s %s" % (
71 SCRIPT
, SUMMARY
, options
, FIXED
, STD_OPTIONS
)
72 self
.check_run(command
)
73 expected
= (open(EXPECTED_NAME
).readlines())
74 actual
= open(output
).readlines()
75 self
.assertEquals(expected
, actual
)
77 def test_summary_replay(self
):
78 """Ensure a summary file can be replayed against a DC
81 command
= "%s %s %s %s" % (SCRIPT
, SUMMARY
, FIXED
, STD_OPTIONS
)
82 self
.check_run(command
)
84 def test_summary_replay_no_fixed(self
):
85 """Ensure a summary file with no fixed password fails
88 command
= "%s %s %s" % (SCRIPT
, SUMMARY
, STD_OPTIONS
)
89 self
.check_exit_code(command
, 1)
91 def test_model_replay(self
):
92 """Ensure a model can be replayed against a DC
95 model
= "testdata/traffic-sample-very-short.model"
96 command
= "%s %s-D 5 %s %s" % (SCRIPT
, model
, FIXED
, STD_OPTIONS
)
97 self
.check_run(command
)
99 def test_generate_users_only_no_password(self
):
100 """Ensure the generate users only fails if no fixed_password supplied"
102 options
= ("--generate-users-only --number-of-users 20 "
103 "--number-of-groups 5 --average-groups-per-user 2")
104 summary
= "testdata/traffic-sample-very-short.txt"
105 command
= "%s %s %s %s" % (SCRIPT
, summary
, options
, STD_OPTIONS
)
106 self
.check_exit_code(command
, 1)