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