2015-08-27 03:42:02 +03:00
|
|
|
// Copyright 2015 The Go Authors. All rights reserved.
|
2014-12-16 22:15:52 +03:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package google
|
|
|
|
|
2017-12-22 00:34:39 +03:00
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
2014-12-16 22:15:52 +03:00
|
|
|
|
|
|
|
func TestSDKConfig(t *testing.T) {
|
2015-02-12 07:46:13 +03:00
|
|
|
sdkConfigPath = func() (string, error) {
|
|
|
|
return "testdata/gcloud", nil
|
|
|
|
}
|
|
|
|
|
2014-12-16 22:15:52 +03:00
|
|
|
tests := []struct {
|
|
|
|
account string
|
|
|
|
accessToken string
|
|
|
|
err bool
|
|
|
|
}{
|
|
|
|
{"", "bar_access_token", false},
|
|
|
|
{"foo@example.com", "foo_access_token", false},
|
|
|
|
{"bar@example.com", "bar_access_token", false},
|
2015-02-09 12:18:20 +03:00
|
|
|
{"baz@serviceaccount.example.com", "", true},
|
2014-12-16 22:15:52 +03:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
c, err := NewSDKConfig(tt.account)
|
2015-02-09 12:18:20 +03:00
|
|
|
if got, want := err != nil, tt.err; got != want {
|
2014-12-16 22:15:52 +03:00
|
|
|
if !tt.err {
|
2016-07-18 21:05:36 +03:00
|
|
|
t.Errorf("got %v, want nil", err)
|
2014-12-16 22:15:52 +03:00
|
|
|
} else {
|
2016-07-18 21:05:36 +03:00
|
|
|
t.Errorf("got nil, want error")
|
2014-12-16 22:15:52 +03:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2015-02-09 12:18:20 +03:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2014-12-16 22:15:52 +03:00
|
|
|
tok := c.initialToken
|
|
|
|
if tok == nil {
|
2016-07-18 21:05:36 +03:00
|
|
|
t.Errorf("got nil, want %q", tt.accessToken)
|
2014-12-16 22:15:52 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if tok.AccessToken != tt.accessToken {
|
2016-07-18 21:05:36 +03:00
|
|
|
t.Errorf("got %q, want %q", tok.AccessToken, tt.accessToken)
|
2014-12-16 22:15:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-22 00:34:39 +03:00
|
|
|
|
|
|
|
func TestParseINI(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
ini string
|
|
|
|
want map[string]map[string]string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
`root = toor
|
|
|
|
[foo]
|
|
|
|
bar = hop
|
|
|
|
ini = nin
|
|
|
|
`,
|
|
|
|
map[string]map[string]string{
|
|
|
|
"": {"root": "toor"},
|
|
|
|
"foo": {"bar": "hop", "ini": "nin"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"\t extra \t = whitespace \t\r\n \t [everywhere] \t \r\n here \t = \t there \t \r\n",
|
|
|
|
map[string]map[string]string{
|
|
|
|
"": {"extra": "whitespace"},
|
|
|
|
"everywhere": {"here": "there"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`[empty]
|
|
|
|
[section]
|
|
|
|
empty=
|
|
|
|
`,
|
|
|
|
map[string]map[string]string{
|
|
|
|
"": {},
|
|
|
|
"empty": {},
|
|
|
|
"section": {"empty": ""},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`ignore
|
|
|
|
[invalid
|
|
|
|
=stuff
|
|
|
|
;comment=true
|
|
|
|
`,
|
|
|
|
map[string]map[string]string{
|
|
|
|
"": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
result, err := parseINI(strings.NewReader(tt.ini))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("parseINI(%q) error %v, want: no error", tt.ini, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(result, tt.want) {
|
|
|
|
t.Errorf("parseINI(%q) = %#v, want: %#v", tt.ini, result, tt.want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|