2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Google の container-structure-test を CircleCI で流す

Last updated at Posted at 2020-01-02

はじめに

  • コンテナのテストを行う container-structure-test を使ってみる
  • CircleCI に組み込み、CI/CD パイプライン上で container-structure-test を活用してみる

container-structure-test とは

container-structure-test を使ってみる

テスト用コンテナイメージの作成

Dockerfile

Dockerfile
FROM busybox
LABEL maintainer="tsubasaogawa"

WORKDIR /root

ENV STAGE "dev"

COPY ./docker-entrypoint.sh /root/

RUN date > /tmp/date.log

ENTRYPOINT ["sh", "./docker-entrypoint.sh"]

docker-entrypoint.sh

dockedr-entrypoint.sh
#!sh -eu

# say hello
echo 'hello'

exit 0

container-structure-test 用の config を作成

.container-structure-test/config.yaml
schemaVersion: "2.0.0"

commandTests:
  - name: "it says hello"
    command: "sh"
    args:
      - "./docker-entrypoint.sh"
    expectedOutput:
      - "hello"

fileExistenceTests:
  - name: "date file exists"
    path: "/tmp/date.log"
    shouldExist: true
    permissions: "-rw-r--r--"

metadataTest:
  env:
    - key: "STAGE"
      value: "dev"
  labels:
    - key: "maintainer"
      value: "tsubasaogawa"
  exposedPorts: []
  volumes: []
  entrypoint:
    - "sh"
    - "./docker-entrypoint.sh"
  cmd: []
  workdir: "/root"

container-structure-test の実行

# コンテナビルド
docker build -t tsubasaogawa/test-for-cst:0.1 .

# container-structure-test のインストール
curl -LO https://storage.googleapis.com/container-structure-test/latest/container-structure-test-linux-amd64 \
  && chmod +x container-structure-test-linux-amd64 \
  && sudo mv container-structure-test-linux-amd64 /usr/local/bin/container-structure-test

# 実行
container-structure-test test \
  --image tsubasaogawa/test-for-cst:0.1 \
  --config .container-structure-test/config.yaml

====================================
====== Test file: config.yaml ======
====================================
=== RUN: Command Test: it says hello
--- PASS
stdout: hello

=== RUN: File Existence Test: date file exists
--- PASS

=== RUN: Metadata Test
--- PASS


=============================================================
========================== RESULTS ==========================
=============================================================
Passes:      3
Failures:    0
Total tests: 3

PASS

テストに成功するとコマンドの終了コードは 0, Failure が生じると 1 となる。

CircleCI に組み込む

.circleci/config.yml
version: "2.1"

jobs:
  test:
    docker:
      - image: docker:19.03.5-git
    steps:
      - checkout
      - setup_remote_docker
      - run:
          name: "Install contrainer-structure-test"
          command: |
            apk update && apk add curl
            curl -LO https://storage.googleapis.com/container-structure-test/latest/container-structure-test-linux-amd64
            chmod +x container-structure-test-linux-amd64
            mv container-structure-test-linux-amd64 /usr/local/bin/container-structure-test
      - run:
          name: "Build container"
          command: docker build -t tsubasaogawa/test-for-cst:test .
      - run:
          name: "Run test"
          command: |
            container-structure-test test \
              --image tsubasaogawa/test-for-cst:test \
              --config .container-structure-test/config.yaml

workflows:
  version: "2"
  run_test:
    jobs:
      - test

ポイント

備考

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?