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
ガチ勢によるPipeCD運用大全〜滑らかなCI/CDを添えて〜 / ai-pipecd-encyclopedia
cyberagentdevelopers
PRO
3
200
オーティファイ会社紹介資料 / Autify Company Deck
autifyhq
9
120k
急成長中のWINTICKETにおける品質と開発スピードと向き合ったQA戦略と今後の展望 / winticket-autify
cyberagentdevelopers
PRO
1
160
とあるユーザー企業におけるリスクベースで考えるセキュリティ業務のお話し
4su_para
3
320
2024-10-30-reInventStandby_StudyGroup_Intro
shinichirokawano
1
620
Vueで Webコンポーネントを作って Reactで使う / 20241030-cloudsign-vuefes_after_night
bengo4com
4
2.5k
Java x Spring Boot Warm up
kazu_kichi_67
2
490
Product Engineer Night #6プロダクトエンジニアを育む仕組み・施策
hacomono
PRO
1
470
君は隠しイベントを見つけれるか?
mujyun
0
290
Gradle: The Build System That Loves To Hate You
aurimas
2
140
よくわからんサービスについての問い合わせが来たときの強い味方 Amazon Q について
kazzpapa3
0
220
CyberAgent 生成AI Deep Dive with Amazon Web Services / genai-aws
cyberagentdevelopers
PRO
1
480
Featured
See All Featured
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
167
49k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.8k
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
Optimizing for Happiness
mojombo
376
69k
A Philosophy of Restraint
colly
203
16k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
290
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
Speed Design
sergeychernyshev
24
570
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
28
7.9k
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