blob: 4057abdd1bab9b3342d013784c0b19a9302516bc [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2009 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//
rspangler@google.com49fdf182009-10-10 00:57:34 +000016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#include "update_engine/common/action_processor.h"
Alex Deymoaab50e32014-11-10 19:55:35 -080018
Alex Deymo39910dc2015-11-09 17:04:30 -080019#include <string>
Amin Hassanid3f4bea2018-04-30 14:52:40 -070020#include <utility>
Alex Deymo14fd1ec2016-02-24 22:03:57 -080021
22#include <gtest/gtest.h>
23
Alex Deymo39910dc2015-11-09 17:04:30 -080024#include "update_engine/common/action.h"
Alex Deymo14fd1ec2016-02-24 22:03:57 -080025#include "update_engine/common/mock_action.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000026
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080027using std::string;
28
rspangler@google.com49fdf182009-10-10 00:57:34 +000029namespace chromeos_update_engine {
30
31using chromeos_update_engine::ActionPipe;
32
33class ActionProcessorTestAction;
34
Amin Hassanib2689592019-01-13 17:04:28 -080035template <>
rspangler@google.com49fdf182009-10-10 00:57:34 +000036class ActionTraits<ActionProcessorTestAction> {
37 public:
38 typedef string OutputObjectType;
39 typedef string InputObjectType;
40};
41
42// This is a simple Action class for testing.
Yunlian Jianga178e5e2013-04-05 14:41:56 -070043class ActionProcessorTestAction : public Action<ActionProcessorTestAction> {
44 public:
rspangler@google.com49fdf182009-10-10 00:57:34 +000045 typedef string InputObjectType;
46 typedef string OutputObjectType;
47 ActionPipe<string>* in_pipe() { return in_pipe_.get(); }
48 ActionPipe<string>* out_pipe() { return out_pipe_.get(); }
49 ActionProcessor* processor() { return processor_; }
50 void PerformAction() {}
51 void CompleteAction() {
52 ASSERT_TRUE(processor());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070053 processor()->ActionComplete(this, ErrorCode::kSuccess);
rspangler@google.com49fdf182009-10-10 00:57:34 +000054 }
55 string Type() const { return "ActionProcessorTestAction"; }
56};
57
rspangler@google.com49fdf182009-10-10 00:57:34 +000058namespace {
59class MyActionProcessorDelegate : public ActionProcessorDelegate {
60 public:
61 explicit MyActionProcessorDelegate(const ActionProcessor* processor)
Andrew de los Reyes3270f742010-07-15 22:28:14 -070062 : processor_(processor),
63 processing_done_called_(false),
64 processing_stopped_called_(false),
65 action_completed_called_(false),
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070066 action_exit_code_(ErrorCode::kError) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000067
Darin Petkovc1a8b422010-07-19 11:34:49 -070068 virtual void ProcessingDone(const ActionProcessor* processor,
David Zeuthena99981f2013-04-29 13:42:47 -070069 ErrorCode code) {
rspangler@google.com49fdf182009-10-10 00:57:34 +000070 EXPECT_EQ(processor_, processor);
71 EXPECT_FALSE(processing_done_called_);
72 processing_done_called_ = true;
73 }
74 virtual void ProcessingStopped(const ActionProcessor* processor) {
75 EXPECT_EQ(processor_, processor);
76 EXPECT_FALSE(processing_stopped_called_);
77 processing_stopped_called_ = true;
78 }
adlr@google.comc98a7ed2009-12-04 18:54:03 +000079 virtual void ActionCompleted(ActionProcessor* processor,
80 AbstractAction* action,
David Zeuthena99981f2013-04-29 13:42:47 -070081 ErrorCode code) {
rspangler@google.com49fdf182009-10-10 00:57:34 +000082 EXPECT_EQ(processor_, processor);
83 EXPECT_FALSE(action_completed_called_);
84 action_completed_called_ = true;
Darin Petkovc1a8b422010-07-19 11:34:49 -070085 action_exit_code_ = code;
rspangler@google.com49fdf182009-10-10 00:57:34 +000086 }
87
88 const ActionProcessor* processor_;
89 bool processing_done_called_;
90 bool processing_stopped_called_;
91 bool action_completed_called_;
David Zeuthena99981f2013-04-29 13:42:47 -070092 ErrorCode action_exit_code_;
rspangler@google.com49fdf182009-10-10 00:57:34 +000093};
Alex Vakulenkod2779df2014-06-16 13:19:00 -070094} // namespace
rspangler@google.com49fdf182009-10-10 00:57:34 +000095
Alex Deymo14fd1ec2016-02-24 22:03:57 -080096class ActionProcessorTest : public ::testing::Test {
97 void SetUp() override {
98 action_processor_.set_delegate(&delegate_);
99 // Silence Type() calls used for logging.
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700100 mock_action_.reset(new testing::StrictMock<MockAction>());
101 mock_action_ptr_ = mock_action_.get();
102 action_.reset(new ActionProcessorTestAction());
103 action_ptr_ = action_.get();
104 EXPECT_CALL(*mock_action_, Type()).Times(testing::AnyNumber());
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800105 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000106
Amin Hassanib2689592019-01-13 17:04:28 -0800107 void TearDown() override { action_processor_.set_delegate(nullptr); }
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800108
109 protected:
110 // The ActionProcessor under test.
111 ActionProcessor action_processor_;
112
113 MyActionProcessorDelegate delegate_{&action_processor_};
114
115 // Common actions used during most tests.
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700116 std::unique_ptr<testing::StrictMock<MockAction>> mock_action_;
117 testing::StrictMock<MockAction>* mock_action_ptr_;
118 std::unique_ptr<ActionProcessorTestAction> action_;
119 ActionProcessorTestAction* action_ptr_;
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800120};
121
122TEST_F(ActionProcessorTest, SimpleTest) {
123 EXPECT_FALSE(action_processor_.IsRunning());
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700124 action_processor_.EnqueueAction(std::move(action_));
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800125 EXPECT_FALSE(action_processor_.IsRunning());
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700126 EXPECT_FALSE(action_ptr_->IsRunning());
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800127 action_processor_.StartProcessing();
128 EXPECT_TRUE(action_processor_.IsRunning());
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700129 EXPECT_TRUE(action_ptr_->IsRunning());
130 action_ptr_->CompleteAction();
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800131 EXPECT_FALSE(action_processor_.IsRunning());
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700132 EXPECT_EQ(action_processor_.current_action(), nullptr);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000133}
134
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800135TEST_F(ActionProcessorTest, DelegateTest) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700136 action_processor_.EnqueueAction(std::move(action_));
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800137 action_processor_.StartProcessing();
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700138 action_ptr_->CompleteAction();
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800139 EXPECT_TRUE(delegate_.processing_done_called_);
140 EXPECT_TRUE(delegate_.action_completed_called_);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000141}
142
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800143TEST_F(ActionProcessorTest, StopProcessingTest) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700144 action_processor_.EnqueueAction(std::move(action_));
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800145 action_processor_.StartProcessing();
146 action_processor_.StopProcessing();
147 EXPECT_TRUE(delegate_.processing_stopped_called_);
148 EXPECT_FALSE(delegate_.action_completed_called_);
149 EXPECT_FALSE(action_processor_.IsRunning());
150 EXPECT_EQ(nullptr, action_processor_.current_action());
151}
152
153TEST_F(ActionProcessorTest, ChainActionsTest) {
154 // This test doesn't use a delegate since it terminates several actions.
155 action_processor_.set_delegate(nullptr);
156
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700157 auto action0 = std::make_unique<ActionProcessorTestAction>();
158 auto action1 = std::make_unique<ActionProcessorTestAction>();
159 auto action2 = std::make_unique<ActionProcessorTestAction>();
160 auto action0_ptr = action0.get();
161 auto action1_ptr = action1.get();
162 auto action2_ptr = action2.get();
163 action_processor_.EnqueueAction(std::move(action0));
164 action_processor_.EnqueueAction(std::move(action1));
165 action_processor_.EnqueueAction(std::move(action2));
166
Amin Hassani667cf7b2018-07-25 14:32:00 -0700167 EXPECT_EQ(action_processor_.actions_.size(), 3u);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700168 EXPECT_EQ(action_processor_.actions_[0].get(), action0_ptr);
169 EXPECT_EQ(action_processor_.actions_[1].get(), action1_ptr);
170 EXPECT_EQ(action_processor_.actions_[2].get(), action2_ptr);
171
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800172 action_processor_.StartProcessing();
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700173 EXPECT_EQ(action0_ptr, action_processor_.current_action());
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800174 EXPECT_TRUE(action_processor_.IsRunning());
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700175 action0_ptr->CompleteAction();
176 EXPECT_EQ(action1_ptr, action_processor_.current_action());
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800177 EXPECT_TRUE(action_processor_.IsRunning());
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700178 action1_ptr->CompleteAction();
179 EXPECT_EQ(action2_ptr, action_processor_.current_action());
180 EXPECT_TRUE(action_processor_.actions_.empty());
181 EXPECT_TRUE(action_processor_.IsRunning());
182 action2_ptr->CompleteAction();
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800183 EXPECT_EQ(nullptr, action_processor_.current_action());
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700184 EXPECT_TRUE(action_processor_.actions_.empty());
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800185 EXPECT_FALSE(action_processor_.IsRunning());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000186}
187
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800188TEST_F(ActionProcessorTest, DefaultDelegateTest) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700189 // Just make sure it doesn't crash.
190 action_processor_.EnqueueAction(std::move(action_));
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800191 action_processor_.StartProcessing();
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700192 action_ptr_->CompleteAction();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000193
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700194 action_.reset(new ActionProcessorTestAction());
195 action_processor_.EnqueueAction(std::move(action_));
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800196 action_processor_.StartProcessing();
197 action_processor_.StopProcessing();
198}
rspangler@google.com49fdf182009-10-10 00:57:34 +0000199
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700200// This test suspends and resume the action processor while running one action.
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800201TEST_F(ActionProcessorTest, SuspendResumeTest) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700202 action_processor_.EnqueueAction(std::move(mock_action_));
rspangler@google.com49fdf182009-10-10 00:57:34 +0000203
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800204 testing::InSequence s;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700205 EXPECT_CALL(*mock_action_ptr_, PerformAction());
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800206 action_processor_.StartProcessing();
207
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700208 EXPECT_CALL(*mock_action_ptr_, SuspendAction());
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800209 action_processor_.SuspendProcessing();
210 // Suspending the processor twice should not suspend the action twice.
211 action_processor_.SuspendProcessing();
212
213 // IsRunning should return whether there's is an action doing some work, even
214 // if it is suspended.
215 EXPECT_TRUE(action_processor_.IsRunning());
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700216 EXPECT_EQ(mock_action_ptr_, action_processor_.current_action());
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800217
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700218 EXPECT_CALL(*mock_action_ptr_, ResumeAction());
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800219 action_processor_.ResumeProcessing();
220
221 // Calling ResumeProcessing twice should not affect the action_.
222 action_processor_.ResumeProcessing();
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700223 action_processor_.ActionComplete(mock_action_ptr_, ErrorCode::kSuccess);
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800224}
225
226// This test suspends an action that presumably doesn't support suspend/resume
227// and it finished before being resumed.
228TEST_F(ActionProcessorTest, ActionCompletedWhileSuspendedTest) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700229 action_processor_.EnqueueAction(std::move(mock_action_));
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800230
231 testing::InSequence s;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700232 EXPECT_CALL(*mock_action_ptr_, PerformAction());
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800233 action_processor_.StartProcessing();
234
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700235 EXPECT_CALL(*mock_action_ptr_, SuspendAction());
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800236 action_processor_.SuspendProcessing();
237
238 // Simulate the action completion while suspended. No other call to
239 // |mock_action_| is expected at this point.
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700240 action_processor_.ActionComplete(mock_action_ptr_, ErrorCode::kSuccess);
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800241
242 // The processing should not be done since the ActionProcessor is suspended
243 // and the processing is considered to be still running until resumed.
244 EXPECT_FALSE(delegate_.processing_done_called_);
245 EXPECT_TRUE(action_processor_.IsRunning());
246
247 action_processor_.ResumeProcessing();
248 EXPECT_TRUE(delegate_.processing_done_called_);
249 EXPECT_FALSE(delegate_.processing_stopped_called_);
250}
251
252TEST_F(ActionProcessorTest, StoppedWhileSuspendedTest) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700253 action_processor_.EnqueueAction(std::move(mock_action_));
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800254
255 testing::InSequence s;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700256 EXPECT_CALL(*mock_action_ptr_, PerformAction());
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800257 action_processor_.StartProcessing();
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700258 EXPECT_CALL(*mock_action_ptr_, SuspendAction());
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800259 action_processor_.SuspendProcessing();
260
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700261 EXPECT_CALL(*mock_action_ptr_, TerminateProcessing());
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800262 action_processor_.StopProcessing();
263 // Stopping the processing should abort the current execution no matter what.
264 EXPECT_TRUE(delegate_.processing_stopped_called_);
265 EXPECT_FALSE(delegate_.processing_done_called_);
266 EXPECT_FALSE(delegate_.action_completed_called_);
267 EXPECT_FALSE(action_processor_.IsRunning());
268 EXPECT_EQ(nullptr, action_processor_.current_action());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000269}
270
271} // namespace chromeos_update_engine