masayuki14 / worklog

Record working log by issues.
MIT License
0 stars 0 forks source link

PHPUnit by Docker #1

Closed masayuki14 closed 7 years ago

masayuki14 commented 7 years ago

Vagrant(CentOS6) で動かしている Symfony プロジェクトのテスト実行をDockerでできるように。 まずはMacでやってみてだめならVMのほうでやってみる。

参考ページ

masayuki14 commented 7 years ago

Docker Imageを落としてくる

$ docker pull phpunit/phpunit
Using default tag: latest
latest: Pulling from phpunit/phpunit
2aecc7e1714b: Pull complete
dc14c07df0c7: Pull complete
5e71d4da7091: Pull complete
Digest: sha256:b75368f8dbbf972055ec40791f41374ab50fbd0baa3712933a3a0be85028b8ec
Status: Downloaded newer image for phpunit/phpunit:latest
masayuki14 commented 7 years ago

phpunit.xmlapp/phpunit.xml があるのでそれ。

masayuki14 commented 7 years ago

PHPUnitコンテナを介しテストします。

$ docker run -v $(pwd):/app phpunit/phpunit run

とりあえずうごかないやな。

masayuki14 commented 7 years ago

-v: 問題があるので /tmp にプロジェクトのシンボリックリンクをつくってそこで作業する。

masayuki14 commented 7 years ago

https://hub.docker.com/r/phpunit/phpunit/ 公式ページを参照する。

masayuki14 commented 7 years ago

最新(6.0.6)はPHP7 のサポートっぽい。

masayuki14 commented 7 years ago
% docker run -v $(pwd):/app phpunit/phpunit --configuration /app/app

...

/app/vendor/jms/di-extra-bundle/Finder/PatternFinder.php:156
/app/vendor/jms/di-extra-bundle/Finder/PatternFinder.php:63
/app/vendor/jms/di-extra-bundle/DependencyInjection/Compiler/AnnotationConfigurationPass.php:91
/app/vendor/jms/di-extra-bundle/DependencyInjection/Compiler/AnnotationConfigurationPass.php:62
/app/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Compiler/Compiler.php:104
/app/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/ContainerBuilder.php:598
/app/app/bootstrap.php.cache:2754
/app/app/bootstrap.php.cache:2529
/app/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php:147
/app/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Test/WebTestCase.php:33
/app/src/AppBundle/Tests/Controller/DefaultControllerTest.php:11

ERRORS!
Tests: 4, Assertions: 0, Errors: 4.

https://github.com/JulienBreux/phpunit-docker/blob/master/6.0.6/Dockerfile#L57-L61 コンテナの /app がWorkdirになってるのでそこにマウントする必要がある。

--configurephpunit.xml の指定をして動かす方法ぽい。

masayuki14 commented 7 years ago

php6でテストしてほしいの。 https://github.com/JulienBreux/phpunit-docker/tree/master/5.4.2 Dockerfile をみてると 5.4.3 からphp7になってるので 5.4.2 でやってみよう。

masayuki14 commented 7 years ago
%  docker pull phpunit/phpunit:5.4.2
5.4.2: Pulling from phpunit/phpunit
51f5c6a04d83: Pull complete
3234e782e15b: Pull complete
6450a2e3df13: Pull complete
a42226649bd8: Pull complete
d949d6fac6b4: Pull complete
239f4c7193af: Pull complete
b4901d6b46ce: Pull complete
4c58095fbe14: Pull complete
7f4085ad855d: Pull complete
3641c1a77089: Pull complete
6f5a87555c0c: Pull complete
f10483f071cb: Pull complete
aa5c3a3cd999: Pull complete
e1e6224625a3: Pull complete
8d9c8e817224: Pull complete
a6ebd93e94cb: Pull complete
3955fa0495ec: Pull complete
Digest: sha256:852d9e5ab2f43df0e14aea6b2d033a25320576790bb3fe15c93f1e913cf273ff
Status: Downloaded newer image for phpunit/phpunit:5.4.2
masayuki14 commented 7 years ago
% docker run -v $(pwd):/app --rm phpunit/phpunit:5.4.2 --configuration /app/app/phpunit.xml

メッセージは latest よりちゃんと出てる。


  [Doctrine\DBAL\Exception\DriverException]
  An exception occured in driver: could not find driver

  [Doctrine\DBAL\Driver\PDOException]
  could not find driver

  [PDOException]
  could not find driver

  [Doctrine\DBAL\Exception\DriverException]
  An exception occured in driver: could not find driver

  [Doctrine\DBAL\Driver\PDOException]
  could not find driver

  [PDOException]
  could not find driver

doctrine:migrations:migrate [--write-sql] [--dry-run] [--query-time] [--allow-no-migration] [--configuration [CONFIGURATION]] [--db-configuration [DB-CONFIGURATION]] [--db DB] [--em EM] [--shard SHARD] [-h|--help] [-q|--quiet] [-v|vv|vvv|--verbose] [-V|--version] [--ansi] [--no-ansi] [-n|--no-interaction] [-s|--shell] [--process-isolation] [-e|--env ENV] [--no-debug] [--] <command> [<version>]

  > fixtures found:
      - /app/src/AppBundle/DataFixtures/ORM/Category.yml
      - /app/src/AppBundle/DataFixtures/ORM/Prefecture.yml
      - /app/src/AppBundle/DataFixtures/ORM/Region.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/CategoryTreePath.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/follow.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/Information.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/InformationCategory.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/Sheet.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/Spot.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/SpotImage.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/Stamp.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/User.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/UserSheet.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/UserSheetClip.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/UserSheetComment.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/UserSheetCommentLike.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/UserSheetLike.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/UserStamp.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/UserStampComment.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/UserStampCommentLike.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/UserStampLike.yml

  [Doctrine\DBAL\Exception\DriverException]
  An exception occured in driver: could not find driver

  [Doctrine\DBAL\Driver\PDOException]
  could not find driver

  [PDOException]
  could not find driver

hautelook_alice:doctrine:fixtures:load [-b|--bundle [BUNDLE]] [--fixtures [FIXTURES]] [-em|--manager MANAGER] [--append] [--shard SHARD] [--purge-with-truncate] [-h|--help] [-q|--quiet] [-v|vv|vvv|--verbose] [-V|--version] [--ansi] [--no-ansi] [-n|--no-interaction] [-s|--shell] [--process-isolation] [-e|--env ENV] [--no-debug] [--] <command>

PHPUnit 5.4.8 by Sebastian Bergmann and contributors.

FFF.                                                                4 / 4 (100%)

Time: 28.83 seconds, Memory: 28.00MB

There were 3 failures:

1) AppBundle\Tests\Controller\Api\v1\CategoryControllerTest::testIndex
Failed asserting that false is true.

/app/src/AppBundle/Tests/Controller/Api/v1/CategoryControllerTest.php:16

2) AppBundle\Tests\Controller\Api\v1\PrefectureControllerTest::testGetPrefecture
Failed asserting that false is true.

/app/src/AppBundle/Tests/Controller/Api/v1/PrefectureControllerTest.php:18

3) AppBundle\Tests\Controller\Api\v1\RegionControllerTest::testGetRegions
Failed asserting that false is true.

/app/src/AppBundle/Tests/Controller/Api/v1/RegionControllerTest.php:18

FAILURES!
Tests: 4, Assertions: 5, Failures: 3.

そうかー。Databaseへの接続問題かー。。。

どうしよう。

masayuki14 commented 7 years ago

CentOSの方でDocker動かしてDBのポートフォワードすれば動くのでは?

masayuki14 commented 7 years ago

ということでCentOS6.6にDockerをインストールする。 http://qiita.com/gillax/items/6f864e7c21c6a3e4173b

masayuki14 commented 7 years ago
$ sudo yum update

$ cat > docker.repo <<-EOF
[dockerrepo]
name=Docker Repository
baseurl=https://yum.dockerproject.org/repo/main/centos/6
enabled=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpg
EOF

$ sudo mv docker.repo /etc/yum.repos.d/
$ sudo yum install -y docker-engine

$ docker -v
Docker version 1.7.1, build 786b29d
masayuki14 commented 7 years ago

どっちだろう?わからんからどっちもいれよう。ついでにfish補完も.

$ sudo yum install doker-engine docker-io docker-io-fish-completion
masayuki14 commented 7 years ago

docker ぐるーぷがないようなのでとりあえず sudo つけて動かす。

masayuki14 commented 7 years ago

インストールしたのでVMで同じことする

$ cd /web/backend
$ sudo docker pull phpunit/phpunit:5.4.2
$ sudo docker run -v $(pwd):/app --rm -p 3306:3306 phpunit/phpunit:5.4.2 --configuration /app/app/phpunit.xml
$ sudo docker run -v $(pwd):/app --rm -p 3306:3306 phpunit/phpunit:5.4.2 --configuration /app/app/phpunit.xml
Error response from daemon: Cannot start container 117a33851308cbb73bfeb8829f830ab1270aa422f84b9117a820655cdeaabf0e: Error starting userland proxy: listen tcp 0
.0.0.0:3306: bind: address already in use

ポートをつなぐ向きが違う。

masayuki14 commented 7 years ago
$ sudo docker run -v $(pwd):/app --rm --expose=3306 phpunit/phpunit:5.4.2 --configuration /app/app/phpunit.xml

うまくいかない。

masayuki14 commented 7 years ago
$ sudo docker run -v $(pwd):/app --rm --expose=3306 phpunit/phpunit:5.4.2 --configuration /app/app/phpunit.xml

 // Clearing the cache for the test environment with debug true

 [OK] Cache for the "test" environment (debug=true) was successfully cleared.

  [Doctrine\DBAL\Exception\DriverException]
  An exception occured in driver: could not find driver

  [Doctrine\DBAL\Driver\PDOException]
  could not find driver

  [PDOException]
  could not find driver

  > fixtures found:
      - /app/src/AppBundle/DataFixtures/ORM/Category.yml
      - /app/src/AppBundle/DataFixtures/ORM/Prefecture.yml
      - /app/src/AppBundle/DataFixtures/ORM/Region.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/CategoryTreePath.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/follow.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/Information.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/InformationCategory.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/Sheet.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/Spot.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/SpotImage.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/Stamp.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/User.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/UserSheet.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/UserSheetClip.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/UserSheetComment.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/UserSheetCommentLike.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/UserSheetLike.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/UserStamp.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/UserStampComment.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/UserStampCommentLike.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/UserStampLike.yml

  [Doctrine\DBAL\Exception\DriverException]
  An exception occured in driver: could not find driver

  [Doctrine\DBAL\Driver\PDOException]
  could not find driver

  [PDOException]
  could not find driver

hautelook_alice:doctrine:fixtures:load [-b|--bundle [BUNDLE]] [--fixtures [FIXTURES]] [-em|--manager MANAGER] [--append] [--shard SHARD] [--purge-with-truncate] [-h|--help] [-q|--quiet] [-v|vv|vvv|--verbose] [-V|--version] [--ansi] [--no-ansi] [-n|--no-interaction] [-s|--shell] [--process-isolation] [-e|--env ENV] [--no-debug] [--] <command>

PHPUnit 5.4.8 by Sebastian Bergmann and contributors.

FFF.                                                                4 / 4 (100%)

Time: 15.16 seconds, Memory: 28.00MB

There were 3 failures:

1) AppBundle\Tests\Controller\Api\v1\CategoryControllerTest::testIndex
Failed asserting that false is true.

/app/src/AppBundle/Tests/Controller/Api/v1/CategoryControllerTest.php:16

2) AppBundle\Tests\Controller\Api\v1\PrefectureControllerTest::testGetPrefecture
Failed asserting that false is true.

/app/src/AppBundle/Tests/Controller/Api/v1/PrefectureControllerTest.php:18

3) AppBundle\Tests\Controller\Api\v1\RegionControllerTest::testGetRegions
Failed asserting that false is true.

/app/src/AppBundle/Tests/Controller/Api/v1/RegionControllerTest.php:18

FAILURES!
Tests: 4, Assertions: 5, Failures: 3.

うまくいってないわけじゃなかった。 PDO_Mysql がないからDB周りが動いてない。 テスト自体は成功してるものもある。

masayuki14 commented 7 years ago

https://github.com/JulienBreux/phpunit-docker/blob/master/5.4.2/Dockerfile#L35

Entrypoint が phpunit になっているので、$ docker run の最後のコマンドと引数はそのまま phpunit に渡されている。したがって --configurephpunit の引数になる。

masayuki14 commented 7 years ago

phpunit/phpunit:5.4.2 をベースに Dockerfile を作るアプローチをとる。

masayuki14 commented 7 years ago

docker-php-ext-install とは・・・。

docker-php-ext-installはphp:5.6-apacheイメージに含まれるユーティリティで、オプションを追加してPHPをリコンパイルするものらしいです

http://qiita.com/naga3/items/be1a062075db9339762d

masayuki14 commented 7 years ago
FROM phpunit/phpunit:5.4.2

RUN apt-get -y update && \
    apt-get -y upgrade && \
    apt-get -y install apt-utils

RUN docker-php-ext-install pdo_mysql
$ docker build -t stampking/phpunit:5.4.2 .

$ sudo docker run -v $(pwd):/app --rm --expose=3306 stampking/phpunit:5.4.2 --configuration /app/app/phpunit.xml

動いた。 PDO_Mysql に起因するエラーは解消された。 が、DBにつながらないってはなし。

masayuki14 commented 7 years ago

Guest to Host の通信でポートフォワードする必要がある。

masayuki14 commented 7 years ago

Guest to Host 通信での参考。

masayuki14 commented 7 years ago

ENTRYPOINT が指定されているので、起動時に上書きしてコンテナを実行する。

$ sudo docker run -v $(pwd):/app -it --rm --entrypoint="/bin/bash" stampking/phpunit:5.4.2

コンテナ、VMのそれぞれでIPを確認。$ ip addr でそれぞれ確認する。

masayuki14 commented 7 years ago

コンテナからVMにsshで接続してみる。

# ssh vagrant@172.17.42.1
The authenticity of host '172.17.42.1 (172.17.42.1)' can't be established.
RSA key fingerprint is 18:ed:f4:05:e7:b6:26:d5:e7:72:53:7e:c9:5b:df:c8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.17.42.1' (RSA) to the list of known hosts.
vagrant@172.17.42.1's password:
Last login: Mon Jun 19 02:19:24 2017 from 10.0.2.2
Welcome to your Vagrant-built virtual machine.

つながった。

masayuki14 commented 7 years ago

今度はポートフォワードして 3306 をつなぐ。

# ssh -gfNL 3306:localhost:3306 vagrant@172.17.42.1

そのごテスト実行する

# cd /app
# /usr/local/bin/phpunit -c app/

 // Clearing the cache for the test environment with debug true

 [OK] Cache for the "test" environment (debug=true) was successfully cleared.

                    Application Migrations

No migrations to execute.
  > fixtures found:
      - /app/src/AppBundle/DataFixtures/ORM/Category.yml
      - /app/src/AppBundle/DataFixtures/ORM/Prefecture.yml
      - /app/src/AppBundle/DataFixtures/ORM/Region.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/CategoryTreePath.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/follow.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/Information.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/InformationCategory.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/Sheet.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/Spot.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/SpotImage.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/Stamp.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/User.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/UserSheet.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/UserSheetClip.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/UserSheetComment.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/UserSheetCommentLike.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/UserSheetLike.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/UserStamp.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/UserStampComment.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/UserStampCommentLike.yml
      - /app/src/AppBundle/DataFixtures/ORM/test/UserStampLike.yml
  > purging database
  > fixtures loaded
PHPUnit 5.4.8 by Sebastian Bergmann and contributors.

....                                                                4 / 4 (100%)

Time: 7.56 seconds, Memory: 30.00MB

OK (4 tests, 112 assertions)
masayuki14 commented 7 years ago

これを自動化したいんだがなぁ。

masayuki14 commented 7 years ago

ポートフォワードとテスト実行を ENTRYPOINT で実行させる。 2つのコマンドを実行するので強引に実現した。

FROM phpunit/phpunit:5.4.2

RUN apt-get -y update && \
    apt-get -y upgrade && \
    apt-get -y install apt-utils

RUN docker-php-ext-install pdo_mysql

ENTRYPOINT ssh -oStrictHostKeyChecking=no -i /tmp/private_key -gfNL 3306:localhost:3306 vagrant@$(ip route | grep default | cut -d' ' -f3) && /bin/bash && \
           /usr/local/bin/phpunit -c /app/app
$ sudo docker build -t sandbox/phpunit:5.4.2 ./docker
$ sudo docker run -v $(pwd):/app -v /vagrant/.vagrant/machines/default/virtualbox/private_key:/tmp/private_key --rm  sandbox/phpunit:5.4.2
masayuki14 commented 7 years ago

決め打ちポイント

これでコンテナ起動時にホストにSSH接続してからphpunitを実行する。 でもこれだとphpunitの引数を指定できない。 やはりDBはコンテナを立ててつなぐようにしないといけない。