|
| 1 | +package envconfig_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/opendecree/decree/sdk/configclient" |
| 10 | + envconfig "github.com/opendecree/decree/sdk/contrib/envconfig" |
| 11 | +) |
| 12 | + |
| 13 | +// stubTransport returns predetermined field values by path. |
| 14 | +type stubTransport struct { |
| 15 | + values map[string]*configclient.TypedValue |
| 16 | + err error |
| 17 | +} |
| 18 | + |
| 19 | +func (s *stubTransport) GetField(_ context.Context, req *configclient.GetFieldRequest) (*configclient.GetFieldResponse, error) { |
| 20 | + if s.err != nil { |
| 21 | + return nil, s.err |
| 22 | + } |
| 23 | + v, ok := s.values[req.FieldPath] |
| 24 | + if !ok { |
| 25 | + return nil, errors.New("field not found: " + req.FieldPath) |
| 26 | + } |
| 27 | + return &configclient.GetFieldResponse{FieldPath: req.FieldPath, Value: v}, nil |
| 28 | +} |
| 29 | + |
| 30 | +func (s *stubTransport) GetConfig(_ context.Context, _ *configclient.GetConfigRequest) (*configclient.GetConfigResponse, error) { |
| 31 | + return &configclient.GetConfigResponse{}, nil |
| 32 | +} |
| 33 | + |
| 34 | +func (s *stubTransport) GetFields(_ context.Context, req *configclient.GetFieldsRequest) (*configclient.GetFieldsResponse, error) { |
| 35 | + vals := make([]configclient.ConfigValue, 0, len(req.FieldPaths)) |
| 36 | + for _, p := range req.FieldPaths { |
| 37 | + v, ok := s.values[p] |
| 38 | + if ok { |
| 39 | + vals = append(vals, configclient.ConfigValue{FieldPath: p, Value: v}) |
| 40 | + } |
| 41 | + } |
| 42 | + return &configclient.GetFieldsResponse{Values: vals}, nil |
| 43 | +} |
| 44 | + |
| 45 | +func (s *stubTransport) SetField(_ context.Context, _ *configclient.SetFieldRequest) (*configclient.SetFieldResponse, error) { |
| 46 | + return &configclient.SetFieldResponse{}, nil |
| 47 | +} |
| 48 | + |
| 49 | +func (s *stubTransport) SetFields(_ context.Context, _ *configclient.SetFieldsRequest) (*configclient.SetFieldsResponse, error) { |
| 50 | + return &configclient.SetFieldsResponse{}, nil |
| 51 | +} |
| 52 | + |
| 53 | +func (s *stubTransport) Subscribe(_ context.Context, _ *configclient.SubscribeRequest) (configclient.Subscription, error) { |
| 54 | + return nil, nil |
| 55 | +} |
| 56 | + |
| 57 | +func newClient(values map[string]*configclient.TypedValue) *configclient.Client { |
| 58 | + return configclient.New(&stubTransport{values: values}) |
| 59 | +} |
| 60 | + |
| 61 | +func TestProcess_AllTypes(t *testing.T) { |
| 62 | + client := newClient(map[string]*configclient.TypedValue{ |
| 63 | + "app.name": configclient.StringVal("myapp"), |
| 64 | + "app.debug": configclient.BoolVal(true), |
| 65 | + "app.count": configclient.IntVal(42), |
| 66 | + "app.rate": configclient.FloatVal(1.5), |
| 67 | + "app.timeout": configclient.DurationVal(5 * time.Second), |
| 68 | + }) |
| 69 | + |
| 70 | + type Config struct { |
| 71 | + Name string `decree:"app.name"` |
| 72 | + Debug bool `decree:"app.debug"` |
| 73 | + Count int64 `decree:"app.count"` |
| 74 | + Rate float64 `decree:"app.rate"` |
| 75 | + Timeout time.Duration `decree:"app.timeout"` |
| 76 | + } |
| 77 | + |
| 78 | + var cfg Config |
| 79 | + if err := envconfig.Process(context.Background(), client, "t1", &cfg); err != nil { |
| 80 | + t.Fatalf("Process: %v", err) |
| 81 | + } |
| 82 | + if cfg.Name != "myapp" { |
| 83 | + t.Errorf("Name = %q, want %q", cfg.Name, "myapp") |
| 84 | + } |
| 85 | + if !cfg.Debug { |
| 86 | + t.Error("Debug = false, want true") |
| 87 | + } |
| 88 | + if cfg.Count != 42 { |
| 89 | + t.Errorf("Count = %d, want 42", cfg.Count) |
| 90 | + } |
| 91 | + if cfg.Rate != 1.5 { |
| 92 | + t.Errorf("Rate = %f, want 1.5", cfg.Rate) |
| 93 | + } |
| 94 | + if cfg.Timeout != 5*time.Second { |
| 95 | + t.Errorf("Timeout = %v, want 5s", cfg.Timeout) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func TestProcess_SkipsUntagged(t *testing.T) { |
| 100 | + client := newClient(map[string]*configclient.TypedValue{}) |
| 101 | + |
| 102 | + type Config struct { |
| 103 | + Name string `decree:"app.name"` |
| 104 | + Untagged string |
| 105 | + Skipped string `decree:"-"` |
| 106 | + unexported string //nolint:unused |
| 107 | + } |
| 108 | + |
| 109 | + _ = client |
| 110 | + // Process should not error for untagged or "-"-tagged or unexported fields |
| 111 | + client2 := newClient(map[string]*configclient.TypedValue{ |
| 112 | + "app.name": configclient.StringVal("x"), |
| 113 | + }) |
| 114 | + var cfg Config |
| 115 | + if err := envconfig.Process(context.Background(), client2, "t1", &cfg); err != nil { |
| 116 | + t.Fatalf("unexpected error: %v", err) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +func TestProcess_NonPointerError(t *testing.T) { |
| 121 | + type Config struct{} |
| 122 | + err := envconfig.Process(context.Background(), nil, "", Config{}) |
| 123 | + if err == nil { |
| 124 | + t.Error("expected error for non-pointer, got nil") |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +func TestProcess_FieldError(t *testing.T) { |
| 129 | + stub := &stubTransport{err: errors.New("transport error")} |
| 130 | + client := configclient.New(stub) |
| 131 | + |
| 132 | + type Config struct { |
| 133 | + Name string `decree:"app.name"` |
| 134 | + } |
| 135 | + var cfg Config |
| 136 | + err := envconfig.Process(context.Background(), client, "t1", &cfg) |
| 137 | + if err == nil { |
| 138 | + t.Error("expected error, got nil") |
| 139 | + } |
| 140 | +} |
0 commit comments