@@ -35,17 +35,29 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize
3535
3636// Run creates an instance of the OpenSearch container type
3737func Run (ctx context.Context , img string , opts ... testcontainers.ContainerCustomizer ) (* OpenSearchContainer , error ) {
38- req := testcontainers.ContainerRequest {
39- Image : img ,
40- ExposedPorts : []string {defaultHTTPPort , "9600/tcp" },
41- Env : map [string ]string {
38+ // Gather all config options (defaults and then apply provided options)
39+ settings := defaultOptions ()
40+ for _ , opt := range opts {
41+ if apply , ok := opt .(Option ); ok {
42+ if err := apply (settings ); err != nil {
43+ return nil , fmt .Errorf ("apply option: %w" , err )
44+ }
45+ }
46+ }
47+
48+ username := settings .Username
49+ password := settings .Password
50+
51+ moduleOpts := []testcontainers.ContainerCustomizer {
52+ testcontainers .WithEnv (map [string ]string {
4253 "discovery.type" : "single-node" ,
4354 "DISABLE_INSTALL_DEMO_CONFIG" : "true" ,
4455 "DISABLE_SECURITY_PLUGIN" : "true" ,
45- "OPENSEARCH_USERNAME" : defaultUsername ,
46- "OPENSEARCH_PASSWORD" : defaultPassword ,
47- },
48- HostConfigModifier : func (hc * container.HostConfig ) {
56+ "OPENSEARCH_USERNAME" : username ,
57+ "OPENSEARCH_PASSWORD" : password ,
58+ }),
59+ testcontainers .WithExposedPorts (defaultHTTPPort , "9600/tcp" ),
60+ testcontainers .WithHostConfigModifier (func (hc * container.HostConfig ) {
4961 hc .Ulimits = []* units.Ulimit {
5062 {
5163 Name : "memlock" ,
@@ -58,73 +70,47 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
5870 Hard : 65536 ,
5971 },
6072 }
61- },
62- }
63-
64- genericContainerReq := testcontainers.GenericContainerRequest {
65- ContainerRequest : req ,
66- Started : true ,
67- }
68-
69- // Gather all config options (defaults and then apply provided options)
70- settings := defaultOptions ()
71- for _ , opt := range opts {
72- if apply , ok := opt .(Option ); ok {
73- apply (settings )
74- }
75- if err := opt .Customize (& genericContainerReq ); err != nil {
76- return nil , err
77- }
78- }
79-
80- // set credentials if they are provided, otherwise use the defaults
81- if settings .Username != "" {
82- genericContainerReq .Env ["OPENSEARCH_USERNAME" ] = settings .Username
73+ }),
74+ // the wait strategy does not support TLS at the moment,
75+ // so we need to disable it in the strategy for now.
76+ testcontainers .WithWaitStrategy (wait .ForHTTP ("/" ).
77+ WithPort ("9200" ).
78+ WithTLS (false ).
79+ WithStartupTimeout (120 * time .Second ).
80+ WithStatusCodeMatcher (func (status int ) bool {
81+ return status == 200
82+ }).
83+ WithBasicAuth (username , password ).
84+ WithResponseMatcher (func (body io.Reader ) bool {
85+ bs , err := io .ReadAll (body )
86+ if err != nil {
87+ return false
88+ }
89+
90+ type response struct {
91+ Tagline string `json:"tagline"`
92+ }
93+
94+ var r response
95+ err = json .Unmarshal (bs , & r )
96+ if err != nil {
97+ return false
98+ }
99+
100+ return r .Tagline == "The OpenSearch Project: https://opensearch.org/"
101+ })),
83102 }
84- if settings .Password != "" {
85- genericContainerReq .Env ["OPENSEARCH_PASSWORD" ] = settings .Password
86- }
87-
88- username := genericContainerReq .Env ["OPENSEARCH_USERNAME" ]
89- password := genericContainerReq .Env ["OPENSEARCH_PASSWORD" ]
90-
91- // the wat strategy does not support TLS at the moment,
92- // so we need to disable it in the strategy for now.
93- genericContainerReq .WaitingFor = wait .ForHTTP ("/" ).
94- WithPort ("9200" ).
95- WithTLS (false ).
96- WithStartupTimeout (120 * time .Second ).
97- WithStatusCodeMatcher (func (status int ) bool {
98- return status == 200
99- }).
100- WithBasicAuth (username , password ).
101- WithResponseMatcher (func (body io.Reader ) bool {
102- bs , err := io .ReadAll (body )
103- if err != nil {
104- return false
105- }
106-
107- type response struct {
108- Tagline string `json:"tagline"`
109- }
110-
111- var r response
112- err = json .Unmarshal (bs , & r )
113- if err != nil {
114- return false
115- }
116103
117- return r .Tagline == "The OpenSearch Project: https://opensearch.org/"
118- })
104+ moduleOpts = append (moduleOpts , opts ... )
119105
120- container , err := testcontainers .GenericContainer (ctx , genericContainerReq )
106+ ctr , err := testcontainers .Run (ctx , img , moduleOpts ... )
121107 var c * OpenSearchContainer
122- if container != nil {
123- c = & OpenSearchContainer {Container : container , User : username , Password : password }
108+ if ctr != nil {
109+ c = & OpenSearchContainer {Container : ctr , User : username , Password : password }
124110 }
125111
126112 if err != nil {
127- return c , fmt .Errorf ("generic container : %w" , err )
113+ return c , fmt .Errorf ("run opensearch : %w" , err )
128114 }
129115
130116 return c , nil
0 commit comments