registry_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package apiv1
  2. import (
  3. "context"
  4. "fmt"
  5. "reflect"
  6. "sync"
  7. "testing"
  8. )
  9. func TestRegister(t *testing.T) {
  10. t.Cleanup(func() {
  11. registry = new(sync.Map)
  12. })
  13. type args struct {
  14. t Type
  15. fn CertificateAuthorityServiceNewFunc
  16. }
  17. tests := []struct {
  18. name string
  19. args args
  20. want CertificateAuthorityService
  21. wantErr bool
  22. }{
  23. {"ok", args{"TestCAS", func(ctx context.Context, opts Options) (CertificateAuthorityService, error) {
  24. return &testCAS{}, nil
  25. }}, &testCAS{}, false},
  26. {"error", args{"ErrorCAS", func(ctx context.Context, opts Options) (CertificateAuthorityService, error) {
  27. return nil, fmt.Errorf("an error")
  28. }}, nil, true},
  29. }
  30. for _, tt := range tests {
  31. t.Run(tt.name, func(t *testing.T) {
  32. Register(tt.args.t, tt.args.fn)
  33. fmt.Println(registry)
  34. fn, ok := registry.Load(tt.args.t.String())
  35. if !ok {
  36. t.Errorf("Register() failed")
  37. return
  38. }
  39. got, err := fn.(CertificateAuthorityServiceNewFunc)(context.Background(), Options{})
  40. if (err != nil) != tt.wantErr {
  41. t.Errorf("CertificateAuthorityServiceNewFunc() error = %v, wantErr %v", err, tt.wantErr)
  42. return
  43. }
  44. if !reflect.DeepEqual(got, tt.want) {
  45. t.Errorf("CertificateAuthorityServiceNewFunc() = %v, want %v", got, tt.want)
  46. }
  47. })
  48. }
  49. }
  50. func TestLoadCertificateAuthorityServiceNewFunc(t *testing.T) {
  51. mockRegister(t)
  52. type args struct {
  53. t Type
  54. }
  55. tests := []struct {
  56. name string
  57. args args
  58. want CertificateAuthorityService
  59. wantOk bool
  60. }{
  61. {"default", args{""}, &testCAS{name: SoftCAS}, true},
  62. {"SoftCAS", args{"SoftCAS"}, &testCAS{name: SoftCAS}, true},
  63. {"CloudCAS", args{"CloudCAS"}, &testCAS{name: CloudCAS}, true},
  64. {"softcas", args{"softcas"}, &testCAS{name: SoftCAS}, true},
  65. {"cloudcas", args{"cloudcas"}, &testCAS{name: CloudCAS}, true},
  66. {"FailCAS", args{"FailCAS"}, nil, false},
  67. }
  68. for _, tt := range tests {
  69. t.Run(tt.name, func(t *testing.T) {
  70. fn, ok := LoadCertificateAuthorityServiceNewFunc(tt.args.t)
  71. if ok != tt.wantOk {
  72. t.Errorf("LoadCertificateAuthorityServiceNewFunc() ok = %v, want %v", ok, tt.wantOk)
  73. return
  74. }
  75. if ok {
  76. got, err := fn(context.Background(), Options{})
  77. if err != nil {
  78. t.Errorf("CertificateAuthorityServiceNewFunc() error = %v", err)
  79. return
  80. }
  81. if !reflect.DeepEqual(got, tt.want) {
  82. t.Errorf("CertificateAuthorityServiceNewFunc() = %v, want %v", got, tt.want)
  83. }
  84. }
  85. })
  86. }
  87. }