From 9c16c9332450d94abc991b9f1d16579f87f24581 Mon Sep 17 00:00:00 2001 From: Vinnie Magro Date: Thu, 30 Jan 2025 12:48:55 -0800 Subject: [PATCH] [antlir2][tests] add idmap test to basic rust test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: As I start to enable more rootless test execution, it's important to exercise the tests-for-the-tests that we have here. This diff makes sure that in the test container `root` is id `0` and `antlir` is `1000`, as set in the `antlir` uidmap. Test Plan: ``` ❯ buck2 targets fbcode//antlir/antlir2/testing/tests: | grep rootless | xargs buck2 test Buck UI: https://www.internalfb.com/buck2/0f869c2e-df9e-4a80-8956-813d1c69ad71 Test UI: https://www.internalfb.com/intern/testinfra/testrun/10133099228309166 Tests finished: Pass 52. Fail 0. Fatal 0. Skip 0. Build failure 0 ``` Reviewed By: justintrudell Differential Revision: D68905617 fbshipit-source-id: 36872d9c60b5c3ada03f00defbb8daa17fa1aa75 --- antlir/antlir2/testing/tests/BUCK | 17 +++++++++++++++++ antlir/antlir2/testing/tests/test.rs | 12 ++++++++++++ 2 files changed, 29 insertions(+) diff --git a/antlir/antlir2/testing/tests/BUCK b/antlir/antlir2/testing/tests/BUCK index fab169eb8cd..bc23a57d5fd 100644 --- a/antlir/antlir2/testing/tests/BUCK +++ b/antlir/antlir2/testing/tests/BUCK @@ -24,6 +24,23 @@ image.layer( "slow-unit.service", target = "default.target", ), + feature.user_add( + home_dir = "/", + primary_group = "antlir", + shell = "/bin/bash", + uidmap = "antlir", + username = "antlir", + ), + feature.group_add( + groupname = "antlir", + uidmap = "antlir", + ), + feature.install_text( + dst = "/antlir.txt", + group = "antlir", + text = "I am antlir\n", + user = "antlir", + ), ], ) diff --git a/antlir/antlir2/testing/tests/test.rs b/antlir/antlir2/testing/tests/test.rs index 3a3d187b736..02f35558059 100644 --- a/antlir/antlir2/testing/tests/test.rs +++ b/antlir/antlir2/testing/tests/test.rs @@ -5,6 +5,7 @@ * LICENSE file in the root directory of this source tree. */ +use std::os::unix::fs::MetadataExt; use std::path::Path; use nix::unistd::getuid; @@ -55,3 +56,14 @@ fn tmpfs_tmp() { fn tmpfs_run() { test_tmpfs("/run"); } + +#[test] +fn id_mapping() { + let meta = std::fs::metadata("/").expect("failed to stat /"); + assert_eq!(0, meta.uid()); + assert_eq!(0, meta.gid()); + + let meta = std::fs::metadata("/antlir.txt").expect("failed to stat /antlir.txt"); + assert_eq!(1000, meta.uid()); + assert_eq!(1000, meta.gid()); +}