blob: 9e59080c9e9d0747e8ae6ac2804fefb12d24cc19 [file] [log] [blame]
Felipe Leme4c2d6632016-09-28 14:32:00 -07001/*
2 * Copyright (C) 2016 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
17#include "dumpstate.h"
18
19#include <gmock/gmock.h>
20#include <gtest/gtest.h>
21
22#include <libgen.h>
23#include <unistd.h>
24
25#include <android-base/file.h>
26
27using ::testing::EndsWith;
28using ::testing::IsEmpty;
29using ::testing::StrEq;
30using ::testing::StartsWith;
31using ::testing::Test;
32using ::testing::internal::CaptureStderr;
33using ::testing::internal::CaptureStdout;
34using ::testing::internal::GetCapturedStderr;
35using ::testing::internal::GetCapturedStdout;
36
37// Not used on test cases yet...
38void dumpstate_board(void) {
39}
40
41class DumpstateTest : public Test {
42 public:
43 void SetUp() {
44 SetDryRun(false);
45 }
46
47 // Runs a command and capture `stdout` and `stderr`.
48 int RunCommand(const std::string& title, const std::vector<std::string>& fullCommand,
49 const CommandOptions& options = CommandOptions::DEFAULT) {
50 CaptureStdout();
51 CaptureStderr();
52 int status = ds_.RunCommand(title, fullCommand, options);
53 out = GetCapturedStdout();
54 err = GetCapturedStderr();
55 return status;
56 }
57
58 // `stdout` and `stderr` from the last command ran.
59 std::string out, err;
60
61 std::string testPath = dirname(android::base::GetExecutablePath().c_str());
62 std::string simpleBin = testPath + "/../dumpstate_test_fixture/dumpstate_test_fixture";
63
64 void SetDryRun(bool dryRun) {
65 ds_.dryRun_ = dryRun;
66 }
67
68 private:
69 Dumpstate& ds_ = Dumpstate::GetInstance();
70};
71
72TEST_F(DumpstateTest, RunCommandNoArgs) {
73 EXPECT_EQ(-1, RunCommand("", {}));
74}
75
76TEST_F(DumpstateTest, RunCommandNoTitle) {
77 EXPECT_EQ(0, RunCommand("", {simpleBin}));
78 EXPECT_THAT(out, StrEq("stdout\n"));
79 EXPECT_THAT(err, StrEq("stderr\n"));
80}
81
82TEST_F(DumpstateTest, RunCommandWithTitle) {
83 EXPECT_EQ(0, RunCommand("I AM GROOT", {simpleBin}));
84 EXPECT_THAT(err, StrEq("stderr\n"));
85 // We don't know the exact duration, so we check the prefix and suffix
86 EXPECT_THAT(out, StartsWith("------ I AM GROOT (" + simpleBin + ") ------\nstdout\n------"));
87 EXPECT_THAT(out, EndsWith("s was the duration of 'I AM GROOT' ------\n"));
88}
89
90TEST_F(DumpstateTest, RunCommandRedirectStderr) {
91 EXPECT_EQ(
92 0, RunCommand("", {simpleBin}, CommandOptions::WithTimeout(10).RedirectStderr().Build()));
93 EXPECT_THAT(out, IsEmpty());
94 EXPECT_THAT(err, StrEq("stderr\nstdout\n"));
95}
96
97TEST_F(DumpstateTest, RunCommandWithOneArg) {
98 EXPECT_EQ(0, RunCommand("", {simpleBin, "one"}));
99 EXPECT_THAT(err, IsEmpty());
100 EXPECT_THAT(out, StrEq("one\n"));
101}
102
103TEST_F(DumpstateTest, RunCommandWithNoArgs) {
104 EXPECT_EQ(0, RunCommand("", {simpleBin, "one", "is", "the", "loniest", "number"}));
105 EXPECT_THAT(err, IsEmpty());
106 EXPECT_THAT(out, StrEq("one is the loniest number\n"));
107}
108
109TEST_F(DumpstateTest, RunCommandDryRun) {
110 SetDryRun(true);
111 EXPECT_EQ(0, RunCommand("I AM GROOT", {simpleBin}));
112 // We don't know the exact duration, so we check the prefix and suffix
113 EXPECT_THAT(out, StartsWith("------ I AM GROOT (" + simpleBin +
114 ") ------\n\t(skipped on dry run)\n------"));
115 EXPECT_THAT(out, EndsWith("s was the duration of 'I AM GROOT' ------\n"));
116 EXPECT_THAT(err, IsEmpty());
117}
118
119TEST_F(DumpstateTest, RunCommandDryRunNoTitle) {
120 SetDryRun(true);
121 EXPECT_EQ(0, RunCommand("", {simpleBin}));
122 EXPECT_THAT(out, IsEmpty());
123 EXPECT_THAT(err, IsEmpty());
124}
125
126TEST_F(DumpstateTest, RunCommandDryRunAlways) {
127 SetDryRun(true);
128 EXPECT_EQ(0, RunCommand("", {simpleBin}, CommandOptions::WithTimeout(10).Always().Build()));
129 EXPECT_THAT(out, StrEq("stdout\n"));
130 EXPECT_THAT(err, StrEq("stderr\n"));
131}
132
133// TODO: add test for other scenarios:
134// - AsRoot()
135// - DropRoot
136// - WithLoggingMessage()
137// - command does not exist (invalid path)
138// - command times out
139// - command exits with a different value
140// - command is killed before timed out
141// - test progress
142
143// TODO: test DumpFile()