Home | History | Annotate | Download | only in metric
      1 /*
      2  * Copyright (C) 2018 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.device.metric;
     17 
     18 import static org.junit.Assert.*;
     19 
     20 import com.android.tradefed.config.ConfigurationDef;
     21 import com.android.tradefed.config.OptionSetter;
     22 import com.android.tradefed.device.ITestDevice;
     23 import com.android.tradefed.invoker.IInvocationContext;
     24 import com.android.tradefed.invoker.InvocationContext;
     25 import com.android.tradefed.metrics.proto.MetricMeasurement.Metric;
     26 import com.android.tradefed.result.ITestInvocationListener;
     27 import com.android.tradefed.result.LogDataType;
     28 import com.android.tradefed.result.TestDescription;
     29 import com.android.tradefed.util.proto.TfMetricProtoUtil;
     30 
     31 import org.easymock.Capture;
     32 import org.easymock.EasyMock;
     33 import org.junit.Before;
     34 import org.junit.Test;
     35 import org.junit.runner.RunWith;
     36 import org.junit.runners.JUnit4;
     37 
     38 import java.io.File;
     39 import java.util.HashMap;
     40 import java.util.Map;
     41 
     42 /** Unit tests for {@link FilePullerLogCollector}. */
     43 @RunWith(JUnit4.class)
     44 public class FilePullerLogCollectorTest {
     45 
     46     private FilePullerLogCollector mCollector;
     47     private ITestInvocationListener mMockListener;
     48     private IInvocationContext mContext;
     49     private ITestDevice mMockDevice;
     50 
     51     @Before
     52     public void setUp() throws Exception {
     53         mMockListener = EasyMock.createMock(ITestInvocationListener.class);
     54         mMockDevice = EasyMock.createMock(ITestDevice.class);
     55         mContext = new InvocationContext();
     56         mContext.addAllocatedDevice(ConfigurationDef.DEFAULT_DEVICE_NAME, mMockDevice);
     57         mCollector = new FilePullerLogCollector();
     58         OptionSetter setter = new OptionSetter(mCollector);
     59         setter.setOptionValue("pull-pattern-keys", "log.*");
     60     }
     61 
     62     /**
     63      * Test that if the pattern of a metric match the requested pattern we attemp to pull it as a
     64      * log file.
     65      */
     66     @Test
     67     public void testPullAndLog() throws Exception {
     68         ITestInvocationListener listener = mCollector.init(mContext, mMockListener);
     69         TestDescription test = new TestDescription("class", "test");
     70         Map<String, String> metrics = new HashMap<>();
     71         metrics.put("log1", "/data/local/tmp/log1.txt");
     72         metrics.put("another_metrics", "57");
     73 
     74         Capture<HashMap<String, Metric>> capture = new Capture<>();
     75         mMockListener.testStarted(test, 0L);
     76         EasyMock.expect(mMockDevice.pullFile("/data/local/tmp/log1.txt"))
     77                 .andReturn(new File("file"));
     78         EasyMock.expect(mMockDevice.executeShellCommand("rm -f /data/local/tmp/log1.txt"))
     79                 .andReturn("");
     80         mMockListener.testLog(
     81                 EasyMock.eq("file"), EasyMock.eq(LogDataType.TEXT), EasyMock.anyObject());
     82         mMockListener.testEnded(EasyMock.eq(test), EasyMock.eq(50L), EasyMock.capture(capture));
     83 
     84         EasyMock.replay(mMockDevice, mMockListener);
     85         listener.testStarted(test, 0L);
     86         listener.testEnded(test, 50L, TfMetricProtoUtil.upgradeConvert(metrics));
     87         EasyMock.verify(mMockDevice, mMockListener);
     88         HashMap<String, Metric> metricCaptured = capture.getValue();
     89         assertEquals(
     90                 "57", metricCaptured.get("another_metrics").getMeasurements().getSingleString());
     91         assertEquals(
     92                 "/data/local/tmp/log1.txt",
     93                 metricCaptured.get("log1").getMeasurements().getSingleString());
     94     }
     95 
     96     /**
     97      * Test that if the pattern of a metric match the requested pattern but we don't collect on test
     98      * cases then nothing is done.
     99      */
    100     @Test
    101     public void testSkipTestCollection() throws Exception {
    102         OptionSetter setter = new OptionSetter(mCollector);
    103         setter.setOptionValue("collect-on-run-ended-only", "true");
    104         ITestInvocationListener listener = mCollector.init(mContext, mMockListener);
    105         TestDescription test = new TestDescription("class", "test");
    106         Map<String, String> metrics = new HashMap<>();
    107         metrics.put("log1", "/data/local/tmp/log1.txt");
    108         metrics.put("another_metrics", "57");
    109 
    110         Capture<HashMap<String, Metric>> capture = new Capture<>();
    111         mMockListener.testStarted(test, 0L);
    112         mMockListener.testEnded(EasyMock.eq(test), EasyMock.eq(50L), EasyMock.capture(capture));
    113 
    114         EasyMock.replay(mMockDevice, mMockListener);
    115         listener.testStarted(test, 0L);
    116         listener.testEnded(test, 50L, TfMetricProtoUtil.upgradeConvert(metrics));
    117         EasyMock.verify(mMockDevice, mMockListener);
    118         HashMap<String, Metric> metricCaptured = capture.getValue();
    119         assertEquals(
    120                 "57", metricCaptured.get("another_metrics").getMeasurements().getSingleString());
    121         assertEquals(
    122                 "/data/local/tmp/log1.txt",
    123                 metricCaptured.get("log1").getMeasurements().getSingleString());
    124     }
    125 }
    126