Realm Builder

Realm Builder 程式庫允許在執行階段建構運作範圍和個別測試案例專用的模擬元件,協助您進行元件的整合測試。

如果測試想要啟動子項元件,Realm Builder 可能非常適合協助進行測試。

如果測試要因每個測試案例量身打造專屬的運作範圍,或為每個測試案例包含模擬元件的運作範圍而受益,那麼使用靜態元件資訊清單或許能更輕鬆實作、瞭解及維護測試。如果測試確實呼叫上述兩者之一 (或兩者皆有),就很適合使用 Realm Builder。

Realm Builder 程式庫支援多種語言,且每種語言可用的確切語意和功能可能有所不同。如需功能與支援語言的完整清單,請參閱語言功能矩陣

新增依附元件

Realm Builder 用戶端程式庫需要特殊功能才能運作。因此,使用這個程式庫的測試必須在測試元件的資訊清單中加入必要的資料分割:

include: [
    "sys/component/realm_builder.shard.cml",
    // ...
],

接著,您應該為測試的語言新增 GN 依附元件:

Rust

將 Rust Realm Builder 程式庫新增至 BUILD.gn 檔案

deps = [
  "//src/lib/fuchsia-component-test",

  # ...
]

C++

將 C++ Realm Builder 程式庫新增至 BUILD.gn 檔案

deps = [
  "//sdk/lib/sys/component/cpp/testing:cpp",

  # ...
]

初始化運作範圍建構工具

新增必要的依附元件後,請在測試元件中初始化 Realm Builder。

建議:為每個測試案例初始化獨立的建構工具執行個體。

不建議使用:在所有測試案例之間使用共用建構工具執行個體。

Rust

本節假設您要編寫非同步測試,且元件的某些部分看起來會像這樣:

#[fuchsia::test]
async fn test() -> Result<(), Error> {
    // ...
}

匯入 Realm Builder 程式庫

use {
    // ...
    fuchsia_component_test::{
        Capability, ChildOptions, LocalComponentHandles, RealmBuilder, Ref, Route,
    },
    futures::{StreamExt, TryStreamExt},
};

初始化 RealmBuilder 結構

為測試中的每個測試案例建立新的 RealmBuilder 執行個體。這會建立不重複的獨立子項領域,確保單一測試案例的副作用不會影響其他測試案例的副作用。

let builder = RealmBuilder::new().await?;

C++

本節假設您要編寫非同步測試,且測試會在訊息迴圈中執行。這類情況通常如下所示:

#include <lib/async-loop/cpp/loop.h>
#include <lib/async-loop/default.h>

TEST(SampleTest, CallEcho) {
    async::Loop loop(&kAsyncLoopConfigAttachToCurrentThread);
    // Test code below
}

匯入 Realm Builder 程式庫

#include <lib/sys/component/cpp/testing/realm_builder.h>

使用程式庫命名空間 此為選用步驟。它會匯入整個程式庫的命名空間,方便您編寫及讀取測試。

// NOLINTNEXTLINE
using namespace component_testing;

初始化 Realm::Builder 類別

為測試中的每個測試案例建立新的 Realm::Builder 執行個體。這會建立不重複的獨立子項領域,確保單一測試案例的副作用不會影響其他測試案例的副作用。

auto builder = RealmBuilder::Create();

建構運作範圍

使用已建構的目標 Realm Builder 物件後,您現在可以開始組合領域。

使用 Realm Builder 執行個體,透過語言的新增元件函式,在領域中新增子元件。每個子項元件都需要以下項目:

  1. 元件名稱:領域內元件的專屬 ID。如果是靜態元件,這會對應至元件資訊清單的 children 區段中所列執行個體的 name 屬性。
  2. 元件來源:定義建立領域時建立元件的方式。如為靜態元件,這個值應為包含有效元件網址URL。這會對應至元件資訊清單 children 區段中所列出執行個體的 url 屬性。

以下範例會將兩個靜態子項元件新增至已建立的領域:

  • 元件 echo_server 會從絕對元件網址載入
  • 從相對元件網址載入 echo_client 元件

Rust

// Add component to the realm, which is fetched using a URL.
let echo_server = builder
    .add_child(
        "echo_server",
        "fuchsia-pkg://fuchsia.com/realm-builder-examples#meta/echo_server.cm",
        ChildOptions::new(),
    )
    .await?;
// Add component to the realm, which is fetched using a fragment-only URL.
// The child is not exposing a service, so the `eager` option ensures the
// child starts when the realm is built.
let echo_client = builder
    .add_child("echo_client", "#meta/echo_client.cm", ChildOptions::new().eager())
    .await?;

C++

// Add component server to the realm, which is fetched using a URL.
builder.AddChild("echo_server",
                 "fuchsia-pkg://fuchsia.com/realm-builder-examples#meta/echo_server.cm");
// Add component to the realm, which is fetched using a fragment-only URL. The
// child is not exposing a service, so the `EAGER` option ensures the child
// starts when the realm is built.
builder.AddChild("echo_client", "#meta/echo_client.cm",
                 ChildOptions{.startup_mode = StartupMode::EAGER});

新增模擬元件

模擬元件可讓測試提供本機實作,做為專屬元件。Realm Builder 會實作通訊協定,讓元件架構將本機實作視為元件,並處理傳入的 FIDL 連線。本機實作可以保留使用測試案例特有的狀態,讓每個建構的領域都有特定用途的模擬。

以下範例是實作 fidl.examples.routing.echo.Echo 通訊協定的模擬元件。

首先,您必須實作模擬元件。

Rust

在 Rust 中,模擬元件是透過具有下列簽名的函式實作:

/// The implementation for a mock component. The contained function is called when the framework
/// asks the mock to run, and the function is given the component's handles for its namespace and
/// outgoing directory. The mock component may then use this handles to run a ServiceFs, access
/// capabilities as the mock, or perform other such actions.
#[derive(Clone)]
pub struct Mock(
    Arc<dyn Fn(MockHandles) -> BoxFuture<'static, Result<(), Error>> + Sync + Send + 'static>,
);

MockHandles 是一個結構體,包含元件傳入和傳出功能的控制代碼:

/// The handles from the framework over which the mock should interact with other components.
pub struct MockHandles {
    namespace: HashMap<String, fio::DirectoryProxy>,

    /// The outgoing directory handle for a mock component. This can be used to run a ServiceFs for
    /// the mock.
    pub outgoing_dir: ServerEnd<fio::DirectoryMarker>,
}

模擬元件的實作如下所示:

async fn echo_server_mock(handles: LocalComponentHandles) -> Result<(), Error> {
    // Create a new ServiceFs to host FIDL protocols from
    let mut fs = fserver::ServiceFs::new();

    // Add the echo protocol to the ServiceFs
    fs.dir("svc").add_fidl_service(IncomingService::Echo);

    // Run the ServiceFs on the outgoing directory handle from the mock handles
    fs.serve_connection(handles.outgoing_dir)?;

    fs.for_each_concurrent(0, move |IncomingService::Echo(stream)| async move {
        stream
            .map(|result| result.context("Request came with error"))
            .try_for_each(|request| async move {
                match request {
                    fecho::EchoRequest::EchoString { value, responder } => {
                        responder
                            .send(value.as_ref().map(|s| &**s))
                            .expect("failed to send echo response");
                    }
                }
                Ok(())
            })
            .await
            .context("Failed to serve request stream")
            .unwrap_or_else(|e| eprintln!("Error encountered: {:?}", e))
    })
    .await;

    Ok(())
}

C++

在 C++ 中,系統會建立繼承 LocalComponent 介面的類別並覆寫 Start 方法,藉此實作模擬元件。

// The interface for backing implementations of components with a Source of Mock.
class LocalComponentImpl {
 public:
  virtual ~LocalComponentImpl();

  // Invoked when the Component Manager issues a Start request to the component.
  // |mock_handles| contains the outgoing directory and namespace of
  // the component.
  virtual void OnStart() = 0;

  // The LocalComponentImpl derived class may override this method to be informed if
  // ComponentController::Stop() was called on the controller associated with
  // the component instance. The ComponentController binding will be dropped
  // automatically, immediately after LocalComponentImpl::OnStop() returns.
  virtual void OnStop() {}

  // The component can call this method to terminate its instance. This will
  // release the handles, and drop the |ComponentController|, informing
  // component manager that the component has stopped. Calling |Exit()| will
  // also cause the Realm to drop the |LocalComponentImpl|, which should
  // destruct the component, and the handles and bindings held by the component.
  // Therefore the |LocalComponentImpl| should not do anything else after
  // calling |Exit()|.
  //
  // This method is not valid until |OnStart()| is invoked.
  void Exit(zx_status_t return_code = ZX_OK);

  // Returns the namespace provided to the mock component.
  //
  // This method is not valid until |OnStart()| is invoked.
  fdio_ns_t* ns();

  // Returns a wrapper around the component's outgoing directory. The mock
  // component may publish capabilities using the returned object.
  //
  // This method is not valid until |OnStart()| is invoked.
  sys::OutgoingDirectory* outgoing();

  // Convenience method to construct a ServiceDirectory by opening a handle to
  // "/svc" in the namespace object returned by `ns()`.
  //
  // This method is not valid until |OnStart()| is invoked.
  sys::ServiceDirectory svc();

 private:
  friend internal::LocalComponentRunner;
  // The |LocalComponentHandles| are set by the |LocalComponentRunner| after
  // construction by the factory, and before calling |OnStart()|
  std::unique_ptr<LocalComponentHandles> handles_;
};

LocalComponentHandles 類別包含元件傳入和傳出功能的控制代碼:

// Handles provided to mock component.
class LocalComponentHandles final {
 public:
  // ...

  // Returns the namespace provided to the mock component. The returned pointer
  // will be invalid once *this is destroyed.
  fdio_ns_t* ns();

  // Returns a wrapper around the component's outgoing directory. The mock
  // component may publish capabilities using the returned object. The returned
  // pointer will be invalid once *this is destroyed.
  sys::OutgoingDirectory* outgoing();

  // Convenience method to construct a ServiceDirectory by opening a handle to
  // "/svc" in the namespace object returned by `ns()`.
  sys::ServiceDirectory svc();

  // ...
};

模擬元件的實作如下所示:

class LocalEchoServerImpl : public fidl::examples::routing::echo::Echo, public LocalComponentImpl {
 public:
  explicit LocalEchoServerImpl(async_dispatcher_t* dispatcher) : dispatcher_(dispatcher) {}

  // Override `OnStart` from `LocalComponentImpl` class.
  void OnStart() override {
    // When `OnStart()` is called, this implementation can call methods to
    // access handles to the component's incoming capabilities (`ns()` and
    // `svc()`) and outgoing capabilities (`outgoing()`).
    ASSERT_EQ(outgoing()->AddPublicService(bindings_.GetHandler(this, dispatcher_)), ZX_OK);
  }

  // Override `EchoString` from `Echo` protocol.
  void EchoString(::fidl::StringPtr value, EchoStringCallback callback) override {
    callback(std::move(value));
  }

 private:
  async_dispatcher_t* dispatcher_;
  fidl::BindingSet<fidl::examples::routing::echo::Echo> bindings_;
};

模擬實作完成後,您可以將其加入領域:

Rust

let echo_server = builder
    .add_local_child(
        "echo_server",
        move |handles: LocalComponentHandles| Box::pin(echo_server_mock(handles)),
        ChildOptions::new(),
    )
    .await?;

C++

// Add component to the realm, providing a mock implementation
builder.AddLocalChild("echo_server",
                      [&]() { return std::make_unique<LocalEchoServerImpl>(dispatcher()); });

路徑功能

根據預設,建立的領域中沒有功能路徑。如要使用 Realm Builder 將功能轉送至元件,請使用適當的能力路徑呼叫新增路徑函式。

在子項元件之間轉送

以下範例會為元件 echo_serverfidl.examples.routing.echo.Echo 通訊協定新增能力路徑至 offer 元件 echo_client

Rust

builder
    .add_route(
        Route::new()
            .capability(Capability::protocol_by_name("fidl.examples.routing.echo.Echo"))
            .from(&echo_server)
            .to(&echo_client),
    )
    .await?;

C++

builder.AddRoute(Route{.capabilities = {Protocol{"fidl.examples.routing.echo.Echo"}},
                       .source = ChildRef{"echo_server"},
                       .targets = {ChildRef{"echo_client"}}});

公開運作領域功能

如要將在建立運作範圍內提供的功能轉送至測試元件,請將能力路徑的目標設為 parent。建立的領域會自動 exposes 能力設為其父項。這可讓 Realm Builder 執行個體存取公開的能力。

以下範例將 fidl.examples.routing.echo.Echo 通訊協定公開給父項測試元件:

Rust

builder
    .add_route(
        Route::new()
            .capability(Capability::protocol_by_name("fidl.examples.routing.echo.Echo"))
            .from(&echo_server)
            .to(Ref::parent()),
    )
    .await?;

C++

builder.AddRoute(Route{.capabilities = {Protocol{"fidl.examples.routing.echo.Echo"}},
                       .source = ChildRef{"echo_server"},
                       .targets = {ParentRef()}});

提供測試功能

如要將功能從測試元件轉送至已建立運作範圍內的元件,請將能力路徑的來源設為 parent。這包括Realm Builder 資料分割提供的測試功能:

{
    protocol: [
        "fuchsia.diagnostics.ArchiveAccessor",
        "fuchsia.inspect.InspectSink",
    ],
    from: "parent",
    to: [ "#realm_builder" ],
},
{
    event_stream: [
        "capability_requested",
        "destroyed",
        "directory_ready",
        "running_v2",
        "started",
        "stopped",
    ],
    from: "parent",
    to: "#realm_builder",
},

請考慮以下範例,將 fuchsia.logger.LogSink 通訊協定從測試元件轉送至領域的子項元件:

Rust

builder
    .add_route(
        Route::new()
            .capability(Capability::protocol_by_name("fuchsia.logger.LogSink"))
            .from(Ref::parent())
            .to(&echo_server)
            .to(&echo_client),
    )
    .await?;

C++

builder.AddRoute(Route{.capabilities = {Protocol{"fuchsia.logger.LogSink"}},
                       .source = ParentRef(),
                       .targets = {ChildRef{"echo_server"}, ChildRef{"echo_client"}}});

建立領域

新增測試案例所需的所有元件和路徑後,請使用建構方法建立領域,並將其元件準備好開始執行。

Rust

let realm = builder.build().await?;

C++

auto realm = builder.Build(dispatcher());

請使用建構方法傳回的領域執行其他工作。領域中的任何緊急元件都會立即執行,使用 parent 轉送的所有功能現在都可供測試存取。

Rust

let echo = realm.root.connect_to_protocol_at_exposed_dir::<fecho::EchoMarker>()?;
assert_eq!(echo.echo_string(Some("hello")).await?, Some("hello".to_owned()));

建議:在每次測試結束時對運作範圍執行個體呼叫 destroy(),以確保下一個測試案例前可拆除。

不建議:等待領域物件超出範圍,指示元件管理服務刪除領域及其子項。

C++

auto echo = realm.component().ConnectSync<fidl::examples::routing::echo::Echo>();
fidl::StringPtr response;
echo->EchoString("hello", &response);
ASSERT_EQ(response, "hello");

當領域物件超出範圍時,元件管理服務會刪除領域及其子項。

進階設定

修改產生的資訊清單

如果新增路線方法支援的能力轉送功能不足,您可以手動調整資訊清單宣告。Realm Builder 針對下列元件類型支援這項功能:

  • Realm Builder 建立的模擬元件。
  • 與測試元件位於同一套件中的網址元件。

建構領域之後:

  1. 使用已建構領域的 get decl 方法取得特定子項的資訊清單。
  2. 修改適當的資訊清單屬性。
  3. 呼叫取代 decl 方法來取代元件的更新資訊清單。

Rust

let mut root_decl = builder.get_realm_decl().await?;
// root_decl is mutated in whatever way is needed
builder.replace_realm_decl(root_decl).await?;

let mut a_decl = builder.get_component_decl("a").await?;
// a_decl is mutated in whatever way is needed
builder.replace_component_decl("a", a_decl).await?;

C++

auto root_decl = realm_builder.GetRealmDecl();
// ... root_decl is mutated as needed
realm_builder.ReplaceRealmDecl(std::move(root_decl));

auto a_decl = realm_builder.GetComponentDecl("a");
// ... a_decl is mutated as needed
realm_builder.ReplaceComponentDecl(std::move(a_decl));

為修改過的元件新增路徑時,請將這些路徑直接新增至您取得資訊清單的建構領域,而不是使用建構工具執行個體。如此可確保在建立運作範圍時,依據修改後的元件正確驗證路徑。

測試元件元件

Realm Builder 子項元件的 Moniker 如下所示:

fuchsia_component_test_collection:child-name/component-name

該路徑名稱由下列元素組成:

  • child-name:由 Realm Builder 程式庫建立,為領域範圍集合自動產生的名稱。透過呼叫所建構運作領域的 child_name() 函式取得。
  • component-name建構領域時,提供給 Add Component 元件的「元件名稱」參數。

如要取得子名稱,請在建構的領域中叫用以下方法:

Rust

println!("Child Name: {}", realm.root.child_name());

C++

std::cout << "Child Name: " << realm.component().GetChildName() << std::endl;

疑難排解

能力路徑無效

新增路徑函式無法驗證功能是否從測試元件正確提供給已建立的能力領域。

如果您嘗試使用 parent 來源轉送功能,卻沒有對應的 offer,系統將無法解決開啟能力的要求,並且會看到類似以下的錯誤訊息:

[86842.196][klog][E] [component_manager] ERROR: Required protocol `fidl.examples.routing.echo.Echo` was not available for target component `/core/test_manager/tests:auto-10238282593681900609/test_wrapper/test_root/fuchsia_component_test_
[86842.197][klog][I] collection:auto-4046836611955440668/echo-client`: An `offer from parent` declaration was found at `/core/test_manager/tests:auto-10238282593681900609/test_wrapper/test_root/fuchsia_component_test_colle
[86842.197][klog][I] ction:auto-4046836611955440668` for `fidl.examples.routing.echo.Echo`, but no matching `offer` declaration was found in the parent

如要進一步瞭解如何正確提供測試控制器的功能,請參閱提供測試功能

語言特徵矩陣

Rust C++
舊版元件
模擬元件
覆寫設定值
操控元件宣告