JAWS-UG福岡:Reboot#4 四十歳からの boto3 とテスト/jawsug_fukuoka_04_syorou_and_boto3_with_test

26c17d2f70a57a8fd9a70885682fcd74?s=47 Yohei Kawahara
April 20, 2017
2k

JAWS-UG福岡:Reboot#4 四十歳からの boto3 とテスト/jawsug_fukuoka_04_syorou_and_boto3_with_test

2017/04/22(土) に執り行われる「JAWS-UG福岡:Reboot#4、荒木さんとAWSの話をしてみたり JAWS DAYS 参加者から話をきいてみたりしよう」でお話します〜

26c17d2f70a57a8fd9a70885682fcd74?s=128

Yohei Kawahara

April 20, 2017
Tweet

Transcript

  1. ࢛ेࡀ͔Βͷ boto3 ͱςετ JAWS-UG෱ԬɿRebootˌ4 ߥ໦͞ΜͱAWSͷ࿩Λͯ͠ΈͨΓ JAWS DAYS ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏ 1 JAWS-UG෱ԬɿRebootˌ4ɺߥ໦͞ΜͱAWSͷ࿩Λͯ͠ΈͨΓ

    JAWS DAYS ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏
  2. ͋Μͨ୭Ͷʁ • ઒ݪ ༸ฏ(͔ͬͺͱݺΜͰͶ ! ) • iret גࣜձࣾ cloudpack

    ࣄۀ෦ • AWS Πϯϑϥͷӡ༻อकΛੜۀͱ͠ ͓ͯΓ·͢ • twitter : @inokara • Facebook : inokappa • ޷͖ͳതଟห : Α͔Ζ͏΋Μ 2 JAWS-UG෱ԬɿRebootˌ4ɺߥ໦͞ΜͱAWSͷ࿩Λͯ͠ΈͨΓ JAWS DAYS ࢀ Ճऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏
  3. ຊࢿྉͰར༻ͨ͠؀ڥ Pyhon $ python --version Python 2.7.13 $ pip freeze

    | egrep 'boto|moto' boto==2.44.0 boto3==1.4.2 botocore==1.4.85 moto==0.4.30 3 JAWS-UG෱ԬɿRebootˌ4ɺߥ໦͞ΜͱAWSͷ࿩Λͯ͠ΈͨΓ JAWS DAYS ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏
  4. ຊࢿྉͰར༻ͨ͠؀ڥ Ruby $ ruby --version ruby 2.3.0p0 (2015-12-25 revision 53290)

    [x86_64-darwin15] $ gem list | grep aws aws-sdk (2.9.7) aws-sdk-core (2.9.7) aws-sdk-resources (2.9.7) aws-sigv4 (1.0.0) 4 JAWS-UG෱ԬɿRebootˌ4ɺߥ໦͞ΜͱAWSͷ࿩Λͯ͠ΈͨΓ JAWS DAYS ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏
  5. ࢛ेࡀ͔Βͷ boto3 ͱςετ 5 JAWS-UG෱ԬɿRebootˌ4ɺߥ໦͞ΜͱAWSͷ࿩Λͯ͠ΈͨΓ JAWS DAYS ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏

  6. զʑͷ໨ͷલͰͦ͜͸͔ͱͳ͘ಈ͍ ͍ͯΔ AWS Ϧιʔε... 6 JAWS-UG෱ԬɿRebootˌ4ɺߥ໦͞ΜͱAWSͷ࿩Λͯ͠ΈͨΓ JAWS DAYS ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏

  7. Α͠ɺaws-sdk(boto3) Λ࢖ͬͨπʔ ϧΛ࡞ͬͯίʔυͰ؅ཧ΍ʂ ! 7 JAWS-UG෱ԬɿRebootˌ4ɺߥ໦͞ΜͱAWSͷ࿩Λͯ͠ΈͨΓ JAWS DAYS ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏

  8. ςετ༻ͷϦιʔε࡞ͬͯͱ... ! σ όοάͯ͠ͱ... 8 JAWS-UG෱ԬɿRebootˌ4ɺߥ໦͞ΜͱAWSͷ࿩Λͯ͠ΈͨΓ JAWS DAYS ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏

  9. ϦιʔεΛফͯ͠...σόοάͯ͠ͱ... ! 9 JAWS-UG෱ԬɿRebootˌ4ɺߥ໦͞ΜͱAWSͷ࿩Λͯ͠ΈͨΓ JAWS DAYS ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏

  10. ͏ΘͬɺϓϩάϥϜ͕σάϨͯͯ͠ EC2 શ෦ফͯ͠΋ͨ...(࣮࿩) ! ※ ࣗ෼ͷݕূ؀ڥͩͬͨͷͰɺटΛׅΒͣʹࡁΜͩ... 10 JAWS-UG෱ԬɿRebootˌ4ɺߥ໦͞ΜͱAWSͷ࿩Λͯ͠ΈͨΓ JAWS DAYS

    ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏
  11. ͜Ε͸৺ଁʹΘΔ͔͹͍ɺͲ͛Μ͔ͤ Μͱ͍͔Μ͖͟... 11 JAWS-UG෱ԬɿRebootˌ4ɺߥ໦͞ΜͱAWSͷ࿩Λͯ͠ΈͨΓ JAWS DAYS ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏

  12. Α͠ɺςετΛॻ͘͜ͱʹ͠Α͏ 12 JAWS-UG෱ԬɿRebootˌ4ɺߥ໦͞ΜͱAWSͷ࿩Λͯ͠ΈͨΓ JAWS DAYS ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏

  13. ςετΛॻ͘ͳΒɺϞοΫͱ͍͏΋ ͷΛ࢖͑͹ྑ͍Β͍͠Ͷ... 13 JAWS-UG෱ԬɿRebootˌ4ɺߥ໦͞ΜͱAWSͷ࿩Λͯ͠ΈͨΓ JAWS DAYS ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏

  14. ϞοΫͱ͸ ϞοΫΦϒδΣΫτʢMock Objectʣͱ͸ɺιϑτ΢ΣΞςετ ࣌ɺಛʹςετۦಈ։ൃɺϏϔΠϏΞۦಈ։ൃʹ͓͚Δ୅༻ͷԼ ҐϞδϡʔϧελϒͷҰछɻελϒͱൺֱͯ͠ɺݕࠪର৅ͷϞδϡ ʔϧ͕ͦͷԼҐϞδϡʔϧΛਖ਼͘͠ར༻͍ͯ͠Δ͔Ͳ͏͔Λݕূ ͢Δͷʹ࢖ΘΕΔɻ — https:/ /ja.wikipedia.org/wiki/ϞοΫΦϒδΣΫτ

    ͏ʔΜɺͪΐͬͱཧղʹۤ͠Ήɻ 14 JAWS-UG෱ԬɿRebootˌ4ɺߥ໦͞ΜͱAWSͷ࿩Λͯ͠ΈͨΓ JAWS DAYS ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏
  15. ϞοΫͱ͸(2) http:/ /blog.morizyun.com/blog/mock-stub-outline-rspec-ruby/ ʹΑΔ ͱ... • Mock is a method/object

    that simulates the behavior of a real method/object. • A mock is an object that we can set expectations on, and which will verify that the expected actions have indeed occurred. ͳΔ΄Ͳ... 15 JAWS-UG෱ԬɿRebootˌ4ɺߥ໦͞ΜͱAWSͷ࿩Λͯ͠ΈͨΓ JAWS DAYS ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏
  16. Mock Λ࢖͑͹ϝιου΍ΦϒδΣΫ τͷظ଴͞ΕΔϨεϙϯε͕࣮ࡍʹൃ ੜ͍ͯ͠Δ͔Λݕূ͢Δ͜ͱ͕ग़དྷ Δ...ͱ͍͏ཧղ. (ޡΓ͕͋Δ৔߹ʹ͸ࢦఠΛ͓ئ͍͠·͢!! ! ) 16 JAWS-UG෱ԬɿRebootˌ4ɺߥ໦͞ΜͱAWSͷ࿩Λͯ͠ΈͨΓ

    JAWS DAYS ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏
  17. ͳΔ΄ͲɺϞοΫΛ࢖͑͹ɺϞϊϗ ϯͷ AWS ϦιʔεʹΞΫηε͠ͳ͘ ͯ΋ಈ࡞֬ೝ΍ςετ͕ग़དྷͦ͏΍ Ͷ. ͏͔ͬΓ࡟আ΋๷͛ͦ͏΍. 17 JAWS-UG෱ԬɿRebootˌ4ɺߥ໦͞ΜͱAWSͷ࿩Λͯ͠ΈͨΓ JAWS

    DAYS ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏
  18. ͫΌ͋ɺaws-sdk(ࠓճ͸ boto3) ͷϨ εϙϯεΛγϡϛϨʔτͯ͘͠ΕΔϞ δϡʔϧͬͯ͋Δͷʁ 18 JAWS-UG෱ԬɿRebootˌ4ɺߥ໦͞ΜͱAWSͷ࿩Λͯ͠ΈͨΓ JAWS DAYS ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏

  19. moto 19 JAWS-UG෱ԬɿRebootˌ4ɺߥ໦͞ΜͱAWSͷ࿩Λͯ͠ΈͨΓ JAWS DAYS ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏

  20. moto ͱ͸ • https:/ /github.com/spulec/moto • Moto is a library

    that allows your python tests to easily mock out the boto library. • Moto ͸ Python ςετͰ boto ϥΠϒϥϦΛ؆୯ʹ໛฿Ͱ͖ΔΑ ͏ʹ͢ΔϥΠϒϥϦͰ͢(Powerded by Google ຋༁) • શͯͷαʔϏε͸αϙʔτ͞Ε͍ͯͳ͍͕ɺओཁͳαʔϏε͸ ໢ཏ͞Ε͍ͯΔ໛༷ 20 JAWS-UG෱ԬɿRebootˌ4ɺߥ໦͞ΜͱAWSͷ࿩Λͯ͠ΈͨΓ JAWS DAYS ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏
  21. moto sample(1) UnitTest Ͱ࢖͏৔߹ 21 JAWS-UG෱ԬɿRebootˌ4ɺߥ໦͞ΜͱAWSͷ࿩Λͯ͠ΈͨΓ JAWS DAYS ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏

  22. UnitTest Ͱ࢖͏৔߹ ςετ͍ͨؔ͠਺ʹσίϨʔλΛ͚ͭΔ͜ͱͰ moto ͷػೳΛར༻ ͢Δ͜ͱ͕ग़དྷ·͢. import unittest from moto

    import mock_ec2 ... @mock_ec2 def test_list_ec2_instances(self): instances = [e for e in list_ec2_instances()] self.assertEqual([], instances) 22 JAWS-UG෱ԬɿRebootˌ4ɺߥ໦͞ΜͱAWSͷ࿩Λͯ͠ΈͨΓ JAWS DAYS ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏
  23. σϞ 23 JAWS-UG෱ԬɿRebootˌ4ɺߥ໦͞ΜͱAWSͷ࿩Λͯ͠ΈͨΓ JAWS DAYS ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏

  24. ৄ͘͠͸αϯϓϧ΁ Go!! ! https:/ /github.com/inokappa/moto-fuyuki 24 JAWS-UG෱ԬɿRebootˌ4ɺߥ໦͞ΜͱAWSͷ࿩Λͯ͠ΈͨΓ JAWS DAYS ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏

  25. moto sample(2) Standalone Server Mode 25 JAWS-UG෱ԬɿRebootˌ4ɺߥ໦͞ΜͱAWSͷ࿩Λͯ͠ΈͨΓ JAWS DAYS ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏

  26. Standalone Server Mode AWS SDK ͷϨεϙϯεΛฦ͢αʔόʔΛىಈ͢Δ͜ͱ͕ग़དྷ·͢. $ moto_server ec2 *

    Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) $ moto_server s3 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) 26 JAWS-UG෱ԬɿRebootˌ4ɺߥ໦͞ΜͱAWSͷ࿩Λͯ͠ΈͨΓ JAWS DAYS ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏
  27. ͱ͍͏͜ͱ͸... 27 JAWS-UG෱ԬɿRebootˌ4ɺߥ໦͞ΜͱAWSͷ࿩Λͯ͠ΈͨΓ JAWS DAYS ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏

  28. awscli sample awscli Ͱ΋ར༻ՄೳͳΜͰ͢. $ aws \ > --profile mock_profile

    \ > --region us-west-2 \ > --endpoint-url http://127.0.0.1:5000 \ > ec2 describe-instances \ > --query 'Reservations[].Instances[].InstanceId' --output text 28 JAWS-UG෱ԬɿRebootˌ4ɺߥ໦͞ΜͱAWSͷ࿩Λͯ͠ΈͨΓ JAWS DAYS ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏
  29. Rspec sample Rspec ͷ before ۟Ͱ moto-server ΛݺͿ͜ͱͩͬͯग़དྷ·͢. describe 'MyEc2'

    do before(:all) do @output, @input = PTY.spawn("moto_server ec2") client = Aws::EC2::Client.new(region: 'us-west-2', endpoint: 'http://127.0.0.1:5000', profile: 'mock_profile') res = client.run_instances({ image_id: 'ami-20d1c544', min_count: 3, max_count: 3 }) @instance_ids = res.instances.map { |instance| instance.instance_id } ... 29 JAWS-UG෱ԬɿRebootˌ4ɺߥ໦͞ΜͱAWSͷ࿩Λͯ͠ΈͨΓ JAWS DAYS ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏
  30. σϞ 30 JAWS-UG෱ԬɿRebootˌ4ɺߥ໦͞ΜͱAWSͷ࿩Λͯ͠ΈͨΓ JAWS DAYS ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏

  31. ৄ͘͠͸αϯϓϧ΁ Go!! ! https:/ /github.com/inokappa/moto-server-sample 31 JAWS-UG෱ԬɿRebootˌ4ɺߥ໦͞ΜͱAWSͷ࿩Λͯ͠ΈͨΓ JAWS DAYS ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏

  32. ·ͱΊ • Mock Λ࢖͑͹ɺϥΠϒϥϦͷςετޮ཰͸͕͋Δ • Moto ͸ boto ϥΠϒϥϦͷϨεϙϯεΛ໛฿ͯ͘͠ΕΔ •

    Moto ͷݺͼग़͠͸σίϨʔλΛఆٛ͢Δ͚ͩͰ؆୯ • Moto ͷ Standalone Server Mode ͸ଞͷݴޠ͔Β΋ར༻Մೳ • ࢛ेࡀ͔Β͸πʔϧΛॻ͍ͨΒςετΛ͠Α͏ 32 JAWS-UG෱ԬɿRebootˌ4ɺߥ໦͞ΜͱAWSͷ࿩Λͯ͠ΈͨΓ JAWS DAYS ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏
  33. Special Thanks • cloudpack 1p ͞Μ ! • https:/ /github.com/spulec/moto

    ! • http:/ /blog.morizyun.com/blog/mock-stub-outline-rspec-ruby/ ! • https:/ /blog.hipchat.com/2017/03/08/better-python-testing- mocking-aws/ ! 33 JAWS-UG෱ԬɿRebootˌ4ɺߥ໦͞ΜͱAWSͷ࿩Λͯ͠ΈͨΓ JAWS DAYS ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏
  34. ͓ΘΓ! 34 JAWS-UG෱ԬɿRebootˌ4ɺߥ໦͞ΜͱAWSͷ࿩Λͯ͠ΈͨΓ JAWS DAYS ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏