containerized-go-dev icon indicating copy to clipboard operation
containerized-go-dev copied to clipboard

Error in unit test: read-only file system

Open arossmann opened this issue 3 years ago • 2 comments

My application creates files and in my test I check whether the creation was successful or not. with go test everything is fine, but with the containerised build I get the error of read-only file system I tried it with setting the mount to "bind" FROM base AS unit-test RUN --mount=target=. \ --mount=type=bind,target=/root/.cache/go-build \ go test -v . but without success.

Any hints for me?

arossmann avatar Mar 23 '21 08:03 arossmann

Hi Arne @arossmann maybe you try once this containerized starter which was influenced from Chris one.

https://github.com/TomFreudenberg/golang-cli-cmd-dev-starter

I had the sme experience as you "read only" - for me it happens only when using docker build and use the mount experimental features in Dockerfile on OSX. I guess you also use a Mac?

For that I rewrote the processes and put the build statements into the Makefile with docker run commands.

Hope that helps for you.

Cheers Tom

TomFreudenberg avatar Apr 26 '21 20:04 TomFreudenberg

Bind mounts in this context mean bind mounting of the build context, not the files directly on the host. Note that the default mount without specifying a type is bind.

By default bind mounts are readonly with the option for read-write but written files are discarded from the final build.

If you want to keep the outputs of tests, you try to output them to somewhere outside of the working directory that you've bind mounted into or you can do a COPY . . instead of the mount=target=..

Old:

FROM base AS unit-test
RUN --mount=target=. \
    --mount=type=cache,target=/go/pkg/mod \
    --mount=type=cache,target=/root/.cache/go-build \
    mkdir /out && go test -v -coverprofile=/out/cover.out ./...

New:

FROM base AS unit-test
COPY . .
RUN --mount=type=cache,target=/go/pkg/mod \
    --mount=type=cache,target=/root/.cache/go-build \
    mkdir /out && go test -v -coverprofile=/out/cover.out ./...

chris-crone avatar Dec 01 '21 15:12 chris-crone