Skip to content
This repository was archived by the owner on Mar 18, 2025. It is now read-only.

Commit 00146fe

Browse files
committed
Fixed expected names for Username option
The previous renaming from User to Username hasn't changed all the available matches.
1 parent 9577fd0 commit 00146fe

File tree

2 files changed

+43
-12
lines changed

2 files changed

+43
-12
lines changed

pkg/remotewrite/config.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
"github.com/grafana/xk6-output-prometheus-remote/pkg/remote"
1212
"github.com/kubernetes/helm/pkg/strvals"
13-
1413
"go.k6.io/k6/lib/types"
1514
"gopkg.in/guregu/null.v3"
1615
)
@@ -43,6 +42,7 @@ type Config struct {
4342
PushInterval types.NullDuration `json:"pushInterval" envconfig:"K6_PROMETHEUS_PUSH_INTERVAL"`
4443
}
4544

45+
// NewConfig creates an Output's configuration.
4646
func NewConfig() Config {
4747
return Config{
4848
URL: null.StringFrom(defaultURL),
@@ -54,6 +54,7 @@ func NewConfig() Config {
5454
}
5555
}
5656

57+
// RemoteConfig creates a configuration for the HTTP Remote-write client.
5758
func (conf Config) RemoteConfig() (*remote.HTTPConfig, error) {
5859
hc := remote.HTTPConfig{
5960
Timeout: defaultTimeout,
@@ -83,8 +84,7 @@ func (conf Config) RemoteConfig() (*remote.HTTPConfig, error) {
8384
return &hc, nil
8485
}
8586

86-
// From here till the end of the file partial duplicates waiting for config refactor (k6 #883)
87-
87+
// Apply merges applied Config into base.
8888
func (base Config) Apply(applied Config) Config {
8989
if applied.URL.Valid {
9090
base.URL = applied.URL
@@ -115,7 +115,7 @@ func (base Config) Apply(applied Config) Config {
115115
return base
116116
}
117117

118-
// ParseArg takes an arg string and converts it to a config
118+
// ParseArg creates a Config parsing an arg string.
119119
func ParseArg(arg string) (Config, error) {
120120
var c Config
121121
params, err := strvals.Parse(arg)
@@ -131,7 +131,7 @@ func ParseArg(arg string) (Config, error) {
131131
c.InsecureSkipTLSVerify = null.BoolFrom(v)
132132
}
133133

134-
if v, ok := params["user"].(string); ok {
134+
if v, ok := params["username"].(string); ok {
135135
c.Username = null.StringFrom(v)
136136
}
137137

@@ -158,7 +158,7 @@ func ParseArg(arg string) (Config, error) {
158158
}
159159

160160
// GetConsolidatedConfig combines {default config values + JSON config +
161-
// environment vars + arg config values}, and returns the final result.
161+
// environment vars + arg config values}, and returns the final merged configuration.
162162
func GetConsolidatedConfig(jsonRawConf json.RawMessage, env map[string]string, arg string) (Config, error) {
163163
result := NewConfig()
164164
if jsonRawConf != nil {
@@ -211,7 +211,7 @@ func GetConsolidatedConfig(jsonRawConf json.RawMessage, env map[string]string, a
211211
}
212212
}
213213

214-
if user, userDefined := env["K6_PROMETHEUS_USER"]; userDefined {
214+
if user, userDefined := env["K6_PROMETHEUS_USERNAME"]; userDefined {
215215
result.Username = null.StringFrom(user)
216216
}
217217

pkg/remotewrite/config_test.go

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,10 @@ func TestConfigParseArg(t *testing.T) {
6363
assert.Nil(t, err)
6464
assert.Equal(t, null.StringFrom("https://prometheus.remote:3412/write"), c.URL)
6565

66-
c, err = ParseArg("url=https://prometheus.remote:3412/write,insecureSkipTLSVerify=false,user=user,password=pass")
66+
c, err = ParseArg("url=https://prometheus.remote:3412/write,insecureSkipTLSVerify=false")
6767
assert.Nil(t, err)
6868
assert.Equal(t, null.StringFrom("https://prometheus.remote:3412/write"), c.URL)
6969
assert.Equal(t, null.BoolFrom(false), c.InsecureSkipTLSVerify)
70-
assert.Equal(t, null.StringFrom("user"), c.Username)
71-
assert.Equal(t, null.StringFrom("pass"), c.Password)
7270

7371
c, err = ParseArg("url=http://prometheus.remote:3412/write,pushInterval=2s")
7472
assert.Nil(t, err)
@@ -153,9 +151,9 @@ func TestConfigConsolidation(t *testing.T) {
153151
},
154152
},
155153
"mixed_success": {
156-
jsonRaw: json.RawMessage(fmt.Sprintf(`{"url":"%s","mapping":"raw"}`, u.String())),
154+
jsonRaw: json.RawMessage(fmt.Sprintf(`{"url":"%s"}`, u.String())),
157155
env: map[string]string{"K6_PROMETHEUS_INSECURE_SKIP_TLS_VERIFY": "false", "K6_PROMETHEUS_USER": "u"},
158-
arg: "user=user",
156+
arg: "username=user",
159157
config: Config{
160158
URL: null.StringFrom(u.String()),
161159
InsecureSkipTLSVerify: null.BoolFrom(false),
@@ -247,3 +245,36 @@ func TestConfigConsolidation(t *testing.T) {
247245
})
248246
}
249247
}
248+
249+
func TestConfigBasicAuth(t *testing.T) {
250+
t.Parallel()
251+
252+
cases := map[string]struct {
253+
arg string
254+
env map[string]string
255+
jsonRaw json.RawMessage
256+
}{
257+
"JSON": {jsonRaw: json.RawMessage(`{"username":"user1","password":"pass1"}`)},
258+
"Env": {env: map[string]string{"K6_PROMETHEUS_USERNAME": "user1", "K6_PROMETHEUS_PASSWORD": "pass1"}},
259+
"Arg": {arg: "username=user1,password=pass1"},
260+
}
261+
262+
expconfig := Config{
263+
URL: null.StringFrom("http://localhost:9090/api/v1/write"),
264+
InsecureSkipTLSVerify: null.BoolFrom(true),
265+
Username: null.StringFrom("user1"),
266+
Password: null.StringFrom("pass1"),
267+
PushInterval: types.NullDurationFrom(5 * time.Second),
268+
Headers: make(map[string]string),
269+
}
270+
271+
for name, tc := range cases {
272+
tc := tc
273+
t.Run(name, func(t *testing.T) {
274+
c, err := GetConsolidatedConfig(
275+
tc.jsonRaw, tc.env, tc.arg)
276+
require.NoError(t, err)
277+
assert.Equal(t, expconfig, c)
278+
})
279+
}
280+
}

0 commit comments

Comments
 (0)