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
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/crostini/ansible/ansible_management_service.h"
#include "base/memory/raw_ptr.h"
#include "base/test/bind.h"
#include "base/test/test_future.h"
#include "chrome/browser/ash/crostini/ansible/ansible_management_service_factory.h"
#include "chrome/browser/ash/crostini/ansible/ansible_management_test_helper.h"
#include "chrome/browser/ash/crostini/crostini_pref_names.h"
#include "chrome/browser/ash/crostini/crostini_test_util.h"
#include "chrome/browser/ui/views/crostini/crostini_ansible_software_config_view.h"
#include "chrome/test/base/testing_profile.h"
#include "chromeos/ash/components/dbus/chunneld/chunneld_client.h"
#include "chromeos/ash/components/dbus/cicerone/cicerone_client.h"
#include "chromeos/ash/components/dbus/concierge/concierge_client.h"
#include "chromeos/ash/components/dbus/seneschal/seneschal_client.h"
#include "components/prefs/pref_service.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using base::test::TestFuture;
namespace crostini {
class AnsibleManagementServiceTest : public testing::Test,
public AnsibleManagementService::Observer {
public:
AnsibleManagementServiceTest() {
ash::ChunneldClient::InitializeFake();
ash::CiceroneClient::InitializeFake();
ash::ConciergeClient::InitializeFake();
ash::SeneschalClient::InitializeFake();
profile_ = std::make_unique<TestingProfile>();
crostini_manager_ = CrostiniManager::GetForProfile(profile_.get());
ansible_management_service_ =
AnsibleManagementServiceFactory::GetForProfile(profile_.get());
test_helper_ =
std::make_unique<AnsibleManagementTestHelper>(profile_.get());
test_helper_->SetUpAnsiblePlaybookPreference();
SetUpViewsEnvironmentForTesting();
}
AnsibleManagementServiceTest(const AnsibleManagementServiceTest&) = delete;
AnsibleManagementServiceTest& operator=(const AnsibleManagementServiceTest&) =
delete;
~AnsibleManagementServiceTest() override {
TearDownViewsEnvironmentForTesting();
test_helper_.reset();
ansible_management_service_->Shutdown();
crostini_manager_->Shutdown();
profile_.reset();
ash::SeneschalClient::Shutdown();
ash::ConciergeClient::Shutdown();
ash::CiceroneClient::Shutdown();
ash::ChunneldClient::Shutdown();
}
void SetUp() override {
is_install_ansible_success_ = true;
is_apply_ansible_success_ = true;
ansible_management_service_->AddObserver(this);
}
void TearDown() override {
ansible_management_service_->RemoveObserver(this);
}
CrostiniAnsibleSoftwareConfigView* ActiveView(
const guest_os::GuestId& container_id) {
if (ansible_management_service_->GetDialogWidgetForTesting(container_id)) {
return (CrostiniAnsibleSoftwareConfigView*)ansible_management_service_
->GetDialogWidgetForTesting(container_id)
->widget_delegate();
} else {
return nullptr;
}
}
// AnsibleManagementService::Observer
void OnAnsibleSoftwareConfigurationStarted(
const guest_os::GuestId& container_id) override {}
void OnAnsibleSoftwareConfigurationFinished(
const guest_os::GuestId& container_id,
bool success) override {}
void OnAnsibleSoftwareConfigurationUiPrompt(
const guest_os::GuestId& container_id,
bool interactive) override {
if (interactive) {
// Press retry/ok on dialog if it's waiting for input.
ActiveView(container_id)->Accept();
}
}
void OnAnsibleSoftwareInstall(
const guest_os::GuestId& container_id) override {
if (is_install_ansible_success_) {
test_helper_->SendSucceededInstallSignal();
} else {
test_helper_->SendFailedInstallSignal();
}
}
void OnApplyAnsiblePlaybook(const guest_os::GuestId& container_id) override {
if (is_apply_ansible_success_) {
test_helper_->SendSucceededApplySignal();
} else {
test_helper_->SendFailedApplySignal();
}
}
private:
raw_ptr<CrostiniManager, DanglingUntriaged> crostini_manager_;
raw_ptr<AnsibleManagementService, DanglingUntriaged>
ansible_management_service_;
bool is_install_ansible_success_;
bool is_apply_ansible_success_;
protected:
AnsibleManagementService* ansible_management_service() {
return ansible_management_service_;
}
void SetInstallAnsibleStatus(bool status) {
is_install_ansible_success_ = status;
}
void SetApplyAnsibleStatus(bool status) {
is_apply_ansible_success_ = status;
}
content::BrowserTaskEnvironment task_environment_;
std::unique_ptr<AnsibleManagementTestHelper> test_helper_;
std::unique_ptr<TestingProfile> profile_;
base::WeakPtrFactory<AnsibleManagementServiceTest> weak_ptr_factory_{this};
};
TEST_F(AnsibleManagementServiceTest, ConfigureContainerSuccess) {
test_helper_->SetUpAnsibleInstallation(
vm_tools::cicerone::InstallLinuxPackageResponse::STARTED);
test_helper_->SetUpPlaybookApplication(
vm_tools::cicerone::ApplyAnsiblePlaybookResponse::STARTED);
TestFuture<bool> result_future;
ansible_management_service()->ConfigureContainer(
DefaultContainerId(),
profile_->GetPrefs()->GetFilePath(
prefs::kCrostiniAnsiblePlaybookFilePath),
result_future.GetCallback());
EXPECT_TRUE(result_future.Get());
}
TEST_F(AnsibleManagementServiceTest, ConfigureContainerInstallFail) {
test_helper_->SetUpAnsibleInstallation(
vm_tools::cicerone::InstallLinuxPackageResponse::FAILED);
TestFuture<bool> result_future;
ansible_management_service()->ConfigureContainer(
DefaultContainerId(),
profile_->GetPrefs()->GetFilePath(
prefs::kCrostiniAnsiblePlaybookFilePath),
result_future.GetCallback());
EXPECT_FALSE(result_future.Get());
}
TEST_F(AnsibleManagementServiceTest, ConfigureContainerInstallSignalFail) {
test_helper_->SetUpAnsibleInstallation(
vm_tools::cicerone::InstallLinuxPackageResponse::STARTED);
SetInstallAnsibleStatus(false);
TestFuture<bool> result_future;
ansible_management_service()->ConfigureContainer(
DefaultContainerId(),
profile_->GetPrefs()->GetFilePath(
prefs::kCrostiniAnsiblePlaybookFilePath),
result_future.GetCallback());
EXPECT_FALSE(result_future.Get());
}
TEST_F(AnsibleManagementServiceTest, ConfigureContainerApplyFail) {
test_helper_->SetUpAnsibleInstallation(
vm_tools::cicerone::InstallLinuxPackageResponse::STARTED);
test_helper_->SetUpPlaybookApplication(
vm_tools::cicerone::ApplyAnsiblePlaybookResponse::FAILED);
TestFuture<bool> result_future;
ansible_management_service()->ConfigureContainer(
DefaultContainerId(),
profile_->GetPrefs()->GetFilePath(
prefs::kCrostiniAnsiblePlaybookFilePath),
result_future.GetCallback());
EXPECT_FALSE(result_future.Get());
}
TEST_F(AnsibleManagementServiceTest, ConfigureContainerApplySignalFail) {
test_helper_->SetUpAnsibleInstallation(
vm_tools::cicerone::InstallLinuxPackageResponse::STARTED);
test_helper_->SetUpPlaybookApplication(
vm_tools::cicerone::ApplyAnsiblePlaybookResponse::STARTED);
SetApplyAnsibleStatus(false);
TestFuture<bool> result_future;
ansible_management_service()->ConfigureContainer(
DefaultContainerId(),
profile_->GetPrefs()->GetFilePath(
prefs::kCrostiniAnsiblePlaybookFilePath),
result_future.GetCallback());
EXPECT_FALSE(result_future.Get());
}
TEST_F(AnsibleManagementServiceTest,
CouldNotConfigureContainerAfterSuccessfullConfiguration) {
test_helper_->SetUpAnsibleInstallation(
vm_tools::cicerone::InstallLinuxPackageResponse::STARTED);
test_helper_->SetUpPlaybookApplication(
vm_tools::cicerone::ApplyAnsiblePlaybookResponse::STARTED);
TestFuture<bool> result_future;
ansible_management_service()->ConfigureContainer(
DefaultContainerId(),
profile_->GetPrefs()->GetFilePath(
prefs::kCrostiniAnsiblePlaybookFilePath),
result_future.GetCallback());
EXPECT_TRUE(result_future.Get());
TestFuture<bool> result_future2;
ansible_management_service()->ConfigureContainer(
DefaultContainerId(),
profile_->GetPrefs()->GetFilePath(
prefs::kCrostiniAnsiblePlaybookFilePath),
result_future2.GetCallback());
EXPECT_FALSE(result_future2.Get());
}
TEST_F(AnsibleManagementServiceTest,
CouldConfigureContainerAfterFailedConfiguration) {
test_helper_->SetUpAnsibleInstallation(
vm_tools::cicerone::InstallLinuxPackageResponse::FAILED);
TestFuture<bool> result_future;
// Unsuccessful sequence of events.
ansible_management_service()->ConfigureContainer(
DefaultContainerId(),
profile_->GetPrefs()->GetFilePath(
prefs::kCrostiniAnsiblePlaybookFilePath),
result_future.GetCallback());
EXPECT_FALSE(result_future.Get());
TestFuture<bool> result_future2;
test_helper_->SetUpAnsibleInstallation(
vm_tools::cicerone::InstallLinuxPackageResponse::STARTED);
test_helper_->SetUpPlaybookApplication(
vm_tools::cicerone::ApplyAnsiblePlaybookResponse::STARTED);
ansible_management_service()->ConfigureContainer(
DefaultContainerId(),
profile_->GetPrefs()->GetFilePath(
prefs::kCrostiniAnsiblePlaybookFilePath),
result_future2.GetCallback());
EXPECT_TRUE(result_future2.Get());
}
} // namespace crostini
|