1 /* 2 * Copyright (C) 2016 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.tradefed.targetprep; 18 19 import static org.easymock.EasyMock.anyLong; 20 import static org.easymock.EasyMock.anyObject; 21 import static org.easymock.EasyMock.createMock; 22 import static org.easymock.EasyMock.createNiceMock; 23 import static org.easymock.EasyMock.expect; 24 import static org.easymock.EasyMock.replay; 25 26 import com.android.tradefed.build.BuildInfo; 27 import com.android.tradefed.build.IBuildInfo; 28 import com.android.tradefed.device.ITestDevice; 29 import com.android.tradefed.util.CommandResult; 30 import com.android.tradefed.util.CommandStatus; 31 import com.android.tradefed.util.IRunUtil; 32 33 import junit.framework.TestCase; 34 35 import java.io.File; 36 37 public class PythonVirtualenvPreparerTest extends TestCase { 38 private PythonVirtualenvPreparer mPreparer; 39 private IRunUtil mMockRunUtil; 40 private ITestDevice mMockDevice; 41 42 @Override 43 public void setUp() throws Exception { 44 mMockRunUtil = createNiceMock(IRunUtil.class); 45 mMockDevice = createMock(ITestDevice.class); 46 expect(mMockDevice.getSerialNumber()).andStubReturn("SERIAL"); 47 mPreparer = new PythonVirtualenvPreparer(); 48 mPreparer.mRunUtil = mMockRunUtil; 49 } 50 51 public void testInstallDeps_reqFile_success() throws Exception { 52 mPreparer.setRequirementsFile(new File("foobar")); 53 expect(mMockRunUtil.runTimedCmd(anyLong(), 54 (String)anyObject(), (String)anyObject(), (String)anyObject(), (String)anyObject())) 55 .andReturn(new CommandResult(CommandStatus.SUCCESS)); 56 replay(mMockRunUtil); 57 IBuildInfo buildInfo = new BuildInfo(); 58 mPreparer.installDeps(buildInfo, mMockDevice); 59 assertTrue(buildInfo.getFile("PYTHONPATH") != null); 60 } 61 62 public void testInstallDeps_depModule_success() throws Exception { 63 mPreparer.addDepModule("blahblah"); 64 expect(mMockRunUtil.runTimedCmd(anyLong(), 65 (String)anyObject(), (String)anyObject(), (String)anyObject())).andReturn( 66 new CommandResult(CommandStatus.SUCCESS)); 67 replay(mMockRunUtil); 68 IBuildInfo buildInfo = new BuildInfo(); 69 mPreparer.installDeps(buildInfo, mMockDevice); 70 assertTrue(buildInfo.getFile("PYTHONPATH") != null); 71 } 72 73 public void testInstallDeps_reqFile_failure() throws Exception { 74 mPreparer.setRequirementsFile(new File("foobar")); 75 expect(mMockRunUtil.runTimedCmd(anyLong(), 76 (String)anyObject(), (String)anyObject(), (String)anyObject(), (String)anyObject())) 77 .andReturn(new CommandResult(CommandStatus.TIMED_OUT)); 78 replay(mMockRunUtil); 79 IBuildInfo buildInfo = new BuildInfo(); 80 try { 81 mPreparer.installDeps(buildInfo, mMockDevice); 82 fail("installDeps succeeded despite a failed command"); 83 } catch (TargetSetupError e) { 84 assertTrue(buildInfo.getFile("PYTHONPATH") == null); 85 } 86 } 87 88 public void testInstallDeps_depModule_failure() throws Exception { 89 mPreparer.addDepModule("blahblah"); 90 expect(mMockRunUtil.runTimedCmd(anyLong(), 91 (String)anyObject(), (String)anyObject(), (String)anyObject())).andReturn( 92 new CommandResult(CommandStatus.TIMED_OUT)); 93 replay(mMockRunUtil); 94 IBuildInfo buildInfo = new BuildInfo(); 95 try { 96 mPreparer.installDeps(buildInfo, mMockDevice); 97 fail("installDeps succeeded despite a failed command"); 98 } catch (TargetSetupError e) { 99 assertTrue(buildInfo.getFile("PYTHONPATH") == null); 100 } 101 } 102 103 public void testInstallDeps_noDeps() throws Exception { 104 BuildInfo buildInfo = new BuildInfo(); 105 mPreparer.installDeps(buildInfo, mMockDevice); 106 assertTrue(buildInfo.getFile("PYTHONPATH") == null); 107 } 108 }