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
LINEスキマニにおけるフロントエンド開発
lycorptech_jp
PRO
0
330
サイボウズフロントエンドエキスパートチームについて / FrontendExpert Team
cybozuinsideout
PRO
5
38k
Server-Side Engineer of LINE Sukimani
lycorp_recruit_jp
0
280
フロントエンド設計にモブ設計を導入してみた / 20241212_cloudsign_TechFrontMeetup
bengo4com
0
1.9k
Qiita埋め込み用スライド
naoki_0531
0
5.2k
LINE Developersプロダクト(LIFF/LINE Login)におけるフロントエンド開発
lycorptech_jp
PRO
0
130
オプトインカメラ:UWB測位を応用したオプトイン型のカメラ計測
matthewlujp
0
190
祝!Iceberg祭開幕!re:Invent 2024データレイク関連アップデート10分総ざらい
kniino
3
330
20241220_S3 tablesの使い方を検証してみた
handy
4
660
PHPerのための計算量入門/Complexity101 for PHPer
hanhan1978
5
260
統計データで2024年の クラウド・インフラ動向を眺める
ysknsid25
2
860
私なりのAIのご紹介 [2024年版]
qt_luigi
1
120
Featured
See All Featured
Building a Scalable Design System with Sketch
lauravandoore
460
33k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
1.2k
Art, The Web, and Tiny UX
lynnandtonic
298
20k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
111
49k
Done Done
chrislema
182
16k
The Pragmatic Product Professional
lauravandoore
32
6.3k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
7k
The Cult of Friendly URLs
andyhume
78
6.1k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
Being A Developer After 40
akosma
87
590k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.3k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
44
9.3k
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