E2Eテスト実行環境 (selenium + docker)

要求を満たすために構築方法を調査する。

## 要求
* 簡単に構築できる
* selenium grid環境
* ローカルマシン内で完結
* Infrastructure as code
* 軽い

## 1. docker hostを構築 @ Virtualbox

1. docker for macをインストール
 
    docker-machineコマンドなどももつかえるようになった

    https://github.com/elgalu/docker-selenium に従い実施

2. dockerのホストサーバ作成

    ```bash:dockerホストサーバ作成
$ docker-machine create --driver virtualbox default
``` 
    【引数の説明】
    * create : ホストOSを作成
    * --driver : どこでつくるか (vitrualbox/amazonec2を指定する)
    * default : サーバ名

    ```bash:作成されたことを確認
    $ docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER        ERRORS
default   *        virtualbox   Running   tcp://192.168.99.100:2376           v17.03.0-ce
```
3. dockerホストとして使うマシンをdefaultという名前のVMに設定する。
    
    ```bash:
    $ eval "$(docker-machine env default)"
    ```

4. docker seleniumをpull

    ```
    $ docker pull elgalu/selenium #upgrades to latest if a newer version is available
    ```
    
5. docker run

    ```bash:
    $ docker run -d --name=grid -p 4444:24444 -p 5900:25900 \
    -e TZ="Asia/Tokyo" --shm-size=1g elgalu/selenium
4394cb095f13ca6b09f7a037704d5e5210af8a96897ba74ee14da34a13c1073e
```

    ```bash:
    $ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                                              NAMES
4394cb095f13        elgalu/selenium     "entry.sh"          7 minutes ago       Up 7 minutes        0.0.0.0:4444->24444/tcp, 0.0.0.0:5900->25900/tcp   grid
```

6. テスト前にseleniumの設定確認(推奨)

    ```:bash
    docker exec grid wait_all_done 30s
Waiting for docker-selenium to be ready...
Container docker internal IP: 172.17.0.2
Note if you are in Mac (OSX) 'boot2docker ip' or 'docker-machine ip default' will tell you the relevant IP
Selenium all done and ready for testing!
```

7. grid console確認

    ```bash:dockerホストサーバのIP確認
    $ docker-machine ip default
    ```

    そこにアクセスすると見れました。
    ![Kobito.JE7F9F.png](https://qiita-image-store.s3.amazonaws.com/0/10800/69458411-4f73-93be-93dd-b9befb977c50.png "Kobito.JE7F9F.png")
    
    
8. 停止

    ```
    $ docker exec grid stop
    ```

9. vncの設定も含めてdocker run

    ```bash:
$ docker run -d --name=grid -p 4444:24444 -p 5900:25900 \
  --shm-size=1g -e VNC_PASSWORD=hola \
  elgalu/selenium
$ docker exec grid wait_all_done 10s
Waiting for docker-selenium to be ready...
Container docker internal IP: 172.17.0.2
Note if you are in Mac (OSX) 'boot2docker ip' or 'docker-machine ip default' will tell you the relevant IP
Selenium all done and ready for testing!
$ open vnc://:hola@192.168.88.199:5900
```

## 2. docker hostを構築 @ AWS EC2
1. aws cli インストール & aws configure作成

    ```bash:aws_configure
$ aws configure
AWS Access Key ID [None]: ********************
AWS Secret Access Key [None]: *****************************************
Default region name [None]: ap-northeast-1
Default output format [None]:
```

2. docker host作成

    ```bash:
$ docker-machine create --driver amazonec2 --amazonec2-region ap-northeast-1 --amazonec2-vpc-id vpc-******** docker-test
Running pre-create checks...
Creating machine...
(docker-test) Launching instance...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with ubuntu(systemd)...
Installing Docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env docker-test
```

3. docker-machine envに設定

    ```bash
$ eval "$(docker-machine env docker-test)"
```

4. docker run

    ```bash
$ docker run -d --name=grid -p 4444:24444 -p 5900:25900 \
-e TZ="Asia/Tokyo" --shm-size=1g elgalu/selenium
```

5. grid consoleで確認
    security group に4444の穴を開ける必要がある

    ![Kobito.U4nGoG.png](https://qiita-image-store.s3.amazonaws.com/0/10800/8ae257f0-2767-4662-b118-3ed26e5d09c9.png "Kobito.U4nGoG.png")

    見れました :+1: 	
    ![Kobito.u3amGb.png](https://qiita-image-store.s3.amazonaws.com/0/10800/5ff74b52-f1e0-fc55-7b66-65e5e440e1cb.png "Kobito.u3amGb.png")

6. docker stop & docker host stop

    ```bash
    $ % docker stop grid
grid
    $ docker-machine stop docker-test
Stopping "docker-test"...
Machine "docker-test" was stopped.

## 参考リンク
* scaleするのに良さそう。
    https://github.com/zalando/zalenium

    > A Selenium Grid extension to scale up and down your local grid dynamically with docker containers. It uses docker-selenium to run your tests in Firefox and Chrome locally, and when you need a different browser, your tests get redirected to Sauce Labs and/or BrowserStack and/or TestingBot.