Upgrade to Pro — share decks privately, control downloads, hide ads and more …

サーバーレスのテストを取り巻く環境

 サーバーレスのテストを取り巻く環境

サーバーレスに入門したので、
サーバーレスのテストを取り巻く開発環境についての考察を書いてみました

Yuji Takaesu

July 20, 2017
Tweet

More Decks by Yuji Takaesu

Other Decks in Technology

Transcript

  1. © Serverworks Co., Ltd. All right reserved. αʔόʔϨεʹೖ໳ͨ͠ͷͰɺ αʔόʔϨεͷςετΛऔΓר͘։ൃ؀ڥ 2017/7/14

    ͞͹ΘͷʮΘʯ#1 αʔόʔϨεςετೖ໳ʢԾʣ ٕज़4՝ ߴߐऱ ༞࣏ʢ͔ͨ͑͢ Ώ͏͡ʣ
  2. © Serverworks Co., Ltd. All right reserved. Who am I

    • גࣜձࣾαʔόʔϫʔΫε ٕज़4՝ • େࡕΦϑΟεɺೖࣾ3ϲ݄ͱͪΐͬͱ • ޷͖ͳݴޠ • Ruby • લ৬Ͱ͸RailsͰͷΞϓϦέʔγϣϯ։ൃΛओʹ΍͍ͬͯ·ͨ͠ • ޷͖ͳAWSαʔϏε • S3 • Twitter: @yusabana Facebook: yu.takaesu 2
  3. ։ൃͰ࢖༻͢Δπʔϧ • OSSͷϛυϧ΢ΣΞ • DB: MySQL, PostgreSQL • Cache: Redis,

    Memcached • Mail: Postfix • OSSͷϓϩάϥϛϯάݴޠɺϑϨʔϜϫʔΫ • PHP,Laravel • Ruby, Rails • nodejs • … etc 6
  4. LocalStackͷΠϯετʔϧ • Πϯετʔϧํ๏͸READMEʹ • https://github.com/localstack/localstack#installing • DockerΛ࢖ͬͯىಈ͢Δͱ؆୯ʢDocker͸ΠϯετʔϧࡁΈΛ૝ఆʣ • ࣍ͷ2ͭͷίϚϯυΛଧ͚ͭͩ •

    ىಈͨ͠Β http://localhost:8080 ͰμογϡϘʔυʹΞΫηε 27 $ git clone https://github.com/localstack/localstack $ docker-compose up MacΛར༻͍ͯ͠Δ৔߹͸ҎԼͰىಈ͢Δ $ TMPDIR=/private$TMPDIR docker-compose up
  5. LocalStack্ͷS3Λaws-sdkͰ৮ͬͯΈΔ 28 [όέοτΛ࡞Δ] $ aws --endpoint-url=http://localhost:4572 s3 mb s3://mytest/ make_bucket:

    mytest [όέοτͷଘࡏ֬ೝ] $ aws --endpoint-url=http://localhost:4572 s3 ls 2006-02-04 01:45:09 mytest [ϑΝΠϧΛΞοϓϩʔυ] $ aws --endpoint-url=http://localhost:4572 s3 cp test.png s3://mytest upload: ./test.png to s3://mytest/test.png [ϑΝΠϧͷଘࡏ֬ೝ] $ aws --endpoint-url=http://localhost:4572 s3 ls s3://mytest 2017-07-18 21:12:10 0 test.png
  6. PythonΛ࢖ͬͨDynamoDB΁ΞΫηε͢Δ(ϓϩμΫγϣϯίʔυ) 31 import boto3 class Dynamodb(): def __init__(self, table_name): self.origindb

    = boto3.resource( 'dynamodb', region_name=‘ap-northeast-1', endpoint_url='http://localhost:4569' ) self.table = self.create_table(table_name) def create_table(self, table_name): table = self.origindb.create_table( #….লུ def put_item(self, item): return self.table.put_item(Item=item) # ଓ͘ # ଓ͖ def get_item(self, year, title): res = self.table.get_item( Key={ 'year': year, 'title': title, }, ) if 'Item' in res: return res['Item'] else: return None def drop_table(self): self.table.delete()
  7. PythonΛ࢖ͬͨDynamoDB΁ΞΫηε͢Δֶशςετ(ςετίʔυ) 32 import unittest import decimal from dynamodb import Dynamodb

    class TestDynamodbOps(unittest.TestCase): def setUp(self): self.db = Dynamodb('Movies') self.db.put_item( { 'year': 2017, 'title': 'The Movie', 'info': { 'rating': decimal.Decimal(0) } } ) def tearDown(self): self.db.drop_table() #ଓ͘ #ଓ͖ def test_valid_get_item(self): item = self.db.get_item(2017, 'The Movie') assert item is not None def test_invalid_get_item(self): item = self.db.get_item(2017, 'InvalidTitle’) assert item is None if __name__ == "__main__": unittest.main()
  8. αʔόʔϨεςετ؀ڥͷ՝୊ • ·ͩ·ͩൃల్্ • LocalStack΋2016೥11݄͘Β͍ʹ։ൃ͕࢝·ͬͨ͹͔Γ • όάͬΆ͍ڍಈ΋݁ߏ͋Δ • ೔ຊޠͳͲͷϚϧνόΠτจࣈؔ࿈Λ࢖͏ͱಈ͔ͳ͔ͬͨΓ… •

    ίϯτϦϏϡʔτνϟϯε • ඞཁʹԠͯ͡ςετίʔυ্Ͱ͸ϞοΫ΍ελϒΛ࢖͏ • ελϒɾϑΣΠΫɾϞοΫͱ͸ http://d.hatena.ne.jp/a-hisame/20090212/1234450327 35