1 //
2 // Copyright (C) 2023 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 "common/libs/utils/contains.h"
22 #include "host/commands/cvd/types.h"
23 #include "host/commands/cvd/unittests/server/cmd_runner.h"
24 #include "host/commands/cvd/unittests/server/utils.h"
25
26 namespace cuttlefish {
27
TEST(CvdClear,ClearAfterThreeStarts)28 TEST(CvdClear, ClearAfterThreeStarts) {
29 cvd_common::Envs envs;
30 envs["HOME"] = StringFromEnv("HOME", "");
31 CmdRunner::Run("cvd reset -y", envs);
32
33 cvd_common::Args start_two_instances_args{
34 "cvd",
35 "--disable_default_group",
36 "start",
37 "--report_anonymous_usage_stats=yes",
38 "--daemon",
39 "--norestart_subprocesses",
40 "--num_instances=2"};
41 cvd_common::Args start_three_instances_args{
42 "cvd",
43 "--disable_default_group",
44 "start",
45 "--report_anonymous_usage_stats=yes",
46 "--daemon",
47 "--norestart_subprocesses",
48 "--num_instances=3"};
49 cvd_common::Args start_one_instances_args{
50 "cvd",
51 "--disable_default_group",
52 "start",
53 "--report_anonymous_usage_stats=yes",
54 "--daemon",
55 "--norestart_subprocesses",
56 "--num_instances=1"};
57
58 auto cmd_start_two = CmdRunner::Run(start_two_instances_args, envs);
59 ASSERT_TRUE(cmd_start_two.Success()) << cmd_start_two.Stderr();
60 auto cmd_start_three = CmdRunner::Run(start_three_instances_args, envs);
61 ASSERT_TRUE(cmd_start_three.Success()) << cmd_start_three.Stderr();
62 auto cmd_start_one = CmdRunner::Run(start_one_instances_args, envs);
63 ASSERT_TRUE(cmd_start_one.Success()) << cmd_start_one.Stderr();
64
65 auto cmd_fleet = CmdRunner::Run("cvd fleet", envs);
66 ASSERT_TRUE(cmd_fleet.Success()) << cmd_fleet.Stderr();
67 ASSERT_EQ(NumberOfOccurrences(cmd_fleet.Stdout(), "instance_name"), 2 + 3 + 1)
68 << cmd_fleet.Stdout();
69
70 auto cmd_stop = CmdRunner::Run("cvd clear", envs);
71 ASSERT_TRUE(cmd_stop.Success()) << cmd_stop.Stderr();
72
73 cmd_fleet = CmdRunner::Run("cvd fleet", envs);
74 ASSERT_FALSE(Contains(cmd_fleet.Stdout(), "instance_name"));
75
76 // clean up for the next test
77 CmdRunner::Run("cvd reset -y", envs);
78 }
79
80 } // namespace cuttlefish
81