Search code examples
node.jsdockerskaffold

Does Skaffold run tests in containers built using the image being tested?


To confirm whether the tests specified in skaffold.yaml are run in a container or locally, I printed the node version.

>node --version
v18.12.1

I ran the same command in skaffold.yaml.

apiVersion: skaffold/v4beta2
kind: Config
metadata:
  name: microservices-quiz-app
build:
  artifacts:
    - image: auth-img
      context: auth
      docker:
        dockerfile: Dockerfile
      sync:
        manual:
          - src: src/**/*.js
            dest: .
test:
  - image: auth-img
    context: auth
    custom:
      - command: node --version
manifests:
  rawYaml:
  - auth/k8s/*.yml
  - ingress-srv.yml

node version after skaffold dev

The phrasing in the documentation indicates it is run locally "It will run on the local machine". documentation

If I connect directly to the container built using auth-img after skaffold dev, I see a different version.

node version in container

Is there a way to run the tests in a container with the necessary dependencies (e.g. jest, supertest) to run the tests?

Why isn't Skaffold using the image being tested; are we supposed to not include dev dependencies in the image?

FROM node:18-alpine
# ENV NODE_ENV=production

WORKDIR /app

COPY ["package.json", "package-lock.json*", "./"]

# RUN npm install --production
RUN npm install

COPY . .

# CMD ["npm", "start"]
CMD [ "npm", "run", "dev" ]

Solution

  • We can use docker to run a container using the generated image. Skaffold provides the variable IMAGE.

    test:
      # auth
      - image: auth-img-dev
        context: auth
        custom:
          # run test in container
          - command: echo $IMAGE && docker run -e CI=true $IMAGE npm test