summaryrefslogtreecommitdiffstats
path: root/python/samba/tests/blackbox/traffic_learner.py
blob: ac941cea81c5afae8387b93886a302a56701c585 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# Black box tests for script/traffic_leaner
#
# Copyright (C) Catalyst IT Ltd. 2017
#
# 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/>.

"""Blackbox tests for traffic_leaner"""

import os
import json
import tempfile
from samba.emulate import traffic

from samba.tests import BlackboxTestCase

LEARNER  = "script/traffic_learner"
DATA_DIR = "python/samba/tests/blackbox/testdata"


class TrafficLearnerTests(BlackboxTestCase):

    def test_no_output_file(self):
        """Run the script with no output file specified. Should fail."""
        self.check_exit_code(LEARNER, 1)

    def test_model_generation(self):
        """Ensure a model is generated from a summary file and it is
           correct"""

        with self.mktemp() as output:
            summary = os.path.join(DATA_DIR, "traffic-sample-very-short.txt")
            command = "%s %s --out %s" % (LEARNER, summary, output)
            self.check_run(command)

            expected_fn = os.path.join(DATA_DIR, "traffic_learner.expected")
            expected = traffic.TrafficModel()
            f=open(expected_fn)
            expected.load(f)
            f.close()

            f=open(output)
            actual = traffic.TrafficModel()
            actual.load(f)
            f.close()

            actual_ngrams = {k: sorted(v) for k, v in actual.ngrams.items()}
            expected_ngrams = {k: sorted(v) for k, v in expected.ngrams.items()}

            self.assertEqual(expected_ngrams, actual_ngrams)

            actual_details = {k: sorted(v) for k, v in actual.query_details.items()}
            expected_details = {k: sorted(v) for k, v in expected.query_details.items()}
            self.assertEqual(expected_details, actual_details)
            self.assertEqual(expected.cumulative_duration, actual.cumulative_duration)
            self.assertEqual(expected.packet_rate, actual.packet_rate)

            with open(expected_fn) as f1, open(output) as f2:
                expected_json = json.load(f1)
                actual_json = json.load(f2)
                self.assertEqual(expected_json, actual_json)