mirror of
https://github.com/steinhobelgruen/netatmo-exporter.git
synced 2024-12-12 08:53:55 +00:00
220 lines
4.4 KiB
Go
220 lines
4.4 KiB
Go
package main
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
|
|
netatmo "github.com/exzz/netatmo-api-go"
|
|
)
|
|
|
|
func TestParseConfig(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
args []string
|
|
env map[string]string
|
|
wantConfig config
|
|
wantErr error
|
|
}{
|
|
{
|
|
name: "no args",
|
|
args: []string{},
|
|
env: map[string]string{},
|
|
wantConfig: config{},
|
|
wantErr: errNoBinaryName,
|
|
},
|
|
{
|
|
name: "success",
|
|
args: []string{
|
|
"test-cmd",
|
|
"--" + flagNetatmoClientID,
|
|
"id",
|
|
"--" + flagNetatmoClientSecret,
|
|
"secret",
|
|
"--" + flagNetatmoUsername,
|
|
"username",
|
|
"--" + flagNetatmoPassword,
|
|
"password",
|
|
},
|
|
env: map[string]string{},
|
|
wantConfig: config{
|
|
Addr: defaultConfig.Addr,
|
|
Netatmo: netatmo.Config{
|
|
ClientID: "id",
|
|
ClientSecret: "secret",
|
|
Username: "username",
|
|
Password: "password",
|
|
},
|
|
},
|
|
wantErr: nil,
|
|
},
|
|
{
|
|
name: "all env",
|
|
args: []string{
|
|
"test-cmd",
|
|
},
|
|
env: map[string]string{
|
|
envVarListenAddress: ":8080",
|
|
envVarNetatmoClientID: "id",
|
|
envVarNetatmoClientSecret: "secret",
|
|
envVarNetatmoUsername: "username",
|
|
envVarNetatmoPassword: "password",
|
|
},
|
|
wantConfig: config{
|
|
Addr: ":8080",
|
|
Netatmo: netatmo.Config{
|
|
ClientID: "id",
|
|
ClientSecret: "secret",
|
|
Username: "username",
|
|
Password: "password",
|
|
},
|
|
},
|
|
wantErr: nil,
|
|
},
|
|
{
|
|
name: "no addr",
|
|
args: []string{
|
|
"test-cmd",
|
|
"--" + flagListenAddress,
|
|
"",
|
|
"--" + flagNetatmoClientID,
|
|
"id",
|
|
"--" + flagNetatmoClientSecret,
|
|
"secret",
|
|
"--" + flagNetatmoUsername,
|
|
"username",
|
|
"--" + flagNetatmoPassword,
|
|
"password",
|
|
},
|
|
env: map[string]string{},
|
|
wantConfig: config{
|
|
Addr: defaultConfig.Addr,
|
|
Netatmo: netatmo.Config{
|
|
ClientID: "id",
|
|
ClientSecret: "secret",
|
|
Username: "username",
|
|
Password: "password",
|
|
},
|
|
},
|
|
wantErr: errNoListenAddress,
|
|
},
|
|
{
|
|
name: "no client id",
|
|
args: []string{
|
|
"test-cmd",
|
|
"--" + flagNetatmoClientSecret,
|
|
"secret",
|
|
"--" + flagNetatmoUsername,
|
|
"username",
|
|
"--" + flagNetatmoPassword,
|
|
"password",
|
|
},
|
|
env: map[string]string{},
|
|
wantConfig: config{
|
|
Addr: defaultConfig.Addr,
|
|
Netatmo: netatmo.Config{
|
|
ClientID: "id",
|
|
ClientSecret: "secret",
|
|
Username: "username",
|
|
Password: "password",
|
|
},
|
|
},
|
|
wantErr: errNoNetatmoClientID,
|
|
},
|
|
{
|
|
name: "no client secret",
|
|
args: []string{
|
|
"test-cmd",
|
|
"--" + flagNetatmoClientID,
|
|
"id",
|
|
"--" + flagNetatmoUsername,
|
|
"username",
|
|
"--" + flagNetatmoPassword,
|
|
"password",
|
|
},
|
|
env: map[string]string{},
|
|
wantConfig: config{
|
|
Addr: defaultConfig.Addr,
|
|
Netatmo: netatmo.Config{
|
|
ClientID: "id",
|
|
ClientSecret: "secret",
|
|
Username: "username",
|
|
Password: "password",
|
|
},
|
|
},
|
|
wantErr: errNoNetatmoClientSecret,
|
|
},
|
|
{
|
|
name: "no username",
|
|
args: []string{
|
|
"test-cmd",
|
|
"--" + flagNetatmoClientID,
|
|
"id",
|
|
"--" + flagNetatmoClientSecret,
|
|
"secret",
|
|
"--" + flagNetatmoPassword,
|
|
"password",
|
|
},
|
|
env: map[string]string{},
|
|
wantConfig: config{
|
|
Addr: defaultConfig.Addr,
|
|
Netatmo: netatmo.Config{
|
|
ClientID: "id",
|
|
ClientSecret: "secret",
|
|
Username: "username",
|
|
Password: "password",
|
|
},
|
|
},
|
|
wantErr: errNoNetatmoUsername,
|
|
},
|
|
{
|
|
name: "no password",
|
|
args: []string{
|
|
"test-cmd",
|
|
"--" + flagNetatmoClientID,
|
|
"id",
|
|
"--" + flagNetatmoClientSecret,
|
|
"secret",
|
|
"--" + flagNetatmoUsername,
|
|
"username",
|
|
},
|
|
env: map[string]string{},
|
|
wantConfig: config{
|
|
Addr: defaultConfig.Addr,
|
|
Netatmo: netatmo.Config{
|
|
ClientID: "id",
|
|
ClientSecret: "secret",
|
|
Username: "username",
|
|
Password: "password",
|
|
},
|
|
},
|
|
wantErr: errNoNetatmoPassword,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
getenv := func(key string) string {
|
|
return tt.env[key]
|
|
}
|
|
|
|
config, err := parseConfig(tt.args, getenv)
|
|
|
|
if err != tt.wantErr {
|
|
t.Errorf("got error %q, want %q", err, tt.wantErr)
|
|
}
|
|
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
if !reflect.DeepEqual(config, tt.wantConfig) {
|
|
t.Errorf("got config %v, want %v", config, tt.wantConfig)
|
|
}
|
|
})
|
|
}
|
|
}
|