1 /*
2  * Copyright (C) 2020 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.invoker.shard;
17 
18 import com.android.ddmlib.Log.LogLevel;
19 import com.android.tradefed.build.StubBuildProvider;
20 import com.android.tradefed.config.Configuration;
21 import com.android.tradefed.config.ConfigurationException;
22 import com.android.tradefed.config.IConfiguration;
23 import com.android.tradefed.config.IDeviceConfiguration;
24 import com.android.tradefed.log.LogUtil.CLog;
25 import com.android.tradefed.util.keystore.IKeyStoreClient;
26 
27 import java.util.Arrays;
28 import java.util.List;
29 
30 /** Replicate a setup for one device to all other devices that will be part of sharding. */
31 public class ParentShardReplicate {
32 
33     /**
34      * Clone the setup for one devices to as many devices as sharding will target.
35      *
36      * @param config The config that will carry the replicate.
37      * @param client The keystore client.
38      */
replicatedSetup(IConfiguration config, IKeyStoreClient client)39     public static void replicatedSetup(IConfiguration config, IKeyStoreClient client) {
40         if (!config.getCommandOptions().shouldUseReplicateSetup()) {
41             return;
42         }
43         if (config.getDeviceConfig().size() != 1) {
44             return;
45         }
46         Integer shardCount = config.getCommandOptions().getShardCount();
47         Integer shardIndex = config.getCommandOptions().getShardIndex();
48         if (shardCount == null || shardIndex != null) {
49             return;
50         }
51         CLog.logAndDisplay(LogLevel.DEBUG, "Using replicated setup.");
52         try {
53             List<IDeviceConfiguration> currentConfigs = config.getDeviceConfig();
54             for (int i = 0; i < shardCount - 1; i++) {
55                 IConfiguration deepCopy =
56                         config.partialDeepClone(Arrays.asList(Configuration.DEVICE_NAME), client);
57                 String newName = String.format("expanded-%s", i);
58                 IDeviceConfiguration newDeviceConfig =
59                         deepCopy.getDeviceConfig().get(0).clone(newName);
60                 // Stub the build provider since it should never be called
61                 newDeviceConfig.addSpecificConfig(new StubBuildProvider());
62                 currentConfigs.add(newDeviceConfig);
63             }
64             config.setDeviceConfigList(currentConfigs);
65         } catch (ConfigurationException e) {
66             CLog.e("Failed replicated setup configuration:");
67             CLog.e(e);
68         }
69     }
70 }
71