blob: b67eca9c001c002977be483ecf504c57525377ae [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
35template<>
36class 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
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800107 void TearDown() override {
108 action_processor_.set_delegate(nullptr);
109 }
110
111 protected:
112 // The ActionProcessor under test.
113 ActionProcessor action_processor_;
114
115 MyActionProcessorDelegate delegate_{&action_processor_};
116
117 // Common actions used during most tests.
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700118 std::unique_ptr<testing::StrictMock<MockAction>> mock_action_;
119 testing::StrictMock<MockAction>* mock_action_ptr_;
120 std::unique_ptr<ActionProcessorTestAction> action_;
121 ActionProcessorTestAction* action_ptr_;
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800122};
123
124TEST_F(ActionProcessorTest, SimpleTest) {
125 EXPECT_FALSE(action_processor_.IsRunning());
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700126 action_processor_.EnqueueAction(std::move(action_));
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800127 EXPECT_FALSE(action_processor_.IsRunning());
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700128 EXPECT_FALSE(action_ptr_->IsRunning());
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800129 action_processor_.StartProcessing();
130 EXPECT_TRUE(action_processor_.IsRunning());
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700131 EXPECT_TRUE(action_ptr_->IsRunning());
132 action_ptr_->CompleteAction();
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800133 EXPECT_FALSE(action_processor_.IsRunning());
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700134 EXPECT_EQ(action_processor_.current_action(), nullptr);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000135}
136
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800137TEST_F(ActionProcessorTest, DelegateTest) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700138 action_processor_.EnqueueAction(std::move(action_));
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800139 action_processor_.StartProcessing();
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700140 action_ptr_->CompleteAction();
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800141 EXPECT_TRUE(delegate_.processing_done_called_);
142 EXPECT_TRUE(delegate_.action_completed_called_);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000143}
144
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800145TEST_F(ActionProcessorTest, StopProcessingTest) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700146 action_processor_.EnqueueAction(std::move(action_));
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800147 action_processor_.StartProcessing();
148 action_processor_.StopProcessing();
149 EXPECT_TRUE(delegate_.processing_stopped_called_);
150 EXPECT_FALSE(delegate_.action_completed_called_);
151 EXPECT_FALSE(action_processor_.IsRunning());
152 EXPECT_EQ(nullptr, action_processor_.current_action());
153}
154
155TEST_F(ActionProcessorTest, ChainActionsTest) {
156 // This test doesn't use a delegate since it terminates several actions.
157 action_processor_.set_delegate(nullptr);
158
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700159 auto action0 = std::make_unique<ActionProcessorTestAction>();
160 auto action1 = std::make_unique<ActionProcessorTestAction>();
161 auto action2 = std::make_unique<ActionProcessorTestAction>();
162 auto action0_ptr = action0.get();
163 auto action1_ptr = action1.get();
164 auto action2_ptr = action2.get();
165 action_processor_.EnqueueAction(std::move(action0));
166 action_processor_.EnqueueAction(std::move(action1));
167 action_processor_.EnqueueAction(std::move(action2));
168
169 EXPECT_EQ(action_processor_.actions_.size(), 3);
170 EXPECT_EQ(action_processor_.actions_[0].get(), action0_ptr);
171 EXPECT_EQ(action_processor_.actions_[1].get(), action1_ptr);
172 EXPECT_EQ(action_processor_.actions_[2].get(), action2_ptr);
173
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800174 action_processor_.StartProcessing();
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700175 EXPECT_EQ(action0_ptr, action_processor_.current_action());
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800176 EXPECT_TRUE(action_processor_.IsRunning());
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700177 action0_ptr->CompleteAction();
178 EXPECT_EQ(action1_ptr, action_processor_.current_action());
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800179 EXPECT_TRUE(action_processor_.IsRunning());
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700180 action1_ptr->CompleteAction();
181 EXPECT_EQ(action2_ptr, action_processor_.current_action());
182 EXPECT_TRUE(action_processor_.actions_.empty());
183 EXPECT_TRUE(action_processor_.IsRunning());
184 action2_ptr->CompleteAction();
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800185 EXPECT_EQ(nullptr, action_processor_.current_action());
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700186 EXPECT_TRUE(action_processor_.actions_.empty());
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800187 EXPECT_FALSE(action_processor_.IsRunning());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000188}
189
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800190TEST_F(ActionProcessorTest, DefaultDelegateTest) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700191 // Just make sure it doesn't crash.
192 action_processor_.EnqueueAction(std::move(action_));
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800193 action_processor_.StartProcessing();
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700194 action_ptr_->CompleteAction();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000195
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700196 action_.reset(new ActionProcessorTestAction());
197 action_processor_.EnqueueAction(std::move(action_));
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800198 action_processor_.StartProcessing();
199 action_processor_.StopProcessing();
200}
rspangler@google.com49fdf182009-10-10 00:57:34 +0000201
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700202// This test suspends and resume the action processor while running one action.
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800203TEST_F(ActionProcessorTest, SuspendResumeTest) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700204 action_processor_.EnqueueAction(std::move(mock_action_));
rspangler@google.com49fdf182009-10-10 00:57:34 +0000205
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800206 testing::InSequence s;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700207 EXPECT_CALL(*mock_action_ptr_, PerformAction());
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800208 action_processor_.StartProcessing();
209
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700210 EXPECT_CALL(*mock_action_ptr_, SuspendAction());
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800211 action_processor_.SuspendProcessing();
212 // Suspending the processor twice should not suspend the action twice.
213 action_processor_.SuspendProcessing();
214
215 // IsRunning should return whether there's is an action doing some work, even
216 // if it is suspended.
217 EXPECT_TRUE(action_processor_.IsRunning());
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700218 EXPECT_EQ(mock_action_ptr_, action_processor_.current_action());
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800219
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700220 EXPECT_CALL(*mock_action_ptr_, ResumeAction());
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800221 action_processor_.ResumeProcessing();
222
223 // Calling ResumeProcessing twice should not affect the action_.
224 action_processor_.ResumeProcessing();
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700225 action_processor_.ActionComplete(mock_action_ptr_, ErrorCode::kSuccess);
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800226}
227
228// This test suspends an action that presumably doesn't support suspend/resume
229// and it finished before being resumed.
230TEST_F(ActionProcessorTest, ActionCompletedWhileSuspendedTest) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700231 action_processor_.EnqueueAction(std::move(mock_action_));
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800232
233 testing::InSequence s;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700234 EXPECT_CALL(*mock_action_ptr_, PerformAction());
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800235 action_processor_.StartProcessing();
236
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700237 EXPECT_CALL(*mock_action_ptr_, SuspendAction());
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800238 action_processor_.SuspendProcessing();
239
240 // Simulate the action completion while suspended. No other call to
241 // |mock_action_| is expected at this point.
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700242 action_processor_.ActionComplete(mock_action_ptr_, ErrorCode::kSuccess);
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800243
244 // The processing should not be done since the ActionProcessor is suspended
245 // and the processing is considered to be still running until resumed.
246 EXPECT_FALSE(delegate_.processing_done_called_);
247 EXPECT_TRUE(action_processor_.IsRunning());
248
249 action_processor_.ResumeProcessing();
250 EXPECT_TRUE(delegate_.processing_done_called_);
251 EXPECT_FALSE(delegate_.processing_stopped_called_);
252}
253
254TEST_F(ActionProcessorTest, StoppedWhileSuspendedTest) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700255 action_processor_.EnqueueAction(std::move(mock_action_));
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800256
257 testing::InSequence s;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700258 EXPECT_CALL(*mock_action_ptr_, PerformAction());
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800259 action_processor_.StartProcessing();
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700260 EXPECT_CALL(*mock_action_ptr_, SuspendAction());
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800261 action_processor_.SuspendProcessing();
262
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700263 EXPECT_CALL(*mock_action_ptr_, TerminateProcessing());
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800264 action_processor_.StopProcessing();
265 // Stopping the processing should abort the current execution no matter what.
266 EXPECT_TRUE(delegate_.processing_stopped_called_);
267 EXPECT_FALSE(delegate_.processing_done_called_);
268 EXPECT_FALSE(delegate_.action_completed_called_);
269 EXPECT_FALSE(action_processor_.IsRunning());
270 EXPECT_EQ(nullptr, action_processor_.current_action());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000271}
272
273} // namespace chromeos_update_engine