|
| 1 | +package config_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "time" |
| 5 | + |
| 6 | + . "github.com/onsi/ginkgo/v2" |
| 7 | + . "github.com/onsi/gomega" |
| 8 | + |
| 9 | + "ocm.software/ocm/api/oci/config" |
| 10 | + "ocm.software/ocm/api/oci/cpi" |
| 11 | +) |
| 12 | + |
| 13 | +func dur(s string) *cpi.Duration { |
| 14 | + d := cpi.Duration(s) |
| 15 | + return &d |
| 16 | +} |
| 17 | + |
| 18 | +var _ = Describe("http config", func() { |
| 19 | + Context("apply", func() { |
| 20 | + It("applies timeout via ApplyTo", func() { |
| 21 | + ctx := cpi.New() |
| 22 | + cfg := &config.HTTPConfig{HTTPSettings: cpi.HTTPSettings{Timeout: dur("5m")}} |
| 23 | + |
| 24 | + Expect(cfg.ApplyTo(ctx.ConfigContext(), ctx)).To(Succeed()) |
| 25 | + Expect(ctx.GetHTTPSettings().GetTimeout()).To(HaveValue(Equal(5 * time.Minute))) |
| 26 | + }) |
| 27 | + |
| 28 | + It("applies via config context", func() { |
| 29 | + ctx := cpi.New() |
| 30 | + cfg := &config.HTTPConfig{HTTPSettings: cpi.HTTPSettings{Timeout: dur("30s")}} |
| 31 | + |
| 32 | + Expect(ctx.ConfigContext().ApplyConfig(cfg, "programmatic")).To(Succeed()) |
| 33 | + Expect(ctx.GetHTTPSettings().GetTimeout()).To(HaveValue(Equal(30 * time.Second))) |
| 34 | + }) |
| 35 | + |
| 36 | + It("parses all fields from JSON config", func() { |
| 37 | + ctx := cpi.New() |
| 38 | + raw := []byte(`{"type":"http.config.ocm.software/v1alpha1","timeout":"10s","tcpDialTimeout":"15s","tcpKeepAlive":"20s","tlsHandshakeTimeout":"5s","responseHeaderTimeout":"8s","idleConnTimeout":"45s"}`) |
| 39 | + cfg, err := ctx.ConfigContext().GetConfigForData(raw, nil) |
| 40 | + Expect(err).To(Succeed()) |
| 41 | + Expect(ctx.ConfigContext().ApplyConfig(cfg, "config file")).To(Succeed()) |
| 42 | + |
| 43 | + g := ctx.GetHTTPSettings() |
| 44 | + Expect(g.GetTimeout()).To(HaveValue(Equal(10 * time.Second))) |
| 45 | + Expect(g.TCPDialTimeout.TimeDuration()).To(HaveValue(Equal(15 * time.Second))) |
| 46 | + Expect(g.TCPKeepAlive.TimeDuration()).To(HaveValue(Equal(20 * time.Second))) |
| 47 | + Expect(g.TLSHandshakeTimeout.TimeDuration()).To(HaveValue(Equal(5 * time.Second))) |
| 48 | + Expect(g.ResponseHeaderTimeout.TimeDuration()).To(HaveValue(Equal(8 * time.Second))) |
| 49 | + Expect(g.IdleConnTimeout.TimeDuration()).To(HaveValue(Equal(45 * time.Second))) |
| 50 | + }) |
| 51 | + |
| 52 | + It("successive ApplyConfig overrides only non-nil fields", func() { |
| 53 | + ctx := cpi.New() |
| 54 | + |
| 55 | + first := &config.HTTPConfig{ |
| 56 | + HTTPSettings: cpi.HTTPSettings{ |
| 57 | + TCPDialTimeout: dur("15s"), |
| 58 | + }, |
| 59 | + } |
| 60 | + Expect(first.ApplyTo(ctx.ConfigContext(), ctx)).To(Succeed()) |
| 61 | + |
| 62 | + second := &config.HTTPConfig{HTTPSettings: cpi.HTTPSettings{Timeout: dur("1m")}} |
| 63 | + Expect(second.ApplyTo(ctx.ConfigContext(), ctx)).To(Succeed()) |
| 64 | + |
| 65 | + g := ctx.GetHTTPSettings() |
| 66 | + Expect(g.GetTimeout()).To(HaveValue(Equal(1 * time.Minute))) |
| 67 | + Expect(g.TCPDialTimeout.TimeDuration()).To(HaveValue(Equal(15 * time.Second))) |
| 68 | + }) |
| 69 | + |
| 70 | + It("applies via generic config wrapper", func() { |
| 71 | + ctx := cpi.New() |
| 72 | + raw := []byte(` |
| 73 | +type: generic.config.ocm.software/v1 |
| 74 | +configurations: |
| 75 | + - type: http.config.ocm.software |
| 76 | + timeout: "10s" |
| 77 | + tcpDialTimeout: "15s" |
| 78 | + tcpKeepAlive: "20s" |
| 79 | + tlsHandshakeTimeout: "5s" |
| 80 | + responseHeaderTimeout: "8s" |
| 81 | + idleConnTimeout: "45s" |
| 82 | +`) |
| 83 | + cfg, err := ctx.ConfigContext().GetConfigForData(raw, nil) |
| 84 | + Expect(err).To(Succeed()) |
| 85 | + Expect(ctx.ConfigContext().ApplyConfig(cfg, "config file")).To(Succeed()) |
| 86 | + |
| 87 | + g := ctx.GetHTTPSettings() |
| 88 | + Expect(g.GetTimeout()).To(HaveValue(Equal(10 * time.Second))) |
| 89 | + Expect(g.GetTCPDialTimeout()).To(HaveValue(Equal(15 * time.Second))) |
| 90 | + Expect(g.GetTCPKeepAlive()).To(HaveValue(Equal(20 * time.Second))) |
| 91 | + Expect(g.GetTLSHandshakeTimeout()).To(HaveValue(Equal(5 * time.Second))) |
| 92 | + Expect(g.GetResponseHeaderTimeout()).To(HaveValue(Equal(8 * time.Second))) |
| 93 | + Expect(g.GetIdleConnTimeout()).To(HaveValue(Equal(45 * time.Second))) |
| 94 | + }) |
| 95 | + |
| 96 | + It("rejects invalid duration string", func() { |
| 97 | + ctx := cpi.New() |
| 98 | + raw := []byte(`{"type":"http.config.ocm.software/v1alpha1","timeout":"notaduration"}`) |
| 99 | + _, err := ctx.ConfigContext().GetConfigForData(raw, nil) |
| 100 | + Expect(err).To(HaveOccurred()) |
| 101 | + Expect(err.Error()).To(ContainSubstring("invalid duration: notaduration")) |
| 102 | + }) |
| 103 | + |
| 104 | + It("default settings are nil", func() { |
| 105 | + ctx := cpi.New() |
| 106 | + g := ctx.GetHTTPSettings() |
| 107 | + Expect(g.Timeout).To(BeNil()) |
| 108 | + Expect(g.TCPDialTimeout).To(BeNil()) |
| 109 | + Expect(g.TCPKeepAlive).To(BeNil()) |
| 110 | + Expect(g.TLSHandshakeTimeout).To(BeNil()) |
| 111 | + Expect(g.ResponseHeaderTimeout).To(BeNil()) |
| 112 | + Expect(g.IdleConnTimeout).To(BeNil()) |
| 113 | + }) |
| 114 | + |
| 115 | + It("nil timeout returns nil not zero", func() { |
| 116 | + ctx := cpi.New() |
| 117 | + g := ctx.GetHTTPSettings() |
| 118 | + timeout, err := g.GetTimeout() |
| 119 | + Expect(err).To(Succeed()) |
| 120 | + Expect(timeout).To(BeNil()) |
| 121 | + }) |
| 122 | + }) |
| 123 | +}) |
0 commit comments