Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

͋Μͨ୭Ͷʁ • ઒ݪ ༸ฏ(͔ͬͺͱݺΜͰͶ ! ) • iret גࣜձࣾ cloudpack ࣄۀ෦ • AWS Πϯϑϥͷӡ༻อकΛੜۀͱ͠ ͓ͯΓ·͢ • twitter : @inokara • Facebook : inokappa • ޷͖ͳതଟห : Α͔Ζ͏΋Μ 2 JAWS-UG෱ԬɿRebootˌ4ɺߥ໦͞ΜͱAWSͷ࿩Λͯ͠ΈͨΓ JAWS DAYS ࢀ Ճऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏

Slide 3

Slide 3 text

ຊࢿྉͰར༻ͨ͠؀ڥ 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 ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏

Slide 4

Slide 4 text

ຊࢿྉͰར༻ͨ͠؀ڥ 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 ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

ϞοΫͱ͸ ϞοΫΦϒδΣΫτʢMock Objectʣͱ͸ɺιϑτ΢ΣΞςετ ࣌ɺಛʹςετۦಈ։ൃɺϏϔΠϏΞۦಈ։ൃʹ͓͚Δ୅༻ͷԼ ҐϞδϡʔϧελϒͷҰछɻελϒͱൺֱͯ͠ɺݕࠪର৅ͷϞδϡ ʔϧ͕ͦͷԼҐϞδϡʔϧΛਖ਼͘͠ར༻͍ͯ͠Δ͔Ͳ͏͔Λݕূ ͢Δͷʹ࢖ΘΕΔɻ — https:/ /ja.wikipedia.org/wiki/ϞοΫΦϒδΣΫτ ͏ʔΜɺͪΐͬͱཧղʹۤ͠Ήɻ 14 JAWS-UG෱ԬɿRebootˌ4ɺߥ໦͞ΜͱAWSͷ࿩Λͯ͠ΈͨΓ JAWS DAYS ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏

Slide 15

Slide 15 text

ϞοΫͱ͸(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 ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏

Slide 16

Slide 16 text

Mock Λ࢖͑͹ϝιου΍ΦϒδΣΫ τͷظ଴͞ΕΔϨεϙϯε͕࣮ࡍʹൃ ੜ͍ͯ͠Δ͔Λݕূ͢Δ͜ͱ͕ग़དྷ Δ...ͱ͍͏ཧղ. (ޡΓ͕͋Δ৔߹ʹ͸ࢦఠΛ͓ئ͍͠·͢!! ! ) 16 JAWS-UG෱ԬɿRebootˌ4ɺߥ໦͞ΜͱAWSͷ࿩Λͯ͠ΈͨΓ JAWS DAYS ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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 ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏

Slide 21

Slide 21 text

moto sample(1) UnitTest Ͱ࢖͏৔߹ 21 JAWS-UG෱ԬɿRebootˌ4ɺߥ໦͞ΜͱAWSͷ࿩Λͯ͠ΈͨΓ JAWS DAYS ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏

Slide 22

Slide 22 text

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 ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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 ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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 ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏

Slide 29

Slide 29 text

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 ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

·ͱΊ • Mock Λ࢖͑͹ɺϥΠϒϥϦͷςετޮ཰͸͕͋Δ • Moto ͸ boto ϥΠϒϥϦͷϨεϙϯεΛ໛฿ͯ͘͠ΕΔ • Moto ͷݺͼग़͠͸σίϨʔλΛఆٛ͢Δ͚ͩͰ؆୯ • Moto ͷ Standalone Server Mode ͸ଞͷݴޠ͔Β΋ར༻Մೳ • ࢛ेࡀ͔Β͸πʔϧΛॻ͍ͨΒςετΛ͠Α͏ 32 JAWS-UG෱ԬɿRebootˌ4ɺߥ໦͞ΜͱAWSͷ࿩Λͯ͠ΈͨΓ JAWS DAYS ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏

Slide 33

Slide 33 text

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 ࢀՃऀ͔Β࿩Λ͖͍ͯΈͨΓ͠Α͏

Slide 34

Slide 34 text

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