From 053216284bf2ad2b68a92a7f5b7c90d0ecb6e3b0 Mon Sep 17 00:00:00 2001
From: Bryan Henry <bryanhenry@google.com>
Date: Sat, 11 May 2019 19:26:55 +0000
Subject: [PATCH] [component_manager] Split into lib & bin targets to improve
 testability

A future change will move some of the startup code out of main() to
enable testing it in multiple different environments.

Test: Refactor, no change in behavior. Covered by existing tests.

Change-Id: I1fb82522268acdc0265aba6490230fb4404e995f
---
 build/rust/rustc_artifact.gni                 |  2 ++
 src/sys/component_manager/BUILD.gn            | 25 ++++++++++++++++--
 .../src/elf_runner/library_loader.rs          |  1 -
 .../src/fuchsia_boot_resolver.rs              |  1 -
 .../src/fuchsia_pkg_resolver.rs               |  1 -
 src/sys/component_manager/src/lib.rs          | 16 ++++++++++++
 src/sys/component_manager/src/main.rs         | 26 +++++--------------
 src/sys/component_manager/src/model/model.rs  |  1 -
 .../component_manager/src/model/namespace.rs  |  2 +-
 .../component_manager/src/model/routing.rs    |  2 +-
 .../src/model/tests/routing_test_helpers.rs   |  1 -
 src/sys/component_manager/src/ns_util.rs      |  2 +-
 12 files changed, 51 insertions(+), 29 deletions(-)
 create mode 100644 src/sys/component_manager/src/lib.rs

diff --git a/build/rust/rustc_artifact.gni b/build/rust/rustc_artifact.gni
index 302d7f4c24f..ec9cb517376 100644
--- a/build/rust/rustc_artifact.gni
+++ b/build/rust/rustc_artifact.gni
@@ -324,6 +324,8 @@ template("rustc_artifact") {
   extra_inputs = [ zircon_lib_dirs_file ]
   generated_file("${build_target_name}_zircon_libs") {
     forward_variables_from(invoker, [ "testonly" ])
+    # Clear possibly inherited visibility before setting our own.
+    visibility = []
     visibility = [ ":$build_target_name" ]
     deps = [
       third_party_build,
diff --git a/src/sys/component_manager/BUILD.gn b/src/sys/component_manager/BUILD.gn
index 46af162c365..c82352bf90b 100644
--- a/src/sys/component_manager/BUILD.gn
+++ b/src/sys/component_manager/BUILD.gn
@@ -4,14 +4,18 @@
 
 import("//build/package.gni")
 import("//build/rust/rustc_binary.gni")
+import("//build/rust/rustc_library.gni")
 import("//build/test/test_package.gni")
 import("//build/testing/environments.gni")
 
-rustc_binary("bin") {
-  name = "component_manager"
+rustc_library("lib") {
+  name = "component_manager_lib"
   with_unit_tests = true
   edition = "2018"
 
+  # Only for internal use.
+  visibility = [ "//src/sys/component_manager/*" ]
+
   deps = [
     "//garnet/examples/fidl/services:echo-rustc",
     "//garnet/lib/rust/cm_fidl_translator",
@@ -45,6 +49,23 @@ rustc_binary("bin") {
   ]
 }
 
+rustc_binary("bin") {
+  name = "component_manager"
+  with_unit_tests = true
+  edition = "2018"
+
+  deps = [
+    ":lib",
+    "//garnet/public/lib/fidl/rust/fidl",
+    "//garnet/public/rust/fuchsia-async",
+    "//garnet/public/rust/fuchsia-component",
+    "//sdk/fidl/fuchsia.pkg:fuchsia.pkg-rustc",
+    "//third_party/rust_crates:failure",
+    "//third_party/rust_crates:futures-preview",
+    "//third_party/rust_crates:log",
+  ]
+}
+
 # This manifest is consumed by the ZBI rule in //build/images to add component_manager to bootfs.
 generate_manifest("component_manager.bootfs") {
   deps = [
diff --git a/src/sys/component_manager/src/elf_runner/library_loader.rs b/src/sys/component_manager/src/elf_runner/library_loader.rs
index 3aa90e00d04..69cec6be7a6 100644
--- a/src/sys/component_manager/src/elf_runner/library_loader.rs
+++ b/src/sys/component_manager/src/elf_runner/library_loader.rs
@@ -10,7 +10,6 @@ use {
     fidl_fuchsia_ldsvc::{LoaderRequest, LoaderRequestStream},
     fuchsia_async as fasync, fuchsia_zircon as zx,
     futures::{TryFutureExt, TryStreamExt},
-    io_util,
     log::*,
     std::collections::HashMap,
     std::path::PathBuf,
diff --git a/src/sys/component_manager/src/fuchsia_boot_resolver.rs b/src/sys/component_manager/src/fuchsia_boot_resolver.rs
index 6609ffb050e..0891669c4fe 100644
--- a/src/sys/component_manager/src/fuchsia_boot_resolver.rs
+++ b/src/sys/component_manager/src/fuchsia_boot_resolver.rs
@@ -9,7 +9,6 @@ use {
     fidl_fuchsia_sys2 as fsys,
     fuchsia_uri::boot_uri::BootUri,
     futures::future::FutureObj,
-    io_util,
     std::path::PathBuf,
 };
 
diff --git a/src/sys/component_manager/src/fuchsia_pkg_resolver.rs b/src/sys/component_manager/src/fuchsia_pkg_resolver.rs
index 6a0c887145f..6fa3022b1a7 100644
--- a/src/sys/component_manager/src/fuchsia_pkg_resolver.rs
+++ b/src/sys/component_manager/src/fuchsia_pkg_resolver.rs
@@ -13,7 +13,6 @@ use {
     fuchsia_uri::pkg_uri::PkgUri,
     fuchsia_zircon as zx,
     futures::future::FutureObj,
-    io_util,
     std::path::PathBuf,
 };
 
diff --git a/src/sys/component_manager/src/lib.rs b/src/sys/component_manager/src/lib.rs
new file mode 100644
index 00000000000..4ff203cbabd
--- /dev/null
+++ b/src/sys/component_manager/src/lib.rs
@@ -0,0 +1,16 @@
+// Copyright 2019 The Fuchsia Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#![feature(async_await, await_macro)]
+// This is needed for the pseudo_directory nesting in crate::model::tests
+#![recursion_limit = "128"]
+
+pub mod elf_runner;
+pub mod fuchsia_boot_resolver;
+pub mod fuchsia_pkg_resolver;
+pub mod klog;
+pub mod model;
+
+mod directory_broker;
+mod ns_util;
diff --git a/src/sys/component_manager/src/main.rs b/src/sys/component_manager/src/main.rs
index 89ed8a9a118..0b5db3c18c3 100644
--- a/src/sys/component_manager/src/main.rs
+++ b/src/sys/component_manager/src/main.rs
@@ -3,33 +3,21 @@
 // found in the LICENSE file.
 
 #![feature(async_await, await_macro)]
-#![deny(warnings)]
-// Temporarily allow dead code during early development since not all
-// features are fully exercised.
-#![allow(dead_code)]
-// This is needed for the pseudo_directory nesting in crate::model::tests
-#![recursion_limit = "128"]
-
-mod directory_broker;
-mod elf_runner;
-mod fuchsia_boot_resolver;
-mod fuchsia_pkg_resolver;
-mod klog;
-mod model;
-mod ns_util;
 
 use {
-    elf_runner::ElfRunner,
+    component_manager_lib::{
+        elf_runner::ElfRunner,
+        fuchsia_boot_resolver::{self, FuchsiaBootResolver},
+        fuchsia_pkg_resolver::{self, FuchsiaPkgResolver},
+        klog,
+        model::{AbsoluteMoniker, Model, ModelParams, ResolverRegistry},
+    },
     failure::{self, Error, ResultExt},
     fidl_fuchsia_pkg::PackageResolverMarker,
     fuchsia_async as fasync,
-    fuchsia_boot_resolver::FuchsiaBootResolver,
     fuchsia_component::client::connect_to_service,
-    fuchsia_pkg_resolver::FuchsiaPkgResolver,
     futures::prelude::*,
-    io_util,
     log::*,
-    model::{AbsoluteMoniker, Model, ModelParams, ResolverRegistry},
     std::env,
     std::process,
     std::sync::Arc,
diff --git a/src/sys/component_manager/src/model/model.rs b/src/sys/component_manager/src/model/model.rs
index 1c868ce289c..6eb1e855ee2 100644
--- a/src/sys/component_manager/src/model/model.rs
+++ b/src/sys/component_manager/src/model/model.rs
@@ -14,7 +14,6 @@ use {
         future::{join_all, FutureObj},
         lock::Mutex,
     },
-    io_util,
     std::sync::Arc,
 };
 
diff --git a/src/sys/component_manager/src/model/namespace.rs b/src/sys/component_manager/src/model/namespace.rs
index bfc0fd825a4..d4b156c63f8 100644
--- a/src/sys/component_manager/src/model/namespace.rs
+++ b/src/sys/component_manager/src/model/namespace.rs
@@ -5,7 +5,7 @@
 use {
     crate::model::*,
     crate::ns_util::PKG_PATH,
-    crate::{directory_broker, io_util},
+    crate::directory_broker,
     cm_rust::{self, ComponentDecl, UseDirectoryDecl, UseServiceDecl},
     fidl::endpoints::{create_endpoints, ClientEnd, ServerEnd},
     fidl_fuchsia_io::{DirectoryProxy, NodeMarker, MODE_TYPE_DIRECTORY, OPEN_RIGHT_READABLE},
diff --git a/src/sys/component_manager/src/model/routing.rs b/src/sys/component_manager/src/model/routing.rs
index 9acecc5f7b9..88cedb0f166 100644
--- a/src/sys/component_manager/src/model/routing.rs
+++ b/src/sys/component_manager/src/model/routing.rs
@@ -7,7 +7,7 @@ use {
     cm_rust::{self, Capability, ExposeDecl, ExposeSource, OfferDecl, OfferSource},
     failure::format_err,
     fidl_fuchsia_io::{MODE_TYPE_DIRECTORY, MODE_TYPE_SERVICE, OPEN_RIGHT_READABLE},
-    fuchsia_zircon as zx, io_util,
+    fuchsia_zircon as zx,
     std::sync::Arc,
 };
 
diff --git a/src/sys/component_manager/src/model/tests/routing_test_helpers.rs b/src/sys/component_manager/src/model/tests/routing_test_helpers.rs
index e8ee6da16a5..84aa3426dae 100644
--- a/src/sys/component_manager/src/model/tests/routing_test_helpers.rs
+++ b/src/sys/component_manager/src/model/tests/routing_test_helpers.rs
@@ -23,7 +23,6 @@ use {
     fuchsia_zircon::HandleBased,
     futures::lock::Mutex,
     futures::TryStreamExt,
-    io_util,
     std::{
         collections::{HashMap, HashSet},
         convert::TryFrom,
diff --git a/src/sys/component_manager/src/ns_util.rs b/src/sys/component_manager/src/ns_util.rs
index e1f1a8d1acf..49d39db5786 100644
--- a/src/sys/component_manager/src/ns_util.rs
+++ b/src/sys/component_manager/src/ns_util.rs
@@ -6,7 +6,7 @@ use {
     failure::{err_msg, Error},
     fidl::endpoints::ClientEnd,
     fidl_fuchsia_io::DirectoryProxy,
-    fidl_fuchsia_sys2 as fsys, io_util,
+    fidl_fuchsia_sys2 as fsys,
     lazy_static::lazy_static,
     std::collections::HashMap,
     std::path::PathBuf,
-- 
GitLab