Home | History | Annotate | Download | only in testtype
      1 /*
      2  * Copyright (C) 2017 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 package com.android.tradefed.testtype;
     17 
     18 import static org.junit.Assert.assertEquals;
     19 import static org.junit.Assert.assertTrue;
     20 import static org.junit.Assert.fail;
     21 
     22 import com.android.tradefed.config.OptionSetter;
     23 import com.android.tradefed.metrics.proto.MetricMeasurement.Metric;
     24 import com.android.tradefed.result.ITestInvocationListener;
     25 import com.android.tradefed.util.FileUtil;
     26 
     27 import org.easymock.EasyMock;
     28 import org.junit.After;
     29 import org.junit.Before;
     30 import org.junit.Test;
     31 import org.junit.runner.RunWith;
     32 import org.junit.runners.JUnit4;
     33 
     34 import java.io.File;
     35 import java.io.IOException;
     36 import java.util.HashMap;
     37 
     38 /**
     39  * Unit test for {@link NoisyDryRunTest}.
     40  */
     41 @RunWith(JUnit4.class)
     42 public class NoisyDryRunTestTest {
     43 
     44     private File mFile;
     45     private ITestInvocationListener mMockListener;
     46 
     47     @Before
     48     public void setUp() throws Exception {
     49         mFile = FileUtil.createTempFile("NoisyDryRunTestTest", "tmpFile");
     50         mMockListener = EasyMock.createMock(ITestInvocationListener.class);
     51     }
     52 
     53     @After
     54     public void tearDown() {
     55         FileUtil.deleteFile(mFile);
     56     }
     57 
     58     @Test
     59     public void testRun() throws Exception {
     60         FileUtil.writeToFile("tf/fake\n"
     61                 + "tf/fake", mFile);
     62         mMockListener.testRunStarted("com.android.tradefed.testtype.NoisyDryRunTest_parseFile", 1);
     63         mMockListener.testStarted(EasyMock.anyObject());
     64         mMockListener.testEnded(
     65                 EasyMock.anyObject(), (HashMap<String, Metric>) EasyMock.anyObject());
     66         mMockListener.testRunEnded(EasyMock.eq(0l), (HashMap<String, Metric>) EasyMock.anyObject());
     67 
     68         mMockListener.testRunStarted("com.android.tradefed.testtype.NoisyDryRunTest_parseCommands",
     69                 2);
     70         mMockListener.testStarted(EasyMock.anyObject());
     71         mMockListener.testEnded(
     72                 EasyMock.anyObject(), (HashMap<String, Metric>) EasyMock.anyObject());
     73         mMockListener.testStarted(EasyMock.anyObject());
     74         mMockListener.testEnded(
     75                 EasyMock.anyObject(), (HashMap<String, Metric>) EasyMock.anyObject());
     76         mMockListener.testRunEnded(EasyMock.eq(0l), (HashMap<String, Metric>) EasyMock.anyObject());
     77         replayMocks();
     78 
     79         NoisyDryRunTest noisyDryRunTest = new NoisyDryRunTest();
     80         OptionSetter setter = new OptionSetter(noisyDryRunTest);
     81         setter.setOptionValue("cmdfile", mFile.getAbsolutePath());
     82         noisyDryRunTest.run(mMockListener);
     83         verifyMocks();
     84     }
     85 
     86     /**
     87      * Test loading a configuration with a USE_KEYSTORE option specified. It should still load and
     88      * we fake the keystore to simply validate the overall structure.
     89      */
     90     @Test
     91     public void testRun_withKeystore() throws Exception {
     92         FileUtil.writeToFile("tf/fake --fail-invocation-with-cause USE_KEYSTORE@fake\n", mFile);
     93         mMockListener.testRunStarted("com.android.tradefed.testtype.NoisyDryRunTest_parseFile", 1);
     94         mMockListener.testStarted(EasyMock.anyObject());
     95         mMockListener.testEnded(
     96                 EasyMock.anyObject(), (HashMap<String, Metric>) EasyMock.anyObject());
     97         mMockListener.testRunEnded(EasyMock.eq(0l), (HashMap<String, Metric>) EasyMock.anyObject());
     98 
     99         mMockListener.testRunStarted(
    100                 "com.android.tradefed.testtype.NoisyDryRunTest_parseCommands", 1);
    101         mMockListener.testStarted(EasyMock.anyObject());
    102         mMockListener.testEnded(
    103                 EasyMock.anyObject(), (HashMap<String, Metric>) EasyMock.anyObject());
    104 
    105         mMockListener.testRunEnded(EasyMock.eq(0l), (HashMap<String, Metric>) EasyMock.anyObject());
    106         replayMocks();
    107 
    108         NoisyDryRunTest noisyDryRunTest = new NoisyDryRunTest();
    109         OptionSetter setter = new OptionSetter(noisyDryRunTest);
    110         setter.setOptionValue("cmdfile", mFile.getAbsolutePath());
    111         noisyDryRunTest.run(mMockListener);
    112         verifyMocks();
    113     }
    114 
    115     @Test
    116     public void testRun_invalidCmdFile() throws Exception {
    117         FileUtil.deleteFile(mFile);
    118         mMockListener.testRunStarted("com.android.tradefed.testtype.NoisyDryRunTest_parseFile", 1);
    119         mMockListener.testStarted(EasyMock.anyObject());
    120         mMockListener.testEnded(
    121                 EasyMock.anyObject(), (HashMap<String, Metric>) EasyMock.anyObject());
    122         mMockListener.testFailed(EasyMock.anyObject(), EasyMock.anyObject());
    123         mMockListener.testRunEnded(EasyMock.eq(0l), (HashMap<String, Metric>) EasyMock.anyObject());
    124         replayMocks();
    125 
    126         NoisyDryRunTest noisyDryRunTest = new NoisyDryRunTest();
    127         OptionSetter setter = new OptionSetter(noisyDryRunTest);
    128         setter.setOptionValue("cmdfile", mFile.getAbsolutePath());
    129         noisyDryRunTest.run(mMockListener);
    130         verifyMocks();
    131     }
    132 
    133     @Test
    134     public void testRun_invalidCmdLine() throws Exception {
    135         FileUtil.writeToFile("tf/fake\n"
    136                 + "invalid --option value2", mFile);
    137         mMockListener.testRunStarted("com.android.tradefed.testtype.NoisyDryRunTest_parseFile", 1);
    138         mMockListener.testStarted(EasyMock.anyObject());
    139         mMockListener.testEnded(
    140                 EasyMock.anyObject(), (HashMap<String, Metric>) EasyMock.anyObject());
    141         mMockListener.testRunEnded(EasyMock.eq(0l), (HashMap<String, Metric>) EasyMock.anyObject());
    142 
    143         mMockListener.testRunStarted("com.android.tradefed.testtype.NoisyDryRunTest_parseCommands",
    144                 2);
    145         mMockListener.testStarted(EasyMock.anyObject());
    146         mMockListener.testEnded(
    147                 EasyMock.anyObject(), (HashMap<String, Metric>) EasyMock.anyObject());
    148         mMockListener.testStarted(EasyMock.anyObject());
    149         mMockListener.testFailed(EasyMock.anyObject(), EasyMock.anyObject());
    150         mMockListener.testEnded(
    151                 EasyMock.anyObject(), (HashMap<String, Metric>) EasyMock.anyObject());
    152         mMockListener.testRunEnded(EasyMock.eq(0l), (HashMap<String, Metric>) EasyMock.anyObject());
    153         replayMocks();
    154 
    155         NoisyDryRunTest noisyDryRunTest = new NoisyDryRunTest();
    156         OptionSetter setter = new OptionSetter(noisyDryRunTest);
    157         setter.setOptionValue("cmdfile", mFile.getAbsolutePath());
    158         noisyDryRunTest.run(mMockListener);
    159         verifyMocks();
    160     }
    161 
    162     @Test
    163     public void testCheckFileWithTimeout() throws Exception {
    164         NoisyDryRunTest noisyDryRunTest = new NoisyDryRunTest() {
    165             long mCurrentTime = 0;
    166             @Override
    167             void sleep() {
    168             }
    169 
    170             @Override
    171             long currentTimeMillis() {
    172                 mCurrentTime += 5 * 1000;
    173                 return mCurrentTime;
    174             }
    175         };
    176         OptionSetter setter = new OptionSetter(noisyDryRunTest);
    177         setter.setOptionValue("timeout", "100");
    178         noisyDryRunTest.checkFileWithTimeout(mFile);
    179     }
    180 
    181     @Test
    182     public void testCheckFileWithTimeout_missingFile() throws Exception {
    183         NoisyDryRunTest noisyDryRunTest = new NoisyDryRunTest() {
    184             long mCurrentTime = 0;
    185 
    186             @Override
    187             void sleep() {
    188             }
    189 
    190             @Override
    191             long currentTimeMillis() {
    192                 mCurrentTime += 5 * 1000;
    193                 return mCurrentTime;
    194             }
    195         };
    196         OptionSetter setter = new OptionSetter(noisyDryRunTest);
    197         setter.setOptionValue("timeout", "100");
    198         File missingFile = new File("missing_file");
    199         try {
    200             noisyDryRunTest.checkFileWithTimeout(missingFile);
    201             fail("Should have thrown IOException");
    202         } catch (IOException e) {
    203             assertEquals(missingFile.getAbsoluteFile() + " doesn't exist.", e.getMessage());
    204             assertTrue(true);
    205         }
    206     }
    207 
    208     @Test
    209     public void testCheckFileWithTimeout_delayFile() throws Exception {
    210         FileUtil.deleteFile(mFile);
    211         NoisyDryRunTest noisyDryRunTest = new NoisyDryRunTest() {
    212             long mCurrentTime = 0;
    213 
    214             @Override
    215             void sleep() {
    216             }
    217 
    218             @Override
    219             long currentTimeMillis() {
    220                 mCurrentTime += 5 * 1000;
    221                 if (mCurrentTime > 10 * 1000) {
    222                     try {
    223                         mFile.createNewFile();
    224                     } catch (IOException e) {
    225                     }
    226                 }
    227                 return mCurrentTime;
    228             }
    229         };
    230         OptionSetter setter = new OptionSetter(noisyDryRunTest);
    231         setter.setOptionValue("timeout", "100000");
    232         noisyDryRunTest.checkFileWithTimeout(mFile);
    233     }
    234 
    235     private void replayMocks() {
    236         EasyMock.replay(mMockListener);
    237     }
    238 
    239     private void verifyMocks() {
    240         EasyMock.verify(mMockListener);
    241     }
    242 }
    243