@@ -35,17 +35,27 @@ 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+ apply (settings )
43+ }
44+ }
45+
46+ username := settings .Username
47+ password := settings .Password
48+
49+ moduleOpts := []testcontainers.ContainerCustomizer {
50+ testcontainers .WithEnv (map [string ]string {
4251 "discovery.type" : "single-node" ,
4352 "DISABLE_INSTALL_DEMO_CONFIG" : "true" ,
4453 "DISABLE_SECURITY_PLUGIN" : "true" ,
45- "OPENSEARCH_USERNAME" : defaultUsername ,
46- "OPENSEARCH_PASSWORD" : defaultPassword ,
47- },
48- HostConfigModifier : func (hc * container.HostConfig ) {
54+ "OPENSEARCH_USERNAME" : username ,
55+ "OPENSEARCH_PASSWORD" : password ,
56+ }),
57+ testcontainers .WithExposedPorts (defaultHTTPPort , "9600/tcp" ),
58+ testcontainers .WithHostConfigModifier (func (hc * container.HostConfig ) {
4959 hc .Ulimits = []* units.Ulimit {
5060 {
5161 Name : "memlock" ,
@@ -58,73 +68,47 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
5868 Hard : 65536 ,
5969 },
6070 }
61- },
62- }
63-
64- genericContainerReq := testcontainers.GenericContainerRequest {
65- ContainerRequest : req ,
66- Started : true ,
71+ }),
72+ // the wait strategy does not support TLS at the moment,
73+ // so we need to disable it in the strategy for now.
74+ testcontainers .WithWaitStrategy (wait .ForHTTP ("/" ).
75+ WithPort ("9200" ).
76+ WithTLS (false ).
77+ WithStartupTimeout (120 * time .Second ).
78+ WithStatusCodeMatcher (func (status int ) bool {
79+ return status == 200
80+ }).
81+ WithBasicAuth (username , password ).
82+ WithResponseMatcher (func (body io.Reader ) bool {
83+ bs , err := io .ReadAll (body )
84+ if err != nil {
85+ return false
86+ }
87+
88+ type response struct {
89+ Tagline string `json:"tagline"`
90+ }
91+
92+ var r response
93+ err = json .Unmarshal (bs , & r )
94+ if err != nil {
95+ return false
96+ }
97+
98+ return r .Tagline == "The OpenSearch Project: https://opensearch.org/"
99+ })),
67100 }
68101
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
83- }
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- }
116-
117- return r .Tagline == "The OpenSearch Project: https://opensearch.org/"
118- })
102+ moduleOpts = append (moduleOpts , opts ... )
119103
120- container , err := testcontainers .GenericContainer (ctx , genericContainerReq )
104+ ctr , err := testcontainers .Run (ctx , img , moduleOpts ... )
121105 var c * OpenSearchContainer
122- if container != nil {
123- c = & OpenSearchContainer {Container : container , User : username , Password : password }
106+ if ctr != nil {
107+ c = & OpenSearchContainer {Container : ctr , User : username , Password : password }
124108 }
125109
126110 if err != nil {
127- return c , fmt .Errorf ("generic container : %w" , err )
111+ return c , fmt .Errorf ("run opensearch : %w" , err )
128112 }
129113
130114 return c , nil
0 commit comments