• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #pragma once
17 
18 #include <optional>
19 #include <set>
20 #include <string>
21 #include <unordered_map>
22 #include <unordered_set>
23 #include <vector>
24 
25 #include <gtest/gtest.h>
26 
27 #include "host/commands/cvd/selector/constant_reference.h"
28 #include "host/commands/cvd/selector/instance_database.h"
29 
30 namespace cuttlefish {
31 namespace selector {
32 
33 /**
34  * Creates n mock HOME directories, one per group. Also, creates
35  * 1 mock ANDROID_HOST_OUT with a mock launcher file.
36  *
37  * The test suite is to assess InstanceDatabase APIs such as
38  * adding groups, adding instances to the groups, etc. The thing
39  * is that the InstanceDatabase APIs will check if HOME and/or
40  * ANDROID_HOST_OUT are directories. Also, for ANDROID_HOST_OUT,
41  * as a bare minimum validity check, it will see if there is a launcher
42  * file under the bin directory of it.
43  *
44  * Thus, the mock environment should prepare an actual directories with
45  * a mock launcher file(s). In case the test runs/tests in the suite run
46  * in parallel, we give each test run a unique directory, and that's why
47  * all mock homes are under a temp directory created by mkdtemp()
48  *
49  */
50 class CvdInstanceDatabaseTest : public ::testing::Test {
51  protected:
52   enum class ErrorCode : std::int32_t {
53     kOk,
54     kFileError,
55     kInstanceDabaseError,
56   };
57 
58   struct SetupError {
59     ErrorCode error_code;
60     std::string msg;
61   };
62 
63   CvdInstanceDatabaseTest();
64   ~CvdInstanceDatabaseTest();
65 
SetUpOk()66   bool SetUpOk() const { return error_.error_code == ErrorCode::kOk; }
Workspace()67   const std::string& Workspace() const { return workspace_dir_; }
68   /*
69    * Returns a valid host artifacts dir, which is a prerequisite for
70    * InstanceDatabase APIs.
71    */
HostArtifactsPath()72   const std::string& HostArtifactsPath() const {
73     return android_artifacts_path_;
74   }
75 
76   // Adds InstanceGroups, each by:
77   //    "mkdir" : Workspace() + "/" + base_name, HostArtifactsPath()
78   //    db_.AddInstanceGroup()
79   bool AddGroups(const std::unordered_set<std::string>& base_names);
80   struct InstanceInfo {
81     unsigned id;
82     std::string per_instance_name;
83   };
84   bool AddInstances(const std::string& group_name,
85                     const std::vector<InstanceInfo>& instances_info);
GetDb()86   InstanceDatabase& GetDb() { return db_; }
Error()87   const SetupError& Error() const { return error_; }
88 
89  private:
90   void ClearWorkspace();
91   bool InitWorkspace();
92   bool InitMockAndroidHostOut();
93   // set error_ when there is an error
94   void SetErrorCode(const ErrorCode error_code, const std::string& msg);
95 
96   std::string android_artifacts_path_;
97   std::string workspace_dir_;
98   SetupError error_;
99   InstanceDatabase db_;
100 };
101 
102 using CvdInstanceDatabaseJsonTest = CvdInstanceDatabaseTest;
103 
104 }  // namespace selector
105 }  // namespace cuttlefish
106