| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // | 
|  | 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.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 16 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #include "update_engine/common/action_processor.h" | 
| Alex Deymo | aab50e3 | 2014-11-10 19:55:35 -0800 | [diff] [blame] | 18 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 19 | #include <string> | 
| Alex Deymo | 14fd1ec | 2016-02-24 22:03:57 -0800 | [diff] [blame] | 20 |  | 
|  | 21 | #include <gtest/gtest.h> | 
|  | 22 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 23 | #include "update_engine/common/action.h" | 
| Alex Deymo | 14fd1ec | 2016-02-24 22:03:57 -0800 | [diff] [blame] | 24 | #include "update_engine/common/mock_action.h" | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 25 |  | 
| Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 26 | using std::string; | 
|  | 27 |  | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 28 | namespace chromeos_update_engine { | 
|  | 29 |  | 
|  | 30 | using chromeos_update_engine::ActionPipe; | 
|  | 31 |  | 
|  | 32 | class ActionProcessorTestAction; | 
|  | 33 |  | 
|  | 34 | template<> | 
|  | 35 | class ActionTraits<ActionProcessorTestAction> { | 
|  | 36 | public: | 
|  | 37 | typedef string OutputObjectType; | 
|  | 38 | typedef string InputObjectType; | 
|  | 39 | }; | 
|  | 40 |  | 
|  | 41 | // This is a simple Action class for testing. | 
| Yunlian Jiang | a178e5e | 2013-04-05 14:41:56 -0700 | [diff] [blame] | 42 | class ActionProcessorTestAction : public Action<ActionProcessorTestAction> { | 
|  | 43 | public: | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 44 | typedef string InputObjectType; | 
|  | 45 | typedef string OutputObjectType; | 
|  | 46 | ActionPipe<string>* in_pipe() { return in_pipe_.get(); } | 
|  | 47 | ActionPipe<string>* out_pipe() { return out_pipe_.get(); } | 
|  | 48 | ActionProcessor* processor() { return processor_; } | 
|  | 49 | void PerformAction() {} | 
|  | 50 | void CompleteAction() { | 
|  | 51 | ASSERT_TRUE(processor()); | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 52 | processor()->ActionComplete(this, ErrorCode::kSuccess); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 53 | } | 
|  | 54 | string Type() const { return "ActionProcessorTestAction"; } | 
|  | 55 | }; | 
|  | 56 |  | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 57 | namespace { | 
|  | 58 | class MyActionProcessorDelegate : public ActionProcessorDelegate { | 
|  | 59 | public: | 
|  | 60 | explicit MyActionProcessorDelegate(const ActionProcessor* processor) | 
| Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 61 | : processor_(processor), | 
|  | 62 | processing_done_called_(false), | 
|  | 63 | processing_stopped_called_(false), | 
|  | 64 | action_completed_called_(false), | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 65 | action_exit_code_(ErrorCode::kError) {} | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 66 |  | 
| Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 67 | virtual void ProcessingDone(const ActionProcessor* processor, | 
| David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 68 | ErrorCode code) { | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 69 | EXPECT_EQ(processor_, processor); | 
|  | 70 | EXPECT_FALSE(processing_done_called_); | 
|  | 71 | processing_done_called_ = true; | 
|  | 72 | } | 
|  | 73 | virtual void ProcessingStopped(const ActionProcessor* processor) { | 
|  | 74 | EXPECT_EQ(processor_, processor); | 
|  | 75 | EXPECT_FALSE(processing_stopped_called_); | 
|  | 76 | processing_stopped_called_ = true; | 
|  | 77 | } | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 78 | virtual void ActionCompleted(ActionProcessor* processor, | 
|  | 79 | AbstractAction* action, | 
| David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 80 | ErrorCode code) { | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 81 | EXPECT_EQ(processor_, processor); | 
|  | 82 | EXPECT_FALSE(action_completed_called_); | 
|  | 83 | action_completed_called_ = true; | 
| Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 84 | action_exit_code_ = code; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 85 | } | 
|  | 86 |  | 
|  | 87 | const ActionProcessor* processor_; | 
|  | 88 | bool processing_done_called_; | 
|  | 89 | bool processing_stopped_called_; | 
|  | 90 | bool action_completed_called_; | 
| David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 91 | ErrorCode action_exit_code_; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 92 | }; | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 93 | }  // namespace | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 94 |  | 
| Alex Deymo | 14fd1ec | 2016-02-24 22:03:57 -0800 | [diff] [blame] | 95 | class ActionProcessorTest : public ::testing::Test { | 
|  | 96 | void SetUp() override { | 
|  | 97 | action_processor_.set_delegate(&delegate_); | 
|  | 98 | // Silence Type() calls used for logging. | 
|  | 99 | EXPECT_CALL(mock_action_, Type()).Times(testing::AnyNumber()); | 
|  | 100 | } | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 101 |  | 
| Alex Deymo | 14fd1ec | 2016-02-24 22:03:57 -0800 | [diff] [blame] | 102 | void TearDown() override { | 
|  | 103 | action_processor_.set_delegate(nullptr); | 
|  | 104 | } | 
|  | 105 |  | 
|  | 106 | protected: | 
|  | 107 | // The ActionProcessor under test. | 
|  | 108 | ActionProcessor action_processor_; | 
|  | 109 |  | 
|  | 110 | MyActionProcessorDelegate delegate_{&action_processor_}; | 
|  | 111 |  | 
|  | 112 | // Common actions used during most tests. | 
|  | 113 | testing::StrictMock<MockAction> mock_action_; | 
|  | 114 | ActionProcessorTestAction action_; | 
|  | 115 | }; | 
|  | 116 |  | 
|  | 117 | TEST_F(ActionProcessorTest, SimpleTest) { | 
|  | 118 | EXPECT_FALSE(action_processor_.IsRunning()); | 
|  | 119 | action_processor_.EnqueueAction(&action_); | 
|  | 120 | EXPECT_FALSE(action_processor_.IsRunning()); | 
|  | 121 | EXPECT_FALSE(action_.IsRunning()); | 
|  | 122 | action_processor_.StartProcessing(); | 
|  | 123 | EXPECT_TRUE(action_processor_.IsRunning()); | 
|  | 124 | EXPECT_TRUE(action_.IsRunning()); | 
|  | 125 | EXPECT_EQ(action_processor_.current_action(), &action_); | 
|  | 126 | action_.CompleteAction(); | 
|  | 127 | EXPECT_FALSE(action_processor_.IsRunning()); | 
|  | 128 | EXPECT_FALSE(action_.IsRunning()); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 129 | } | 
|  | 130 |  | 
| Alex Deymo | 14fd1ec | 2016-02-24 22:03:57 -0800 | [diff] [blame] | 131 | TEST_F(ActionProcessorTest, DelegateTest) { | 
|  | 132 | action_processor_.EnqueueAction(&action_); | 
|  | 133 | action_processor_.StartProcessing(); | 
|  | 134 | action_.CompleteAction(); | 
|  | 135 | EXPECT_TRUE(delegate_.processing_done_called_); | 
|  | 136 | EXPECT_TRUE(delegate_.action_completed_called_); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 137 | } | 
|  | 138 |  | 
| Alex Deymo | 14fd1ec | 2016-02-24 22:03:57 -0800 | [diff] [blame] | 139 | TEST_F(ActionProcessorTest, StopProcessingTest) { | 
|  | 140 | action_processor_.EnqueueAction(&action_); | 
|  | 141 | action_processor_.StartProcessing(); | 
|  | 142 | action_processor_.StopProcessing(); | 
|  | 143 | EXPECT_TRUE(delegate_.processing_stopped_called_); | 
|  | 144 | EXPECT_FALSE(delegate_.action_completed_called_); | 
|  | 145 | EXPECT_FALSE(action_processor_.IsRunning()); | 
|  | 146 | EXPECT_EQ(nullptr, action_processor_.current_action()); | 
|  | 147 | } | 
|  | 148 |  | 
|  | 149 | TEST_F(ActionProcessorTest, ChainActionsTest) { | 
|  | 150 | // This test doesn't use a delegate since it terminates several actions. | 
|  | 151 | action_processor_.set_delegate(nullptr); | 
|  | 152 |  | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 153 | ActionProcessorTestAction action1, action2; | 
| Alex Deymo | 14fd1ec | 2016-02-24 22:03:57 -0800 | [diff] [blame] | 154 | action_processor_.EnqueueAction(&action1); | 
|  | 155 | action_processor_.EnqueueAction(&action2); | 
|  | 156 | action_processor_.StartProcessing(); | 
|  | 157 | EXPECT_EQ(&action1, action_processor_.current_action()); | 
|  | 158 | EXPECT_TRUE(action_processor_.IsRunning()); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 159 | action1.CompleteAction(); | 
| Alex Deymo | 14fd1ec | 2016-02-24 22:03:57 -0800 | [diff] [blame] | 160 | EXPECT_EQ(&action2, action_processor_.current_action()); | 
|  | 161 | EXPECT_TRUE(action_processor_.IsRunning()); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 162 | action2.CompleteAction(); | 
| Alex Deymo | 14fd1ec | 2016-02-24 22:03:57 -0800 | [diff] [blame] | 163 | EXPECT_EQ(nullptr, action_processor_.current_action()); | 
|  | 164 | EXPECT_FALSE(action_processor_.IsRunning()); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 165 | } | 
|  | 166 |  | 
| Alex Deymo | 14fd1ec | 2016-02-24 22:03:57 -0800 | [diff] [blame] | 167 | TEST_F(ActionProcessorTest, DtorTest) { | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 168 | ActionProcessorTestAction action1, action2; | 
|  | 169 | { | 
|  | 170 | ActionProcessor action_processor; | 
|  | 171 | action_processor.EnqueueAction(&action1); | 
|  | 172 | action_processor.EnqueueAction(&action2); | 
|  | 173 | action_processor.StartProcessing(); | 
|  | 174 | } | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 175 | EXPECT_EQ(nullptr, action1.processor()); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 176 | EXPECT_FALSE(action1.IsRunning()); | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 177 | EXPECT_EQ(nullptr, action2.processor()); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 178 | EXPECT_FALSE(action2.IsRunning()); | 
|  | 179 | } | 
|  | 180 |  | 
| Alex Deymo | 14fd1ec | 2016-02-24 22:03:57 -0800 | [diff] [blame] | 181 | TEST_F(ActionProcessorTest, DefaultDelegateTest) { | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 182 | // Just make sure it doesn't crash | 
| Alex Deymo | 14fd1ec | 2016-02-24 22:03:57 -0800 | [diff] [blame] | 183 | action_processor_.EnqueueAction(&action_); | 
|  | 184 | action_processor_.StartProcessing(); | 
|  | 185 | action_.CompleteAction(); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 186 |  | 
| Alex Deymo | 14fd1ec | 2016-02-24 22:03:57 -0800 | [diff] [blame] | 187 | action_processor_.EnqueueAction(&action_); | 
|  | 188 | action_processor_.StartProcessing(); | 
|  | 189 | action_processor_.StopProcessing(); | 
|  | 190 | } | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 191 |  | 
| Alex Deymo | 14fd1ec | 2016-02-24 22:03:57 -0800 | [diff] [blame] | 192 | // This test suspends and resume the action processor while running one action_. | 
|  | 193 | TEST_F(ActionProcessorTest, SuspendResumeTest) { | 
|  | 194 | action_processor_.EnqueueAction(&mock_action_); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 195 |  | 
| Alex Deymo | 14fd1ec | 2016-02-24 22:03:57 -0800 | [diff] [blame] | 196 | testing::InSequence s; | 
|  | 197 | EXPECT_CALL(mock_action_, PerformAction()); | 
|  | 198 | action_processor_.StartProcessing(); | 
|  | 199 |  | 
|  | 200 | EXPECT_CALL(mock_action_, SuspendAction()); | 
|  | 201 | action_processor_.SuspendProcessing(); | 
|  | 202 | // Suspending the processor twice should not suspend the action twice. | 
|  | 203 | action_processor_.SuspendProcessing(); | 
|  | 204 |  | 
|  | 205 | // IsRunning should return whether there's is an action doing some work, even | 
|  | 206 | // if it is suspended. | 
|  | 207 | EXPECT_TRUE(action_processor_.IsRunning()); | 
|  | 208 | EXPECT_EQ(&mock_action_, action_processor_.current_action()); | 
|  | 209 |  | 
|  | 210 | EXPECT_CALL(mock_action_, ResumeAction()); | 
|  | 211 | action_processor_.ResumeProcessing(); | 
|  | 212 |  | 
|  | 213 | // Calling ResumeProcessing twice should not affect the action_. | 
|  | 214 | action_processor_.ResumeProcessing(); | 
|  | 215 |  | 
|  | 216 | action_processor_.ActionComplete(&mock_action_, ErrorCode::kSuccess); | 
|  | 217 | } | 
|  | 218 |  | 
|  | 219 | // This test suspends an action that presumably doesn't support suspend/resume | 
|  | 220 | // and it finished before being resumed. | 
|  | 221 | TEST_F(ActionProcessorTest, ActionCompletedWhileSuspendedTest) { | 
|  | 222 | action_processor_.EnqueueAction(&mock_action_); | 
|  | 223 |  | 
|  | 224 | testing::InSequence s; | 
|  | 225 | EXPECT_CALL(mock_action_, PerformAction()); | 
|  | 226 | action_processor_.StartProcessing(); | 
|  | 227 |  | 
|  | 228 | EXPECT_CALL(mock_action_, SuspendAction()); | 
|  | 229 | action_processor_.SuspendProcessing(); | 
|  | 230 |  | 
|  | 231 | // Simulate the action completion while suspended. No other call to | 
|  | 232 | // |mock_action_| is expected at this point. | 
|  | 233 | action_processor_.ActionComplete(&mock_action_, ErrorCode::kSuccess); | 
|  | 234 |  | 
|  | 235 | // The processing should not be done since the ActionProcessor is suspended | 
|  | 236 | // and the processing is considered to be still running until resumed. | 
|  | 237 | EXPECT_FALSE(delegate_.processing_done_called_); | 
|  | 238 | EXPECT_TRUE(action_processor_.IsRunning()); | 
|  | 239 |  | 
|  | 240 | action_processor_.ResumeProcessing(); | 
|  | 241 | EXPECT_TRUE(delegate_.processing_done_called_); | 
|  | 242 | EXPECT_FALSE(delegate_.processing_stopped_called_); | 
|  | 243 | } | 
|  | 244 |  | 
|  | 245 | TEST_F(ActionProcessorTest, StoppedWhileSuspendedTest) { | 
|  | 246 | action_processor_.EnqueueAction(&mock_action_); | 
|  | 247 |  | 
|  | 248 | testing::InSequence s; | 
|  | 249 | EXPECT_CALL(mock_action_, PerformAction()); | 
|  | 250 | action_processor_.StartProcessing(); | 
|  | 251 | EXPECT_CALL(mock_action_, SuspendAction()); | 
|  | 252 | action_processor_.SuspendProcessing(); | 
|  | 253 |  | 
|  | 254 | EXPECT_CALL(mock_action_, TerminateProcessing()); | 
|  | 255 | action_processor_.StopProcessing(); | 
|  | 256 | // Stopping the processing should abort the current execution no matter what. | 
|  | 257 | EXPECT_TRUE(delegate_.processing_stopped_called_); | 
|  | 258 | EXPECT_FALSE(delegate_.processing_done_called_); | 
|  | 259 | EXPECT_FALSE(delegate_.action_completed_called_); | 
|  | 260 | EXPECT_FALSE(action_processor_.IsRunning()); | 
|  | 261 | EXPECT_EQ(nullptr, action_processor_.current_action()); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 262 | } | 
|  | 263 |  | 
|  | 264 | }  // namespace chromeos_update_engine |