LoginSignup
6
4

More than 5 years have passed since last update.

fakes3+nginxで結合テスト用S3を立てる

Posted at

S3がダウンした場合の結合テストをしたい事はよくある。そんな時はfakes3が便利。以下の感じで簡単にモックを立てられる。

# fakes3 server -r /tmp -p 80

でも、実際に結合テストに使おうとすると課題が出てくる。

1. httpとhttpsの両方サポートしないといけない

ライブラリによっては、httpとhttpsの両方の口を開けておかないといけない。以下でSSL対応できる。

# fakes3 server -r /tmp -p 443 --sslcert=/path/to/cert  --sslkey=/path/to/key

80番ポートと443番ポート両方別々にプロセスを立てて、ディレクトリだけ共有したらいいけどなんかいまいち。

2. nginxのヘッダ転送

fakes3をhttp/httpsの両方対応させるために、fakes3の前段にリバースプロキシとしてnginxを置く。

最初の設定

server {
    listen 80;

    location / {
        proxy_pass http://127.0.0.1:8080;
    }
}

server {
    listen 443 ssl;
    server_name *.example.com;
    ssl_certificate /path/to/cert;
    ssl_certificate_key /path/to/key;

    location / {
        proxy_pass http://127.0.0.1:8080;
    }
}

この設定では、fakes3までリクエストは通るけど、何故かfake s3上にファイルが作られなかった

ここをみると、fakes3はHostヘッダを見てbucket名を特定するため、nginxでHostヘッダをfakes3に転送する必要がある。

修正後

server {
    listen 80;

    location / {
        proxy_set_header Host $host;
        proxy_pass http://127.0.0.1:8080;
    }
}

server {
    listen 443 ssl;
    server_name *.example.com;
    ssl_certificate /path/to/cert;
    ssl_certificate_key /path/to/key;

    location / {
        proxy_set_header Host $host;
        proxy_pass http://127.0.0.1:8080;
    }
}

これでうまくいった。あとはfakes3をいじれば、s3のダウンとか、変なハングとかをモックできる。

6
4
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
6
4