19
19
import static java .util .stream .Collectors .joining ;
20
20
import static java .util .stream .Collectors .toList ;
21
21
import static java .util .stream .Collectors .toSet ;
22
+ import static org .apache .commons .io .FileUtils .listFiles ;
22
23
import static org .apache .geode .management .internal .cli .commands .ExportLogsCommand .FORMAT ;
23
24
import static org .apache .geode .management .internal .cli .commands .ExportLogsCommand .ONLY_DATE_FORMAT ;
24
25
import static org .assertj .core .api .Assertions .assertThat ;
27
28
import java .io .IOException ;
28
29
import java .io .Serializable ;
29
30
import java .nio .charset .Charset ;
31
+ import java .nio .file .Path ;
30
32
import java .time .LocalDateTime ;
31
33
import java .time .ZoneId ;
32
34
import java .time .ZonedDateTime ;
41
43
42
44
import org .apache .commons .io .FileUtils ;
43
45
import org .apache .logging .log4j .Logger ;
46
+ import org .junit .After ;
44
47
import org .junit .Before ;
45
48
import org .junit .Rule ;
46
49
import org .junit .Test ;
47
50
import org .junit .experimental .categories .Category ;
51
+ import org .junit .rules .TemporaryFolder ;
48
52
49
53
import org .apache .geode .cache .Cache ;
50
54
import org .apache .geode .distributed .ConfigurationProperties ;
@@ -70,12 +74,18 @@ public class ExportLogsDUnitTest {
70
74
@ Rule
71
75
public GfshCommandRule gfshConnector = new GfshCommandRule ();
72
76
73
- private MemberVM locator ;
77
+ @ Rule
78
+ public TemporaryFolder temporaryFolder = new TemporaryFolder ();
79
+
80
+ protected MemberVM locator ;
74
81
private MemberVM server1 ;
75
82
private MemberVM server2 ;
76
83
77
84
private Map <MemberVM , List <LogLine >> expectedMessages ;
78
85
86
+ public File getWorkingDirectory () throws Exception {
87
+ return locator .getWorkingDir ();
88
+ }
79
89
80
90
@ Before
81
91
public void setup () throws Exception {
@@ -103,9 +113,53 @@ public void setup() throws Exception {
103
113
});
104
114
}
105
115
116
+ connect ();
117
+ }
118
+
119
+ public void connect () throws Exception {
106
120
gfshConnector .connectAndVerify (locator );
107
121
}
108
122
123
+ @ After
124
+ public void after () throws Exception {
125
+ Stream .of (getWorkingDirectory ().listFiles ())
126
+ .filter (f -> f .getName ().endsWith (".zip" )).forEach (file -> file .delete ());
127
+ }
128
+
129
+ @ Test
130
+ public void withFiles_savedToLocatorWorkingDir () throws Exception {
131
+ String [] extensions = {"zip" };
132
+ // Expects locator to produce file in own working directory when connected via JMX
133
+ gfshConnector .executeCommand ("export logs" );
134
+ assertThat (listFiles (getWorkingDirectory (), extensions , false )).isNotEmpty ();
135
+ }
136
+
137
+ @ Test
138
+ public void withFiles_savedToLocatorSpecifiedRelativeDir () throws Exception {
139
+ String [] extensions = {"zip" };
140
+ Path workingDirPath = getWorkingDirectory ().toPath ();
141
+ Path subdirPath = workingDirPath .resolve ("some" ).resolve ("test" ).resolve ("directory" );
142
+ Path relativeDir = workingDirPath .relativize (subdirPath );
143
+ // Expects locator to produce file in own working directory when connected via JMX
144
+ gfshConnector .executeCommand ("export logs --dir=" + relativeDir .toString ());
145
+ assertThat (listFiles (getWorkingDirectory (), extensions , false )).isEmpty ();
146
+ assertThat (listFiles (getWorkingDirectory (), extensions , true )).isNotEmpty ();
147
+ assertThat (listFiles (subdirPath .toFile (), extensions , false )).isNotEmpty ();
148
+ }
149
+
150
+ @ Test
151
+ public void withFiles_savedToLocatorSpecifiedAbsoluteDir () throws Exception {
152
+ String [] extensions = {"zip" };
153
+ Path workingDirPath = getWorkingDirectory ().toPath ();
154
+ Path absoluteDirPath =
155
+ workingDirPath .resolve ("some" ).resolve ("test" ).resolve ("directory" ).toAbsolutePath ();
156
+ // Expects locator to produce file in own working directory when connected via JMX
157
+ gfshConnector .executeCommand ("export logs --dir=" + absoluteDirPath .toString ());
158
+ assertThat (listFiles (getWorkingDirectory (), extensions , false )).isEmpty ();
159
+ assertThat (listFiles (getWorkingDirectory (), extensions , true )).isNotEmpty ();
160
+ assertThat (listFiles (absoluteDirPath .toFile (), extensions , false )).isNotEmpty ();
161
+ }
162
+
109
163
@ Test
110
164
public void startAndEndDateCanIncludeLogs () throws Exception {
111
165
ZonedDateTime now = LocalDateTime .now ().atZone (ZoneId .systemDefault ());
@@ -219,7 +273,7 @@ public void exportLogsRegionIsCleanedUpProperly() throws IOException, ClassNotFo
219
273
}
220
274
221
275
222
- private void verifyZipFileContents (Set <String > acceptedLogLevels ) throws IOException {
276
+ private void verifyZipFileContents (Set <String > acceptedLogLevels ) throws Exception {
223
277
File unzippedLogFileDir = unzipExportedLogs ();
224
278
225
279
Set <File > dirsFromZipFile =
@@ -270,19 +324,19 @@ private void verifyLogFileContents(Set<String> acceptedLogLevels, File dirForMem
270
324
271
325
}
272
326
273
- private File unzipExportedLogs () throws IOException {
274
- File locatorWorkingDir = locator . getWorkingDir ();
275
- List <File > filesInDir = Stream .of (locatorWorkingDir .listFiles ()).collect (toList ());
327
+ private File unzipExportedLogs () throws Exception {
328
+ File locatorWorkingDir = getWorkingDirectory ();
329
+ List <File > filesInDir = Stream .of (getWorkingDirectory () .listFiles ()).collect (toList ());
276
330
assertThat (filesInDir ).isNotEmpty ();
277
331
278
332
279
- List <File > zipFilesInDir = Stream .of (locatorWorkingDir .listFiles ())
333
+ List <File > zipFilesInDir = Stream .of (getWorkingDirectory () .listFiles ())
280
334
.filter (f -> f .getName ().endsWith (".zip" )).collect (toList ());
281
335
assertThat (zipFilesInDir )
282
336
.describedAs (filesInDir .stream ().map (File ::getAbsolutePath ).collect (joining ("," )))
283
337
.hasSize (1 );
284
338
285
- File unzippedLogFileDir = new File ( locatorWorkingDir , "unzippedLogs" );
339
+ File unzippedLogFileDir = temporaryFolder . newFolder ( "unzippedLogs" );
286
340
ZipUtils .unzip (zipFilesInDir .get (0 ).getCanonicalPath (), unzippedLogFileDir .getCanonicalPath ());
287
341
return unzippedLogFileDir ;
288
342
}
0 commit comments