fn create_test_repo()

in sources/api/migration/migrator/src/test.rs [97:183]


fn create_test_repo() -> TestRepo {
    // This is where the signed TUF repo will exist when we are done. It is the
    // root directory of the `TestRepo` we will return when we are done.
    let test_repo_dir = TempDir::new().unwrap();
    let metadata_path = test_repo_dir.path().join("metadata");
    let targets_path = test_repo_dir.path().join("targets");

    // This is where we will stage the TUF repository targets prior to signing them. We are using
    // symlinks from `tuf_indir` to `tuf_outdir/targets` so we keep both in the same `TempDir`.
    let tuf_indir = test_repo_dir.path();

    // Create a Manifest and save it to the tuftool_indir for signing.
    let mut manifest = update_metadata::Manifest::default();
    // insert the following migrations to the manifest. note that the first migration would sort
    // later than the second migration alphabetically. this is to help ensure that migrations
    // are running in their listed order (rather than sorted order as in previous
    // implementations).
    manifest.migrations.insert(
        (Version::new(0, 99, 0), Version::new(0, 99, 1)),
        vec![FIRST_MIGRATION.into(), SECOND_MIGRATION.into()],
    );
    update_metadata::write_file(tuf_indir.join("manifest.json").as_path(), &manifest).unwrap();

    // Create an script that we can use as the 'migration' that migrator will run. This script will
    // write its name and arguments to a file named result.txt in the directory that is the parent
    // of --source-datastore. result.txt can then be used to see what migrations ran, and in what
    // order. Note that tests are sensitive to the order and number of arguments passed. If
    // --source-datastore is given at a different position then the tests will fail and the script
    // will need to be updated.
    let migration_a = create_test_migration(FIRST_MIGRATION);
    let migration_b = create_test_migration(SECOND_MIGRATION);

    // Save lz4 compressed copies of the migration script into the tuftool_indir.
    compress(migration_a.as_bytes(), &tuf_indir.join(FIRST_MIGRATION));
    compress(migration_b.as_bytes(), &tuf_indir.join(SECOND_MIGRATION));

    // Create and sign the TUF repository.
    let mut editor = tough::editor::RepositoryEditor::new(root()).unwrap();
    let long_ago: DateTime<Utc> = DateTime::parse_from_rfc3339("1970-01-01T00:00:00Z")
        .unwrap()
        .into();
    let one = std::num::NonZeroU64::new(1).unwrap();
    editor
        .targets_version(one)
        .unwrap()
        .targets_expires(long_ago)
        .unwrap()
        .snapshot_version(one)
        .snapshot_expires(long_ago)
        .timestamp_version(one)
        .timestamp_expires(long_ago);

    fs::read_dir(tuf_indir)
        .unwrap()
        .filter(|dir_entry_result| {
            if let Ok(dir_entry) = dir_entry_result {
                return dir_entry.path().is_file();
            }
            false
        })
        .for_each(|dir_entry_result| {
            let dir_entry = dir_entry_result.unwrap();
            editor
                .add_target(
                    dir_entry.file_name().to_str().unwrap(),
                    tough::schema::Target::from_path(dir_entry.path()).unwrap(),
                )
                .unwrap();
        });
    let signed_repo = editor
        .sign(&[Box::new(tough::key_source::LocalKeySource { path: pem() })])
        .unwrap();
    signed_repo
        .link_targets(
            tuf_indir,
            &targets_path,
            tough::editor::signed::PathExists::Fail,
        )
        .unwrap();
    signed_repo.write(&metadata_path).unwrap();

    TestRepo {
        _tuf_dir: test_repo_dir,
        metadata_path,
        targets_path,
    }
}