- LocalStack ã¨ã¯
- Testcontainers ã¨ã¯
- ã»ããã¢ãã
- ãã¹ã対象ã³ã¼ã
- ãã¹ãã³ã¼ãã®å®è£
- ã¾ã¨ã
æ¬è¨äºæ㯠ãµã¼ãã¼ã¯ã¼ã¯ã¹ã¢ããã³ãã«ã¬ã³ãã¼ 2024 - ã·ãªã¼ãº 1 ã® 15 æ¥ç®ã®è¨äºã§ãã
LocalStack ã¨ã¯
ãã¡ãã¯ãåç¥ã®æ¹ãå¤ããã¨æãã¾ãããLocalStack 㯠AWS ã®ä¸»è¦ãªãµã¼ãã¹ããã¼ã«ã«ç°å¢ã§æ¨¡æ¬çã«åä½ããããã¼ã«ã§ããããã«ãããå®éã® AWS ç°å¢ãå©ç¨ããã«ãã¼ã«ã«ã§ã¢ããªã±ã¼ã·ã§ã³ã®éçºã»ãã¹ããå¯è½ã«ãªãã¾ãã
Testcontainers ã¨ã¯
Testcontainersã¯ãã³ã³ããã使ç¨ããèªåãã¹ããç°¡åã«å®ç¾ããããã®ã©ã¤ãã©ãªã§ãããã¹ãç°å¢ã«å¿ è¦ãªãã¼ã¿ãã¼ã¹ãã¡ãã»ã¼ã¸ãã¥ã¼ãªã©ãDockerã³ã³ããã§åçã«èµ·åãããã¹ããçµäºããã¨èªåçã«ã¯ãªã¼ã³ã¢ããããã¾ããããã«ãããä¾åé¢ä¿ãéé¢ããä¿¡é ¼æ§ã®é«ããã¹ããå¯è½ã«ãªãã¾ãã
Thoughtworks ã® Technology Radar Volume 31 Languages & Frameworks ã§ã dbt ã¨ä¸¦ã㧠Adopt (å¼·ãæ¨å¥¨) ã«ä½ç½®ä»ãããã¦ãã¾ãã
Testcontainers 㯠GoãJavaãNode.jsãPython çã11 ã®è¨èªã§å®è£ ããã¦ããã1 ã¤ã®è¨èªã§ä½¿ãæ¹ãç¿å¾ãã¦ãã¾ãã°ãä»ã®è¨èªã§ãåæ§ã®ããã¼ã§ã³ã³ãããå©ç¨ãããã¹ããå®è£ ãããã¨ãã§ãã¾ãã対å¿è¨èªã¯ä»¥ä¸ãªã³ã¯ãã確èªãã¦ãã ããã
ã»ããã¢ãã
ä»å㯠Python ã®ãã¹ãã³ã¼ãå 㧠Testcontainers 㧠LocalStack ã管çãã¦ããããã¾ãã
å®è¡ç°å¢ã¯ä»¥ä¸ã®éãã§ãã
$ sw_vers ProductName: macOS ProductVersion: 15.1.1 BuildVersion: 24B91 $ docker -h Docker version 20.10.17, build 100c701 $ python -V Python 3.12.4
ã¾ã㯠Python ã®ä»®æ³ç°å¢ããä½æãã¾ãã
$ mkdir testcontainer-blog && $_ $ python -m venv .venv $ source .venv/bin/activate
boto3ãtestcontainers-pythonãpytest ãã¤ã³ã¹ãã¼ã«ãã¾ãã
$ pip install boto3 testcontainers pytest
ãã¹ã対象ã³ã¼ã
ç°¡åã§ããã以ä¸ã®ãããªãã¹ã対象ã³ã¼ããç¨æãã¾ããããã©ã¡ã¼ã¿ã¹ãã¢ããå¤ãåå¾ãã¦è¿ãé¢æ°ã§ããå¼æ°ã« boto3 ã® SSM ã¯ã©ã¤ã¢ã³ãããã©ã¡ã¼ã¿åãã¨ãã¾ãã
# main.py def get_parameter_value(ssm_client, parameter_name): response = ssm_client.get_parameter( Name=parameter_name ) return response['Parameter']['Value']
ãã¹ãã³ã¼ãã®å®è£
Testcontainers çµç±ã§ LocalStack ã使ç¨ãããã¹ãã³ã¼ããå®è£
ãã¾ããå
¨ä½åã¯ãã®ãããªå½¢ã¨ãªãã¾ããput_parameter
ã§ãã¹ããã¼ã¿ãæå
¥ããã¹ã対象ã¡ã½ããï¼get_parameter_value
ï¼ã®å®è¡ãæå¾ã«ã¢ãµã¼ã·ã§ã³ãè¡ã£ã¦ãã¾ãã
# test_main.py from main import get_parameter_value from testcontainers.localstack import LocalStackContainer def test_get_parameter_value(): with LocalStackContainer() as localstack: # arrange client = localstack.get_client("ssm") client.put_parameter( Name="sample", Type="String", Value="sample-value" ) # act value = get_parameter_value(client, "sample") # assert assert value == "sample-value"
ãã¤ã³ã㯠LocalStackContainer
ã§ããã³ã³ããã¹ãããã¼ã¸ã£ã¼ã¨ãã¦å©ç¨å¯è½ã§ãåå¦çï¼__enter__
ï¼ã§ LocalStack ã®ã¤ã¡ã¼ã¸ã® Pull ããèµ·åãå¾å¦çï¼__exit__
ï¼ã§èµ·åããã³ã³ããã®ç ´æ£ã¾ã§è¡ã£ã¦ããã¾ãã
ã¾ããlocalstack.get_client("ssm")
㧠LocalStack åãã®è¨å®ãæ½ãã client ãåå¾ã§ãã¾ãã
Testcontainers ãå©ç¨ããªãå ´åãdocker-compose.yml çã« LocalStack ãå®ç¾©ããèµ·åãèµ·åãããã¼ããã¨ã³ããã¤ã³ãã«æã¤ã¯ã©ã¤ã¢ã³ããå®ç¾©ããå¿ è¦ãããã¾ããã¾ãããã¹ãç¶æ³ã«å¿ããã³ã³ããã®ç ´æ£çãèªåã§å®è£ ããå¿ è¦ããããããããªãç°¡åã«ã³ã³ããã使ç¨ãããã¹ããæ¸ãããã¨ããåããããã ãããã¨æãã¾ãã
ãµã³ãã«ã§ã¯ 1 ã¤ã®ãã¹ãã¡ã½ããå ã§èµ·åã»ç ´æ£ããã¦ãã¾ãããåæ§ã«è¤æ°ã¡ã½ããå®è£ ãã¦ãã¾ãã¨åãã¹ãã¡ã½ããæ¯ã«ã³ã³ããã®èµ·åã»ç ´æ£ãè¡ã£ã¦ãã¾ããããfixture çãé§ä½¿ãã¦ã³ã³ããã®ã©ã¤ããµã¤ã¯ã«ã調æ´ããã¨è¯ãã§ãããã
ã¾ã¨ã
Testcontainers ãå©ç¨ããã¨ã³ã³ããã使ç¨ãããã¹ããç°¡åã«æ¸ããã¨ãã§ãã¾ãããã¾ãããã¹ãã³ã¼ãã®å®è£ ã ãã§ãªããAWS ç°å¢ã®ç¨æãªãã«æå ã§æ¤è¨¼ã³ã¼ããæ¸ããã¨ãã§ãã¾ããæ§ã ãªè¨èªã§å©ç¨ã§ãã¾ãã®ã§ãæ¯éã試ãããã ããã°ã¨æãã¾ãã