1 //
2 // Copyright (C) 2022 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 #include <android-base/strings.h>
17 #include <gtest/gtest.h>
18
19 #include "host/commands/cvd/selector/start_selector_parser.h"
20 #include "host/commands/cvd/unittests/selector/parser_ids_helper.h"
21
22 namespace cuttlefish {
23 namespace selector {
24
TEST_P(InstanceIdTest,InstanceIdCalculation)25 TEST_P(InstanceIdTest, InstanceIdCalculation) {
26 auto parser = StartSelectorParser::ConductSelectFlagsParser(
27 uid_, selector_args_, cmd_args_, envs_);
28
29 ASSERT_EQ(parser.ok(), expected_result_);
30 if (!expected_result_) {
31 return;
32 }
33 ASSERT_EQ(parser->InstanceIds(), expected_ids_);
34 ASSERT_EQ(parser->RequestedNumInstances(), requested_num_instances_);
35 }
36
37 INSTANTIATE_TEST_SUITE_P(
38 CvdParser, InstanceIdTest,
39 testing::Values(
40 InstanceIdTestInput{.cuttlefish_instance = std::nullopt,
41 .expected_ids = std::nullopt,
42 .requested_num_instances = 1,
43 .expected_result = true},
44 InstanceIdTestInput{.cuttlefish_instance = "8",
45 .expected_ids = std::vector<unsigned>{8},
46 .requested_num_instances = 1,
47 .expected_result = true},
48 InstanceIdTestInput{.cmd_args = "--num_instances=2",
49 .expected_ids = std::nullopt,
50 .requested_num_instances = 2,
51 .expected_result = true},
52 InstanceIdTestInput{.cmd_args = "--num_instances=2",
53 .cuttlefish_instance = "8",
54 .expected_ids = std::vector<unsigned>{8, 9},
55 .requested_num_instances = 2,
56 .expected_result = true},
57 InstanceIdTestInput{
58 .cmd_args = "--base_instance_num=10 --num_instances=2",
59 .cuttlefish_instance = "8",
60 .expected_ids = std::vector<unsigned>{10, 11},
61 .requested_num_instances = 2,
62 .expected_result = true},
63 InstanceIdTestInput{.cmd_args = "--instance_nums 2",
64 .cuttlefish_instance = std::nullopt,
65 .expected_ids = std::vector<unsigned>{2},
66 .requested_num_instances = 1,
67 .expected_result = true},
68 InstanceIdTestInput{.cmd_args = "--instance_nums 2,5,6",
69 .cuttlefish_instance = std::nullopt,
70 .expected_ids = std::vector<unsigned>{2, 5, 6},
71 .requested_num_instances = 3,
72 .expected_result = true},
73 InstanceIdTestInput{
74 .cmd_args = "--instance_nums 2,5,6 --num_instances=3",
75 .cuttlefish_instance = std::nullopt,
76 .expected_ids = std::vector<unsigned>{2, 5, 6},
77 .requested_num_instances = 3,
78 .expected_result = true},
79 InstanceIdTestInput{
80 .cmd_args = "--instance_nums 2,5,6 --num_instances=3",
81 .selector_args = "--instance_name=c-1,c-3,c-5",
82 .cuttlefish_instance = std::nullopt,
83 .expected_ids = std::vector<unsigned>{2, 5, 6},
84 .requested_num_instances = 3,
85 .expected_result = true},
86 InstanceIdTestInput{.selector_args = "--instance_name=c-1,c-3,c-5",
87 .cuttlefish_instance = std::nullopt,
88 .expected_ids = std::nullopt,
89 .requested_num_instances = 3,
90 .expected_result = true},
91 // CUTTLEFISH_INSTANCE should be ignored
92 InstanceIdTestInput{
93 .cmd_args = "--instance_nums 2,5,6 --num_instances=3",
94 .cuttlefish_instance = "8",
95 .expected_ids = std::vector<unsigned>{2, 5, 6},
96 .requested_num_instances = 3,
97 .expected_result = true},
98 // instance_nums and num_instances mismatch
99 InstanceIdTestInput{
100 .cmd_args = "--instance_nums 2,5,6 --num_instances=7",
101 .cuttlefish_instance = std::nullopt,
102 .expected_ids = std::vector<unsigned>{2, 5, 6},
103 .requested_num_instances = 3,
104 .expected_result = false},
105 // --instance_name requested 2 instances while instance_nums 3.
106 InstanceIdTestInput{
107 .cmd_args = "--num_instances=3 --instance_nums 2,5,6",
108 .selector_args = "--instance_name=c-1,c-3",
109 .cuttlefish_instance = std::nullopt,
110 .expected_ids = std::vector<unsigned>{2, 5, 6},
111 .requested_num_instances = 3,
112 .expected_result = false},
113 // --base_instance_num is not allowed with --instance_nums
114 InstanceIdTestInput{
115 .cmd_args = "--instance_nums 2,5,6 --base_instance_num=7",
116 .cuttlefish_instance = std::nullopt,
117 .expected_ids = std::vector<unsigned>{2, 5, 6},
118 .requested_num_instances = 3,
119 .expected_result = false}));
120
121 } // namespace selector
122 } // namespace cuttlefish
123