Skip to content

Commit 46f69f4

Browse files
LokowandtgZPascal
authored andcommitted
Fix JUnit tests for logCache
The old "Applications.logs" method is keept for compatibility and when a log stream is needed. Adding new "logsRecent" method to access the logCache. integration-tests "ApplicationTest.logs" and "ApplicationTest.logsRecent" work. Minor changes in DefaultApplicationsTest in other JUnit tests to make them execute in Eclipse.
1 parent c04c383 commit 46f69f4

File tree

4 files changed

+293
-65
lines changed

4 files changed

+293
-65
lines changed

cloudfoundry-operations/src/main/java/org/cloudfoundry/operations/applications/Applications.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import org.cloudfoundry.doppler.LogMessage;
2020
import org.cloudfoundry.logcache.v1.Log;
2121
import org.cloudfoundry.logcache.v1.ReadRequest;
22-
import org.cloudfoundry.logcache.v1.ReadResponse;
22+
2323
import reactor.core.publisher.Flux;
2424
import reactor.core.publisher.Mono;
2525

@@ -129,6 +129,15 @@ public interface Applications {
129129
@Deprecated
130130
Flux<Log> logs(LogsRequest request);
131131

132+
/**
133+
* List the applications logs from logCacheClient.
134+
* If no messages are available, an empty Flux is returned.
135+
*
136+
* @param request the application logs request
137+
* @return the applications logs
138+
*/
139+
Flux<Log> logsRecent(ReadRequest request);
140+
132141
/**
133142
* List the applications logs.
134143
* Only works with {@code Loggregator < 107.0}, shipped in {@code CFD < 24.3}

cloudfoundry-operations/src/main/java/org/cloudfoundry/operations/applications/DefaultApplications.java

Lines changed: 128 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,10 @@
150150
import org.cloudfoundry.client.v3.tasks.CreateTaskResponse;
151151
import org.cloudfoundry.client.v3.tasks.TaskResource;
152152
import org.cloudfoundry.doppler.DopplerClient;
153-
import org.cloudfoundry.doppler.Envelope;
154153
import org.cloudfoundry.doppler.EventType;
155154
import org.cloudfoundry.doppler.LogMessage;
156155
import org.cloudfoundry.doppler.RecentLogsRequest;
157156
import org.cloudfoundry.doppler.StreamRequest;
158-
import org.cloudfoundry.logcache.v1.EnvelopeType;
159157
import org.cloudfoundry.logcache.v1.Log;
160158
import org.cloudfoundry.logcache.v1.LogCacheClient;
161159
import org.cloudfoundry.logcache.v1.ReadRequest;
@@ -578,6 +576,83 @@ public Flux<ApplicationLog> logs(ApplicationLogsRequest request) {
578576
.build());
579577
}
580578

579+
@SuppressWarnings("deprecation")
580+
@Test
581+
void logsRecent_doppler() {
582+
requestApplications(
583+
this.cloudFoundryClient,
584+
"test-application-name",
585+
TEST_SPACE_ID,
586+
"test-metadata-id");
587+
requestLogsRecent(this.dopplerClient, "test-metadata-id");
588+
this.applications
589+
.logs(LogsRequest.builder().name("test-application-name").recent(true).build())
590+
.as(StepVerifier::create)
591+
.expectNextMatches(log -> log.getMessage().equals("test-log-message-message"))
592+
.expectComplete()
593+
.verify(Duration.ofSeconds(5));
594+
}
595+
596+
@SuppressWarnings("deprecation")
597+
@Test
598+
void logsNoApp_doppler() {
599+
requestApplicationsEmpty(this.cloudFoundryClient, "test-application-name", TEST_SPACE_ID);
600+
601+
this.applications
602+
.verify(Duration.ofSeconds(5));
603+
}
604+
605+
@SuppressWarnings("deprecation")
606+
@Test
607+
void logs_doppler() {
608+
requestApplications(
609+
this.cloudFoundryClient,
610+
"test-application-name",
611+
TEST_SPACE_ID,
612+
"test-metadata-id");
613+
requestLogsStream(this.dopplerClient, "test-metadata-id");
614+
this.applications
615+
.logs(LogsRequest.builder().name("test-application-name").recent(false).build())
616+
.as(StepVerifier::create)
617+
.expectNextMatches(log -> log.getMessage().equals("test-log-message-message"))
618+
.expectComplete()
619+
.verify(Duration.ofSeconds(5));
620+
}
621+
622+
@Test
623+
void logsRecent_LogCache() {
624+
requestApplications(
625+
this.cloudFoundryClient,
626+
"test-application-name",
627+
TEST_SPACE_ID,
628+
"test-metadata-id");
629+
requestLogsRecentLogCache(this.logCacheClient, "test-metadata-id", "test-payload");
630+
this.applications
631+
.logsRecent(ReadRequest.builder().sourceId("test-application-name").build())
632+
.as(StepVerifier::create)
633+
.expectNext(fill(Log.builder()).type(LogType.OUT).build())
634+
.expectComplete()
635+
.verify(Duration.ofSeconds(5));
636+
}
637+
638+
@SuppressWarnings("deprecation")
639+
@Test
640+
void logsRecentNotSet_doppler() {
641+
requestApplications(
642+
this.cloudFoundryClient,
643+
"test-application-name",
644+
TEST_SPACE_ID,
645+
"test-metadata-id");
646+
requestLogsStream(this.dopplerClient, "test-metadata-id");
647+
648+
this.applications
649+
.logs(LogsRequest.builder().name("test-application-name").build())
650+
.as(StepVerifier::create)
651+
.expectNext(fill(LogMessage.builder(), "log-message-").build())
652+
.expectComplete()
653+
.verify(Duration.ofSeconds(5));
654+
}
655+
581656
@Override
582657
@SuppressWarnings("deprecation")
583658
public Mono<Void> push(PushApplicationRequest request) {
@@ -2432,6 +2507,57 @@ private static Mono<AbstractApplicationResource> requestGetApplication(
24322507
.cast(AbstractApplicationResource.class);
24332508
}
24342509

2510+
private static void requestInstancesApplicationFailing(
2511+
CloudFoundryClient cloudFoundryClient, String applicationId) {
2512+
when(cloudFoundryClient
2513+
.applicationsV2()
2514+
.instances(
2515+
ApplicationInstancesRequest.builder()
2516+
.applicationId(applicationId)
2517+
.build()))
2518+
.thenReturn(
2519+
Mono.just(
2520+
fill(
2521+
ApplicationInstancesResponse.builder(),
2522+
"application-instances-")
2523+
.instance(
2524+
"instance-0",
2525+
fill(
2526+
ApplicationInstanceInfo.builder(),
2527+
"application-instance-info-")
2528+
.state("FAILED")
2529+
.build())
2530+
.build()));
2531+
}
2532+
2533+
private static void requestLogsRecentLogCache(
2534+
LogCacheClient logCacheClient, String applicationName, String payload) {
2535+
when(logCacheClient.recentLogs(any()))
2536+
.thenReturn(
2537+
Mono.just(
2538+
fill(ReadResponse.builder())
2539+
.envelopes(
2540+
fill(EnvelopeBatch.builder())
2541+
.batch(
2542+
Arrays.asList(
2543+
fill(org.cloudfoundry
2544+
.logcache.v1
2545+
.Envelope
2546+
.builder())
2547+
.log(
2548+
Log
2549+
.builder()
2550+
.payload(
2551+
payload)
2552+
.type(
2553+
LogType
2554+
.OUT)
2555+
.build())
2556+
.build()))
2557+
.build())
2558+
.build()));
2559+
}
2560+
24352561
private static Flux<DomainResource> requestListDomains(
24362562
CloudFoundryClient cloudFoundryClient, String organizationId) {
24372563
return PaginationUtils.requestClientV3Resources(

cloudfoundry-operations/src/test/java/org/cloudfoundry/operations/applications/DefaultApplicationsTest.java

Lines changed: 97 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,11 @@
140140
import org.cloudfoundry.client.v3.tasks.CreateTaskResponse;
141141
import org.cloudfoundry.client.v3.tasks.TaskResource;
142142
import org.cloudfoundry.doppler.DopplerClient;
143-
import org.cloudfoundry.doppler.Envelope;
144143
import org.cloudfoundry.doppler.EventType;
145144
import org.cloudfoundry.doppler.LogMessage;
146145
import org.cloudfoundry.doppler.RecentLogsRequest;
147146
import org.cloudfoundry.doppler.StreamRequest;
148147
import org.cloudfoundry.logcache.v1.EnvelopeBatch;
149-
import org.cloudfoundry.logcache.v1.EnvelopeType;
150148
import org.cloudfoundry.logcache.v1.Log;
151149
import org.cloudfoundry.logcache.v1.LogCacheClient;
152150
import org.cloudfoundry.logcache.v1.LogType;
@@ -156,7 +154,6 @@
156154
import org.cloudfoundry.util.DateUtils;
157155
import org.cloudfoundry.util.FluentMap;
158156
import org.cloudfoundry.util.ResourceMatchingUtils;
159-
import org.junit.jupiter.api.Disabled;
160157
import org.junit.jupiter.api.Test;
161158
import org.springframework.core.io.ClassPathResource;
162159
import reactor.core.publisher.Flux;
@@ -1316,25 +1313,26 @@ void listTasks() {
13161313
.verify(Duration.ofSeconds(5));
13171314
}
13181315

1316+
@SuppressWarnings("deprecation")
13191317
@Test
1320-
void logs() {
1318+
void logsRecent_doppler() {
13211319
requestApplications(
13221320
this.cloudFoundryClient,
13231321
"test-application-name",
13241322
TEST_SPACE_ID,
13251323
"test-metadata-id");
1326-
requestLogsRecentLogCache(this.logCacheClient, "test-application-name");
1327-
1324+
requestLogsRecent(this.dopplerClient, "test-metadata-id");
13281325
this.applications
13291326
.logs(LogsRequest.builder().name("test-application-name").recent(true).build())
13301327
.as(StepVerifier::create)
1331-
.expectNextMatches(log -> log.getPayload().equals("test-payload"))
1328+
.expectNextMatches(log -> log.getMessage().equals("test-log-message-message"))
13321329
.expectComplete()
13331330
.verify(Duration.ofSeconds(5));
13341331
}
13351332

1333+
@SuppressWarnings("deprecation")
13361334
@Test
1337-
void logsNoApp() {
1335+
void logsNoApp_doppler() {
13381336
requestApplicationsEmpty(this.cloudFoundryClient, "test-application-name", TEST_SPACE_ID);
13391337

13401338
this.applications
@@ -1349,40 +1347,56 @@ void logsNoApp() {
13491347
.verify(Duration.ofSeconds(5));
13501348
}
13511349

1352-
// TODO: it's not passing since recentLogs is not properly implemented yet with logcacheclient
1353-
@Test
1354-
void logsRecent() {
1355-
requestApplications(
1356-
this.cloudFoundryClient,
1357-
"test-application-name",
1358-
TEST_SPACE_ID,
1359-
"test-metadata-id");
1360-
requestLogsRecentLogCache(this.logCacheClient, "test-metadata-id");
1361-
1362-
this.applications
1363-
.logs(LogsRequest.builder().name("test-application-name").build())
1364-
.as(StepVerifier::create)
1365-
.expectNext(fill(Log.builder(), "log-message-").build())
1366-
.expectComplete()
1367-
.verify(Duration.ofSeconds(5));
1368-
}
1369-
// TODO: it's not passing since recentLogs is not properly implemented yet with logcacheclient
1370-
@Test
1371-
void logsRecentNotSet() {
1372-
requestApplications(
1373-
this.cloudFoundryClient,
1374-
"test-application-name",
1375-
TEST_SPACE_ID,
1376-
"test-metadata-id");
1377-
requestLogsStream(this.dopplerClient, "test-metadata-id");
1378-
1379-
this.applications
1380-
.logs(LogsRequest.builder().name("test-application-name").build())
1381-
.as(StepVerifier::create)
1382-
.expectNext(fill(Log.builder(), "log-message-").build())
1383-
.expectComplete()
1384-
.verify(Duration.ofSeconds(5));
1385-
}
1350+
@SuppressWarnings("deprecation")
1351+
@Test
1352+
void logs_doppler() {
1353+
requestApplications(
1354+
this.cloudFoundryClient,
1355+
"test-application-name",
1356+
TEST_SPACE_ID,
1357+
"test-metadata-id");
1358+
requestLogsStream(this.dopplerClient, "test-metadata-id");
1359+
this.applications
1360+
.logs(LogsRequest.builder().name("test-application-name").recent(false).build())
1361+
.as(StepVerifier::create)
1362+
.expectNextMatches(log -> log.getMessage().equals("test-log-message-message"))
1363+
.expectComplete()
1364+
.verify(Duration.ofSeconds(5));
1365+
}
1366+
1367+
@Test
1368+
void logsRecent_LogCache() {
1369+
requestApplications(
1370+
this.cloudFoundryClient,
1371+
"test-application-name",
1372+
TEST_SPACE_ID,
1373+
"test-metadata-id");
1374+
requestLogsRecentLogCache(this.logCacheClient, "test-metadata-id", "test-payload");
1375+
this.applications
1376+
.logsRecent(ReadRequest.builder().sourceId("test-application-name").build())
1377+
.as(StepVerifier::create)
1378+
.expectNext(fill(Log.builder()).type(LogType.OUT).build())
1379+
.expectComplete()
1380+
.verify(Duration.ofSeconds(5));
1381+
}
1382+
1383+
@SuppressWarnings("deprecation")
1384+
@Test
1385+
void logsRecentNotSet_doppler() {
1386+
requestApplications(
1387+
this.cloudFoundryClient,
1388+
"test-application-name",
1389+
TEST_SPACE_ID,
1390+
"test-metadata-id");
1391+
requestLogsStream(this.dopplerClient, "test-metadata-id");
1392+
1393+
this.applications
1394+
.logs(LogsRequest.builder().name("test-application-name").build())
1395+
.as(StepVerifier::create)
1396+
.expectNext(fill(LogMessage.builder(), "log-message-").build())
1397+
.expectComplete()
1398+
.verify(Duration.ofSeconds(5));
1399+
}
13861400

13871401
@Test
13881402
void pushDocker() {
@@ -5328,28 +5342,53 @@ private static void requestListTasksEmpty(
53285342
.build()));
53295343
}
53305344

5331-
private static void requestLogsRecentLogCache(LogCacheClient logCacheClient, String applicationId) {
5332-
when(logCacheClient.recentLogs(
5333-
any()))
5334-
.thenReturn(
5335-
Mono.just(fill(ReadResponse.builder())
5336-
.envelopes(fill(EnvelopeBatch.builder())
5337-
.batch(fill(org.cloudfoundry.logcache.v1.Envelope
5338-
.builder())
5339-
.log(fill(Log.builder())
5340-
.payload("test-payload")
5341-
.type(LogType.OUT)
5342-
.build())
5343-
.build())
5344-
.build())
5345-
.build()));
5345+
private static void requestLogsRecentLogCache(
5346+
LogCacheClient logCacheClient, String applicationName, String payload) {
5347+
when(logCacheClient.recentLogs(any()))
5348+
.thenReturn(
5349+
Mono.just(
5350+
fill(ReadResponse.builder())
5351+
.envelopes(
5352+
fill(EnvelopeBatch.builder())
5353+
.batch(
5354+
Arrays.asList(
5355+
fill(org.cloudfoundry
5356+
.logcache.v1
5357+
.Envelope
5358+
.builder())
5359+
.log(
5360+
Log
5361+
.builder()
5362+
.payload(
5363+
payload)
5364+
.type(
5365+
LogType
5366+
.OUT)
5367+
.build())
5368+
.build()))
5369+
.build())
5370+
.build()));
53465371
}
53475372

53485373
private static void requestLogsStream(DopplerClient dopplerClient, String applicationId) {
53495374
when(dopplerClient.stream(StreamRequest.builder().applicationId(applicationId).build()))
53505375
.thenReturn(
53515376
Flux.just(
5352-
Envelope.builder()
5377+
org.cloudfoundry.doppler.Envelope.builder()
5378+
.eventType(EventType.LOG_MESSAGE)
5379+
.logMessage(
5380+
fill(LogMessage.builder(), "log-message-").build())
5381+
.origin("rsp")
5382+
.build()));
5383+
}
5384+
5385+
@SuppressWarnings("deprecation")
5386+
private static void requestLogsRecent(DopplerClient dopplerClient, String applicationId) {
5387+
when(dopplerClient.recentLogs(
5388+
RecentLogsRequest.builder().applicationId(applicationId).build()))
5389+
.thenReturn(
5390+
Flux.just(
5391+
org.cloudfoundry.doppler.Envelope.builder()
53535392
.eventType(EventType.LOG_MESSAGE)
53545393
.logMessage(
53555394
fill(LogMessage.builder(), "log-message-").build())

0 commit comments

Comments
 (0)