Home | History | Annotate | Download | only in continuous
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.continuous;
     18 
     19 import com.android.ddmlib.testrunner.TestIdentifier;
     20 import com.android.tradefed.build.BuildInfo;
     21 import com.android.tradefed.build.IBuildInfo;
     22 import com.android.tradefed.invoker.IInvocationContext;
     23 import com.android.tradefed.invoker.InvocationContext;
     24 import com.android.tradefed.log.LogUtil.CLog;
     25 import com.android.tradefed.util.IEmail;
     26 import com.android.tradefed.util.IEmail.Message;
     27 
     28 import junit.framework.TestCase;
     29 
     30 import org.easymock.Capture;
     31 import org.easymock.EasyMock;
     32 
     33 import java.util.Collections;
     34 import java.util.Map;
     35 
     36 /** Unit tests for {@link SmokeTestFailureReporter} */
     37 public class SmokeTestFailureReporterTest extends TestCase {
     38     private SmokeTestFailureReporter mReporter = null;
     39     private IEmail mMailer = null;
     40 
     41     private static final String TAG = "DeviceSmokeTests";
     42     private static final String BID = "123456";
     43     private static final String TARGET = "target?";
     44     private static final String FLAVOR = "generic-userdebug";
     45     private static final String BRANCH = "git_master";
     46 
     47     @Override
     48     public void setUp() {
     49         mMailer = EasyMock.createMock(IEmail.class);
     50         mReporter = new SmokeTestFailureReporter(mMailer);
     51     }
     52 
     53     public void testSingleFail() throws Exception {
     54         final String expSubject =
     55                 "DeviceSmokeTests SmokeFAST failed on: BuildInfo{bid=123456, "
     56                         + "target=target?, build_flavor=generic-userdebug, branch=git_master}";
     57         final String expBodyStart = "FooTest#testFoo failed\nStack trace:\nthis is a trace\n";
     58 
     59         final Map<String, String> emptyMap = Collections.emptyMap();
     60         final TestIdentifier testId = new TestIdentifier("FooTest", "testFoo");
     61         final String trace = "this is a trace";
     62 
     63         final Capture<Message> msgCapture = new Capture<Message>();
     64         mMailer.send(EasyMock.capture(msgCapture));
     65         EasyMock.replay(mMailer);
     66 
     67         final IBuildInfo build = new BuildInfo(BID, TARGET);
     68         build.setBuildFlavor(FLAVOR);
     69         build.setBuildBranch(BRANCH);
     70         IInvocationContext context = new InvocationContext();
     71         context.addDeviceBuildInfo("serial", build);
     72         context.setTestTag(TAG);
     73 
     74         mReporter.addDestination("dest.ination (at) email.com");
     75         mReporter.invocationStarted(context);
     76         mReporter.testRunStarted("testrun", 1);
     77         mReporter.testStarted(testId);
     78         mReporter.testFailed(testId, trace);
     79         mReporter.testEnded(testId, emptyMap);
     80         mReporter.testRunEnded(2, emptyMap);
     81         mReporter.invocationEnded(1);
     82 
     83         EasyMock.verify(mMailer);
     84 
     85         assertTrue(msgCapture.hasCaptured());
     86         final Message msg = msgCapture.getValue();
     87         final String subj = msg.getSubject();
     88         final String body = msg.getBody();
     89         CLog.i("subject: %s", subj);
     90         CLog.i("body:\n%s", body);
     91         assertEquals(expSubject, subj);
     92         assertTrue(String.format(
     93                 "Expected body to start with \"\"\"%s\"\"\".  Body was actually: %s\n",
     94                 expBodyStart, body), body.startsWith(expBodyStart));
     95 
     96     }
     97 
     98     public void testTwoPassOneFail() throws Exception {
     99         final String expSubject =
    100                 "DeviceSmokeTests SmokeFAST failed on: BuildInfo{bid=123456, "
    101                         + "target=target?, build_flavor=generic-userdebug, branch=git_master}";
    102         final String expBodyStart = "FooTest#testFail failed\nStack trace:\nthis is a trace\n";
    103 
    104         final Map<String, String> emptyMap = Collections.emptyMap();
    105         final String trace = "this is a trace";
    106         final TestIdentifier testFail = new TestIdentifier("FooTest", "testFail");
    107         final TestIdentifier testPass1 = new TestIdentifier("FooTest", "testPass1");
    108         final TestIdentifier testPass2 = new TestIdentifier("FooTest", "testPass2");
    109 
    110         final Capture<Message> msgCapture = new Capture<Message>();
    111         mMailer.send(EasyMock.capture(msgCapture));
    112         EasyMock.replay(mMailer);
    113 
    114         IBuildInfo build = new BuildInfo(BID, TARGET);
    115         build.setBuildFlavor(FLAVOR);
    116         build.setBuildBranch(BRANCH);
    117         IInvocationContext context = new InvocationContext();
    118         context.addDeviceBuildInfo("serial", build);
    119         context.setTestTag(TAG);
    120 
    121         mReporter.addDestination("dest.ination (at) email.com");
    122         mReporter.invocationStarted(context);
    123         mReporter.testRunStarted("testrun", 1);
    124         mReporter.testStarted(testPass1);
    125         mReporter.testEnded(testPass1, emptyMap);
    126 
    127         mReporter.testStarted(testFail);
    128         mReporter.testFailed(testFail, trace);
    129         mReporter.testEnded(testFail, emptyMap);
    130 
    131         mReporter.testStarted(testPass2);
    132         mReporter.testEnded(testPass2, emptyMap);
    133         mReporter.testRunEnded(2, emptyMap);
    134         mReporter.invocationEnded(1);
    135 
    136         EasyMock.verify(mMailer);
    137 
    138         assertTrue(msgCapture.hasCaptured());
    139         final Message msg = msgCapture.getValue();
    140         final String subj = msg.getSubject();
    141         final String body = msg.getBody();
    142         CLog.i("subject: %s", subj);
    143         CLog.i("body:\n%s", body);
    144         assertEquals(expSubject, subj);
    145         assertTrue(String.format(
    146                 "Expected body to start with \"\"\"%s\"\"\".  Body was actually: %s\n",
    147                 expBodyStart, body), body.startsWith(expBodyStart));
    148     }
    149 }
    150