File tree Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -110,6 +110,31 @@ def get_logs(self):
110
110
)
111
111
return result .stdout , result .stderr
112
112
113
+ def exec_in_container (self , service_name , command ):
114
+ """
115
+ Executes a command in the container of one of the services.
116
+
117
+ Parameters
118
+ ----------
119
+ service_name: str
120
+ Name of the docker compose service to run the command in
121
+ command: list[str]
122
+ The command to execute
123
+
124
+ Returns
125
+ -------
126
+ tuple[str, str, int]
127
+ stdout, stderr, return code
128
+ """
129
+ exec_cmd = self .docker_compose_command () + ['exec' , '-T' , service_name ] + command
130
+ result = subprocess .run (
131
+ exec_cmd ,
132
+ cwd = self .filepath ,
133
+ stdout = subprocess .PIPE ,
134
+ stderr = subprocess .PIPE ,
135
+ )
136
+ return result .stdout .decode ("utf-8" ), result .stderr .decode ("utf-8" ), result .returncode
137
+
113
138
def get_service_port (self , service_name , port ):
114
139
return self ._get_service_info (service_name , port )[1 ]
115
140
Original file line number Diff line number Diff line change @@ -82,3 +82,11 @@ def test_can_pass_env_params_by_env_file():
82
82
check_env_is_set_cmd = 'docker exec tests_mysql_1 printenv | grep TEST_ASSERT_KEY' .split ()
83
83
out = subprocess .run (check_env_is_set_cmd , stdout = subprocess .PIPE )
84
84
assert out .stdout .decode ('utf-8' ).splitlines ()[0 ], 'test_is_passed'
85
+
86
+
87
+ def test_can_exec_commands ():
88
+ with DockerCompose ("tests" ) as compose :
89
+ result = compose .exec_in_container ('hub' , ['echo' , 'my_test' ])
90
+ assert result [0 ] == 'my_test\n ' , "The echo should be successful"
91
+ assert result [1 ] == '' , "stderr should be empty"
92
+ assert result [2 ] == 0 , 'The exit code should be successful'
You can’t perform that action at this time.
0 commit comments