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

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

Yohei Kawahara
April 20, 2017
3k

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

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

Yohei Kawahara

April 20, 2017
Tweet

Transcript

  1. ͋Μͨ୭Ͷʁ • ઒ݪ ༸ฏ(͔ͬͺͱݺΜͰͶ ! ) • iret גࣜձࣾ cloudpack

    ࣄۀ෦ • AWS Πϯϑϥͷӡ༻อकΛੜۀͱ͠ ͓ͯΓ·͢ • twitter : @inokara • Facebook : inokappa • ޷͖ͳതଟห : Α͔Ζ͏΋Μ 2 JAWS-UG෱ԬɿRebootˌ4ɺߥ໦͞ΜͱAWSͷ࿩Λͯ͠ΈͨΓ JAWS DAYS ࢀ Ճऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏
  2. ຊࢿྉͰར༻ͨ͠؀ڥ 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 ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏
  3. ຊࢿྉͰར༻ͨ͠؀ڥ 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 ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏
  4. ϞοΫͱ͸(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 ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏
  5. 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 ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏
  6. 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 ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏
  7. 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 ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏
  8. 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 ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏
  9. 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 ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏
  10. ·ͱΊ • Mock Λ࢖͑͹ɺϥΠϒϥϦͷςετޮ཰͸͕͋Δ • Moto ͸ boto ϥΠϒϥϦͷϨεϙϯεΛ໛฿ͯ͘͠ΕΔ •

    Moto ͷݺͼग़͠͸σίϨʔλΛఆٛ͢Δ͚ͩͰ؆୯ • Moto ͷ Standalone Server Mode ͸ଞͷݴޠ͔Β΋ར༻Մೳ • ࢛ेࡀ͔Β͸πʔϧΛॻ͍ͨΒςετΛ͠Α͏ 32 JAWS-UG෱ԬɿRebootˌ4ɺߥ໦͞ΜͱAWSͷ࿩Λͯ͠ΈͨΓ JAWS DAYS ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏
  11. 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 ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏