-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix config token config detection (#2495)
* Fix config token config detection * Add unit tests
- Loading branch information
1 parent
1d0c181
commit e8fe231
Showing
2 changed files
with
70 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package configuration | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/elastic/elastic-agent/internal/pkg/agent/errors" | ||
) | ||
|
||
func TestElasticsearchFromConnStr(t *testing.T) { | ||
testcases := []struct { | ||
name string | ||
conn string | ||
token string | ||
path string | ||
insecure bool | ||
es Elasticsearch | ||
err error | ||
}{{ | ||
name: "ok", | ||
conn: "https://localhost:9200", | ||
token: "my-token", | ||
path: "", | ||
insecure: false, | ||
es: Elasticsearch{ | ||
Protocol: "https", | ||
Hosts: []string{"localhost:9200"}, | ||
ServiceToken: "my-token", | ||
}, | ||
err: nil, | ||
}, { | ||
name: "ok with path", | ||
conn: "https://localhost:9200", | ||
token: "", | ||
path: "/path/to/token", | ||
insecure: false, | ||
es: Elasticsearch{ | ||
Protocol: "https", | ||
Hosts: []string{"localhost:9200"}, | ||
ServiceTokenPath: "/path/to/token", | ||
}, | ||
err: nil, | ||
}, { | ||
name: "no token or path", | ||
conn: "https://localhost:9200", | ||
token: "", | ||
path: "", | ||
insecure: false, | ||
es: Elasticsearch{}, | ||
err: errors.New("invalid connection string: must include a service token"), | ||
}} | ||
|
||
for _, tc := range testcases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
es, err := ElasticsearchFromConnStr(tc.conn, tc.token, tc.path, tc.insecure) | ||
if tc.err != nil { | ||
assert.EqualError(t, err, tc.err.Error()) | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
assert.Equal(t, tc.es, es) | ||
}) | ||
} | ||
} |