bootstrap_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. *
  3. * Copyright 2022 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package bootstrap
  19. import (
  20. "encoding/json"
  21. "testing"
  22. "google.golang.org/grpc/credentials"
  23. )
  24. const testCredsBuilderName = "test_creds"
  25. var builder = &testCredsBuilder{}
  26. func init() {
  27. RegisterCredentials(builder)
  28. }
  29. type testCredsBuilder struct {
  30. config json.RawMessage
  31. }
  32. func (t *testCredsBuilder) Build(config json.RawMessage) (credentials.Bundle, error) {
  33. t.config = config
  34. return nil, nil
  35. }
  36. func (t *testCredsBuilder) Name() string {
  37. return testCredsBuilderName
  38. }
  39. func TestRegisterNew(t *testing.T) {
  40. c := GetCredentials(testCredsBuilderName)
  41. if c == nil {
  42. t.Fatalf("GetCredentials(%q) credential = nil", testCredsBuilderName)
  43. }
  44. const sampleConfig = "sample_config"
  45. rawMessage := json.RawMessage(sampleConfig)
  46. if _, err := c.Build(rawMessage); err != nil {
  47. t.Errorf("Build(%v) error = %v, want nil", rawMessage, err)
  48. }
  49. if got, want := string(builder.config), sampleConfig; got != want {
  50. t.Errorf("Build config = %v, want %v", got, want)
  51. }
  52. }