Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Software Testing in AWS IoT with The Power of P...
Search
Koji Nakayama
August 31, 2018
Technology
0
1.5k
Software Testing in AWS IoT with The Power of Python
Serverless Meetup Tokyo #10 の登壇資料です。
Koji Nakayama
August 31, 2018
Tweet
Share
More Decks by Koji Nakayama
See All by Koji Nakayama
The ~~Ten~~ Three Most Critical Security Risks in Serverless Architectures
knakayama
2
1.4k
サーバーレス x IoT 〜我々はどういった課題に直面してそれをどのように解決したのか〜
knakayama
0
1.3k
Bloxが切り開くECSの世界
knakayama
1
1.1k
AWS Serverless Application Modelのデプロイ戦略
knakayama
4
2.3k
github-classmethod-study-20170426
knakayama
1
3.6k
サーバレスアーキテクチャはじめの一歩
knakayama
1
1.4k
Other Decks in Technology
See All in Technology
[Ruby] Develop a Morse Code Learning Gem & Beep from Strings
oguressive
1
170
MLOps の現場から
asei
7
650
社外コミュニティで学び社内に活かす共に学ぶプロジェクトの実践/backlogworld2024
nishiuma
0
270
C++26 エラー性動作
faithandbrave
2
790
Opcodeを読んでいたら何故かphp-srcを読んでいた話
murashotaro
0
280
PHPからGoへのマイグレーション for DMMアフィリエイト
yabakokobayashi
1
170
第3回Snowflake女子会_LT登壇資料(合成データ)_Taro_CCCMK
tarotaro0129
0
200
サービスでLLMを採用したばっかりに振り回され続けたこの一年のあれやこれや
segavvy
2
500
LINE Developersプロダクト(LIFF/LINE Login)におけるフロントエンド開発
lycorptech_jp
PRO
0
120
成果を出しながら成長する、アウトプット駆動のキャッチアップ術 / Output-driven catch-up techniques to grow while producing results
aiandrox
0
360
マルチプロダクト開発の現場でAWS Security Hubを1年以上運用して得た教訓
muziyoshiz
3
2.4k
re:Invent 2024 Innovation Talks(NET201)で語られた大切なこと
shotashiratori
0
320
Featured
See All Featured
Measuring & Analyzing Core Web Vitals
bluesmoon
4
170
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
29
2k
Writing Fast Ruby
sferik
628
61k
Imperfection Machines: The Place of Print at Facebook
scottboms
266
13k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
How STYLIGHT went responsive
nonsquared
95
5.2k
Intergalactic Javascript Robots from Outer Space
tanoku
270
27k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
28
900
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
2k
Mobile First: as difficult as doing things right
swwweet
222
9k
Fireside Chat
paigeccino
34
3.1k
Designing on Purpose - Digital PM Summit 2013
jponch
116
7k
Transcript
Software Testing in AWS IoT with The Power of Python
Serverless Meetup Tokyo #10 2018/08/31 Koji Nakayama 1
ᛔ૩奧Օ • Ӿઊ ଛလ • μ϶φϮϊϐϖ໌ୗտᐒ • ςЄϝЄϹφ樄咲᮱ • ςЄϝЄςαϖεЀυϘί
• GitHub: knakayama • অͣΘ΄: 䩚ܖίυί 2
ψϐτϴЀٖͺ͚ͼ • 扖ͯͩ; • AWS IoTΨڥአͭ͵ίЄκϓμώϰ • AWS IoT䌏ͯΡαЀϓνϹЄτϴЀϓφϕ •
Pythonͧ͠Ρͳ΄䋚ᤰֺ • 扖͚ͫͩ; • ϜЄϖγδί΄扖 3
揾ාل樄ͭΔͯ 4
ίυδЀύ 1. AWS IoTΨڥአͭ͵ίЄκϓμώϰ 2. ϓφϕොဩΨᘍ͞Ρ 3. PythonΞΡ䋚ᤰֺ 5
ίυδЀύ 1. AWS IoTΨڥአͭ͵ίЄκϓμώϰ 2. ϓφϕොဩΨᘍ͞Ρ 3. PythonΞΡ䋚ᤰֺ 6
AWS IoTͽͽͣΡԆͩ; • IoTϔϝαφ΄ϔЄό㷧ᵞ • 敋΄֖ᗝఘ䁭Ψࣈ㾩ϤϺϐϕͯΡ • Ⴥଶ/氱ݻͣͿ΄ॠ䶲ఘ䁭ΨίϤϷͽᤒᐏͯΡ • IoTϔϝαφ΄ګக
• 䒐ਛڹείπЀ΄櫮რΨͺͧΡ • 㪥敋ڹ敋΄ჅଶΨ䄜͞Ρ 7
AWS IoTͽͽͣΡԆͩ; • IoTϔϝαφ΄ϔЄό㷧ᵞ ! Քࢧ΅ͩͷΟϢζЄθφ • 敋΄֖ᗝఘ䁭Ψࣈ㾩ϤϺϐϕͯΡ • Ⴥଶ/氱ݻͣͿ΄ॠ䶲ఘ䁭ΨίϤϷͽᤒᐏͯΡ
• IoTϔϝαφ΄ګக • 䒐ਛڹείπЀ΄櫮რΨͺͧΡ • 㪥敋ڹ敋΄ჅଶΨ䄜͞Ρ 8
IoTϔϝαφ΄ϔЄόΨ㷧ᵞͯΡ ίЄκϓμώϰ 9
IoTϔϝαφ΄ϔЄό㷧ᵞ 10
IoTϔϝαφ - AWS IoT 11
AWS IoT - Kinesis Data Streams 12
IoT Rule;AWS IoT SQL • IoT Rule΅SQLΨڥአͭͼIoTϔϝαφ͡ΟᭆΟͼͣ͵ϔЄό Ψ̵֢ͭ姆΄AWSςЄϠφͳ΄ϔЄόΨჁͯͩ;͢ͽͣ Ρ SELECT
*, topic(3) as deviceId, timestamp() as timestamp FROM '$aws/things/+/shadow/update' WHERE state.reported.temperature > 20 13
Kinesis Data Streams - Consumers 14
Consumers - Storage 15
IoTϔϝαφ΄ϔЄόΨ݇ᆙͯΡ ίЄκϓμώϰ 16
IoTϔϝαφ΄ϔЄό݇ᆙ 17
ϳЄσ - ϯϝαϸίϤϷ 18
ϯϝαϸίϤϷ - 扯戣 19
ϯϝαϸίϤϷ - API Gateway 20
ίυδЀύ 1. AWS IoTΨڥአͭ͵ίЄκϓμώϰ 2. ϓφϕොဩΨᘍ͞Ρ ! 3. PythonΞΡ䋚ᤰֺ 21
֜Ψϓφϕͭ͵͚΄͡ • ϓφϕͭ͵͚ᅩ΅2ͺ • IoTϔϝαφ΄ϔЄό΅φϕϹЄυכਂͽͣΡҘ • API͡Οͳ΄ϔЄόΨ݇ᆙͽͣΡҘ • ᥝͯΡαЀϓνϹЄτϴЀϓφϕΨͭ͵͚ 22
αЀϓνϹЄτϴЀϓφϕΨͿͩͽΚΡ͡ • ϯϐμᔮϑЄϸ vs AWS΄䋚厏ह • ϯϐμᔮϑЄϸ(LocalStack/moto/etc...)ͽ䌏䖕ݢᚆ䁰ݳ΅चͳ ͽ亹ΔͱͼΡ • ϢΰЄϖϝϐμϸЄϤΨ㱢ض
• ϯϐμᔮϑЄϸͽ΄αЀϓνϹЄτϴЀϓφϕΨग़ͥ䨗͚ͼ̵AWS 䋚厏हͽ΄αЀϓνϹЄτϴЀΨͥͯΡ䜐ኼ • IoT Rule΅ͳΘͳΘ๚䌏䖕Ξ͜΄ͽAWS΄䋚厏हͽ䋚ෞ 23
ίυδЀύ 1. AWS IoTΨڥአͭ͵ίЄκϓμώϰ 2. ϓφϕොဩΨᘍ͞Ρ 3. PythonΞΡ䋚ᤰֺ ! 24
ϓφϕ΄༷ኼ • MQTTμ϶αίЀϕͽϔЄόΨכਂ • ͩΨϓφϕڹ䋚ෞͭͼͥ͠ • HTTPμ϶αίЀϕͽAPI͡Οͳ΄ϔЄόΨ݇ᆙ • ๗இͭ͵ϔЄό͢ݐͽͣΡ͡ϓφϕ 25
MQTTμ϶αίЀϕͽϔЄόΨכਂ 26
PythonΞΡMQTTള姆 27
PythonΞΡMQTTള姆 28
HTTPμ϶αίЀϕͽϔЄόΨ݇ᆙ 29
HTTPμ϶αίЀϕΞΡϔЄό݇ᆙ 30
HTTPμ϶αίЀϕΞΡϔЄό݇ᆙ 31
HTTPμ϶αίЀϕΞΡϔЄό݇ᆙ 32
ڥአͯΡϯυϲЄϸ • MQTTμ϶αίЀϕ: AWS IoT Device SDK for Python •
ϓφϕϢϹЄϭϼЄμ: pytest • ͳ΄՜: • HTTPμ϶αίЀϕ: requests • Cognito;΄SRP: warrant • AWS IoT;΄Ϸϕ϶α㳌ቘ: tenacity 33
ϓφϕϔЄό΄伛㯪 @pytest.mark.parametrize( 'publish_message', [ (({ 'state': { 'reported': { 'foo':
'bar', 'baz': 'qux' } } })) ], indirect=True) def test_fetch_device_data(api_endpoint, token, publish_message): ... 34
ᥴ抍 • pytest΅fixture;͚͜՛奲ΕͽϥϸϞ樛හΨ conftest.py 懿ᬿݢᚆ1 • ϓφϕξЄφ΄හfixtureΨჁͭͼ̵ϓφϕڹͳΨ䋚ᤈͽͣΡ • ͩͩͽMQTTμ϶αЀϕ͡ΟϔЄό΄Publish;SRP扯戣Ψ亹Δͱͼͥ͠ •
@pytest.mark.parametrize ͽϓφϕϔЄόΨϞ϶ϮЄό۸̵ͭ1ͺ΄ϓφϕξЄφͽ姜嬄 ΨṛΗΡͩ;͢ͽͣΡ2 • indirect=True ͭͼͥ͠;fixtureϓφϕϔЄόΨჁͯͩ;͢ͽͣΡ3 3 https://docs.pytest.org/en/latest/example/parametrize.html#apply-indirect-on-particular-arguments 2 https://docs.pytest.org/en/latest/parametrize.html 1 https://docs.pytest.org/en/latest/plugins.html 35
MQTTμ϶αЀϕ͡Ο΄Publish from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient @pytest.fixture(scope='function') @retry(stop=stop_after_attempt(5), wait=wait_exponential(multiplier=1, max=10)) def
publish_message(request): ... mqtt = AWSIoTMQTTClient('my_thing_01') mqtt.configureEndpoint(..., 8883) mqtt.configureCredentials(...) mqtt.connect() for message in request.param: mqtt.publish(topic, json.dumps(message), 1) time.sleep(5) 36
ᥴ抍 • fixture΅ϔπϹЄόͽ愢හਧͽͣΡ • pytest΅scope;͚͜՛奲ΕͽfixtureΨϓφϕξЄφ樌ͽوํͯΡͩ;͢ݢᚆ4 • ηϣυδμϕ΄ኞ౮πφϕ͢͡͡ΡfixtureΨوํͭͼͥ͠;ϞϢζЄϫЀφ͢ṛΔΡ • tenacity΄ retry
ϔπϹЄόͽAWS IoTεЀϖϪαЀϕ;΄Ϸϕ϶α㳌ቘΨ懿ᬿ5 • pytest䰤伛΄ request fixtureͽϞ϶ϮЄό۸ͫ͵ϓφϕϔЄόΨ݇ᆙͭͼ͚Ρ6 6 https://docs.pytest.org/en/latest/reference.html#request 5 https://github.com/jd/tenacity 4 https://docs.pytest.org/en/latest/fixture.html#scope-sharing-a-fixture-instance-across-tests-in-a-class-module-or- session 37
requestsϯυϲЄϸͽAPIίμψφ import requests @pytest.mark.parametrize( 'publish_message', [ ... def test_fetch_device_data(api_endpoint, token,
publish_message): response = requests.get(f'{api_endpoint}/devices/my_thing_01', headers={'Authorization': token}) body = response.json() assert response.status_code == 200 assert body['deviceId'] == 'my_thing_01' 38
warrantΞΡCognito΄SRP from warrant.aws_srp import AWSSRP @pytest.fixture(scope='session') def token(): config_abs_path =
str(pathlib.Path(__file__).parent.joinpath('configs').resolve()) config = json.load(open(f'{config_abs_path}/config.json', 'r')) aws = AWSSRP(username=config['username'], password=config['password'], pool_id=config['pool_id'], client_id=config['client_id'], client=boto3.client('cognito-idp')) tokens = aws.authenticate_user() return tokens['AuthenticationResult']['AccessToken'] 39
ᥴ抍 • ϓφϕξЄφ΄හ token fixtureΨਧ嬝̵HTTPϥϐύϕЄ μЀΨ戔ਧͭͼAPIΨGET • ݐͭ͵奾ຎ͢㾩ͭ͵Θ΄͡ assert ͽϓφϕ
40
Δ;Η • AWS IoTΨֵ͜;IoTϔϝαφ΄ϔЄό㷧ᵞ/ګக͢ͽͣΡ • ςЄϝЄϹφίϤϷξЄτϴЀ΄αЀϓνϹЄτϴЀϓφϕ΅ ϓφϕ䖕ͮͼ䋚ᤈ厏हΨᘍ䚅ͯΏͣ • Python΄ϯυϲЄϸΨֵ͜;क़;墋㶨αЀϓνϹЄτϴЀ ϓφϕ䨗ͧΡ
41