ONSdigital / dp-mongodb-in-memory

Runs an in-memory MongoDB instance for Go unit tests
MIT License
14 stars 13 forks source link

Fix cross device link error in rename #30

Open djosix opened 5 months ago

djosix commented 5 months ago

Extract mongod to the destination folder to ensure source and target for renaming are on the same device.

Fixes #27

What

After extracting mongod from the downloaded tgz file, mongod is renamed to the binary path in the cache dir. If the temp dir and the cache dir are not on the same FS the rename operation may fail (at least in unix). We can simply extract the mongod to the cache dir instead of the temp dir, so the source and target are always on the same FS.

Running GitLab CI pipeline and the mongod path is cached, before:

=== RUN   TestXXX
--- FAIL: TestXXX (82.42s)
panic: failed to init mongodb: rename /tmp/878417577 /builds/xxx/.cache/dp-mongodb-in-memory/mongodb-linux-x86_64-debian10-4.4.28.tgz/mongod: invalid cross-device link [recovered]
    panic: failed to init mongodb: rename /tmp/878417577 /builds/xxx/.cache/dp-mongodb-in-memory/mongodb-linux-x86_64-debian10-4.4.28.tgz/mongod: invalid cross-device link
goroutine 19 [running]:
testing.tRunner.func1.2({0xbd8020, 0xc000224170})
    /usr/local/go/src/testing/testing.go:1631 +0x24a
testing.tRunner.func1()
    /usr/local/go/src/testing/testing.go:1634 +0x377
panic({0xbd8020?, 0xc000224170?})
    /usr/local/go/src/runtime/panic.go:770 +0x132
yyy/xxx/internal.NewFakeMongoClient(0xc000327520)
    /builds/xxx/internal/mongodb.go:25 +0x2ae
yyy/xxx/internal.NewFakeMongoDB(0xcc819f?)
    /builds/xxx/internal/mongodb.go:18 +0x13
yyy/xxx/zzz.TestXXX(0xc000327520)
    /builds/xxx/zzz/handler_test.go:53 +0x4c
testing.tRunner(0xc000327520, 0xd13770)
    /usr/local/go/src/testing/testing.go:1689 +0xfb
created by testing.(*T).Run in goroutine 1
    /usr/local/go/src/testing/testing.go:1742 +0x390
FAIL    yyy/xxx/zzz 82.433s
FAIL

After this change: all tests passed

How to review

Make sure there is no other problems.

Who can review

Anyone.