@@ -29,6 +29,7 @@ import (
2929 "errors"
3030 "fmt"
3131 "net/http"
32+ "os"
3233 "testing"
3334 "time"
3435
@@ -91,6 +92,35 @@ func TestHttpHappyCasesFromConf(t *testing.T) {
9192 }
9293}
9394
95+ func TestHttpHappyCasesFromEnv (t * testing.T ) {
96+ var (
97+ addr = "localhost:1111"
98+ )
99+
100+ testCases := []httpConfigTestCase {
101+ {
102+ name : "addr only" ,
103+ config : fmt .Sprintf ("http::addr=%s" , addr ),
104+ },
105+ {
106+ name : "auto flush" ,
107+ config : fmt .Sprintf ("http::addr=%s;auto_flush_rows=100;auto_flush_interval=1000;" ,
108+ addr ),
109+ },
110+ }
111+
112+ for _ , tc := range testCases {
113+ t .Run (tc .name , func (t * testing.T ) {
114+ os .Setenv ("QDB_CLIENT_CONF" , tc .config )
115+ sender , err := qdb .LineSenderFromEnv (context .Background ())
116+ assert .NoError (t , err )
117+
118+ sender .Close (context .Background ())
119+ os .Unsetenv ("QDB_CLIENT_CONF" )
120+ })
121+ }
122+ }
123+
94124func TestHttpPathologicalCasesFromConf (t * testing.T ) {
95125 testCases := []httpConfigTestCase {
96126 {
@@ -148,6 +178,42 @@ func TestHttpPathologicalCasesFromConf(t *testing.T) {
148178 }
149179}
150180
181+ func TestHttpPathologicalCasesFromEnv (t * testing.T ) {
182+ // Test a few cases just to make sure that the config is read
183+ // from the env variable.
184+ testCases := []httpConfigTestCase {
185+ {
186+ name : "basic_and_token_auth" ,
187+ config : "http::username=test_user;token=test_token;" ,
188+ expectedErr : "both basic and token" ,
189+ },
190+ {
191+ name : "negative max_buf_size" ,
192+ config : "http::max_buf_size=-1;" ,
193+ expectedErr : "max buffer size is negative" ,
194+ },
195+ {
196+ name : "schema is case-sensitive" ,
197+ config : "hTtp::addr=localhost:1234;" ,
198+ expectedErr : "invalid schema" ,
199+ },
200+ }
201+
202+ for _ , tc := range testCases {
203+ t .Run (tc .name , func (t * testing.T ) {
204+ os .Setenv ("QDB_CLIENT_CONF" , tc .config )
205+ _ , err := qdb .LineSenderFromEnv (context .Background ())
206+ assert .ErrorContains (t , err , tc .expectedErr )
207+ os .Unsetenv ("QDB_CLIENT_CONF" )
208+ })
209+ }
210+ }
211+
212+ func TestHttpEmptyEnvVariableCaseFromEnv (t * testing.T ) {
213+ _ , err := qdb .LineSenderFromEnv (context .Background ())
214+ assert .ErrorContains (t , err , "QDB_CLIENT_CONF environment variable is not set" )
215+ }
216+
151217func TestErrorWhenSenderTypeIsNotSpecified (t * testing.T ) {
152218 ctx := context .Background ()
153219
0 commit comments