Docker CI 不能与 mongodb-memory-server 一起使用

Posted

技术标签:

【中文标题】Docker CI 不能与 mongodb-memory-server 一起使用【英文标题】:Docker CI not working with mongodb-memory-server 【发布时间】:2021-08-23 03:55:37 【问题描述】:

我使用 mongodb-memory-server 在 mongo 中测试了一些存储库功能,并在本地机器上成功运行了我的单元测试,但是当将此代码推送到 GitHub 时,它运行失败。我不确定问题是关于 docker config 还是关于 mongodb-memory-server 版本。 这是来自 GitHub 的日志:

9W45p5LM91Vj","tmpDir":"name":"/tmp/mongo-mem--188-9W45p5LM91Vj","uri":"mongodb://127.0.0.1:42823/d791a878-09ac-4ccc-896d-ea603e2676ad?"
2021-06-05T09:45:33.351Z MongoMS:MongoBinary MongoBinary options: 
  "downloadDir": "/__w/son-git-test/son-git-test/node_modules/.cache/mongodb-memory-server/mongodb-binaries",
  "platform": "linux",
  "arch": "x64",
  "version": "4.2.8",
  "checkMD5": false

2021-06-05T09:45:33.356Z MongoMS:getos Trying LSB-Release
2021-06-05T09:45:33.372Z MongoMS:getos Trying OS-Release
2021-06-05T09:45:33.375Z MongoMS:MongoBinaryDownloadUrl Using "mongodb-linux-x86_64-debian92-4.2.8.tgz" as the Archive String
2021-06-05T09:45:33.375Z MongoMS:MongoBinaryDownloadUrl Using "https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1804-4.2.8.tgz" as the Download-URL
2021-06-05T09:45:33.377Z MongoMS:MongoBinaryDownload Downloading: "https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1804-4.2.8.tgz"
2021-06-05T09:45:33.377Z MongoMS:MongoBinaryDownload trying to download https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1804-4.2.8.tgz
2021-06-05T09:45:34.756Z MongoMS:MongoBinaryDownload moved /__w/son-git-test/son-git-test/node_modules/.cache/mongodb-memory-server/mongodb-binaries/mongodb-linux-x86_64-ubuntu1804-4.2.8.tgz.downloading to /__w/son-git-test/son-git-test/node_modules/.cache/mongodb-memory-server/mongodb-binaries/mongodb-linux-x86_64-ubuntu1804-4.2.8.tgz
2021-06-05T09:45:34.757Z MongoMS:MongoBinaryDownload extract(): /__w/son-git-test/son-git-test/node_modules/.cache/mongodb-memory-server/mongodb-binaries/4.2.8
2021-06-05T09:45:37.293Z MongoMS:MongoBinary MongoBinary: Download lock removed
2021-06-05T09:45:37.294Z MongoMS:MongoBinary MongoBinary: Mongod binary path: "/__w/son-git-test/son-git-test/node_modules/.cache/mongodb-memory-server/mongodb-binaries/4.2.8/mongod"
2021-06-05T09:45:37.309Z MongoMS:MongoInstance Mongo[42823]: Called MongoInstance._launchKiller(parent: 188, child: 203):
2021-06-05T09:45:37.323Z MongoMS:MongoInstance Mongo[42823]: STDERR: /__w/son-git-test/son-git-test/node_modules/.cache/mongodb-memory-server/mongodb-binaries/4.2.8/mongod: error while loading shared libraries: libcurl.so.4: cannot open shared object file: No such file or directory

2021-06-05T09:45:37.324Z MongoMS:MongoInstance Mongo[42823]: Mongod instance closed with an non-0 code!
2021-06-05T09:45:37.324Z MongoMS:MongoInstance Mongo[42823]: CLOSE: 127
2021-06-05T09:45:37.325Z MongoMS:MongoInstance Mongo[42823]: MongodbInstance: Instance has failed: Mongod instance closed with code "127"
2021-06-05T09:45:37.331Z MongoMS:MongoMemoryServer Called MongoMemoryServer.stop() method
2021-06-05T09:45:37.331Z MongoMS:MongoMemoryServer Called MongoMemoryServer.ensureInstance() method
2021-06-05T09:45:37.349Z MongoMS:MongoInstance Mongo[42823]: [MongoKiller]: exit - [null,"SIGTERM"]
FAIL src/squid/squid.controller.spec.ts (9.945 s)
  ● Console

    console.log
      before each

      at Object.<anonymous> (squid/squid.controller.spec.ts:19:13)

    console.log
      Downloading MongoDB 4.2.8: 0 % (0mb / 126.5mb)

      at MongoBinaryDownload.Object.<anonymous>.MongoBinaryDownload.printDownloadProgress (../node_modules/mongodb-memory-server-core/src/util/MongoBinaryDownload.ts:424:15)

  ● SquidController › should be defined

    Failed: "Mongod instance closed with code \"127\""

      16 |   let controller: SquidController;
      17 |
    > 18 |   beforeEach(async () => 
         |   ^
      19 |     console.log('before each');
      20 |     const module: TestingModule = await Test.createTestingModule(
      21 |       imports: [

      at Env.beforeEach (../node_modules/jest-jasmine2/build/jasmineAsyncInstall.js:46:24)
      at Suite.<anonymous> (squid/squid.controller.spec.ts:18:3)
      at Object.<anonymous> (squid/squid.controller.spec.ts:15:1) 

这里是 gitflow 配置:

name: Code quality
on:
  pull_request:
    branches:
      - develop
  push:
    branches:
      - develop

defaults:
  run:
    shell: bash

jobs:
  Code-Quality:
    name: Code quality
    runs-on: ubuntu-latest
    container: node:lts-slim
    steps:
      - uses: actions/checkout@v2
      - name: Install dependency
        run: yarn install --frozen-lockfile
      - name: Check lint and format
        run: |
          yarn format:check
          yarn lint:check
      - name: checking unit test
        run: yarn test

这是单元测试代码:

import  Test, TestingModule  from '@nestjs/testing';
import  MongooseModule  from '@nestjs/mongoose';

import  SquidController  from './squid.controller';
import  SquidService  from './squid.service';
import 
  closeInMongodConnection,
  rootMongooseTestModule,
 from '../test-utils/mongo/MongooseTestModule';
import  SquidSchema  from './model/squid.schema';

// May require additional time for downloading MongoDB binaries
jasmine.DEFAULT_TIMEOUT_INTERVAL = 600000;

describe('SquidController', () => 
  let controller: SquidController;

  beforeEach(async () => 
    console.log('before each');
    const module: TestingModule = await Test.createTestingModule(
      imports: [
        rootMongooseTestModule(),
        MongooseModule.forFeature([ name: 'Squid', schema: SquidSchema ]),
      ],
      controllers: [SquidController],
      providers: [SquidService],
    ).compile();

    controller = module.get<SquidController>(SquidController);
  );

  it('should be defined', () => 
    expect(controller).toBeDefined();
  );

  afterAll(async () => 
    await closeInMongodConnection();
  );
);

【问题讨论】:

【参考方案1】:

搜索后发现问题出在哪里。此问题与 Node 版本有关。 Mongo 没有 Node slim/alpine 的构建版本。 我们可以通过更新节点镜像来修复:(container: node:14.17.0)

name: Code quality
on:
  pull_request:
    branches:
      - develop
  push:
    branches:
      - develop

defaults:
  run:
    shell: bash

jobs:
  Code-Quality:
    name: Code quality
    runs-on: ubuntu-latest
    container: node:14.17.0
    steps:
      - uses: actions/checkout@v2
      - name: Install dependency
        run: yarn install --frozen-lockfile
      - name: Check lint and format
        run: |
          yarn format:check
          yarn lint:check
      - name: checking unit test
        run: yarn test

【讨论】:

以上是关于Docker CI 不能与 mongodb-memory-server 一起使用的主要内容,如果未能解决你的问题,请参考以下文章

CI与CD之Docker上安装Jenkins

CI与CD之Docker上安装Jenkins

docker与jenkins的自动化CI/CD流水线实战

Gitlab+Gitlab-CI+Docker实现持续集成(CI)与持续部署(CD)

CI/CD与Docker

Jenkins与Docker的自动化CI/CD实战