|
| 1 | +package config_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "time" |
| 6 | + |
| 7 | + . "github.com/onsi/ginkgo/v2" |
| 8 | + . "github.com/onsi/gomega" |
| 9 | + |
| 10 | + "ocm.software/ocm/api/oci/config" |
| 11 | + "ocm.software/ocm/api/oci/cpi" |
| 12 | +) |
| 13 | + |
| 14 | +func dur(s string) *cpi.Duration { |
| 15 | + td, err := time.ParseDuration(s) |
| 16 | + if err != nil { |
| 17 | + panic(err) |
| 18 | + } |
| 19 | + d := cpi.Duration(td) |
| 20 | + return &d |
| 21 | +} |
| 22 | + |
| 23 | +func MustGetHTTPSettings(ctx cpi.Context) cpi.HTTPSettings { |
| 24 | + g, err := ctx.GetHTTPSettings() |
| 25 | + ExpectWithOffset(1, err).To(Succeed()) |
| 26 | + return g |
| 27 | +} |
| 28 | + |
| 29 | +var _ = Describe("http config", func() { |
| 30 | + Context("apply", func() { |
| 31 | + It("applies timeout via ApplyTo", func() { |
| 32 | + ctx := cpi.New() |
| 33 | + cfg := &config.HTTPConfig{HTTPSettings: cpi.HTTPSettings{Timeout: dur("5m")}} |
| 34 | + |
| 35 | + Expect(cfg.ApplyTo(ctx.ConfigContext(), ctx)).To(Succeed()) |
| 36 | + g := MustGetHTTPSettings(ctx) |
| 37 | + Expect(time.Duration(*g.Timeout)).To(Equal(5 * time.Minute)) |
| 38 | + }) |
| 39 | + |
| 40 | + It("applies via config context", func() { |
| 41 | + ctx := cpi.New() |
| 42 | + cfg := &config.HTTPConfig{HTTPSettings: cpi.HTTPSettings{Timeout: dur("30s")}} |
| 43 | + |
| 44 | + Expect(ctx.ConfigContext().ApplyConfig(cfg, "programmatic")).To(Succeed()) |
| 45 | + g := MustGetHTTPSettings(ctx) |
| 46 | + Expect(time.Duration(*g.Timeout)).To(Equal(30 * time.Second)) |
| 47 | + }) |
| 48 | + |
| 49 | + It("parses all fields from JSON config", func() { |
| 50 | + ctx := cpi.New() |
| 51 | + raw := []byte(`{"type":"http.config.ocm.software/v1alpha1","timeout":"10s","tcpDialTimeout":"15s","tcpKeepAlive":"20s","tlsHandshakeTimeout":"5s","responseHeaderTimeout":"8s","idleConnTimeout":"45s"}`) |
| 52 | + cfg, err := ctx.ConfigContext().GetConfigForData(raw, nil) |
| 53 | + Expect(err).To(Succeed()) |
| 54 | + Expect(ctx.ConfigContext().ApplyConfig(cfg, "config file")).To(Succeed()) |
| 55 | + |
| 56 | + g := MustGetHTTPSettings(ctx) |
| 57 | + Expect(time.Duration(*g.Timeout)).To(Equal(10 * time.Second)) |
| 58 | + Expect(time.Duration(*g.TCPDialTimeout)).To(Equal(15 * time.Second)) |
| 59 | + Expect(time.Duration(*g.TCPKeepAlive)).To(Equal(20 * time.Second)) |
| 60 | + Expect(time.Duration(*g.TLSHandshakeTimeout)).To(Equal(5 * time.Second)) |
| 61 | + Expect(time.Duration(*g.ResponseHeaderTimeout)).To(Equal(8 * time.Second)) |
| 62 | + Expect(time.Duration(*g.IdleConnTimeout)).To(Equal(45 * time.Second)) |
| 63 | + }) |
| 64 | + |
| 65 | + It("successive ApplyConfig overrides only non-nil fields", func() { |
| 66 | + ctx := cpi.New() |
| 67 | + |
| 68 | + first := &config.HTTPConfig{ |
| 69 | + HTTPSettings: cpi.HTTPSettings{ |
| 70 | + TCPDialTimeout: dur("15s"), |
| 71 | + }, |
| 72 | + } |
| 73 | + Expect(first.ApplyTo(ctx.ConfigContext(), ctx)).To(Succeed()) |
| 74 | + |
| 75 | + second := &config.HTTPConfig{HTTPSettings: cpi.HTTPSettings{Timeout: dur("1m")}} |
| 76 | + Expect(second.ApplyTo(ctx.ConfigContext(), ctx)).To(Succeed()) |
| 77 | + |
| 78 | + g := MustGetHTTPSettings(ctx) |
| 79 | + Expect(time.Duration(*g.Timeout)).To(Equal(1 * time.Minute)) |
| 80 | + Expect(time.Duration(*g.TCPDialTimeout)).To(Equal(15 * time.Second)) |
| 81 | + }) |
| 82 | + |
| 83 | + It("applies via generic config wrapper", func() { |
| 84 | + ctx := cpi.New() |
| 85 | + raw := []byte(` |
| 86 | +type: generic.config.ocm.software/v1 |
| 87 | +configurations: |
| 88 | + - type: http.config.ocm.software |
| 89 | + timeout: "10s" |
| 90 | + tcpDialTimeout: "15s" |
| 91 | + tcpKeepAlive: "20s" |
| 92 | + tlsHandshakeTimeout: "5s" |
| 93 | + responseHeaderTimeout: "8s" |
| 94 | + idleConnTimeout: "45s" |
| 95 | +`) |
| 96 | + cfg, err := ctx.ConfigContext().GetConfigForData(raw, nil) |
| 97 | + Expect(err).To(Succeed()) |
| 98 | + Expect(ctx.ConfigContext().ApplyConfig(cfg, "config file")).To(Succeed()) |
| 99 | + |
| 100 | + g := MustGetHTTPSettings(ctx) |
| 101 | + Expect(time.Duration(*g.Timeout)).To(Equal(10 * time.Second)) |
| 102 | + Expect(time.Duration(*g.TCPDialTimeout)).To(Equal(15 * time.Second)) |
| 103 | + Expect(time.Duration(*g.TCPKeepAlive)).To(Equal(20 * time.Second)) |
| 104 | + Expect(time.Duration(*g.TLSHandshakeTimeout)).To(Equal(5 * time.Second)) |
| 105 | + Expect(time.Duration(*g.ResponseHeaderTimeout)).To(Equal(8 * time.Second)) |
| 106 | + Expect(time.Duration(*g.IdleConnTimeout)).To(Equal(45 * time.Second)) |
| 107 | + }) |
| 108 | + |
| 109 | + DescribeTable("rejects invalid duration values on unmarshal", |
| 110 | + func(jsonValue string, expectedErr string) { |
| 111 | + ctx := cpi.New() |
| 112 | + raw := []byte(`{"type":"http.config.ocm.software/v1alpha1","timeout":` + jsonValue + `}`) |
| 113 | + _, err := ctx.ConfigContext().GetConfigForData(raw, nil) |
| 114 | + Expect(err).To(HaveOccurred()) |
| 115 | + Expect(err.Error()).To(ContainSubstring(expectedErr)) |
| 116 | + }, |
| 117 | + Entry("garbage string", `"notaduration"`, "expected a Go duration string"), |
| 118 | + Entry("number instead of string", `42`, "expected a Go duration string"), |
| 119 | + Entry("boolean instead of string", `true`, "expected a Go duration string"), |
| 120 | + Entry("empty string", `""`, "expected a Go duration string"), |
| 121 | + ) |
| 122 | + |
| 123 | + DescribeTable("rejects negative duration for timeout fields", |
| 124 | + func(field string, cfg *config.HTTPConfig) { |
| 125 | + ctx := cpi.New() |
| 126 | + Expect(cfg.ApplyTo(ctx.ConfigContext(), ctx)).To(MatchError( |
| 127 | + ContainSubstring("invalid value for " + field), |
| 128 | + )) |
| 129 | + }, |
| 130 | + Entry("timeout -5m", "timeout", |
| 131 | + &config.HTTPConfig{HTTPSettings: cpi.HTTPSettings{Timeout: dur("-5m")}}), |
| 132 | + Entry("tcpDialTimeout -10s", "tcpDialTimeout", |
| 133 | + &config.HTTPConfig{HTTPSettings: cpi.HTTPSettings{TCPDialTimeout: dur("-10s")}}), |
| 134 | + Entry("tlsHandshakeTimeout -10h5m", "tlsHandshakeTimeout", |
| 135 | + &config.HTTPConfig{HTTPSettings: cpi.HTTPSettings{TLSHandshakeTimeout: dur("-10h5m")}}), |
| 136 | + Entry("responseHeaderTimeout -1s", "responseHeaderTimeout", |
| 137 | + &config.HTTPConfig{HTTPSettings: cpi.HTTPSettings{ResponseHeaderTimeout: dur("-1s")}}), |
| 138 | + Entry("idleConnTimeout -30s", "idleConnTimeout", |
| 139 | + &config.HTTPConfig{HTTPSettings: cpi.HTTPSettings{IdleConnTimeout: dur("-30s")}}), |
| 140 | + ) |
| 141 | + |
| 142 | + It("allows negative tcpKeepAlive to disable keep-alive probes", func() { |
| 143 | + ctx := cpi.New() |
| 144 | + cfg := &config.HTTPConfig{HTTPSettings: cpi.HTTPSettings{TCPKeepAlive: dur("-1s")}} |
| 145 | + Expect(cfg.ApplyTo(ctx.ConfigContext(), ctx)).To(Succeed()) |
| 146 | + g := MustGetHTTPSettings(ctx) |
| 147 | + Expect(time.Duration(*g.TCPKeepAlive)).To(Equal(-1 * time.Second)) |
| 148 | + }) |
| 149 | + |
| 150 | + DescribeTable("accepts compound duration like 1h5s", |
| 151 | + func(cfg *config.HTTPConfig) { |
| 152 | + ctx := cpi.New() |
| 153 | + Expect(cfg.ApplyTo(ctx.ConfigContext(), ctx)).To(Succeed()) |
| 154 | + }, |
| 155 | + Entry("timeout", &config.HTTPConfig{HTTPSettings: cpi.HTTPSettings{Timeout: dur("1h5s")}}), |
| 156 | + Entry("tcpDialTimeout", &config.HTTPConfig{HTTPSettings: cpi.HTTPSettings{TCPDialTimeout: dur("1h5s")}}), |
| 157 | + Entry("tcpKeepAlive", &config.HTTPConfig{HTTPSettings: cpi.HTTPSettings{TCPKeepAlive: dur("1h5s")}}), |
| 158 | + Entry("tlsHandshakeTimeout", &config.HTTPConfig{HTTPSettings: cpi.HTTPSettings{TLSHandshakeTimeout: dur("1h5s")}}), |
| 159 | + Entry("responseHeaderTimeout", &config.HTTPConfig{HTTPSettings: cpi.HTTPSettings{ResponseHeaderTimeout: dur("1h5s")}}), |
| 160 | + Entry("idleConnTimeout", &config.HTTPConfig{HTTPSettings: cpi.HTTPSettings{IdleConnTimeout: dur("1h5s")}}), |
| 161 | + ) |
| 162 | + |
| 163 | + It("default settings are nil", func() { |
| 164 | + g := MustGetHTTPSettings(cpi.New()) |
| 165 | + Expect(g.Timeout).To(BeNil()) |
| 166 | + Expect(g.TCPDialTimeout).To(BeNil()) |
| 167 | + Expect(g.TCPKeepAlive).To(BeNil()) |
| 168 | + Expect(g.TLSHandshakeTimeout).To(BeNil()) |
| 169 | + Expect(g.ResponseHeaderTimeout).To(BeNil()) |
| 170 | + Expect(g.IdleConnTimeout).To(BeNil()) |
| 171 | + }) |
| 172 | + |
| 173 | + It("nil timeout returns nil not zero", func() { |
| 174 | + g := MustGetHTTPSettings(cpi.New()) |
| 175 | + Expect(g.Timeout).To(BeNil()) |
| 176 | + }) |
| 177 | + |
| 178 | + It("round-trips Duration through MarshalJSON and UnmarshalJSON", func() { |
| 179 | + original := cpi.HTTPSettings{ |
| 180 | + Timeout: dur("5m30s"), |
| 181 | + TCPDialTimeout: dur("15s"), |
| 182 | + } |
| 183 | + data, err := json.Marshal(original) |
| 184 | + Expect(err).To(Succeed()) |
| 185 | + |
| 186 | + var restored cpi.HTTPSettings |
| 187 | + Expect(json.Unmarshal(data, &restored)).To(Succeed()) |
| 188 | + Expect(time.Duration(*restored.Timeout)).To(Equal(5*time.Minute + 30*time.Second)) |
| 189 | + Expect(time.Duration(*restored.TCPDialTimeout)).To(Equal(15 * time.Second)) |
| 190 | + Expect(restored.TCPKeepAlive).To(BeNil()) |
| 191 | + }) |
| 192 | + }) |
| 193 | +}) |
0 commit comments