Search in sources :

Example 16 with GrokPattern

use of org.graylog2.grok.GrokPattern in project graylog2-server by Graylog2.

the class V20191121145100_FixDefaultGrokPatternsTest method upgrade.

@Test
public void upgrade() throws ValidationException {
    final V20191121145100_FixDefaultGrokPatterns.PatternToMigrate patternToMigrate = V20191121145100_FixDefaultGrokPatterns.patternsToMigrate.stream().filter(p -> PATTERN_NAME.equals(p.name())).findFirst().orElseThrow(() -> new IllegalStateException("Test expects pattern with name " + PATTERN_NAME + " to " + "be migrated."));
    final GrokPattern pattern = GrokPattern.builder().name(PATTERN_NAME).pattern(patternToMigrate.migrateFrom()).build();
    when(grokPatternService.loadByName(PATTERN_NAME)).thenReturn(Optional.of(pattern));
    migration.upgrade();
    verify(grokPatternService).update(argThat(p -> PATTERN_NAME.equals(p.name()) && patternToMigrate.migrateTo().equals(p.pattern())));
    verify(configService).write(MigrationCompleted.create(Collections.singleton(PATTERN_NAME)));
}
Also used : InjectMocks(org.mockito.InjectMocks) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) GrokPatternService(org.graylog2.grok.GrokPatternService) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Mock(org.mockito.Mock) MigrationCompleted(org.graylog2.migrations.V20191121145100_FixDefaultGrokPatterns.MigrationCompleted) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) MockitoAnnotations(org.mockito.MockitoAnnotations) Mockito.never(org.mockito.Mockito.never) GrokPattern(org.graylog2.grok.GrokPattern) ClusterConfigService(org.graylog2.plugin.cluster.ClusterConfigService) ValidationException(org.graylog2.plugin.database.ValidationException) Optional(java.util.Optional) Collections(java.util.Collections) Before(org.junit.Before) GrokPattern(org.graylog2.grok.GrokPattern) Test(org.junit.Test)

Example 17 with GrokPattern

use of org.graylog2.grok.GrokPattern in project graylog2-server by Graylog2.

the class ContentPackServiceTest method setUp.

@Before
public void setUp() throws Exception {
    final ContentPackInstallationPersistenceService contentPackInstallationPersistenceService = contentPackInstallService;
    final Set<ConstraintChecker> constraintCheckers = Collections.emptySet();
    pluginMetaData = new HashSet<>();
    outputFactories = new HashMap<>();
    outputFactories2 = new HashMap<>();
    final Map<ModelType, EntityWithExcerptFacade<?, ?>> entityFacades = ImmutableMap.of(ModelTypes.GROK_PATTERN_V1, new GrokPatternFacade(objectMapper, patternService), ModelTypes.STREAM_V1, new StreamFacade(objectMapper, streamService, streamRuleService, alertService, alarmCallbackConfigurationService, legacyAlertConditionMigration, indexSetService, userService), ModelTypes.OUTPUT_V1, new OutputFacade(objectMapper, outputService, pluginMetaData, outputFactories, outputFactories2));
    contentPackService = new ContentPackService(contentPackInstallationPersistenceService, constraintCheckers, entityFacades);
    Map<String, String> entityData = new HashMap<>(2);
    entityData.put("name", "NAME");
    entityData.put("pattern", "\\w");
    grokPattern = GrokPattern.builder().pattern("\\w").name("NAME").build();
    JsonNode jsonData = objectMapper.convertValue(entityData, JsonNode.class);
    EntityV1 entityV1 = EntityV1.builder().id(ModelId.of("12345")).type(ModelTypes.GROK_PATTERN_V1).data(jsonData).build();
    ImmutableSet<Entity> entities = ImmutableSet.of(entityV1);
    NativeEntityDescriptor nativeEntityDescriptor = NativeEntityDescriptor.create(ModelId.of("12345"), "dead-beef1", ModelTypes.GROK_PATTERN_V1, "NAME");
    nativeEntityDescriptors = ImmutableSet.of(nativeEntityDescriptor);
    contentPack = ContentPackV1.builder().description("test").entities(entities).name("test").revision(1).summary("").vendor("").url(URI.create("http://graylog.com")).id(ModelId.of("dead-beef")).build();
    contentPackInstallation = ContentPackInstallation.builder().contentPackId(ModelId.of("dead-beef")).contentPackRevision(1).entities(nativeEntityDescriptors).comment("Installed").parameters(ImmutableMap.copyOf(Collections.emptyMap())).createdAt(Instant.now()).createdBy("me").build();
}
Also used : Entity(org.graylog2.contentpacks.model.entities.Entity) StreamFacade(org.graylog2.contentpacks.facades.StreamFacade) HashMap(java.util.HashMap) JsonNode(com.fasterxml.jackson.databind.JsonNode) EntityV1(org.graylog2.contentpacks.model.entities.EntityV1) EntityWithExcerptFacade(org.graylog2.contentpacks.facades.EntityWithExcerptFacade) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) ModelType(org.graylog2.contentpacks.model.ModelType) ConstraintChecker(org.graylog2.contentpacks.constraints.ConstraintChecker) GrokPatternFacade(org.graylog2.contentpacks.facades.GrokPatternFacade) OutputFacade(org.graylog2.contentpacks.facades.OutputFacade) Before(org.junit.Before)

Example 18 with GrokPattern

use of org.graylog2.grok.GrokPattern in project graylog2-server by Graylog2.

the class GrokExtractorTest method testNamedCapturesOnly.

@Test
public void testNamedCapturesOnly() {
    final Map<String, Object> config = new HashMap<>();
    final GrokPattern mynumber = GrokPattern.create("MYNUMBER", "(?:%{BASE10NUM})");
    patternSet.add(mynumber);
    config.put("named_captures_only", true);
    final GrokExtractor extractor1 = makeExtractor("%{MYNUMBER:num}", config);
    config.put("named_captures_only", true);
    final GrokExtractor extractor2 = makeExtractor("%{MYNUMBER:num;int}", config);
    config.put("named_captures_only", false);
    final GrokExtractor extractor3 = makeExtractor("%{MYNUMBER:num}", config);
    final GrokExtractor extractor4 = makeExtractor("%{MYNUMBER:num}");
    assertThat(extractor1.run("2015")).hasSize(1).containsOnly(new Extractor.Result("2015", "num", -1, -1));
    assertThat(extractor2.run("2015")).hasSize(1).containsOnly(new Extractor.Result(2015, "num", -1, -1));
    assertThat(extractor3.run("2015")).hasSize(2).containsOnly(new Extractor.Result("2015", "num", -1, -1), new Extractor.Result("2015", "BASE10NUM", -1, -1));
    assertThat(extractor4.run("2015")).hasSize(2).containsOnly(new Extractor.Result("2015", "num", -1, -1), new Extractor.Result("2015", "BASE10NUM", -1, -1));
}
Also used : GrokPattern(org.graylog2.grok.GrokPattern) HashMap(java.util.HashMap) Extractor(org.graylog2.plugin.inputs.Extractor) Test(org.junit.Test)

Example 19 with GrokPattern

use of org.graylog2.grok.GrokPattern in project graylog2-server by Graylog2.

the class GrokExtractorTest method setUp.

@Before
public void setUp() throws Exception {
    patternSet = new ArrayList<>();
    final GrokPattern baseNum = GrokPattern.create("BASE10NUM", "(?<![0-9.+-])(?>[+-]?(?:(?:[0-9]+(?:\\.[0-9]+)?)|(?:\\.[0-9]+)))");
    final GrokPattern number = GrokPattern.create("NUMBER", "(?:%{BASE10NUM:UNWANTED})");
    final GrokPattern data = GrokPattern.create("GREEDY", ".*");
    final GrokPattern twoBaseNums = GrokPattern.create("TWOBASENUMS", "%{BASE10NUM} %{BASE10NUM}");
    final GrokPattern test1 = GrokPattern.create("TEST1", "test1");
    final GrokPattern test2 = GrokPattern.create("TEST2", "test2");
    final GrokPattern orPattern = GrokPattern.create("ORTEST", "(%{TEST1:test}|%{TEST2:test})");
    patternSet.add(baseNum);
    patternSet.add(number);
    patternSet.add(data);
    patternSet.add(twoBaseNums);
    patternSet.add(test1);
    patternSet.add(test2);
    patternSet.add(orPattern);
}
Also used : GrokPattern(org.graylog2.grok.GrokPattern) Before(org.junit.Before)

Example 20 with GrokPattern

use of org.graylog2.grok.GrokPattern in project graylog2-server by Graylog2.

the class GrokResourceTest method bulkUpdatePatternsFromTextFileWithCR.

@Test
public void bulkUpdatePatternsFromTextFileWithCR() throws Exception {
    final String patterns = Arrays.stream(GROK_LINES).collect(Collectors.joining("\r"));
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(patterns.getBytes(StandardCharsets.UTF_8));
    final GrokPattern expectedPattern = GrokPattern.create("TEST_PATTERN_0", "Foo");
    final Response response = grokResource.bulkUpdatePatternsFromTextFile(inputStream, true, null);
    assertThat(response.getStatusInfo()).isEqualTo(Response.Status.ACCEPTED);
    assertThat(response.hasEntity()).isFalse();
    await().atMost(Duration.FIVE_SECONDS).until(() -> !subscriber.events.isEmpty());
    assertThat(subscriber.events).containsOnly(GrokPatternsUpdatedEvent.create(Collections.singleton(expectedPattern.name())));
}
Also used : Response(javax.ws.rs.core.Response) GrokPattern(org.graylog2.grok.GrokPattern) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Aggregations

GrokPattern (org.graylog2.grok.GrokPattern)28 Test (org.junit.Test)15 ValidationException (org.graylog2.plugin.database.ValidationException)9 Timed (com.codahale.metrics.annotation.Timed)7 ApiOperation (io.swagger.annotations.ApiOperation)7 NativeEntityDescriptor (org.graylog2.contentpacks.model.entities.NativeEntityDescriptor)7 GrokException (io.krakens.grok.api.exception.GrokException)6 AuditEvent (org.graylog2.audit.jersey.AuditEvent)6 GrokPatternEntity (org.graylog2.contentpacks.model.entities.GrokPatternEntity)6 NotFoundException (org.graylog2.database.NotFoundException)5 PatternSyntaxException (java.util.regex.PatternSyntaxException)4 Response (javax.ws.rs.core.Response)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 POST (javax.ws.rs.POST)3 Path (javax.ws.rs.Path)3 Entity (org.graylog2.contentpacks.model.entities.Entity)3 EntityDescriptor (org.graylog2.contentpacks.model.entities.EntityDescriptor)3 Before (org.junit.Before)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 Grok (io.krakens.grok.api.Grok)2