summaryrefslogtreecommitdiff
path: root/src/com/google/gct/testing/CloudResultsLoader.java
blob: 95669cdf13a5ed45024417ce940bc90be2b684b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.gct.testing;

import com.google.api.client.http.HttpHeaders;
import com.google.api.services.storage.Storage;
import com.google.api.services.storage.model.StorageObject;
import com.google.api.services.testing.model.AndroidDevice;
import com.google.api.services.testing.model.ResultStorage;
import com.google.api.services.testing.model.TestExecution;
import com.google.api.services.testing.model.TestMatrix;
import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.base.Optional;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
import com.google.gct.testing.results.IGoogleCloudTestRunListener;
import com.intellij.execution.process.ProcessHandler;
import com.intellij.execution.process.ProcessOutputTypes;
import org.jetbrains.annotations.NotNull;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.*;

import static com.google.gct.testing.BucketFileMetadata.Type.*;
import static com.google.gct.testing.launcher.CloudAuthenticator.getStorage;
import static com.google.gct.testing.launcher.CloudAuthenticator.getTest;

public class CloudResultsLoader {
  public static final String INFRASTRUCTURE_FAILURE_PREFIX = "Infrastructure Failure:";

  private static final long MAX_SCREENSHOT_DOWNLOAD_SIZE = 512 * 1024 * 1024; // 512 MB

  private static final Function<StorageObject, BucketFileMetadata> TO_BUCKET_FILE = new Function<StorageObject, BucketFileMetadata>() {
    @Override
    public BucketFileMetadata apply(StorageObject input) {
      return new BucketFileMetadata(input.getName());
    }
  };

  private static final Function<BucketFileMetadata, String> TO_COMPLETED_CONFIGURATION_OR_NULL = new Function<BucketFileMetadata, String>() {
    @Override
    public String apply(BucketFileMetadata input) {
      if (input.getType() == DONE) {
        return input.getEncodedConfigurationInstance();
      }
      return null;
    }
  };

  // Used to track whether any new data (except for DONE file) was received from the backend,
  // e.g., new progress status, results file, or screenshot.
  private boolean newDataReceived = false;

  // Is used to support fake buckets only (does not handle cumulative progress).
  private final Function<BucketFileMetadata, BucketFileMetadata> UPDATE_CONFIGURATION_PROGRESS =
    new Function<BucketFileMetadata, BucketFileMetadata>() {
    @Override
    public BucketFileMetadata apply(BucketFileMetadata file) {
      if (file.getType() == PROGRESS) {
        Optional<byte[]> optionalBytes = getFileBytes(bucketName, file);
        if (optionalBytes.isPresent()) {
          String progressLine = new String(optionalBytes.get());
          String encodedConfigurationInstance = file.getEncodedConfigurationInstance();
          String previousProgressLine = getPreviousProgress(encodedConfigurationInstance);
          if (!progressLine.equals(previousProgressLine)) {
            newDataReceived = true;
            configurationProgress.put(encodedConfigurationInstance, progressLine);
            testRunListener.testConfigurationProgress(
              ConfigurationInstance.parseFromEncodedString(encodedConfigurationInstance).getResultsViewerDisplayString(), progressLine);
          }
        }
      }
      return file;
    }
  };

  private final String cloudProjectId;
  private final IGoogleCloudTestRunListener testRunListener;
  private final ProcessHandler processHandler;
  private final String bucketName;
  private final String testMatrixId;
  private final Set<String> allConfigurationInstances = new HashSet<String>();
  private final Set<String> finishedConfigurationInstances = new HashSet<String>();
  private long loadedScreenshotSize = 0;
  private int consecutivePollFailuresCount = 0;
  private boolean webLinkReported = false;

  // Encoded configuration instance -> progress accumulated so far.
  private final Map<String, String> configurationProgress = new HashMap<String, String>();


  public CloudResultsLoader(String cloudProjectId, IGoogleCloudTestRunListener testRunListener, ProcessHandler processHandler,
                            String bucketName, TestMatrix testMatrix) {
    this.cloudProjectId = cloudProjectId;
    this.testRunListener = testRunListener;
    this.processHandler = processHandler;
    this.bucketName = bucketName;
    // testMatrix is null for runs with a fake bucket.
    if (testMatrix != null) {
      testMatrixId = testMatrix.getTestMatrixId();
      for (TestExecution testExecution : testMatrix.getTestExecutions()) {
        allConfigurationInstances.add(getEncodedConfigurationNameForTestExecution(testExecution));
      }
    } else {
      testMatrixId = null;
    }
  }

  //TODO: Check file size after loading it and load the missing parts, if any (i.e., keep loading until the file's size does not change).
  public static Optional<byte[]> getFileBytes(String bucketName, BucketFileMetadata fileMetadata) {
    int chunkSize = 2 * 1000 * 1000; //A bit less than 2MB.
    int currentStart = 0;
    byte [] bytes = null;
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
      do {
        //Retrieve the Get object in each iteration to avoid exceptions while updating request headers with a different range.
        Storage.Objects.Get getObject = getStorage().objects().get(bucketName, fileMetadata.getPath());
        getObject.getMediaHttpDownloader().setDirectDownloadEnabled(true);
        getObject.setRequestHeaders(new HttpHeaders().setRange(String.format("bytes=%d-%d", currentStart, currentStart + chunkSize - 1)));
        getObject.executeMediaAndDownloadTo(out);
        currentStart = currentStart + chunkSize;
      } while (out.size() == currentStart); //Repeat as long as all the requested bytes are loaded.
      bytes = out.toByteArray();
    } catch (Exception e) {
      System.err.println("Failed to load a cloud file: " + fileMetadata.getName());
    } finally {
      if (out != null) {
        try {
          out.close();
        } catch (IOException e) {
          //ignore;
        }
      }
    }
    return Optional.fromNullable(bytes);
  }

  /**
   *
   * @return true if some new data was received from the backend (except for DONE file).
   */
  public boolean updateResults(Map<String, ConfigurationResult> results) {
    newDataReceived = false;
    try {
      if (testMatrixId == null) { // The obsolete logic kept for handling fake buckets.
        Storage.Objects.List objects = getStorage().objects().list(bucketName);
        List<StorageObject> storageObjects = objects.execute().getItems();

        Iterable<BucketFileMetadata> files =
          Iterables.transform(storageObjects, Functions.compose(UPDATE_CONFIGURATION_PROGRESS, TO_BUCKET_FILE));

        Set<String> finishedConfigurations =
          Sets.newHashSet(Iterables.filter(Iterables.transform(files, TO_COMPLETED_CONFIGURATION_OR_NULL), Predicates.notNull()));

        for (BucketFileMetadata file : files) {
          if (file.getType() == PROGRESS) {
            String encodedConfigurationInstance = file.getEncodedConfigurationInstance();
            ConfigurationResult result = getOrCreateConfigurationResult(encodedConfigurationInstance, results);
            result.setComplete(finishedConfigurations.contains(encodedConfigurationInstance));
            result.setInfrastructureFailure(isInfrastructureFailure(getPreviousProgress(encodedConfigurationInstance)));
          }
        }
      } else {
        updateResultsFromApi(results);
      }
      loadResultFiles(results);
      loadScreenshots(results);
    } catch (Exception e) {
      throw new RuntimeException("Failed updating the results from the bucket!", e);
    }
    return newDataReceived;
  }

  private void updateResultsFromApi(Map<String, ConfigurationResult> results) {
    TestMatrix testMatrix = null;
    try {
       testMatrix = getTest().projects().testMatrices().get(cloudProjectId, testMatrixId).execute();
    } catch (Exception e) {
      if (consecutivePollFailuresCount == 2) { // Give up on the 3rd failure in a row.
        for (String configurationInstance : allConfigurationInstances) {
          if (!finishedConfigurationInstances.contains(configurationInstance)) {
            ConfigurationResult result = getOrCreateConfigurationResult(configurationInstance, results);
            result.setInfrastructureFailure(true);
            finishedConfigurationInstances.add(configurationInstance);
          }
        }
        CloudTestingUtils
          .showErrorMessage(null, "Error retrieving matrix test results", "Failed to retrieve results of a cloud test matrix!\n" +
                                                                          "Exception while updating results for test matrix " +
                                                                          testMatrixId +
                                                                          "\n\n" +
                                                                          e.getMessage());
      } else {
        consecutivePollFailuresCount++;
      }
    }
    if (testMatrix != null) {
      updateResultsFromTestMatrix(results, testMatrix);
      consecutivePollFailuresCount = 0; // Reset the consecutive poll failures on every success.
    }
  }

  private void updateResultsFromTestMatrix(Map<String, ConfigurationResult> results, @NotNull TestMatrix testMatrix) {
    String testMatrixState = testMatrix.getState();
    if (testMatrixState.equals("VALIDATING")) {
      return;
    }
    if (!webLinkReported) {
      webLinkReported = true;
      if (!testMatrixState.equals("INVALID")) {
        processHandler.notifyTextAvailable("You can also view test results, along with other runs against this app, on the web:\n" +
                                           getTuxLink(cloudProjectId, testMatrix.getResultStorage()) + " \n\n\n",
                                           ProcessOutputTypes.STDOUT);
      }
    }
    for (TestExecution testExecution : testMatrix.getTestExecutions()) {
      updateResultsFromTestExecution(results, testExecution);
    }
  }

  private void updateResultsFromTestExecution(Map<String, ConfigurationResult> results, TestExecution testExecution) {
    String encodedConfigurationInstance = getEncodedConfigurationNameForTestExecution(testExecution);
    if (finishedConfigurationInstances.contains(encodedConfigurationInstance)) {
      return;
    }
    String testExecutionState = testExecution.getState();
    if (testExecutionState.equals("UNSUPPORTED_ENVIRONMENT")) {
      handleTriggeringError(results, encodedConfigurationInstance,
                            "Skipped triggering the test execution: Incompatible API level for requested model\n");
    } else if (testExecutionState.equals("INCOMPATIBLE_ENVIRONMENT")) {
      // It is not expected to happen for Android Studio client.
      handleTriggeringError(results, encodedConfigurationInstance, "The given APK is not compatible with this configuration\n");
    } else if (testExecutionState.equals("INVALID")) {
      // It is not expected to happen for Android Studio client.
      handleTriggeringError(results, encodedConfigurationInstance, "The provided APK is invalid\n");
    } else if (!testExecutionState.equals("QUEUED")) {
      if (testExecutionState.equals("ERROR")) {
        String diffProgress = INFRASTRUCTURE_FAILURE_PREFIX + " " + testExecution.getTestDetails().getErrorDetails() + "\n";
        String previousProgress = getPreviousProgress(encodedConfigurationInstance);
        if (!previousProgress.endsWith(diffProgress)) {
          reportNewProgress(encodedConfigurationInstance, previousProgress, previousProgress + diffProgress);
        }
      } else if (testExecutionState.equals("IN_PROGRESS")) {
        String newProgress = testExecution.getTestDetails().getProgressDetails();
        String previousProgress = getPreviousProgress(encodedConfigurationInstance);
        if (newProgress != null && !newProgress.equals(previousProgress)) {
          reportNewProgress(encodedConfigurationInstance, previousProgress, newProgress);
        }
      }
      ConfigurationResult result = getOrCreateConfigurationResult(encodedConfigurationInstance, results);
      result.setComplete(testExecutionState.equals("FINISHED"));
      result.setInfrastructureFailure(isInfrastructureFailure(getPreviousProgress(encodedConfigurationInstance)));
      if (result.isNoProgressExpected()) {
        finishedConfigurationInstances.add(encodedConfigurationInstance);
      }
    }
  }

  private void handleTriggeringError(Map<String, ConfigurationResult> results, String encodedConfigurationInstance, String diffProgress) {
    // Probably, no previous progress could be expected in this scenario, but it would not hurt considering it anyway.
    String previousProgress = getPreviousProgress(encodedConfigurationInstance);
    reportNewProgress(encodedConfigurationInstance, previousProgress, previousProgress + diffProgress);
    ConfigurationResult result = getOrCreateConfigurationResult(encodedConfigurationInstance, results);
    result.setTriggeringError(true);
    finishedConfigurationInstances.add(encodedConfigurationInstance);
  }

  private static String getTuxLink(String cloudProjectId, ResultStorage resultStorage) {
    return "https://console.developers.google.com/project/" + cloudProjectId
           + "/clouddev/toolresults/histories/" + resultStorage.getToolResultsHistoryId()
           + "/executions/" + resultStorage.getToolResultsExecutionId();
  }

  private String getEncodedConfigurationNameForTestExecution(TestExecution testExecution) {
    AndroidDevice androidDevice = testExecution.getEnvironment().getAndroidDevice();
    return androidDevice.getAndroidModelId() + ConfigurationInstance.ENCODED_NAME_DELIMITER
           + androidDevice.getAndroidVersionId() + ConfigurationInstance.ENCODED_NAME_DELIMITER
           + androidDevice.getLocale() + ConfigurationInstance.ENCODED_NAME_DELIMITER
           + androidDevice.getOrientation();
  }

  private boolean failedToTriggerTest(String testExecutionId) {
    return testExecutionId.startsWith("Error ") || testExecutionId.startsWith("Skipped ");
  }

  private void reportNewProgress(String encodedConfigurationInstance, String previousProgress, String newProgress) {
    newDataReceived = true;
    configurationProgress.put(encodedConfigurationInstance, newProgress);
    String diffProgress = newProgress.substring(previousProgress.length());
    testRunListener.testConfigurationProgress(
      ConfigurationInstance.parseFromEncodedString(encodedConfigurationInstance).getResultsViewerDisplayString(), diffProgress);
  }

  private ConfigurationResult getOrCreateConfigurationResult(String encodedConfigurationInstance,
                                                             Map<String, ConfigurationResult> results) {

    ConfigurationResult result = results.get(encodedConfigurationInstance);
    if (result == null) {
      result = new ConfigurationResult(encodedConfigurationInstance, bucketName);
      results.put(encodedConfigurationInstance, result);
    }
    return result;
  }

  private static boolean isInfrastructureFailure(String progress) {
    String[] progressLines = progress.split("\n");
    return progressLines[progressLines.length - 1].startsWith(INFRASTRUCTURE_FAILURE_PREFIX);
  }

  private String getPreviousProgress(String encodedConfigurationInstance) {
    String progress = configurationProgress.get(encodedConfigurationInstance);
    return progress == null ? "" : progress;
  }

  private void loadResultFiles(Map<String, ConfigurationResult> results) {
    try {
      Storage.Objects.List objects = getStorage().objects().list(bucketName);
      List<StorageObject> storageObjects = objects.execute().getItems();

      Iterable<BucketFileMetadata> files = Iterables.transform(storageObjects, TO_BUCKET_FILE);

      for (BucketFileMetadata file : files) {
        if (file.getType() == RESULT) {
          String encodedConfigurationInstance = file.getEncodedConfigurationInstance();
          ConfigurationResult configurationResult = results.get(encodedConfigurationInstance);
          if (configurationResult != null && !configurationResult.hasResult()) {
            Optional<String> optionalResult = toOptionalString(getFileBytes(bucketName, file));
            if (optionalResult.isPresent() && file.hasEncodedConfigurationInstance()) {
              newDataReceived = true;
              configurationResult.setResult(optionalResult.get());
            }
          }
        }
      }
    } catch (Exception e) {
      System.out.println(e.getMessage());
    }
  }

  public void loadScreenshots(Map<String, ConfigurationResult> results) {
    if (loadedScreenshotSize > MAX_SCREENSHOT_DOWNLOAD_SIZE) {
      return;
    }
    List<StorageObject> storageObjects = null;
    try {
      Storage.Objects.List objects = getStorage().objects().list(bucketName);
      storageObjects = objects.execute().getItems();
    }
    catch (IOException e) {
      throw new RuntimeException("Failed to retrieve bucket objects: ", e);
    }

    if (storageObjects == null) {
      return;
    }

    Iterable<BucketFileMetadata> files = Iterables.transform(storageObjects, TO_BUCKET_FILE);
    //ArrayList<ScreenshotDownloadThread> downloadThreads = new ArrayList<ScreenshotDownloadThread>();
    for (BucketFileMetadata file : files) {
      if (file.getType() == SCREENSHOT && !isIgnoredScreenshot(file)) {
        ConfigurationResult result = results.get(file.getEncodedConfigurationInstance());
        if (result != null && result.getScreenshotMetadata().get(file.getName()) == null) {
          result.addScreenshotMetadata(file.getName(), file);
          newDataReceived = true;
        }
      }
    }

    // TODO: Replace with a pool of worker threads.
    //final int capParallelThreads = 10;
    //int currentParallelThreads = 0;
    //for (int i = 0; i < downloadThreads.size(); i++) {
    //  if (loadedScreenshotSize > MAX_SCREENSHOT_DOWNLOAD_SIZE) {
    //    joinCurrentParallelThreads(downloadThreads, currentParallelThreads, i);
    //    return;
    //  }
    //  ScreenshotDownloadThread newDownloadThread = downloadThreads.get(i);
    //  loadedScreenshotSize += newDownloadThread.getFileSize();
    //  newDownloadThread.start();
    //  currentParallelThreads++;
    //  if (currentParallelThreads == capParallelThreads) {
    //    // Join the existing threads before proceeding to avoid going over the limit.
    //    joinCurrentParallelThreads(downloadThreads, currentParallelThreads, i);
    //    currentParallelThreads = 0;
    //  }
    //}
    //
    //// Join any remaining threads.
    //for (int i = downloadThreads.size() - currentParallelThreads; i < downloadThreads.size(); i++){
    //  try {
    //    downloadThreads.get(i).join();
    //  }
    //  catch (InterruptedException e) {
    //    //ignore
    //  }
    //}
  }

  private boolean isIgnoredScreenshot(BucketFileMetadata file) {
    return file.getPath().contains("/flipbook/") // Ignore video screenshots (they are stored in flipbook subfolder).
           || file.getName().startsWith("TestRunner-prepareVirtualDevice-beforeunlock-") // Ignore screenshot that we take before unlocking.
           || file.getName().startsWith("TestRunner-prepareVirtualDevice-afterunlock-"); // Ignore screenshot that we take after unlocking.
  }

  //private void joinCurrentParallelThreads(ArrayList<ScreenshotDownloadThread> downloadThreads, int currentParallelThreads,
  //                                        int lastStartedThreadIndex) {
  //  for (int j = lastStartedThreadIndex - currentParallelThreads + 1; j <= lastStartedThreadIndex; j++){
  //    try {
  //      downloadThreads.get(j).join();
  //    }
  //    catch (InterruptedException e) {
  //      //ignore
  //    }
  //  }
  //}

  //private class ScreenshotDownloadThread extends Thread {
  //
  //  private final BucketFileMetadata file;
  //  private final ConfigurationResult result;
  //
  //  private ScreenshotDownloadThread(BucketFileMetadata file, ConfigurationResult result) {
  //    this.file = file;
  //    this.result = result;
  //  }
  //
  //  public long getFileSize() {
  //    try {
  //      return getStorage().objects().get(bucketName, file.getPath()).executeCloudMatrixTests().getSize().longValue();
  //    }
  //    catch (IOException e) {
  //      System.err.println("Failed to estimate a cloud file size: " + file.getName());
  //      return 0;
  //    }
  //  }
  //
  //  @Override
  //  public void run() {
  //    Optional<byte[]> optionalFileBytes = getFileBytes(file);
  //    if (optionalFileBytes.isPresent()) {
  //      BufferedImage image = null;
  //      try {
  //        image = ImageIO.read(new ByteArrayInputStream(optionalFileBytes.get()));
  //      }
  //      catch (IOException e) {
  //        System.out.println("Failed to create an image for screenshot: " + e.getMessage());
  //        return;
  //      }
  //      image.flush();
  //      result.addScreenshotMetadata(file.getName(), image);
  //      // Mark that the new data was received as the last statement to ensure that no failures can follow after that
  //      // (to avoid infinite failure mode).
  //      newDataReceived = true;
  //    }
  //  }
  //}

  private Optional<String> toOptionalString(Optional<byte[]> optionalBytes) {
    return optionalBytes.isPresent()
           ? Optional.of(new String(optionalBytes.get()))
           : Optional.<String>absent();
  }
}