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
社内で最大の技術的負債のリファクタリングに取り組んだお話し
kidooonn
1
540
TanStack Routerに移行するのかい しないのかい、どっちなんだい! / Are you going to migrate to TanStack Router or not? Which one is it?
kaminashi
0
530
The Role of Developer Relations in AI Product Success.
giftojabu1
0
120
Incident Response Practices: Waroom's Features and Future Challenges
rrreeeyyy
0
160
dev 補講: プロダクトセキュリティ / Product security overview
wa6sn
1
2.3k
[CV勉強会@関東 ECCV2024 読み会] オンラインマッピング x トラッキング MapTracker: Tracking with Strided Memory Fusion for Consistent Vector HD Mapping (Chen+, ECCV24)
abemii
0
220
Exadata Database Service on Dedicated Infrastructure(ExaDB-D) UI スクリーン・キャプチャ集
oracle4engineer
PRO
2
3.1k
OCI Security サービス 概要
oracle4engineer
PRO
0
6.5k
Why does continuous profiling matter to developers? #appdevelopercon
salaboy
0
180
Amplify Gen2 Deep Dive / バックエンドの型をいかにしてフロントエンドへ伝えるか #TSKaigi #TSKaigiKansai #AWSAmplifyJP
tacck
PRO
0
370
開発生産性を上げながらビジネスも30倍成長させてきたチームの姿
kamina_zzz
1
1.6k
AWS Lambdaと歩んだ“サーバーレス”と今後 #lambda_10years
yoshidashingo
1
170
Featured
See All Featured
The World Runs on Bad Software
bkeepers
PRO
65
11k
Typedesign – Prime Four
hannesfritz
40
2.4k
A Tale of Four Properties
chriscoyier
156
23k
Mobile First: as difficult as doing things right
swwweet
222
8.9k
Docker and Python
trallard
40
3.1k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
16
2.1k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
Adopting Sorbet at Scale
ufuk
73
9.1k
Thoughts on Productivity
jonyablonski
67
4.3k
Ruby is Unlike a Banana
tanoku
97
11k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.1k
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