1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
ash / components / arc / session / arc_session_runner_unittest.cc [blame]
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/components/arc/session/arc_session_runner.h"
#include <memory>
#include <utility>
#include "ash/components/arc/arc_util.h"
#include "ash/components/arc/test/fake_arc_session.h"
#include "base/command_line.h"
#include "base/run_loop.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/task_environment.h"
#include "chromeos/ash/components/dbus/session_manager/session_manager_client.h"
#include "mojo/public/cpp/system/message_pipe.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/abseil-cpp/absl/cleanup/cleanup.h"
namespace arc {
namespace {
constexpr int kContainerStarting =
static_cast<int>(ArcContainerLifetimeEvent::CONTAINER_STARTING);
constexpr int kContainerFailedToStart =
static_cast<int>(ArcContainerLifetimeEvent::CONTAINER_FAILED_TO_START);
constexpr int kContainerCrashedEarly =
static_cast<int>(ArcContainerLifetimeEvent::CONTAINER_CRASHED_EARLY);
constexpr int kContainerCrashed =
static_cast<int>(ArcContainerLifetimeEvent::CONTAINER_CRASHED);
constexpr char kDefaultLocale[] = "en-US";
UpgradeParams DefaultUpgradeParams() {
UpgradeParams params;
params.locale = kDefaultLocale;
return params;
}
class DoNothingObserver : public ArcSessionRunner::Observer {
public:
void OnSessionStopped(ArcStopReason reason, bool restarting) override {
// Do nothing.
}
void OnSessionRestarting() override {
// Do nothing.
}
};
} // namespace
class ArcSessionRunnerTest : public testing::Test,
public ArcSessionRunner::Observer {
public:
ArcSessionRunnerTest()
: task_environment_(base::test::TaskEnvironment::MainThreadType::UI) {}
ArcSessionRunnerTest(const ArcSessionRunnerTest&) = delete;
ArcSessionRunnerTest& operator=(const ArcSessionRunnerTest&) = delete;
void SetUp() override {
ash::SessionManagerClient::InitializeFakeInMemory();
stop_reason_ = ArcStopReason::SHUTDOWN;
restarting_ = false;
stopped_called_ = false;
restarting_called_ = false;
// We inject FakeArcSession here so we do not need task_runner.
arc_session_runner_ = std::make_unique<ArcSessionRunner>(
base::BindRepeating(FakeArcSession::Create));
arc_session_runner_->AddObserver(this);
}
void TearDown() override {
arc_session_runner_->RemoveObserver(this);
arc_session_runner_.reset();
ash::SessionManagerClient::Shutdown();
}
ArcSessionRunner* arc_session_runner() { return arc_session_runner_.get(); }
FakeArcSession* arc_session() {
return static_cast<FakeArcSession*>(
arc_session_runner_->GetArcSessionForTesting());
}
ArcStopReason stop_reason() {
EXPECT_TRUE(stopped_called());
return stop_reason_;
}
bool restarting() {
EXPECT_TRUE(stopped_called());
return restarting_;
}
bool stopped_called() { return stopped_called_; }
bool restarting_called() { return restarting_called_; }
void ResetArcSessionFactory(
const ArcSessionRunner::ArcSessionFactory& factory) {
arc_session_runner_->RemoveObserver(this);
arc_session_runner_ = std::make_unique<ArcSessionRunner>(factory);
arc_session_runner_->AddObserver(this);
}
static std::unique_ptr<ArcSession> CreateSuspendedArcSession() {
auto arc_session = std::make_unique<FakeArcSession>();
arc_session->SuspendBoot();
return arc_session;
}
static std::unique_ptr<ArcSession> CreateBootFailureArcSession(
ArcStopReason reason) {
auto arc_session = std::make_unique<FakeArcSession>();
arc_session->EnableBootFailureEmulation(reason);
return arc_session;
}
private:
// ArcSessionRunner::Observer:
void OnSessionStopped(ArcStopReason stop_reason, bool restarting) override {
// The instance is already destructed in
// ArcSessionRunner::OnSessionStopped().
stop_reason_ = stop_reason;
restarting_ = restarting;
stopped_called_ = true;
restarting_called_ = false;
}
void OnSessionRestarting() override { restarting_called_ = true; }
ArcStopReason stop_reason_;
bool restarting_;
bool stopped_called_;
bool restarting_called_;
std::unique_ptr<ArcSessionRunner> arc_session_runner_;
base::test::TaskEnvironment task_environment_;
};
// Exercises the basic functionality of the ArcSessionRunner. Observer should
// be notified.
TEST_F(ArcSessionRunnerTest, Basic) {
class Observer : public ArcSessionRunner::Observer {
public:
Observer() = default;
Observer(const Observer&) = delete;
Observer& operator=(const Observer&) = delete;
bool stopped_called() const { return stopped_called_; }
// ArcSessionRunner::Observer:
void OnSessionStopped(ArcStopReason reason, bool restarting) override {
stopped_called_ = true;
}
void OnSessionRestarting() override {}
private:
bool stopped_called_ = false;
};
arc_session_runner()->ResumeRunner();
Observer observer;
arc_session_runner()->AddObserver(&observer);
absl::Cleanup teardown = [this, &observer] {
arc_session_runner()->RemoveObserver(&observer);
};
EXPECT_FALSE(arc_session());
arc_session_runner()->RequestUpgrade(DefaultUpgradeParams());
ASSERT_TRUE(arc_session());
EXPECT_TRUE(arc_session()->is_running());
arc_session_runner()->RequestStop();
EXPECT_FALSE(arc_session());
EXPECT_TRUE(observer.stopped_called());
}
// If the ArcSessionRunner accepts a request to stop ARC instance, it should
// stop it, even mid-startup.
TEST_F(ArcSessionRunnerTest, StopMidStartup) {
ResetArcSessionFactory(
base::BindRepeating(&ArcSessionRunnerTest::CreateSuspendedArcSession));
arc_session_runner()->ResumeRunner();
EXPECT_FALSE(arc_session());
arc_session_runner()->RequestUpgrade(DefaultUpgradeParams());
ASSERT_TRUE(arc_session());
EXPECT_FALSE(arc_session()->is_running());
arc_session_runner()->RequestStop();
EXPECT_FALSE(arc_session());
EXPECT_FALSE(restarting());
}
// Does the same for mini instance.
TEST_F(ArcSessionRunnerTest, StopMidStartup_MiniInstance) {
ResetArcSessionFactory(
base::BindRepeating(&ArcSessionRunnerTest::CreateSuspendedArcSession));
arc_session_runner()->ResumeRunner();
EXPECT_FALSE(arc_session());
arc_session_runner()->RequestStartMiniInstance();
ASSERT_TRUE(arc_session());
EXPECT_FALSE(arc_session()->is_running());
arc_session_runner()->RequestStop();
EXPECT_FALSE(arc_session());
}
// Tests the case where the runner is not resumed yet.
TEST_F(ArcSessionRunnerTest, ExpansionPending) {
EXPECT_FALSE(arc_session());
// When it's not resumed yet, starting instance will be pending.
arc_session_runner()->RequestStartMiniInstance();
EXPECT_FALSE(arc_session());
// It is resumed when they are generated.
arc_session_runner()->ResumeRunner();
ASSERT_TRUE(arc_session());
EXPECT_FALSE(arc_session()->is_running());
}
// Does the same test with a full instance.
TEST_F(ArcSessionRunnerTest, ExpansionPending_FullInstance) {
EXPECT_FALSE(arc_session());
arc_session_runner()->RequestStartMiniInstance();
EXPECT_FALSE(arc_session());
arc_session_runner()->RequestUpgrade(DefaultUpgradeParams());
EXPECT_FALSE(arc_session());
arc_session_runner()->ResumeRunner();
ASSERT_TRUE(arc_session());
EXPECT_TRUE(arc_session()->is_running());
}
// Checks the case where RequestStop() is called before resume.
TEST_F(ArcSessionRunnerTest, ExpansionPending_StopMidStartup) {
EXPECT_FALSE(arc_session());
arc_session_runner()->RequestStartMiniInstance();
EXPECT_FALSE(arc_session());
arc_session_runner()->RequestStop();
EXPECT_FALSE(arc_session());
arc_session_runner()->ResumeRunner();
EXPECT_FALSE(arc_session());
}
// If the boot procedure is failed, then restarting mechanism should not
// triggered.
TEST_F(ArcSessionRunnerTest, BootFailure) {
ResetArcSessionFactory(
base::BindRepeating(&ArcSessionRunnerTest::CreateBootFailureArcSession,
ArcStopReason::GENERIC_BOOT_FAILURE));
arc_session_runner()->ResumeRunner();
EXPECT_FALSE(arc_session());
arc_session_runner()->RequestUpgrade(DefaultUpgradeParams());
EXPECT_EQ(ArcStopReason::GENERIC_BOOT_FAILURE, stop_reason());
EXPECT_FALSE(arc_session());
EXPECT_FALSE(restarting());
}
// Does the same with the mini instance.
TEST_F(ArcSessionRunnerTest, BootFailure_MiniInstance) {
ResetArcSessionFactory(
base::BindRepeating(&ArcSessionRunnerTest::CreateBootFailureArcSession,
ArcStopReason::GENERIC_BOOT_FAILURE));
arc_session_runner()->ResumeRunner();
EXPECT_FALSE(arc_session());
// If starting the mini instance fails, arc_session_runner()'s state goes back
// to STOPPED, but its observers won't be notified.
arc_session_runner()->RequestStartMiniInstance();
arc_session()->EmulateMiniContainerStart();
EXPECT_FALSE(arc_session());
EXPECT_FALSE(stopped_called());
// Also make sure that RequestUpgrade() works just fine after the boot
// failure.
ResetArcSessionFactory(base::BindRepeating(FakeArcSession::Create));
arc_session_runner()->ResumeRunner();
arc_session_runner()->RequestUpgrade(DefaultUpgradeParams());
ASSERT_TRUE(arc_session());
EXPECT_TRUE(arc_session()->is_running());
}
// Similary, CRASH should do same for GENERIC_BOOT_FAILURE case, because
// in mini instance, Mojo connection should not be established.
TEST_F(ArcSessionRunnerTest, Crash_MiniInstance) {
ResetArcSessionFactory(
base::BindRepeating(&ArcSessionRunnerTest::CreateBootFailureArcSession,
ArcStopReason::CRASH));
arc_session_runner()->ResumeRunner();
EXPECT_FALSE(arc_session());
// If starting the mini instance fails, arc_session_runner()'s state goes back
// to STOPPED, but its observers won't be notified.
arc_session_runner()->RequestStartMiniInstance();
arc_session()->EmulateMiniContainerStart();
EXPECT_FALSE(arc_session());
EXPECT_FALSE(stopped_called());
}
// Tests that RequestUpgrade works after calling RequestStart.
TEST_F(ArcSessionRunnerTest, Upgrade) {
arc_session_runner()->ResumeRunner();
EXPECT_FALSE(arc_session());
arc_session_runner()->RequestStartMiniInstance();
ASSERT_TRUE(arc_session());
EXPECT_FALSE(arc_session()->is_running());
arc_session_runner()->RequestUpgrade(DefaultUpgradeParams());
ASSERT_TRUE(arc_session());
EXPECT_TRUE(arc_session()->is_running());
}
// If the instance is stopped, it should be re-started.
TEST_F(ArcSessionRunnerTest, Restart) {
arc_session_runner()->ResumeRunner();
arc_session_runner()->SetRestartDelayForTesting(base::TimeDelta());
EXPECT_FALSE(arc_session());
arc_session_runner()->RequestUpgrade(DefaultUpgradeParams());
ASSERT_TRUE(arc_session());
EXPECT_TRUE(arc_session()->is_running());
// Simulate a connection loss.
ASSERT_TRUE(arc_session());
arc_session()->StopWithReason(ArcStopReason::CRASH);
EXPECT_FALSE(arc_session());
EXPECT_TRUE(restarting());
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(restarting_called());
ASSERT_TRUE(arc_session());
EXPECT_TRUE(arc_session()->is_running());
// Checks if the restart retains the original parameter.
EXPECT_EQ(std::string(kDefaultLocale), arc_session()->upgrade_locale_param());
arc_session_runner()->RequestStop();
EXPECT_FALSE(arc_session());
}
TEST_F(ArcSessionRunnerTest, GracefulStop) {
arc_session_runner()->ResumeRunner();
arc_session_runner()->SetRestartDelayForTesting(base::TimeDelta());
EXPECT_FALSE(arc_session());
arc_session_runner()->RequestUpgrade(DefaultUpgradeParams());
ASSERT_TRUE(arc_session());
EXPECT_TRUE(arc_session()->is_running());
// Graceful stop.
arc_session_runner()->RequestStop();
EXPECT_EQ(ArcStopReason::SHUTDOWN, stop_reason());
EXPECT_FALSE(restarting());
EXPECT_FALSE(restarting_called());
EXPECT_FALSE(arc_session());
}
TEST_F(ArcSessionRunnerTest, Shutdown) {
arc_session_runner()->ResumeRunner();
arc_session_runner()->SetRestartDelayForTesting(base::TimeDelta());
EXPECT_FALSE(arc_session());
arc_session_runner()->RequestUpgrade(DefaultUpgradeParams());
ASSERT_TRUE(arc_session());
EXPECT_TRUE(arc_session()->is_running());
// Simulate shutdown.
arc_session_runner()->OnShutdown();
EXPECT_EQ(ArcStopReason::SHUTDOWN, stop_reason());
EXPECT_FALSE(arc_session());
}
// Removing the same observer more than once should be okay.
TEST_F(ArcSessionRunnerTest, RemoveObserverTwice) {
arc_session_runner()->ResumeRunner();
EXPECT_FALSE(arc_session());
DoNothingObserver do_nothing_observer;
arc_session_runner()->AddObserver(&do_nothing_observer);
// Call RemoveObserver() twice.
arc_session_runner()->RemoveObserver(&do_nothing_observer);
arc_session_runner()->RemoveObserver(&do_nothing_observer);
}
// Removing an unknown observer should be allowed.
TEST_F(ArcSessionRunnerTest, RemoveUnknownObserver) {
arc_session_runner()->ResumeRunner();
EXPECT_FALSE(arc_session());
DoNothingObserver do_nothing_observer;
arc_session_runner()->RemoveObserver(&do_nothing_observer);
}
// Tests UMA recording on mini instance -> full instance -> shutdown case.
TEST_F(ArcSessionRunnerTest, UmaRecording_StartUpgradeShutdown) {
base::HistogramTester tester;
arc_session_runner()->ResumeRunner();
arc_session_runner()->RequestStartMiniInstance();
tester.ExpectUniqueSample("Arc.ContainerLifetimeEvent", kContainerStarting,
1 /* count of the sample */);
// Boot continue should not increase the count.
arc_session_runner()->RequestUpgrade(DefaultUpgradeParams());
tester.ExpectUniqueSample("Arc.ContainerLifetimeEvent", kContainerStarting,
1);
// "0" should be recorded as a restart count on shutdown.
arc_session_runner()->OnShutdown();
tester.ExpectUniqueSample("Arc.ContainerRestartAfterCrashCount",
0 /* sample */, 1 /* count of the sample */);
}
// Tests UMA recording on full instance -> shutdown case.
TEST_F(ArcSessionRunnerTest, UmaRecording_StartShutdown) {
base::HistogramTester tester;
arc_session_runner()->ResumeRunner();
arc_session_runner()->RequestUpgrade(DefaultUpgradeParams());
tester.ExpectUniqueSample("Arc.ContainerLifetimeEvent", kContainerStarting,
1);
// "0" should be recorded as a restart count on shutdown.
arc_session_runner()->OnShutdown();
tester.ExpectUniqueSample("Arc.ContainerRestartAfterCrashCount", 0, 1);
}
// Tests UMA recording on mini instance -> full instance -> crash -> shutdown
// case.
TEST_F(ArcSessionRunnerTest, UmaRecording_CrashTwice) {
base::HistogramTester tester;
arc_session_runner()->ResumeRunner();
arc_session_runner()->SetRestartDelayForTesting(base::TimeDelta());
EXPECT_FALSE(arc_session());
arc_session_runner()->RequestStartMiniInstance();
tester.ExpectUniqueSample("Arc.ContainerLifetimeEvent", kContainerStarting,
1);
arc_session_runner()->RequestUpgrade(DefaultUpgradeParams());
// Stop the instance with CRASH.
arc_session()->StopWithReason(ArcStopReason::CRASH);
tester.ExpectBucketCount("Arc.ContainerLifetimeEvent", kContainerCrashed, 1);
tester.ExpectTotalCount("Arc.ContainerLifetimeEvent", 2);
// Restart the instance, then crash the instance again. The second CRASH
// should not affect Arc.ContainerLifetimeEvent.
base::RunLoop().RunUntilIdle();
arc_session()->StopWithReason(ArcStopReason::CRASH);
tester.ExpectBucketCount("Arc.ContainerLifetimeEvent", kContainerCrashed, 1);
tester.ExpectTotalCount("Arc.ContainerLifetimeEvent", 2);
// However, "2" should be recorded as a restart count on shutdown.
base::RunLoop().RunUntilIdle();
arc_session_runner()->OnShutdown();
tester.ExpectUniqueSample("Arc.ContainerRestartAfterCrashCount", 2, 1);
}
// Tests UMA recording on mini instance -> crash -> shutdown case.
TEST_F(ArcSessionRunnerTest, UmaRecording_CrashMini) {
base::HistogramTester tester;
arc_session_runner()->ResumeRunner();
arc_session_runner()->RequestStartMiniInstance();
tester.ExpectUniqueSample("Arc.ContainerLifetimeEvent", kContainerStarting,
1);
// Stop the instance with CRASH.
arc_session()->StopWithReason(ArcStopReason::CRASH);
tester.ExpectBucketCount("Arc.ContainerLifetimeEvent", kContainerCrashedEarly,
1);
tester.ExpectTotalCount("Arc.ContainerLifetimeEvent", 2);
// In this case, no restart happened. "0" should be recorded.
arc_session_runner()->OnShutdown();
tester.ExpectUniqueSample("Arc.ContainerRestartAfterCrashCount", 0, 1);
}
// Tests UMA recording on mini instance -> boot fail -> shutdown case.
TEST_F(ArcSessionRunnerTest, UmaRecording_BootFail) {
base::HistogramTester tester;
arc_session_runner()->ResumeRunner();
arc_session_runner()->RequestStartMiniInstance();
tester.ExpectUniqueSample("Arc.ContainerLifetimeEvent", kContainerStarting,
1);
arc_session()->StopWithReason(ArcStopReason::GENERIC_BOOT_FAILURE);
tester.ExpectBucketCount("Arc.ContainerLifetimeEvent",
kContainerFailedToStart, 1);
tester.ExpectTotalCount("Arc.ContainerLifetimeEvent", 2);
// No restart happened. "0" should be recorded.
arc_session_runner()->OnShutdown();
tester.ExpectUniqueSample("Arc.ContainerRestartAfterCrashCount", 0, 1);
}
// Tests UMA recording on full instance -> low disk -> shutdown case.
TEST_F(ArcSessionRunnerTest, UmaRecording_LowDisk) {
base::HistogramTester tester;
arc_session_runner()->ResumeRunner();
arc_session_runner()->RequestUpgrade(DefaultUpgradeParams());
tester.ExpectUniqueSample("Arc.ContainerLifetimeEvent", kContainerStarting,
1);
// We don't record UMA for LOW_DISK_SPACE.
arc_session()->StopWithReason(ArcStopReason::LOW_DISK_SPACE);
tester.ExpectUniqueSample("Arc.ContainerLifetimeEvent", kContainerStarting,
1);
// No restart happened. "0" should be recorded.
arc_session_runner()->OnShutdown();
tester.ExpectUniqueSample("Arc.ContainerRestartAfterCrashCount", 0, 1);
}
} // namespace arc