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

runnによるAPIのシナリオテストの導入と自動化 / stac2022

runnによるAPIのシナリオテストの導入と自動化 / stac2022

発表者はYAMLベースでAPIのシナリオテストを書くことが可能なrunn (https://github.com/k1LoW/runn) というツールを開発しています。本発表ではrunnの導入や事例、開発者の想定と実際について紹介します。

Ken’ichiro Oyama

December 03, 2022
Tweet

More Decks by Ken’ichiro Oyama

Other Decks in Technology

Transcript

  1. 5 • https://github.com/k1LoW/tbls • CIϑϨϯυϦͳσʔλϕʔευΩϡϝϯτੜ ੒πʔϧ • PostgreSQL, MySQLͳͲͷRDBMS͚ͩͰͳ ͘ɺBigQuery΍Amazon

    DynamoDB, Cloud Spannerͱ͍ͬͨσʔλιʔεʹ΋ରԠ • υΩϡϝϯτΛॆ࣮ͤ͞Δػೳͱͯ͠ϝλ σʔλΛΞϊςʔγϣϯ͢Δ͜ͱ͕Մೳ • ςʔϒϧɺΧϥϜͳͲͷίϝϯτ෇༩͚ͩͰ ͳ͘λάɺϦϨʔγϣϯͳͲ tbls ࡞͍ͬͯΔOSS঺հ
  2. • https://github.com/k1LoW/octocov • ίʔυϝτϦΫεπʔϧ…ίʔυΧόϨοδɺCode to Test Ratioɺςετ࣮ߦ࣌ؒ • ଟ਺ͷϓϩάϥϛϯάݴޠͱɺओཁͳΧόϨοδϨϙʔτϑΥʔϚοτʹରԠ •

    GitHub Actionsͷactionͱͯ͠ίʔυϝτϦΫεΛूܭɺP/RίϝϯτɺϨϙʔτ • CLIπʔϧͱͯ͠ίʔυΧόϨοδΛूܭɻදࣔ • ΧόϨοδόοδੜ੒ octocov 6 6 ࡞͍ͬͯΔOSS঺հ
  3. 9

  4. • runn ( means "Run N". is pronounced /rʌ́n én/.

    ) is a package/tool for running operations following a scenario. • γφϦΦΛYAMLͰॻ͍ͯͦΕΛ΋ͱʹૢ࡞ʢΦϖϨʔγϣϯʣΛࣗಈԽͰ͖Δύο έʔδ/πʔϧ • Goͷύοέʔδͱͯ͠΋ίϚϯυʢrunnίϚϯυʣͱͯ͠΋࢖͑Δ • runn run [γφϦΦσΟϨΫτϦ]/*.yml • ʮΦϖϨʔγϣϯࣗಈԽʯͱ޿ൣғΛλʔήοτʹ͍ͯ͠Δ͕ɺAPIͷγφϦΦς ετ͕ϑΝʔετϓϥΠΦϦςΟͳϢʔεέʔεʢ։ൃͷ͖͔͚ͬͰ΋͋ΔͨΊʣ 10 https://github.com/k1LoW/runn runn
  5. • 1όΠφϦͷγφϦΦ࣮ߦͷCLIίϚϯυʢπʔϧʣͱͯ͠࢖༻Ͱ͖Δ • 1όΠφϦ = CIϑϨϯυϦ • ͞·͟·ͳϓϩτίϧʹରԠ͓ͯ͠Γɺ1ͭͷγφϦΦ಺Ͱෳ਺૊Έ߹ΘͤΔ͜ͱ͕Մೳ • HTTP

    • gRPC • DatabaseʢSQLʣ • Chrome DevTools Protocol • ೚ҙίϚϯυͷ࣮ߦʢϩʔΧϧ/SSHʣ • GoͷTest Helperʢύοέʔδʣͱͯ͠࢖༻Ͱ͖Δ 11 https://github.com/k1LoW/runn runnͷಛ௃ʢv0.53.0ʣʢ1/2ʣ
  6. 12 https://github.com/k1LoW/runn runnͷಛ௃ʢv0.53.0ʣʢ2/2ʣ • γφϦΦϑΝΠϧʢYAMLʣͷϑΥʔϚοτ͕OpenAPI SpecϥΠΫ • γφϦΦ಺ͷεςοϓؒͷ஋͕ࣗಈͰ࿈ܞ͞Ε͍ͯΔ • ʮεςοϓAΛ࣮ߦ͔ͯ͠Βͦͷ݁ՌΛ࢖ͬͯεςοϓBΛ࣮ߦ͢Δʯͱ͍͏γφϦΦΛߏங͠΍͍͢

    • ෳ਺ͷγφϦΦͷ࣮ߦΛ૝ఆ࣮ͨ͠૷ • ෳ਺ͷγφϦΦͷ෼ׂ࣮ߦɺαϯϓϦϯά࣮ߦɺϥϯμϜ࣮ߦͳͲ͕Մೳ • ϧʔϓ࣮ߦ΍ϦτϥΠ࣮ߦͷ࢓૊Έ͕૊ΈࠐΈͰඋΘ͍ͬͯΔ • ಉ͡γφϦΦϑΝΠϧΛ࢖ͬͨෛՙςετʢϩʔυςετʣ͕Մೳ • ίϚϯυΛ runn run ͔Β runn loadt ʹมߋ͢Δ͚ͩɻෳ਺γφϦΦ࣮ߦ΋ͦͷ··Մೳ • γφϦΦ࣮ߦͷϩά΋ه࿥Մೳ • γφϦΦ࣮ߦͷϓϩϑΝΠϧ΋औಘՄೳʢ֤εςοϓͷܦա࣌ؒʣ
  7. • runn͸1ͭҎ্ͷγφϦΦΛ࣮ߦͯ͠ɺͦΕͧΕͷ੒ޭ/ࣦഊΛΈΔ • γφϦΦ͸1ͭҎ্ͷεςοϓ͔Βߏ੒͞Ε͍ͯΔ • 1γφϦΦ1YAMLϑΝΠϧʹͳ͍ͬͯΔʢRunbookͱݺΜͰ͍Δʣ • γφϦΦ͝ͱʹRunnerͱݺΜͰ͍Δεςοϓ࣮ߦίϯϙʔωϯτΛఆٛ͠ɺͦͷRunnerΛ࢖֤ͬͯ εςοϓΛ࣮ߦ͍ͯ͘͠ •

    Runnerʹ͸γφϦΦ಺ʹఆ͕ٛඞཁͳ௨ৗͷRunnerͱɺطʹrunnͰ༻ҙ͍ͯ͠ΔBuilt-in runnerͷ2 छྨ͕͋Δ • Runner: HTTP Runner, DB Runner, gRPC Runner, CDP Runner, SSH Runner • Built-in runner: Exec runner, Test runner, Dump runner, Include runner, Bind runner 13 https://github.com/k1LoW/runn runnͷΞʔΩςΫνϟ
  8. • γφϦΦϑΝΠϧ࡞੒ͷͨΊͷαϙʔτίϚϯυ • runn newͷҾ਺ʹ஋Λ౉͢͜ͱͰγφϦΦϑΝΠϧΛ࡞੒͢Δ • cURLίϚϯυ • gRPCurlίϚϯυ •

    ΞΫηεϩά • ʢOpenAPI SpecͷυΩϡϝϯτʁʣ • ʢPostmanͷCollectionʁʣ • طଘͷγφϦΦʹεςοϓͱͯ͠௥Ճ͢Δ͜ͱ΋Մೳ 24 https://github.com/k1LoW/runn runn new
  9. • ൃදऀ͸ιϑτ΢ΣΞΤϯδχΞ • ओʹόοΫΤϯυͷઃܭͱ։ൃΛ୲౰ • ςετΛઐ໳ʹ͍ͯ͠ΔΤϯδχΞͰ͸ͳ͍ • GoͰAPIαʔόΛ։ൃ͍ͯ͠Δ • ୲౰ίϯϙʔωϯτʢAPIαʔόʣΛઃܭ͍ͯ͘͠ʹ͋ͨͬͯɺෳ਺ͷAPIΛ·͍ͨ

    Ͱ1ͭͷػೳΛ࣮ݱ͢ΔΑ͏ͳઃܭʹͳΔ͜ͱ͕ݟ͖͑ͯͨɻ • APIεΩʔϚͱσʔλϕʔεεΩʔϚ͸৻ॏʹઃܭͨ͠ɻ͔͠͠ɺίʔυϕʔε͸࠷ ॳ͔Β࠷దͳΞʔΩςΫνϟΛબ୒Ͱ͖Δͱ͸ࢥ͑ͳ͔ͬͨɻ 27 ςετπʔϧ։ൃऀͱͯ͠ͷ૝ఆϢʔεέʔε ։ൃͷಈػ
  10. • ػೳ୯Ґʢෳ਺APIάϧʔϓ͔ΒͳΔ͠͹͠͹ εςʔτϑϧͳػೳʣͷςετΛॆ࣮ͤ͞Δ͜ ͱʹΑΓɺͨͱ్͑தͷϦΞʔΩςΫςΟϯά ͕͋ͬͯ΋ػೳͷอূ͕Ͱ͖ΔΑ͏ʹ͍ͨ͠ɻ • ։ൃॳظ͔ΒUnitςετͱಉ༷ʹػೳʹରͯ͠ ͷγφϦΦςετͷॆ࣮Λ໨ࢦ͢ɻ • ͦͷͨΊʹγφϦΦςετΛUnitςετฒΈʹ

    ؆୯ʹ͢Δπʔϧͱͯ͠runnΛ։ൃɻ 28 ςετπʔϧ։ൃऀͱͯ͠ͷ૝ఆϢʔεέʔε Testing trophyʢςεςΟϯάτϩϑΟʔʣ https://kentcdodds.com/blog/the-testing-trophy-and-testing-classifications 
 https://twitter.com/kentcdodds/status/960723172591992832
  11. • GoͰAPIαʔόΛ։ൃ͍ͯ͠Διϑτ΢ΣΞΤϯδχΞ͕ςετίʔυʹ૊Έ͜ΜͰ࢖ ༻͢Δ • net/http/httptest ύοέʔδͱ૬ੑ͕͍͍ • γφϦΦͷ֦ॆ΋YAMLΛॻ͚ͩ͘Ͱྑ͍ • OpenAPI

    SpecϥΠΫͳϑΥʔϚοτΛ࠾༻ͨ͠ͷ΋ɺγφϦΦςετΛॻ͘Τϯ δχΞͱOpenAPIυΩϡϝϯτΛॻ͘ΤϯδχΞΛಉҰਓ෺ͱͯ͠ߟ͍͑ͯͨͨΊ • 1όΠφϦͷͨΊΠϯετʔϧ΋؆୯ɻCI΁ͷ૊ΈࠐΈ΋༰қɻ 29 ςετπʔϧ։ൃऀͱͯ͠ͷ૝ఆϢʔεέʔε ։ൃऀ͕૝ఆ͍ͯͨ͠ओͳϢʔεέʔε
  12. • ૝ఆ֎ͷϢʔεέʔεΛ౿·͑ͯɺ͋ΒͨΊͯ • runn ( means "Run N". is pronounced

    /rʌ́n én/. ) is a package/tool for running operations following a scenario. • ʮΦϖϨʔγϣϯࣗಈԽʯΛπʔϧͷλʔήοτͱ͢ΔͷͰ͋Ε͹ɺʢ౰ॳ͔Β͢ Δͱʣ૝ఆ֎ͳϢʔεέʔε΋෇ਵͨ͠৽ػೳ΋ʮਖ਼౰ͳྲྀΕʯͱ΋͍͑Δ + γφϦΦ࣮ߦϩάͷه࿥ػೳ … ࣮ߦՄೳͳૢ࡞खॱॻͱͯ͠ + SSH Runner … αʔόΦϖϨʔγϣϯࣗಈԽͷखஈͱͯ͠ • ʮ1όΠφϦͰಋೖ͕༰қʯʮՄಡੑߴ͍γφϦΦϑΝΠϧʯͷ2ͭ͸ݫक 36 ࣮ࡍͷϢʔεέʔε ʮΦϖϨʔγϣϯࣗಈԽʯͱ͍͏λʔήοτ
  13. 39 ͓ΘΓʹ ࠓޙ • ࠓrunnʹ଍Γͳ͍΋ͷͱͯ͠͸ʮ೔ຊޠͷ৘ใʯͩͱߟ͍͑ͯΔ • runnͷػೳʹରͯ͠গͳ͍ • ࡞ऀ͕ॻ͍͍ͯͳ͍ͷͰɺͦΕ͸ͦ͏ •

    ݱࡏʮrunn cookbookʯΛZennͷbookͱͯ͠४උத • ϨγϐΛඞཁʹԠͯ͡ਵ࣌௥Ճ͍ͯ͘͠ํࣜ • ΋͠ڵຯ͕Ͱ·ͨ͠Βੋඇࢼ͠ʹ࢖ͬͯΈ͍ͯͩ͘͞ʂ • macOSͳΒ brew install k1LoW/tap/runn • LinuxͳΒ deb΍RPM΍apkύοέʔδ • docker pull ghcr.io/k1low/runn:latest