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.config; 17 18 import static org.easymock.EasyMock.anyObject; 19 import static org.easymock.EasyMock.eq; 20 import static org.junit.Assert.assertEquals; 21 import static org.junit.Assert.assertNotNull; 22 import static org.junit.Assert.fail; 23 24 import com.android.tradefed.sandbox.ISandbox; 25 import com.android.tradefed.sandbox.SandboxConfigDump; 26 import com.android.tradefed.sandbox.SandboxConfigDump.DumpCmd; 27 import com.android.tradefed.sandbox.SandboxConfigurationException; 28 import com.android.tradefed.util.CommandResult; 29 import com.android.tradefed.util.CommandStatus; 30 import com.android.tradefed.util.FileUtil; 31 import com.android.tradefed.util.IRunUtil; 32 import com.android.tradefed.util.IRunUtil.EnvPriority; 33 import com.android.tradefed.util.keystore.StubKeyStoreClient; 34 35 import org.easymock.EasyMock; 36 import org.easymock.IAnswer; 37 import org.junit.After; 38 import org.junit.Before; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 import org.junit.runners.JUnit4; 42 43 import java.io.File; 44 import java.io.IOException; 45 46 /** Unit tests for {@link SandboxConfigurationFactory}. */ 47 @RunWith(JUnit4.class) 48 public class SandboxConfigurationFactoryTest { 49 50 private SandboxConfigurationFactory mFactory; 51 private File mConfig; 52 private ISandbox mFakeSandbox; 53 private IRunUtil mMockRunUtil; 54 55 @Before 56 public void setUp() throws IOException, ConfigurationException { 57 mFactory = SandboxConfigurationFactory.getInstance(); 58 mConfig = FileUtil.createTempFile("sandbox-config-test", ".xml"); 59 mFakeSandbox = EasyMock.createMock(ISandbox.class); 60 mMockRunUtil = EasyMock.createMock(IRunUtil.class); 61 try { 62 GlobalConfiguration.createGlobalConfiguration(new String[] {}); 63 } catch (IllegalStateException ignore) { 64 // ignore the global config re-init 65 } 66 } 67 68 @After 69 public void tearDown() { 70 FileUtil.deleteFile(mConfig); 71 } 72 73 private void expectDumpCmd(CommandResult res) { 74 EasyMock.expect( 75 mMockRunUtil.runTimedCmd( 76 EasyMock.anyLong(), 77 eq("java"), 78 eq("-cp"), 79 anyObject(), 80 eq(SandboxConfigDump.class.getCanonicalName()), 81 eq(DumpCmd.NON_VERSIONED_CONFIG.toString()), 82 EasyMock.anyObject(), 83 eq(mConfig.getAbsolutePath()))) 84 .andAnswer( 85 new IAnswer<CommandResult>() { 86 @Override 87 public CommandResult answer() throws Throwable { 88 String resFile = (String) EasyMock.getCurrentArguments()[6]; 89 FileUtil.writeToFile( 90 "<configuration><test class=\"com.android.tradefed.test" 91 + "type.StubTest\" /></configuration>", 92 new File(resFile)); 93 return res; 94 } 95 }); 96 } 97 98 /** 99 * Test that creating a configuration using a sandbox properly create a {@link IConfiguration}. 100 */ 101 @Test 102 public void testCreateConfigurationFromArgs() throws ConfigurationException { 103 String[] args = new String[] {mConfig.getAbsolutePath()}; 104 mMockRunUtil.unsetEnvVariable(GlobalConfiguration.GLOBAL_CONFIG_VARIABLE); 105 EasyMock.expectLastCall().times(2); 106 mMockRunUtil.setEnvVariable( 107 EasyMock.eq(GlobalConfiguration.GLOBAL_CONFIG_VARIABLE), EasyMock.anyObject()); 108 mMockRunUtil.setEnvVariablePriority(EnvPriority.SET); 109 CommandResult results = new CommandResult(); 110 results.setStatus(CommandStatus.SUCCESS); 111 expectDumpCmd(results); 112 EasyMock.replay(mFakeSandbox, mMockRunUtil); 113 IConfiguration config = 114 mFactory.createConfigurationFromArgs( 115 args, new StubKeyStoreClient(), mFakeSandbox, mMockRunUtil); 116 EasyMock.verify(mFakeSandbox, mMockRunUtil); 117 assertNotNull(config.getConfigurationObject(Configuration.SANDBOX_TYPE_NAME)); 118 assertEquals(mFakeSandbox, config.getConfigurationObject(Configuration.SANDBOX_TYPE_NAME)); 119 } 120 121 /** Test that when the dump config failed, we throw a SandboxConfigurationException. */ 122 @Test 123 public void testCreateConfigurationFromArgs_fail() throws Exception { 124 String[] args = new String[] {mConfig.getAbsolutePath()}; 125 mMockRunUtil.unsetEnvVariable(GlobalConfiguration.GLOBAL_CONFIG_VARIABLE); 126 EasyMock.expectLastCall().times(2); 127 mMockRunUtil.setEnvVariable( 128 EasyMock.eq(GlobalConfiguration.GLOBAL_CONFIG_VARIABLE), EasyMock.anyObject()); 129 mMockRunUtil.setEnvVariablePriority(EnvPriority.SET); 130 CommandResult results = new CommandResult(); 131 results.setStatus(CommandStatus.FAILED); 132 results.setStderr("I failed"); 133 expectDumpCmd(results); 134 // Thin launcher is attempted, and in this case fails, so original exception is thrown. 135 EasyMock.expect( 136 mFakeSandbox.createThinLauncherConfig( 137 EasyMock.anyObject(), EasyMock.anyObject(), 138 EasyMock.anyObject(), EasyMock.anyObject())) 139 .andReturn(null); 140 // in case of failure, tearDown is called right away for cleaning up 141 mFakeSandbox.tearDown(); 142 EasyMock.replay(mFakeSandbox, mMockRunUtil); 143 try { 144 mFactory.createConfigurationFromArgs( 145 args, new StubKeyStoreClient(), mFakeSandbox, mMockRunUtil); 146 fail("Should have thrown an exception."); 147 } catch (SandboxConfigurationException expected) { 148 // expected 149 } 150 EasyMock.verify(mFakeSandbox, mMockRunUtil); 151 } 152 } 153