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 <string>
17 #include <vector>
18
19 #include <gtest/gtest.h>
20
21 #include "host/commands/cvd/selector/instance_group_record.h"
22 #include "host/commands/cvd/selector/instance_record.h"
23
24 namespace cuttlefish {
25 namespace selector {
26
GroupName()27 static std::string GroupName() { return "yah_ong"; }
HomeDir()28 static std::string HomeDir() { return "/home/user"; }
TestBinDir()29 static std::string TestBinDir() { return "/opt/android11"; }
30
31 class CvdInstanceGroupUnitTest : public testing::Test {
32 protected:
CvdInstanceGroupUnitTest()33 CvdInstanceGroupUnitTest()
34 : group_({.group_name = GroupName(),
35 .home_dir = HomeDir(),
36 .host_artifacts_path = TestBinDir(),
37 .product_out_path = TestBinDir()}) {}
Get()38 LocalInstanceGroup& Get() { return group_; }
39 LocalInstanceGroup group_;
40 };
41
42 // CvdInstanceGroupUnitTest + add 4 instances
43 class CvdInstanceGroupSearchUnitTest : public testing::Test {
44 protected:
CvdInstanceGroupSearchUnitTest()45 CvdInstanceGroupSearchUnitTest()
46 : group_({.group_name = GroupName(),
47 .home_dir = HomeDir(),
48 .host_artifacts_path = TestBinDir(),
49 .product_out_path = TestBinDir()}) {
50 is_setup_ =
51 (Get().AddInstance(1, "tv_instance").ok() &&
52 Get().AddInstance(2, "2").ok() && Get().AddInstance(3, "phone").ok() &&
53 Get().AddInstance(7, "tv_instance").ok());
54 is_setup_ = is_setup_ && (Get().Instances().size() == 4);
55 }
Get()56 LocalInstanceGroup& Get() { return group_; }
IsSetup() const57 bool IsSetup() const { return is_setup_; }
58
59 private:
60 bool is_setup_;
61 LocalInstanceGroup group_;
62 };
63
TEST_F(CvdInstanceGroupUnitTest,Fields)64 TEST_F(CvdInstanceGroupUnitTest, Fields) {
65 auto& group = Get();
66
67 ASSERT_EQ(group.InternalGroupName(), "cvd");
68 ASSERT_EQ(group.GroupName(), "yah_ong");
69 ASSERT_EQ(group.HomeDir(), HomeDir());
70 ASSERT_EQ(group.HostArtifactsPath(), TestBinDir());
71 }
72
TEST_F(CvdInstanceGroupUnitTest,AddInstances)73 TEST_F(CvdInstanceGroupUnitTest, AddInstances) {
74 auto& group = Get();
75
76 ASSERT_TRUE(group.AddInstance(1, "tv_instance").ok());
77 ASSERT_TRUE(group.AddInstance(2, "2").ok());
78 ASSERT_TRUE(group.AddInstance(3, "phone").ok());
79 ASSERT_EQ(group.Instances().size(), 3);
80 }
81
TEST_F(CvdInstanceGroupUnitTest,AddInstancesAndListAll)82 TEST_F(CvdInstanceGroupUnitTest, AddInstancesAndListAll) {
83 auto& group = Get();
84 group.AddInstance(1, "tv_instance");
85 group.AddInstance(2, "2");
86 group.AddInstance(3, "phone");
87 if (group.Instances().size() != 3) {
88 GTEST_SKIP() << "AddInstance failed but is verified in other testing.";
89 }
90
91 auto set_result = group.FindAllInstances();
92
93 ASSERT_TRUE(set_result.ok()) << set_result.error().Trace();
94 ASSERT_EQ(set_result->size(), 3);
95 }
96
TEST_F(CvdInstanceGroupSearchUnitTest,SearchById)97 TEST_F(CvdInstanceGroupSearchUnitTest, SearchById) {
98 auto& group = Get();
99 if (!IsSetup()) {
100 /*
101 * Here's why we skip the test rather than see it as a failure.
102 *
103 * 1. The test is specifically designed for searcy-by-id operations.
104 * 2. Adding instance to a group is tested in AddInstances test designed
105 * specifically for it. It's a failure there but not here.
106 */
107 GTEST_SKIP() << "Failed to add instances to the group.";
108 }
109 // valid_ids were added in the CvdInstanceGroupSearchUnitTest_SearchById
110 // constructor.
111 std::vector<unsigned> valid_ids{1, 2, 7};
112 std::vector<unsigned> invalid_ids{20, 0, 5};
113
114 // valid search
115 for (auto const& valid_id : valid_ids) {
116 auto result = group.FindById(valid_id);
117 ASSERT_TRUE(result.ok());
118 auto set = *result;
119 ASSERT_EQ(set.size(), 1);
120 const LocalInstance& instance = *set.cbegin();
121 ASSERT_EQ(instance.InstanceId(), valid_id);
122 }
123
124 // invalid search
125 for (auto const& invalid_id : invalid_ids) {
126 auto result = group.FindById(invalid_id);
127 // it's okay not to be found
128 ASSERT_TRUE(result.ok());
129 ASSERT_TRUE(result->empty());
130 }
131 }
132
133 } // namespace selector
134 } // namespace cuttlefish
135