testcontainers-dotnet icon indicating copy to clipboard operation
testcontainers-dotnet copied to clipboard

[Bug]: Copy a file WithResourceMapping leads to an (empty) directory in the container

Open cimnine opened this issue 5 months ago • 6 comments

Testcontainers version

4.6.0

Using the latest Testcontainers version?

Yes

Host OS

Mac

Host arch

ARM

.NET version

9.0.302

Docker version

➜ podman version
Client:        Podman Engine
Version:       5.5.2
API Version:   5.5.2
Go Version:    go1.24.4
Built:         Tue Jun 24 16:41:45 2025
Build Origin:  brew
OS/Arch:       darwin/arm64

Server:       Podman Engine
Version:      5.2.3
API Version:  5.2.3
Go Version:   go1.22.7
Built:        Tue Sep 24 02:00:00 2024
OS/Arch:      linux/arm64

Docker info

➜ podman info
Client:
  APIVersion: 5.5.2
  BuildOrigin: brew
  Built: 1750776105
  BuiltTime: Tue Jun 24 16:41:45 2025
  GitCommit: ""
  GoVersion: go1.24.4
  Os: darwin
  OsArch: darwin/arm64
  Version: 5.5.2
host:
  arch: arm64
  buildahVersion: 1.37.3
  cgroupControllers:
  - cpuset
  - cpu
  - io
  - memory
  - pids
  - rdma
  - misc
  cgroupManager: systemd
  cgroupVersion: v2
  conmon:
    package: conmon-2.1.12-2.fc40.aarch64
    path: /usr/bin/conmon
    version: 'conmon version 2.1.12, commit: '
  cpuUtilization:
    idlePercent: 98.24
    systemPercent: 0.73
    userPercent: 1.03
  cpus: 6
  databaseBackend: sqlite
  distribution:
    distribution: fedora
    variant: coreos
    version: "40"
  eventLogger: journald
  freeLocks: 2031
  hostname: localhost.localdomain
  idMappings:
    gidmap: null
    uidmap: null
  kernel: 6.10.10-200.fc40.aarch64
  linkmode: dynamic
  logDriver: journald
  memFree: 842330112
  memTotal: 7716700160
  networkBackend: netavark
  networkBackendInfo:
    backend: netavark
    dns:
      package: aardvark-dns-1.12.1-1.20240925084542154505.main.19.g56d105f.fc40.aarch64
      path: /usr/libexec/podman/aardvark-dns
      version: aardvark-dns 1.13.0-dev
    package: netavark-1.12.1-1.20240913132227547291.main.38.gcebebc7.fc40.aarch64
    path: /usr/libexec/podman/netavark
    version: netavark 1.13.0-dev
  ociRuntime:
    name: crun
    package: crun-1.17-1.20240910121144502937.main.3.g4ab4ac0.fc40.aarch64
    path: /usr/bin/crun
    version: |-
      crun version UNKNOWN
      commit: fa61d61334521bb205430e928df99f947716f88c
      rundir: /run/crun
      spec: 1.0.0
      +SYSTEMD +SELINUX +APPARMOR +CAP +SECCOMP +EBPF +CRIU +LIBKRUN +WASM:wasmedge +YAJL
  os: linux
  pasta:
    executable: /usr/bin/pasta
    package: passt-0^20240906.g6b38f07-1.fc40.aarch64
    version: |
      pasta 0^20240906.g6b38f07-1.fc40.aarch64-pasta
      Copyright Red Hat
      GNU General Public License, version 2 or later
        <https://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
      This is free software: you are free to change and redistribute it.
      There is NO WARRANTY, to the extent permitted by law.
  remoteSocket:
    exists: true
    path: /run/podman/podman.sock
  rootlessNetworkCmd: pasta
  security:
    apparmorEnabled: false
    capabilities: CAP_CHOWN,CAP_DAC_OVERRIDE,CAP_FOWNER,CAP_FSETID,CAP_KILL,CAP_NET_BIND_SERVICE,CAP_SETFCAP,CAP_SETGID,CAP_SETPCAP,CAP_SETUID,CAP_SYS_CHROOT
    rootless: false
    seccompEnabled: true
    seccompProfilePath: /usr/share/containers/seccomp.json
    selinuxEnabled: true
  serviceIsRemote: true
  slirp4netns:
    executable: /usr/bin/slirp4netns
    package: slirp4netns-1.2.2-2.fc40.aarch64
    version: |-
      slirp4netns version 1.2.2
      commit: 0ee2d87523e906518d34a6b423271e4826f71faf
      libslirp: 4.7.0
      SLIRP_CONFIG_VERSION_MAX: 4
      libseccomp: 2.5.5
  swapFree: 0
  swapTotal: 0
  uptime: 771h 50m 58.00s (Approximately 32.12 days)
  variant: v8
plugins:
  authorization: null
  log:
  - k8s-file
  - none
  - passthrough
  - journald
  network:
  - bridge
  - macvlan
  - ipvlan
  volume:
  - local
registries:
  search:
  - docker.io
store:
  configFile: /usr/share/containers/storage.conf
  containerStore:
    number: 7
    paused: 0
    running: 7
    stopped: 0
  graphDriverName: overlay
  graphOptions:
    overlay.imagestore: /usr/lib/containers/storage
    overlay.mountopt: nodev,metacopy=on
  graphRoot: /var/lib/containers/storage
  graphRootAllocated: 99252940800
  graphRootUsed: 37016104960
  graphStatus:
    Backing Filesystem: xfs
    Native Overlay Diff: "false"
    Supports d_type: "true"
    Supports shifting: "true"
    Supports volatile: "true"
    Using metacopy: "true"
  imageCopyTmpDir: /var/tmp
  imageStore:
    number: 257
  runRoot: /run/containers/storage
  transientStore: false
  volumePath: /var/lib/containers/storage/volumes
version:
  APIVersion: 5.2.3
  Built: 1727136000
  BuiltTime: Tue Sep 24 02:00:00 2024
  GitCommit: ""
  GoVersion: go1.22.7
  Os: linux
  OsArch: linux/arm64
  Version: 5.2.3

What happened?

I'm adding a file with WithResourceMapping. But inside the container, the file is an empty directory.

    private readonly KeycloakContainer container = new KeycloakBuilder()
        .WithImage("quay.io/keycloak/keycloak:24.0.5")
        .WithUsername(MasterUsername)
        .WithPassword(MasterPassword)
        .WithResourceMapping(
            new FileInfo("TestContainers/temporary.json"),
            "/opt/keycloak/data/import/temporary.json"
        )
        .WithEnvironment("KC_HOSTNAME_STRICT", "false")
        .WithEnvironment("KC_HOSTNAME_STRICT_HTTPS", "false")
        .WithEnvironment("KC_HOSTNAME_STRICT_BACKCHANNEL", "true")
        .WithCommand("--import-realm")
        .Build();
bash-5.1$ ls -la /opt/keycloak/data/import/
total 0
drwxr-xr-x. 3 root     root 28 Jul 17 08:49 .
drwxrwxr-x. 1 keycloak root 30 Jul 17 08:49 ..
drwxr-xr-x. 2 root     root 28 Jul 17 08:49 temporary.json

Relevant log output


Additional information

No response

cimnine avatar Jul 17 '25 08:07 cimnine

The second arg should be a directory (docs). Change it to:

.WithResourceMapping(
    new FileInfo("TestContainers/temporary.json"),
    "/opt/keycloak/data/import/"
)

But inside the container, the file is an empty directory.

Even though the second arg isn't "correct", it shouldn't be empty. Is the path to TestContainers/temporary.json correct?

HofmeisterAn avatar Jul 17 '25 09:07 HofmeisterAn

Even though the second arg isn't "correct", it shouldn't be empty. Is the path to TestContainers/temporary.json correct?

No, you are right. The path isn't empty. It contains the file.

While the issue is resolved for me at this point, I still like to express the following thoughts:

To me, it is unexpected that I can't put a file path as the second parameter, as I'm used to doing this in docker-compose.yml:

services:
  iam:
    image: quay.io/keycloak/keycloak:24.0.5
    environment:
      KEYCLOAK_ADMIN: admin
      KEYCLOAK_ADMIN_PASSWORD: admin
    command:
      - start-dev
      - --import-realm
      - --health-enabled=true
      - --hostname-url=http://localhost:8080
      - --hostname-strict=false
      - --hostname-strict-https=false
      - --hostname-strict-backchannel=true
    volumes:
      - keycloak-db:/opt/keycloak/data/h2
      - ./Requirements/TestContainers/temporary.json:/opt/keycloak/data/import/temporary.json
    ports:
      - '8080:8080'
    healthcheck:
      test: ["CMD", "bash", "-c", "</dev/tcp/localhost/8080"]
      interval: '10s'
      timeout: '1s'
      retries: 5
      start_period: '20s'

I would have appreciated it if the documentation was more explicit about this (that is, expecting only directories).

Also, the API is slightly inconsistent in my opinion, as the overload that accepts a byte array expects a file path. Same method name, various meanings for the second path parameter:

_ = new ContainerBuilder()
  .WithResourceMapping(new DirectoryInfo("."), "/DIRECTORY/");
_ = new ContainerBuilder()
  .WithResourceMapping(new FileInfo("file.txt"), "/DIRECTORY/");
_ = new ContainerBuilder()
  .WithResourceMapping(Encoding.Default.GetBytes("{}"), "/FILE");

cimnine avatar Jul 17 '25 09:07 cimnine

Also, the API is slightly inconsistent in my opinion, as the overload that accepts a byte array expects a file path. Same method name, various meanings for the second path parameter:

Yeah, it's definitely not ideal. It's mostly because of some really old APIs, to keep them backward compatible. Also, when working with just a string, there's no reliable way to tell whether the target is a directory or a file (the overloads use a different arg name, but it's not intuitive, I get that 😄). If the target is a file, use the overload WithResourceMapping(FileInfo source, FileInfo target).

I would have appreciated it if the documentation was more explicit about this (that is, expecting only directories).

Would you be interested in contributing to the (XML) docs to help clarify this?

Edit: We can also refactor this as long as we maintain backward compatibility, so developers can smoothly transition to the new API/behavior. Ideally, we'd use the same source and target types (I guess):

https://github.com/testcontainers/testcontainers-dotnet/blob/94abb4a3eeb3d5ecf9ba569c99832fcb5e9f8a95/src/Testcontainers/Builders/ContainerBuilder%603.cs#L210-L227

HofmeisterAn avatar Jul 17 '25 10:07 HofmeisterAn

Would you be interested in contributing to the (XML) docs to help clarify this?

https://github.com/testcontainers/testcontainers-dotnet/pull/1487 (https://deploy-preview-1487--testcontainers-dotnet.netlify.app/api/create_docker_container/)

Also, when working with just a string, there's no reliable way to tell whether the target is a directory or a file

It's possible to tell whether a path is a directory, which is always the case if the path ends with a /. If a path does not end in a /, it's impossible to tell without breaking backward compatibility.

The string-based methods could be deprecated instead and replaced with the following methods. These signatures are explicit with regards to the target path being a file or directory path.

WithResourceMapping(FileInfo, DirectoryInfo, UnixFileModes fileMode = Unix.FileMode644);
WithResourceMapping(FileInfo, FileInfo, UnixFileModes fileMode = Unix.FileMode644);
WithResourceMapping(DirectoryInfo, DirectoryInfo, UnixFileModes fileMode = Unix.FileMode644);
WithResourceMapping(byte[], FileInfo, UnixFileModes fileMode = Unix.FileMode644);
WithResourceMapping(Uri, FileInfo, UnixFileModes fileMode = Unix.FileMode644);
WithResourceMapping(Uri, DirectoryInfo, UnixFileModes fileMode = Unix.FileMode644);

cimnine avatar Jul 17 '25 11:07 cimnine

The string-based methods could be deprecated instead and replaced with the following methods.

I like this suggestion 👍.

It's possible to tell whether a path is a directory, which is always the case if the path ends with a /.

Exactly. Should we refactor WithResourceMapping(string, string) and accept the breaking change for a better user experience? WDYT?

Would you be interested in contributing to these changes?

HofmeisterAn avatar Jul 17 '25 15:07 HofmeisterAn

Would you be interested in contributing to these changes?

Please see my proposal in PR https://github.com/testcontainers/testcontainers-dotnet/pull/1497.

cimnine avatar Jul 30 '25 19:07 cimnine