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.3k
Other Decks in Technology
See All in Technology
フルカイテン株式会社 採用資料
fullkaiten
0
36k
AWS CDKでデータリストアの運用、どのように設計する?~Aurora・EFSの実践事例を紹介~/aws-cdk-data-restore-aurora-efs
mhrtech
4
650
Oracle Base Database Service 技術詳細
oracle4engineer
PRO
5
49k
pandasはPolarsに性能面で追いつき追い越せるのか
vaaaaanquish
4
4.5k
事業者間調整の行間を読む 調整の具体事例
sugiim
0
1.4k
Jr. Championsになって、強く連携しながらAWSをもっと使いたい!~AWSに対する期待と行動~
amixedcolor
0
190
AWS re:Inventを徹底的に楽しむためのTips / Tips for thoroughly enjoying AWS re:Invent
yuj1osm
1
560
Autify Company Deck
autifyhq
1
39k
独自ツール開発でスタジオ撮影をDX!「VLS(Virtual LED Studio)」 / dx-studio-vls
cyberagentdevelopers
PRO
1
180
プロダクトエンジニアが活躍する環境を作りたくて 事業責任者になった話 ~プロダクトエンジニアの行き着く先~
gimupop
1
480
visionOSでの空間表現実装とImmersive Video表示について / ai-immersive-visionos
cyberagentdevelopers
PRO
1
110
いまならこう作りたい AWSコンテナ[本格]入門ハンズオン 〜2024年版 ハンズオンの構想〜
horsewin
9
2.1k
Featured
See All Featured
Designing Dashboards & Data Visualisations in Web Apps
destraynor
228
52k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Fontdeck: Realign not Redesign
paulrobertlloyd
81
5.2k
Done Done
chrislema
181
16k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
32
1.8k
Designing for Performance
lara
604
68k
Visualization
eitanlees
144
15k
Reflections from 52 weeks, 52 projects
jeffersonlam
346
20k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.2k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
Learning to Love Humans: Emotional Interface Design
aarron
272
40k
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