@@ -35,17 +35,16 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize
35
35
36
36
// Run creates an instance of the OpenSearch container type
37
37
func 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
+ moduleOpts := []testcontainers.ContainerCustomizer {
39
+ testcontainers .WithExposedPorts (defaultHTTPPort , "9600/tcp" ),
40
+ testcontainers .WithEnv (map [string ]string {
42
41
"discovery.type" : "single-node" ,
43
42
"DISABLE_INSTALL_DEMO_CONFIG" : "true" ,
44
43
"DISABLE_SECURITY_PLUGIN" : "true" ,
45
44
"OPENSEARCH_USERNAME" : defaultUsername ,
46
45
"OPENSEARCH_PASSWORD" : defaultPassword ,
47
- },
48
- HostConfigModifier : func (hc * container.HostConfig ) {
46
+ }) ,
47
+ testcontainers . WithHostConfigModifier ( func (hc * container.HostConfig ) {
49
48
hc .Ulimits = []* units.Ulimit {
50
49
{
51
50
Name : "memlock" ,
@@ -58,73 +57,65 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
58
57
Hard : 65536 ,
59
58
},
60
59
}
61
- },
60
+ }) ,
62
61
}
63
62
64
- genericContainerReq := testcontainers.GenericContainerRequest {
65
- ContainerRequest : req ,
66
- Started : true ,
67
- }
63
+ moduleOpts = append (moduleOpts , opts ... )
68
64
69
65
// Gather all config options (defaults and then apply provided options)
70
66
settings := defaultOptions ()
71
67
for _ , opt := range opts {
72
68
if apply , ok := opt .(Option ); ok {
73
69
apply (settings )
74
70
}
75
- if err := opt .Customize (& genericContainerReq ); err != nil {
76
- return nil , err
77
- }
78
71
}
79
72
80
73
// 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" ]
74
+ moduleOpts = append (moduleOpts , testcontainers .WithEnv (map [string ]string {
75
+ "OPENSEARCH_USERNAME" : settings .Username ,
76
+ "OPENSEARCH_PASSWORD" : settings .Password ,
77
+ }))
90
78
91
79
// the wat strategy does not support TLS at the moment,
92
80
// 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
- })
119
-
120
- container , err := testcontainers .GenericContainer (ctx , genericContainerReq )
81
+ moduleOpts = append (moduleOpts , testcontainers .WithAdditionalWaitStrategy (
82
+ wait .ForHTTP ("/" ).
83
+ WithPort ("9200" ).
84
+ WithTLS (false ).
85
+ WithStartupTimeout (120 * time .Second ).
86
+ WithStatusCodeMatcher (func (status int ) bool {
87
+ return status == 200
88
+ }).
89
+ WithBasicAuth (settings .Username , settings .Password ).
90
+ WithResponseMatcher (func (body io.Reader ) bool {
91
+ bs , err := io .ReadAll (body )
92
+ if err != nil {
93
+ return false
94
+ }
95
+
96
+ type response struct {
97
+ Tagline string `json:"tagline"`
98
+ }
99
+
100
+ var r response
101
+ err = json .Unmarshal (bs , & r )
102
+ if err != nil {
103
+ return false
104
+ }
105
+
106
+ return r .Tagline == "The OpenSearch Project: https://opensearch.org/"
107
+ }),
108
+ ),
109
+ )
110
+
111
+ ctr , err := testcontainers .Run (ctx , img , moduleOpts ... )
121
112
var c * OpenSearchContainer
122
- if container != nil {
123
- c = & OpenSearchContainer {Container : container , User : username , Password : password }
113
+ if ctr != nil {
114
+ c = & OpenSearchContainer {Container : ctr , User : settings . Username , Password : settings . Password }
124
115
}
125
116
126
117
if err != nil {
127
- return c , fmt .Errorf ("generic container : %w" , err )
118
+ return c , fmt .Errorf ("run : %w" , err )
128
119
}
129
120
130
121
return c , nil
0 commit comments