def test_slice(self): #, mock_get_pattern): default_pattern1 = Pattern.Pattern(0, [], []) default_pattern1.id = 1 default_pattern2 = Pattern.Pattern(0, [], []) default_pattern2.id = 2 default_pattern3 = Pattern.Pattern(0, [], []) default_pattern3.id = 3 m = Mock() m.side_effect = [default_pattern1, default_pattern2, default_pattern3] # Mock(return_value=self.default_pattern) logflow.logsparser.Journal.Journal.find_pattern = m #mock_get_pattern.return_value = 1 read_data = pickle.dumps( { 'word2vec': { "1": np.asarray([1]*20), "2": np.asarray([2]*20), "3": np.asarray([3]*20),"4": [4]*20, "5": [5]*20, "6": [6]*20, "7": [7]*20 }, 'counter_patterns': { 1:10, 2:100, 3:100, 4:100, 6:1000, 5:1000 }, "LSTM": { 3:self.model.state_dict() } , "dict_patterns": { } }) mockOpen = mock_open(read_data=read_data) with patch('builtins.open', mockOpen): dataset = Dataset(path_model="/", path_data="/", name_model="/") dataset.load_files() read_data_str = "1530388399 2018 Jun 30 21:53:19 m21205 authpriv info sshd pam_unix(sshd:session): session closed, for1 User Root/1 123\n1530388399 2018 Jun 30 21:53:19 m21205 authpriv info sshd pam_unix(sshd:session): session closed, for1 User Root/1 123\n1530388399 2018 Jun 30 21:53:19 m21205 authpriv info sshd pam_unix(sshd:session): session closed, for1 User Root/1 123" # mockOpen = mock_open(read_data=read_data) mockOpen = unittest.mock.mock_open(read_data=''.join(read_data_str)) mockOpen.return_value.__iter__ = lambda self: self mockOpen.return_value.__next__ = lambda self: next(iter(self.readline, '')) with patch('builtins.open', mockOpen): dataset.load_logs() # Normal, return a log output = dataset.get_slice(2) self.assertIsInstance(output, logflow.treebuilding.Log.Log) dataset.show_selected_lines(2)
def __init__(self, line: str, index_line=-1, parser_function=""): self.line = line self.message: List[str] = [] self.pattern = Pattern.Pattern(0, [], []) self.pattern.id = -1 self.vector = -1 self.usable = True self.slice: List[List[float]] = [] self.index_line = index_line self.index_slice: List[int] = [] self.cardinality = -2 self.parser_function = parser_function if self.line != "-1": self.preprocess_line() self.severity = "" self.weight = -1
def setUp(self): self.model = LSTMLayer(num_classes=5) self.default_pattern = Pattern.Pattern(0, [], [])
def __init__(self, dict_patterns, w2v, counter_patterns): self.dict_patterns = dict_patterns self.default_pattern = Pattern.Pattern(0, [], []) self.w2v = w2v self.counter_patterns = counter_patterns