1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.chukwa.validationframework;
19
20
21 import java.io.File;
22 import java.io.IOException;
23 import org.apache.hadoop.chukwa.conf.ChukwaConfiguration;
24 import org.apache.hadoop.chukwa.datacollection.agent.ChukwaAgent;
25 import org.apache.hadoop.chukwa.datacollection.agent.ChukwaAgent.AlreadyRunningException;
26 import org.apache.hadoop.chukwa.datacollection.collector.CollectorStub;
27 import org.apache.hadoop.chukwa.datacollection.controller.ChukwaAgentController;
28 import org.apache.hadoop.chukwa.validationframework.interceptor.ChunkDumper;
29 import org.apache.hadoop.chukwa.validationframework.interceptor.SetupTestClasses;
30 import org.apache.hadoop.chukwa.validationframework.util.DataOperations;
31
32 public class ChukwaAgentToCollectorValidator {
33 public static final int ADD = 100;
34 public static final int VALIDATE = 200;
35
36 private static void usage() {
37 System.out.println("usage ...");
38 System.exit(-1);
39 }
40
41
42
43
44
45
46
47 public static void main(String[] args) throws Throwable {
48 if (args.length != 2) {
49 usage();
50 }
51
52 int command = -1;
53
54 if ("-add".equalsIgnoreCase(args[0])) {
55 command = ChukwaAgentToCollectorValidator.ADD;
56 } else if ("-validate".equalsIgnoreCase(args[0])) {
57 command = ChukwaAgentToCollectorValidator.VALIDATE;
58 } else {
59 usage();
60 }
61
62 String chukwaTestRepository = System.getenv("chukwaTestRepository");
63 if (chukwaTestRepository == null) {
64 chukwaTestRepository = "/tmp/chukwaTestRepository/";
65 }
66
67 if (!chukwaTestRepository.endsWith("/")) {
68 chukwaTestRepository += "/";
69 }
70
71 String fileName = args[1];
72
73 String name = null;
74 if (fileName.indexOf("/") >= 0) {
75 name = fileName.substring(fileName.lastIndexOf("/"));
76 } else {
77 name = fileName;
78 }
79
80 String chukwaTestDirectory = chukwaTestRepository + name;
81 String inputFile = chukwaTestDirectory + "/input/" + name;
82 String outputDir = null;
83
84 if (command == ChukwaAgentToCollectorValidator.ADD) {
85 File dir = new File(chukwaTestDirectory + "/input/");
86 if (dir.exists()) {
87 throw new RuntimeException(
88 "a test with the same input file is already there, remove it first");
89 }
90 dir.mkdirs();
91 DataOperations.copyFile(fileName, inputFile);
92 outputDir = "/gold";
93 } else {
94 outputDir = "/" + System.currentTimeMillis();
95 }
96
97 System.out.println("chukwaTestDirectory [" + chukwaTestDirectory + "]");
98 System.out.println("command ["
99 + ((command == ChukwaAgentToCollectorValidator.ADD) ? "ADD"
100 : "VALIDATE") + "]");
101 System.out.println("fileName [" + inputFile + "]");
102
103 ChukwaConfiguration conf = new ChukwaConfiguration(true);
104 String collectorOutputDir = conf.get("chukwaCollector.outputDir");
105
106 prepareAndSendData(chukwaTestDirectory + outputDir, inputFile,
107 collectorOutputDir);
108 extractRawLog(chukwaTestDirectory + outputDir, name, collectorOutputDir);
109 boolean rawLogTestResult = validateRawLogs(chukwaTestDirectory + outputDir,
110 name);
111
112 boolean binLogTestResult = true;
113
114 if (command == ChukwaAgentToCollectorValidator.VALIDATE) {
115 binLogTestResult = validateOutputs(chukwaTestDirectory + outputDir, name);
116 }
117
118 if (rawLogTestResult == true && binLogTestResult == true) {
119 System.out.println("test OK");
120 System.exit(10);
121 } else {
122 System.out.println("test KO");
123 throw new RuntimeException("test failed for file [" + name + "]");
124 }
125 }
126
127 public static void prepareAndSendData(String dataRootFolder,
128 String inputFile, String dataSinkDirectory) throws Throwable {
129
130 ChunkDumper.testRepositoryDumpDir = dataRootFolder + "/";
131
132 SetupTestClasses.setupClasses();
133
134
135 File collectorDir = new File(dataSinkDirectory);
136 String[] files = collectorDir.list();
137 for (String f : files) {
138 File file = new File(dataSinkDirectory + File.separator + f);
139 file.delete();
140 System.out.println("Deleting previous collectors files: " + f);
141 }
142
143 System.out.println("Starting agent");
144 String[] agentArgs = new String[0];
145 ChukwaAgent.main(agentArgs);
146
147
148 System.out.println("Starting collector");
149 CollectorStub.main(new String[0]);
150
151
152 ChukwaAgent agent = ChukwaAgent.getAgent();
153
154 int portno = 9093;
155 ChukwaAgentController cli = new ChukwaAgentController("localhost", portno);
156
157
158
159
160
161
162 System.out.println("Adding adaptor");
163 String adaptor = cli.add(
164 "org.apache.hadoop.chukwa.datacollection.adaptor.filetailer.CharFileTailingAdaptorUTF8NewLineEscaped",
165 "AutomatedTestType", "0 " + inputFile, 0);
166
167 cli.remove(adaptor);
168 System.out.println("Adaptor removed");
169 agent.shutdown();
170 System.out.println("Shutting down agent");
171 CollectorStub.jettyServer.stop();
172 System.out.println("Shutting down collector");
173 Thread.sleep(2000);
174 }
175
176 public static void extractRawLog(String dataRootFolder, String fileName,
177 String dataSinkDirectory) throws Exception {
178
179 DataOperations
180 .extractRawLogFromDump(dataRootFolder + "/adaptor/", fileName);
181
182 DataOperations.extractRawLogFromDump(dataRootFolder + "/sender/", fileName);
183
184
185 File dir = new File(dataRootFolder + "/collector/");
186 dir.mkdirs();
187
188 File dataSinkDir = new File(dataSinkDirectory);
189 String[] doneFiles = dataSinkDir.list();
190
191 for (String f : doneFiles) {
192 String outputFile = null;
193 if (f.endsWith(".done")) {
194 outputFile = fileName + ".done";
195 } else {
196 outputFile = fileName + ".crc";
197 }
198 System.out.println("Moving that file [" + dataSinkDirectory
199 + File.separator + f + "] to [" + dataRootFolder + "/collector/"
200 + outputFile + "]");
201 DataOperations.copyFile(dataSinkDirectory + File.separator + f,
202 dataRootFolder + "/collector/" + outputFile);
203 }
204
205 DataOperations.extractRawLogFromdataSink(ChunkDumper.testRepositoryDumpDir
206 + "/collector/", fileName);
207 }
208
209 public static boolean validateRawLogs(String dataRootFolder, String fileName) {
210 boolean result = true;
211
212 boolean adaptorMD5 = DataOperations.validateMD5(dataRootFolder
213 + "/../input/" + fileName, dataRootFolder + "/adaptor/" + fileName
214 + ".raw");
215 if (!adaptorMD5) {
216 System.out.println("Adaptor validation failed");
217 result = false;
218 }
219
220 boolean senderMD5 = DataOperations.validateMD5(dataRootFolder
221 + "/../input/" + fileName, dataRootFolder + "/sender/" + fileName
222 + ".raw");
223 if (!senderMD5) {
224 System.out.println("Sender validation failed");
225 result = false;
226 }
227
228 boolean collectorMD5 = DataOperations.validateMD5(dataRootFolder
229 + "/../input/" + fileName, dataRootFolder + "/collector/" + fileName
230 + ".raw");
231 if (!collectorMD5) {
232 System.out.println("collector validation failed");
233 result = false;
234 }
235
236 return result;
237 }
238
239 public static boolean validateOutputs(String dataRootFolder, String fileName) {
240 boolean result = true;
241
242 boolean adaptorMD5 = DataOperations.validateMD5(dataRootFolder
243 + "/../gold/adaptor/" + fileName + ".bin", dataRootFolder + "/adaptor/"
244 + fileName + ".bin");
245 if (!adaptorMD5) {
246 System.out.println("Adaptor bin validation failed");
247 result = false;
248 }
249
250 boolean senderMD5 = DataOperations.validateMD5(dataRootFolder
251 + "/../gold/sender/" + fileName + ".bin", dataRootFolder + "/sender/"
252 + fileName + ".bin");
253 if (!senderMD5) {
254 System.out.println("Sender bin validation failed");
255 result = false;
256 }
257
258
259
260
261
262
263
264
265
266
267 return result;
268 }
269 }