/mixer/adapter/stackdriver/helper/common_test.go

https://github.com/alipay/sofa-mesh · Go · 176 lines · 151 code · 11 blank · 14 comment · 13 complexity · c499c91ad56940a217b0918617953112 MD5 · raw file

  1. // Copyright 2017 Istio Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package helper
  15. import (
  16. "errors"
  17. "fmt"
  18. "reflect"
  19. "testing"
  20. gapiopts "google.golang.org/api/option"
  21. "istio.io/istio/mixer/adapter/stackdriver/config"
  22. )
  23. func TestToOpts(t *testing.T) {
  24. tests := []struct {
  25. name string
  26. cfg *config.Params
  27. out []gapiopts.ClientOption // we only assert that the types match, so contents of the option don't matter
  28. }{
  29. {"empty", &config.Params{}, []gapiopts.ClientOption{}},
  30. {"api key", &config.Params{Creds: &config.Params_ApiKey{}}, []gapiopts.ClientOption{gapiopts.WithAPIKey("")}},
  31. {"app creds", &config.Params{Creds: &config.Params_AppCredentials{}}, []gapiopts.ClientOption{}},
  32. {"service account",
  33. &config.Params{Creds: &config.Params_ServiceAccountPath{}},
  34. []gapiopts.ClientOption{gapiopts.WithCredentialsFile("")}},
  35. {"endpoint",
  36. &config.Params{Endpoint: "foo.bar"},
  37. []gapiopts.ClientOption{gapiopts.WithEndpoint("")}},
  38. {"endpoint + svc account",
  39. &config.Params{Endpoint: "foo.bar", Creds: &config.Params_ServiceAccountPath{}},
  40. []gapiopts.ClientOption{gapiopts.WithEndpoint(""), gapiopts.WithCredentialsFile("")}},
  41. }
  42. for idx, tt := range tests {
  43. t.Run(fmt.Sprintf("[%d] %s", idx, tt.name), func(t *testing.T) {
  44. opts := ToOpts(tt.cfg)
  45. if len(opts) != len(tt.out) {
  46. t.Errorf("len(toOpts(%v)) = %d, expected %d", tt.cfg, len(opts), len(tt.out))
  47. }
  48. optSet := make(map[gapiopts.ClientOption]struct{})
  49. for _, opt := range opts {
  50. optSet[opt] = struct{}{}
  51. }
  52. for _, expected := range tt.out {
  53. found := false
  54. for _, actual := range opts {
  55. // We care that the types are what we expect, not necessarily that they're identical
  56. found = found || (reflect.TypeOf(expected) == reflect.TypeOf(actual))
  57. }
  58. if !found {
  59. t.Errorf("toOpts() = %v, wanted opt '%v' (type %v)", opts, expected, reflect.TypeOf(expected))
  60. }
  61. }
  62. })
  63. }
  64. }
  65. func TestMetadata(t *testing.T) {
  66. tests := []struct {
  67. name string
  68. shouldFill shouldFillFn
  69. projectIDFn metadataFn
  70. locationFn metadataFn
  71. clusterNameFn metadataFn
  72. want Metadata
  73. }{
  74. {
  75. "should not fill",
  76. func() bool { return false },
  77. func() (string, error) { return "pid", nil },
  78. func() (string, error) { return "location", nil },
  79. func() (string, error) { return "cluster", nil },
  80. Metadata{ProjectID: "", Location: "", ClusterName: ""},
  81. },
  82. {
  83. "should fill",
  84. func() bool { return true },
  85. func() (string, error) { return "pid", nil },
  86. func() (string, error) { return "location", nil },
  87. func() (string, error) { return "cluster", nil },
  88. Metadata{ProjectID: "pid", Location: "location", ClusterName: "cluster"},
  89. },
  90. {
  91. "project id error",
  92. func() bool { return true },
  93. func() (string, error) { return "", errors.New("error") },
  94. func() (string, error) { return "location", nil },
  95. func() (string, error) { return "cluster", nil },
  96. Metadata{ProjectID: "", Location: "location", ClusterName: "cluster"},
  97. },
  98. {
  99. "location error",
  100. func() bool { return true },
  101. func() (string, error) { return "pid", nil },
  102. func() (string, error) { return "location", errors.New("error") },
  103. func() (string, error) { return "cluster", nil },
  104. Metadata{ProjectID: "pid", Location: "", ClusterName: "cluster"},
  105. },
  106. {
  107. "cluster name error",
  108. func() bool { return true },
  109. func() (string, error) { return "pid", nil },
  110. func() (string, error) { return "location", nil },
  111. func() (string, error) { return "cluster", errors.New("error") },
  112. Metadata{ProjectID: "pid", Location: "location", ClusterName: ""},
  113. },
  114. }
  115. for idx, tt := range tests {
  116. t.Run(fmt.Sprintf("[%d] %s", idx, tt.name), func(t *testing.T) {
  117. mg := NewMetadataGenerator(tt.shouldFill, tt.projectIDFn, tt.locationFn, tt.clusterNameFn)
  118. got := mg.GenerateMetadata()
  119. if !reflect.DeepEqual(got, tt.want) {
  120. t.Errorf("Unexpected generated metadata: want %v got %v", tt.want, got)
  121. }
  122. })
  123. }
  124. }
  125. func TestFillProjectMetadata(t *testing.T) {
  126. tests := []struct {
  127. name string
  128. md Metadata
  129. in map[string]string
  130. want map[string]string
  131. }{
  132. {
  133. "empty metadata",
  134. Metadata{ProjectID: "", Location: "", ClusterName: ""},
  135. map[string]string{"project_id": "pid", "location": "location", "cluster_name": ""},
  136. map[string]string{"project_id": "pid", "location": "location", "cluster_name": ""},
  137. },
  138. {
  139. "fill metadata",
  140. Metadata{ProjectID: "pid", Location: "location", ClusterName: "cluster"},
  141. map[string]string{"project_id": "", "location": "location", "cluster_name": ""},
  142. map[string]string{"project_id": "pid", "location": "location", "cluster_name": "cluster"},
  143. },
  144. {
  145. "do not override",
  146. Metadata{ProjectID: "pid", Location: "location", ClusterName: "cluster"},
  147. map[string]string{"project_id": "id", "location": "l", "cluster_name": "c"},
  148. map[string]string{"project_id": "id", "location": "l", "cluster_name": "c"},
  149. },
  150. {
  151. "unrelated field",
  152. Metadata{ProjectID: "", Location: "", ClusterName: "cluster"},
  153. map[string]string{"project_id": "pid", "location": "location", "cluster": ""},
  154. map[string]string{"project_id": "pid", "location": "location", "cluster": ""},
  155. },
  156. }
  157. for idx, tt := range tests {
  158. t.Run(fmt.Sprintf("[%d] %s", idx, tt.name), func(t *testing.T) {
  159. tt.md.FillProjectMetadata(tt.in)
  160. if !reflect.DeepEqual(tt.in, tt.want) {
  161. t.Errorf("Unexpected map value, want %v got %v", tt.want, tt.in)
  162. }
  163. })
  164. }
  165. }