You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

108 lines
2.1 KiB

  1. // Copyright 2015 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package google
  5. import (
  6. "reflect"
  7. "strings"
  8. "testing"
  9. )
  10. func TestSDKConfig(t *testing.T) {
  11. sdkConfigPath = func() (string, error) {
  12. return "testdata/gcloud", nil
  13. }
  14. tests := []struct {
  15. account string
  16. accessToken string
  17. err bool
  18. }{
  19. {"", "bar_access_token", false},
  20. {"foo@example.com", "foo_access_token", false},
  21. {"bar@example.com", "bar_access_token", false},
  22. {"baz@serviceaccount.example.com", "", true},
  23. }
  24. for _, tt := range tests {
  25. c, err := NewSDKConfig(tt.account)
  26. if got, want := err != nil, tt.err; got != want {
  27. if !tt.err {
  28. t.Errorf("got %v, want nil", err)
  29. } else {
  30. t.Errorf("got nil, want error")
  31. }
  32. continue
  33. }
  34. if err != nil {
  35. continue
  36. }
  37. tok := c.initialToken
  38. if tok == nil {
  39. t.Errorf("got nil, want %q", tt.accessToken)
  40. continue
  41. }
  42. if tok.AccessToken != tt.accessToken {
  43. t.Errorf("got %q, want %q", tok.AccessToken, tt.accessToken)
  44. }
  45. }
  46. }
  47. func TestParseINI(t *testing.T) {
  48. tests := []struct {
  49. ini string
  50. want map[string]map[string]string
  51. }{
  52. {
  53. `root = toor
  54. [foo]
  55. bar = hop
  56. ini = nin
  57. `,
  58. map[string]map[string]string{
  59. "": {"root": "toor"},
  60. "foo": {"bar": "hop", "ini": "nin"},
  61. },
  62. },
  63. {
  64. "\t extra \t = whitespace \t\r\n \t [everywhere] \t \r\n here \t = \t there \t \r\n",
  65. map[string]map[string]string{
  66. "": {"extra": "whitespace"},
  67. "everywhere": {"here": "there"},
  68. },
  69. },
  70. {
  71. `[empty]
  72. [section]
  73. empty=
  74. `,
  75. map[string]map[string]string{
  76. "": {},
  77. "empty": {},
  78. "section": {"empty": ""},
  79. },
  80. },
  81. {
  82. `ignore
  83. [invalid
  84. =stuff
  85. ;comment=true
  86. `,
  87. map[string]map[string]string{
  88. "": {},
  89. },
  90. },
  91. }
  92. for _, tt := range tests {
  93. result, err := parseINI(strings.NewReader(tt.ini))
  94. if err != nil {
  95. t.Errorf("parseINI(%q) error %v, want: no error", tt.ini, err)
  96. continue
  97. }
  98. if !reflect.DeepEqual(result, tt.want) {
  99. t.Errorf("parseINI(%q) = %#v, want: %#v", tt.ini, result, tt.want)
  100. }
  101. }
  102. }