src/sys/appmgr/storage_unittest.cc
// Test that when a component's storage directory is moved once it is assigned an instance ID.
Test that when a component's storage directory is moved once it is assigned an instance ID.
Test that when a component ' s storage directory is moved once it is assigned an instance ID .
TEST_F(StorageTest, MoveDirToInstanceId) {
  {
    auto root_realm =
        CreateRootRealm(root_storage_dir(), MakeAppmgrConfigDirWithIndex(kEmptyComponentIdIndex));
    fuchsia::sys::EnvironmentControllerPtr child_env_ctrl;
    auto child_realm =
        CreateChildRealm(root_realm.get(), "child_realm", child_env_ctrl.NewRequest());
    FuchsiaPkgUrl url;
    ZX_ASSERT(url.Parse("fuchsia-pkg://fuchsia.com/my_pkg#meta/my_component.cmx"));
    EXPECT_EQ(child_realm->IsolatedPathForComponentInstance(url, internal::StorageType::DATA),
              files::JoinPath(root_storage_dir(),
                              "data/r/child_realm/fuchsia.com:my_pkg:0#meta:my_component.cmx"));
  }
  EXPECT_TRUE(files::IsDirectory(files::JoinPath(
      root_storage_dir(), "data/r/child_realm/fuchsia.com:my_pkg:0#meta:my_component.cmx")));
  {
    auto root_realm = CreateRootRealm(
        root_storage_dir(), MakeAppmgrConfigDirWithIndex(fxl::Substitute(R"(
        {
          "instances": [
            {
              "instance_id": "$0",
              "appmgr_moniker": {
                "realm_path": ["app", "child_realm"],
                "url": "fuchsia-pkg://fuchsia.com/my_pkg#meta/my_component.cmx"
              }
            }
          ]
        }
      )",
                                                                         kExampleInstanceId)));
    fuchsia::sys::EnvironmentControllerPtr child_env_ctrl;
    auto child_realm =
        CreateChildRealm(root_realm.get(), "child_realm", child_env_ctrl.NewRequest());
    FuchsiaPkgUrl url;
    ZX_ASSERT(url.Parse("fuchsia-pkg://fuchsia.com/my_pkg#meta/my_component.cmx"));
    auto actual_storage_path =
        child_realm->IsolatedPathForComponentInstance(url, internal::StorageType::DATA);
    EXPECT_EQ(actual_storage_path,
              files::JoinPath(root_storage_dir(),
                              fxl::Substitute("data/persistent/$0", kExampleInstanceId)));
  }
  EXPECT_FALSE(files::IsDirectory(files::JoinPath(
      root_storage_dir(), "data/r/child_realm/fuchsia.com:my_pkg:0#meta:my_component.cmx")));
  EXPECT_TRUE(files::IsDirectory(files::JoinPath(
      root_storage_dir(), fxl::Substitute("data/persistent/$0", kExampleInstanceId))));
}
TEST_F ( StorageTest , MoveDirToInstanceId ) { { auto root_realm = CreateRootRealm ( root_storage_dir ( ) , MakeAppmgrConfigDirWithIndex ( kEmptyComponentIdIndex ) ) ; fuchsia :: sys :: EnvironmentControllerPtr child_env_ctrl ; auto child_realm = CreateChildRealm ( root_realm . get ( ) , " " , child_env_ctrl . NewRequest ( ) ) ; FuchsiaPkgUrl url ; ZX_ASSERT ( url . Parse ( " " ) ) ; EXPECT_EQ ( child_realm -> IsolatedPathForComponentInstance ( url , internal :: StorageType :: DATA ) , files :: JoinPath ( root_storage_dir ( ) , " " ) ) ; } EXPECT_TRUE ( files :: IsDirectory ( files :: JoinPath ( root_storage_dir ( ) , " " ) ) ) ; { auto root_realm = CreateRootRealm ( root_storage_dir ( ) , MakeAppmgrConfigDirWithIndex ( fxl :: Substitute ( R" ( 
        {
          "instances": [
            {
              "instance_id": "$0",
              "appmgr_moniker": {
                "realm_path": ["app", "child_realm"],
                "url": "fuchsia-pkg://fuchsia.com/my_pkg#meta/my_component.cmx"
              }
            }
          ]
        }
       ) " , kExampleInstanceId ) ) ) ; fuchsia :: sys :: EnvironmentControllerPtr child_env_ctrl ; auto child_realm = CreateChildRealm ( root_realm . get ( ) , " " , child_env_ctrl . NewRequest ( ) ) ; FuchsiaPkgUrl url ; ZX_ASSERT ( url . Parse ( " " ) ) ; auto actual_storage_path = child_realm -> IsolatedPathForComponentInstance ( url , internal :: StorageType :: DATA ) ; EXPECT_EQ ( actual_storage_path , files :: JoinPath ( root_storage_dir ( ) , fxl :: Substitute ( " " , kExampleInstanceId ) ) ) ; } EXPECT_FALSE ( files :: IsDirectory ( files :: JoinPath ( root_storage_dir ( ) , " " ) ) ) ; EXPECT_TRUE ( files :: IsDirectory ( files :: JoinPath ( root_storage_dir ( ) , fxl :: Substitute ( " " , kExampleInstanceId ) ) ) ) ; }
Test that when a component's storage directory is moved once it is assigned an instance ID.
Test that when a component ' s storage directory is moved once it is assigned an instance ID .
// Step 1: ensure storage directory exists for component without an instance ID. // Ensure that the moniker based directory is created. // Step 2: Spin up the root realm again, this time assigning the component with an instance ID. // Ensure that the moniker based directory has been moved to the instance ID based directory.