@@ -25,7 +25,8 @@ defmodule Testcontainers.RedisContainer do
2525 :port ,
2626 :wait_timeout ,
2727 check_image: @ default_image ,
28- reuse: false
28+ reuse: false ,
29+ password: nil
2930 ]
3031
3132 @ doc """
@@ -35,7 +36,8 @@ defmodule Testcontainers.RedisContainer do
3536 do: % __MODULE__ {
3637 image: @ default_image_with_tag ,
3738 wait_timeout: @ default_wait_timeout ,
38- port: @ default_port
39+ port: @ default_port ,
40+ password: nil
3941 }
4042
4143 @ doc """
@@ -68,6 +70,20 @@ defmodule Testcontainers.RedisContainer do
6870 % { config | port: port }
6971 end
7072
73+ @ doc """
74+ Sets the password for the Redis container.
75+
76+ ## Examples
77+
78+ iex> config = RedisContainer.new()
79+ iex> new_config = RedisContainer.with_password(config, "secret")
80+ iex> new_config.password
81+ "secret"
82+ """
83+ def with_password ( % __MODULE__ { } = config , password ) when is_binary ( password ) do
84+ % { config | password: password }
85+ end
86+
7187 @ doc """
7288 Overrides the default wait timeout used for the Redis container.
7389
@@ -122,8 +138,11 @@ defmodule Testcontainers.RedisContainer do
122138 iex> RedisContainer.connection_url(container)
123139 "http://localhost:32768" # This value will be different depending on the mapped port.
124140 """
125- def connection_url ( % Container { } = container ) ,
126- do: "redis://#{ Testcontainers . get_host ( ) } :#{ port ( container ) } /"
141+ def connection_url ( % Container { } = container ) do
142+ password = container . environment [ :REDIS_PASSWORD ]
143+ auth_part = if password , do: ":#{ password } @" , else: ""
144+ "redis://#{ auth_part } #{ Testcontainers . get_host ( ) } :#{ port ( container ) } /"
145+ end
127146
128147 defimpl ContainerBuilder do
129148 import Container
@@ -149,14 +168,31 @@ defmodule Testcontainers.RedisContainer do
149168 @ spec build ( % RedisContainer { } ) :: % Container { }
150169 @ impl true
151170 def build ( % RedisContainer { } = config ) do
152- new ( config . image )
153- |> with_exposed_port ( config . port )
154- |> with_waiting_strategy (
155- CommandWaitStrategy . new ( [ "redis-cli" , "PING" ] , config . wait_timeout )
156- )
157- |> with_check_image ( config . check_image )
158- |> with_reuse ( config . reuse )
159- |> valid_image! ( )
171+ container =
172+ new ( config . image )
173+ |> with_exposed_port ( config . port )
174+ |> with_check_image ( config . check_image )
175+ |> with_reuse ( config . reuse )
176+
177+ container =
178+ if config . password do
179+ container
180+ |> with_cmd ( [ "redis-server" , "--requirepass" , config . password ] )
181+ |> with_waiting_strategy (
182+ CommandWaitStrategy . new (
183+ [ "redis-cli" , "-a" , config . password , "PING" ] ,
184+ config . wait_timeout
185+ )
186+ )
187+ |> with_environment ( "REDIS_PASSWORD" , config . password )
188+ else
189+ container
190+ |> with_waiting_strategy (
191+ CommandWaitStrategy . new ( [ "redis-cli" , "PING" ] , config . wait_timeout )
192+ )
193+ end
194+
195+ valid_image! ( container )
160196 end
161197
162198 @ impl true
0 commit comments