20170813 django api server unit test and remote debugging

48
Jongwon Han Django API Server Unit Test and Remote Debugging PyCon Korea 2017

Transcript of 20170813 django api server unit test and remote debugging

Page 1: 20170813 django api server unit test and remote debugging

Jongwon�Han

Django�API�Server�Unit�Testand�Remote�Debugging

PyCon�Korea�2017

Page 2: 20170813 django api server unit test and remote debugging

발표자�소개

• 한종원�

• Python과�Cloud�Infra,�Lean/Agile�방법론�그리고�애플의�제품을�사랑.�

• 2012년�석사�학위를�마치고,�startup을�시작(이때부터�Python,�AWS를�production�level에서�사용)�

• '의미가�있는�일을,�올바르게�하고�싶다.'�

• 경력�

• (현)�DevOps�전문�스타트업�‘HB�Smith’�대표�

• 택시�O2O�서비스�스타트업�‘Kanizsa�Lab’의�backend�server�/�infra�devops�담당�

• Cloud�computing�전문�스타트업�'A2�company'�co-founder�(‘KINX’에�인수합병)�

• NEXON�'MapleStory�국내�Live�Team'에서�DBA,�SA로�근무�(산업�기능�요원)

https://www.linkedin.com/in/addnull/

https://hbsmith.io

Page 3: 20170813 django api server unit test and remote debugging

Startup�경력�=�Python�사용�기간

Page 4: 20170813 django api server unit test and remote debugging

• 발표�대상:�중급�Django�API�server�개발자�

• 발표�순서�

• Django�API�server�unit�test�

• 나의�개발�환경�구축기�

• Remote�Debugging�

• Wrap�Up

(예상�발표�시간:�35~40분)

Page 5: 20170813 django api server unit test and remote debugging

Django�API�server�unit�test

Page 6: 20170813 django api server unit test and remote debugging

Django�API�server�unit�test

• Why�Django?�

• Python�이라서!�

• 정형화된�code�structure�

• 풍부한�troubleshooting�과�사용자�그룹

->�누가�개발해도�결과가�비슷

https://www.djangoproject.com/

Page 7: 20170813 django api server unit test and remote debugging

Django�API�server�unit�test

• 물론�단점도�많습니다.�

• 다소�느리고,�무겁죠.�

• 살짝�아쉬운�ORM�최적화.

->�난�API�server만�만들고�싶고,HTML�template�같은�건�안쓰는데…

->�DML은�ORM�한테�맡겨도,database�schema(DDL)는�직접�관리하세요.

Page 8: 20170813 django api server unit test and remote debugging

Django�API�server�unit�test

• 그럼�Django�말고�더�좋은게�있을까요?

->�그래도�API�개발�기한(due�date)을�지키기�위해선, Django�+�DRF�만한�것도�없는�것�같아요.

http://www.django-rest-framework.org/

Page 9: 20170813 django api server unit test and remote debugging

Django�API�server�unit�test

• What�is�Django�API�server�unit�test?�

• 여기서�말하는�‘unit’은�각각의�API를�의미�

• 즉,�unit�test는�실제�API�call을�해보고의도한�결과가�나오는지�확인�

• 되도록�가능한�모든�API�call을�test

->�실행되지�않는�bug는�bug가�아니다?!?!

Page 10: 20170813 django api server unit test and remote debugging

Django�API�server�unit�test

• API�call�example�

• HTTP�method:�

• base�URL:�

• endpoint(path):�

• query�parameters:�

(GET)�https://api.example.com/v1/users/?role=admin&status=active

GET,�POST,�PUT,�DELETE�…

https://api.example.com/v1

/users/

role=admin&status=active

Page 11: 20170813 django api server unit test and remote debugging

Django�API�server�unit�test

• unit(API)�test에서�“의도한�결과”란?

->�성공(2XX)�뿐만�아니라�일부러�실패(4XX)하는지도�확인

Page 12: 20170813 django api server unit test and remote debugging

Django�API�server�unit�test

• “되도록�가능한�모든”�API�call�test를�위해서�

• Django의�in-memory�DB:�

• DRF의�‘APITestCase’�Class:�

• ‘factory_boy’:

->�매번�깨끗한�test�DB�생성

->�API�call�template

->�random�parameter�value�generator

https://github.com/FactoryBoy/factory_boy

Page 13: 20170813 django api server unit test and remote debugging

Django�API�server�unit�test

• 오늘�발표에�사용된�code�

• API�와�unit�test�example

• HTTP�method:�

• endpoint(path):�

• query�parameters:

GET

/numbers/

start=1&end=2

‘start’와�‘end’라는�정수�2개�사이에�있는�모든�정수를�반환하는�API

https://github.com/HardBoiledSmith/tabris/tree/master/pycon_kr_2017_addnull

Page 14: 20170813 django api server unit test and remote debugging

Django�API�server�unit�test

• API�와�unit�test�example�(cont.)

Page 15: 20170813 django api server unit test and remote debugging

Django�API�server�unit�test

• API�와�unit�test�example�(cont.)

Page 16: 20170813 django api server unit test and remote debugging

`

• API�와�unit�test�example�(cont.)

Page 17: 20170813 django api server unit test and remote debugging

Django�API�server�unit�test

• API�와�unit�test�example�(cont.)

Page 18: 20170813 django api server unit test and remote debugging

Django�API�server�unit�test

• unit�test�실행�결과

Page 19: 20170813 django api server unit test and remote debugging

Django�API�server�unit�test

• unit�test�실행�결과�(cont.)

Page 20: 20170813 django api server unit test and remote debugging

나의�개발�환경�구축기

Page 21: 20170813 django api server unit test and remote debugging

나의�개발�환경�구축기

• 저는�Mac�OS�X�에서�PyCharm으로�개발합니다.하지만�실제�서버�환경은�AWS의�Amazon�Linux죠.

CentOS와는�다르다!�CentOS와는!

Page 22: 20170813 django api server unit test and remote debugging

나의�개발�환경�구축기

• 개발�환경과�실제�서버�환경�차이에서�오는�문제들…�

• 그럼�‘Virtualenv’�쓰면�어떤가요?

->�Mac�OS�X에서는�좀�이상하게�동작해요.�

->�그리고�결국�동일한�환경이�아니잖아요.

Page 23: 20170813 django api server unit test and remote debugging

나의�개발�환경�구축기

• 여기�Vagrant�라는�silver�bullet이�있습니다.�

• 심지어�Amazon�Linux�box도�있습니다.

https://www.vagrantup.com/

https://app.vagrantup.com/mvbcoding/boxes/awslinux/

Page 24: 20170813 django api server unit test and remote debugging

나의�개발�환경�구축기

• 즉,�개발을�위한�Amazon�Linux�VM을�만들었습니다.

Page 25: 20170813 django api server unit test and remote debugging

나의�개발�환경�구축기

• ‘Vagrantfile’https://github.com/HardBoiledSmith/tabris/tree/master/pycon_kr_2017_addnull

Page 26: 20170813 django api server unit test and remote debugging

나의�개발�환경�구축기

• ‘provisioning.py’https://github.com/HardBoiledSmith/tabris/tree/master/pycon_kr_2017_addnull

Page 27: 20170813 django api server unit test and remote debugging

나의�개발�환경�구축기

• Vagrant�VM을�개발�환경으로�쓰면,

->�실제�서버와�거의�동일한�환경을�5~10분만에�구축�

->�개발팀의�모든�개발자가�동일한�환경을�사용�

->�뭔가�개발�환경이�이상해지면,�속편하게�VM�날리고,�새로�VM�생성

misconfiguration

dependency�hell

package�version�conflict

OOM garbage�files

Page 28: 20170813 django api server unit test and remote debugging

Remote�Debugging

Page 29: 20170813 django api server unit test and remote debugging

Remote�Debugging

• Why�remote�debugging?

->�실제�서버와�거의�동일한�환경을�위해서�Vagrant�VM을�쓰지만,�

->�PyCharm의�다양하고�편리한�debugger�기능을�쓰고�싶었어요.

Page 30: 20170813 django api server unit test and remote debugging

Remote�Debugging

• Mac�OS�X에서�실행되는�PyCharm�입장에서Vagrant�VM�안의�Django�API�server는�딴�세계

Page 31: 20170813 django api server unit test and remote debugging

Remote�Debugging

• PyCharm에서�지원하는�2가지�remote�debugging�

• Using�a�remote�interpreter�

• Using�Python�Debug�Server

->�기존�source�code�수정�불필요�(추천)

->�기존�source�code�수정�필요

Page 32: 20170813 django api server unit test and remote debugging

Remote�Debugging

• PyCharm�‘Using�a�remote�interpreter’�설정�단계1.�Remote�Interpreter2.�Debug�Configuration 3.�Auto�Deployment

Page 33: 20170813 django api server unit test and remote debugging

Remote�Debugging�-�1.�Remote�Interpreter�설정

• 1.1.�[preferences]�에서‘project�interpreter’�검색1.2.�우측�상단의�������톱니바퀴�클릭1.3.�‘Add�Remote’

Page 34: 20170813 django api server unit test and remote debugging

Remote�Debugging�-�1.�Remote�Interpreter�설정

• 1.4.�SSH�Credentials�입력

Page 35: 20170813 django api server unit test and remote debugging

Remote�Debugging�-�2.�Debug�Configuration�설정

• 2.1.�[Run]�->�[Edit�Configurations]2.2.�좌측�상단의�‘+’�버튼�클릭2.3.�‘Python’�선택

Page 36: 20170813 django api server unit test and remote debugging

Remote�Debugging�-�2.�Debug�Configuration�설정

• 2.4.�오른쪽�화면처럼�설정

->�여기까지만해도,�remote�debugging가능합니다.�

->�하지만,�local(Mac�OS�X)에서Python�code를�변경한�내용이remote�debugging에�바로�반영하려면?

Page 37: 20170813 django api server unit test and remote debugging

Remote�Debugging�-�3.�Auto�Deployment�설정

• 3.1.�[Tools]�->�[Deployment]�������->�[Configuration]3.2.�좌측�상단의�‘+’�버튼�클릭3.3.�‘SFTP’�선택

Page 38: 20170813 django api server unit test and remote debugging

Remote�Debugging�-�3.�Auto�Deployment�설정

• 3.4.�아래�화면처럼�‘Connection’�항목과�‘Mappings’�항목�설정

Page 39: 20170813 django api server unit test and remote debugging

Remote�Debugging�-�3.�Auto�Deployment�설정

• 3.6.�[Tools]�->�[Deployment]�->�[Automatic�Upload]�

• Auto�Deployment�결과�예시

->�이제�local(Mac�OS�X)에서Python�code를�변경한�내용이remote�debugging에�바로�반영됩니다.

Page 40: 20170813 django api server unit test and remote debugging

Remote�Debugging�-�Demo

우측�상단에�‘벌레’�icon�클릭

Page 41: 20170813 django api server unit test and remote debugging

Remote�Debugging�-�Demo

/numbers/�?start�=�1&end�=�10

Page 42: 20170813 django api server unit test and remote debugging

Remote�Debugging�-�Demo

/numbers/�?start�=�10 &end�=�1

Page 43: 20170813 django api server unit test and remote debugging

###�QUIZ�###�‘unit�test’를�‘remote�debugging’하려면?

Page 44: 20170813 django api server unit test and remote debugging

Remote�Debugging

• Debug�Configuration�설정

->�Script�parameters에�

‘runserver�0.0.0.0:80’�대신에�

‘test�-v�3’을�입력

Page 45: 20170813 django api server unit test and remote debugging

Wrap�Up

Page 46: 20170813 django api server unit test and remote debugging

Wrap�Up

• Django�API�server�unit�test�

• Django�+�DRF�+�factory_boy�

• 나의�개발�환경�구축기�

• Mac�OS�X�+�Vagrant(Amazon�Linux)�

• Remote�Debugging�

• PyCharm�‘Using�a�remote�interpreter’

Page 47: 20170813 django api server unit test and remote debugging

Call�to�Action

• 저희는�API�개발팀을�위한�모니터링�서비스를�만들고�있습니다.저희의�early�bird�고객이�되어주세요.�

• 아직�Python�Korea�FG�가입하지�않으셨다구요?�

• 저희�사이트에서�개발팁을�가져가세요!

https://hbsmith.io

https://www.facebook.com/groups/pythonkorea/

https://medium.com/hbsmith

Page 48: 20170813 django api server unit test and remote debugging

Jongwon�Han

감사합니다

[email protected]�010-9166-6855