|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +// TestValidateUpstreamRequestTimeout exercises the validateUpstream branches |
| 9 | +// for upstream.request_timeout that are not covered by existing tests. |
| 10 | +func TestValidateUpstreamRequestTimeout(t *testing.T) { |
| 11 | + cases := []struct { |
| 12 | + name string |
| 13 | + timeout string |
| 14 | + wantError bool |
| 15 | + wantMsg string |
| 16 | + }{ |
| 17 | + { |
| 18 | + name: "invalid_with_extra_text", |
| 19 | + timeout: "5s extra", |
| 20 | + wantError: true, |
| 21 | + wantMsg: "upstream.request_timeout must be a positive duration", |
| 22 | + }, |
| 23 | + { |
| 24 | + name: "negative_duration", |
| 25 | + timeout: "-1s", |
| 26 | + wantError: true, |
| 27 | + wantMsg: "upstream.request_timeout must be a positive duration", |
| 28 | + }, |
| 29 | + { |
| 30 | + name: "zero_duration", |
| 31 | + timeout: "0s", |
| 32 | + wantError: true, |
| 33 | + wantMsg: "upstream.request_timeout must be a positive duration", |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "empty_is_skipped", |
| 37 | + timeout: "", |
| 38 | + wantError: false, |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "valid_positive_duration", |
| 42 | + timeout: "30s", |
| 43 | + wantError: false, |
| 44 | + }, |
| 45 | + } |
| 46 | + |
| 47 | + for _, tc := range cases { |
| 48 | + t.Run(tc.name, func(t *testing.T) { |
| 49 | + cfg := Defaults() |
| 50 | + cfg.Upstream.RequestTimeout = tc.timeout |
| 51 | + err := Validate(&cfg) |
| 52 | + if tc.wantError { |
| 53 | + if err == nil { |
| 54 | + t.Fatalf("Validate() = nil, want error containing %q", tc.wantMsg) |
| 55 | + } |
| 56 | + if !strings.Contains(err.Error(), tc.wantMsg) { |
| 57 | + t.Fatalf("Validate() = %v, want error containing %q", err, tc.wantMsg) |
| 58 | + } |
| 59 | + } else { |
| 60 | + // Only fail if the upstream.request_timeout field itself errors. |
| 61 | + if err != nil && strings.Contains(err.Error(), "upstream.request_timeout") { |
| 62 | + t.Fatalf("Validate() produced unexpected upstream.request_timeout error: %v", err) |
| 63 | + } |
| 64 | + } |
| 65 | + }) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// TestValidateContainerCreateAllowedDeviceRequests exercises the per-entry |
| 70 | +// validation branches inside validateContainerCreateConfig for |
| 71 | +// allowed_device_requests, which were previously unreachable from existing tests. |
| 72 | +func TestValidateContainerCreateAllowedDeviceRequests(t *testing.T) { |
| 73 | + intPtr := func(v int) *int { return &v } |
| 74 | + |
| 75 | + t.Run("empty_driver_rejected", func(t *testing.T) { |
| 76 | + cfg := Defaults() |
| 77 | + cfg.RequestBody.ContainerCreate.AllowedDeviceRequests = []AllowedDeviceRequest{ |
| 78 | + {Driver: "", AllowedCapabilities: [][]string{{"gpu"}}, MaxCount: nil}, |
| 79 | + } |
| 80 | + err := Validate(&cfg) |
| 81 | + if err == nil { |
| 82 | + t.Fatal("Validate() = nil, want error for empty driver") |
| 83 | + } |
| 84 | + if !strings.Contains(err.Error(), "request_body.container_create.allowed_device_requests[0].driver is required") { |
| 85 | + t.Fatalf("Validate() = %v, want driver-required error", err) |
| 86 | + } |
| 87 | + }) |
| 88 | + |
| 89 | + t.Run("whitespace_only_driver_rejected", func(t *testing.T) { |
| 90 | + cfg := Defaults() |
| 91 | + cfg.RequestBody.ContainerCreate.AllowedDeviceRequests = []AllowedDeviceRequest{ |
| 92 | + {Driver: " ", AllowedCapabilities: [][]string{{"gpu"}}, MaxCount: nil}, |
| 93 | + } |
| 94 | + err := Validate(&cfg) |
| 95 | + if err == nil { |
| 96 | + t.Fatal("Validate() = nil, want error for whitespace-only driver") |
| 97 | + } |
| 98 | + if !strings.Contains(err.Error(), "request_body.container_create.allowed_device_requests[0].driver is required") { |
| 99 | + t.Fatalf("Validate() = %v, want driver-required error", err) |
| 100 | + } |
| 101 | + }) |
| 102 | + |
| 103 | + t.Run("empty_capability_set_rejected", func(t *testing.T) { |
| 104 | + cfg := Defaults() |
| 105 | + cfg.RequestBody.ContainerCreate.AllowedDeviceRequests = []AllowedDeviceRequest{ |
| 106 | + { |
| 107 | + Driver: "nvidia.com/gpu", |
| 108 | + // The outer slice has one entry; that entry is an empty capability set. |
| 109 | + AllowedCapabilities: [][]string{{}}, |
| 110 | + MaxCount: nil, |
| 111 | + }, |
| 112 | + } |
| 113 | + err := Validate(&cfg) |
| 114 | + if err == nil { |
| 115 | + t.Fatal("Validate() = nil, want error for empty capability set") |
| 116 | + } |
| 117 | + if !strings.Contains(err.Error(), "request_body.container_create.allowed_device_requests[0].allowed_capabilities[0] must be a non-empty capability set") { |
| 118 | + t.Fatalf("Validate() = %v, want non-empty capability set error", err) |
| 119 | + } |
| 120 | + }) |
| 121 | + |
| 122 | + t.Run("max_count_below_minus_one_rejected", func(t *testing.T) { |
| 123 | + cfg := Defaults() |
| 124 | + cfg.RequestBody.ContainerCreate.AllowedDeviceRequests = []AllowedDeviceRequest{ |
| 125 | + {Driver: "nvidia.com/gpu", AllowedCapabilities: [][]string{{"compute"}}, MaxCount: intPtr(-2)}, |
| 126 | + } |
| 127 | + err := Validate(&cfg) |
| 128 | + if err == nil { |
| 129 | + t.Fatal("Validate() = nil, want error for max_count < -1") |
| 130 | + } |
| 131 | + if !strings.Contains(err.Error(), "request_body.container_create.allowed_device_requests[0].max_count must be -1 or a non-negative integer") { |
| 132 | + t.Fatalf("Validate() = %v, want max_count error", err) |
| 133 | + } |
| 134 | + }) |
| 135 | + |
| 136 | + t.Run("valid_entry_max_count_minus_one", func(t *testing.T) { |
| 137 | + cfg := Defaults() |
| 138 | + cfg.RequestBody.ContainerCreate.AllowedDeviceRequests = []AllowedDeviceRequest{ |
| 139 | + {Driver: "nvidia.com/gpu", AllowedCapabilities: [][]string{{"compute", "utility"}}, MaxCount: intPtr(-1)}, |
| 140 | + } |
| 141 | + err := Validate(&cfg) |
| 142 | + if err != nil && strings.Contains(err.Error(), "allowed_device_requests") { |
| 143 | + t.Fatalf("Validate() produced unexpected allowed_device_requests error: %v", err) |
| 144 | + } |
| 145 | + }) |
| 146 | + |
| 147 | + t.Run("valid_entry_max_count_zero", func(t *testing.T) { |
| 148 | + cfg := Defaults() |
| 149 | + cfg.RequestBody.ContainerCreate.AllowedDeviceRequests = []AllowedDeviceRequest{ |
| 150 | + {Driver: "nvidia.com/gpu", AllowedCapabilities: [][]string{{"compute"}}, MaxCount: intPtr(0)}, |
| 151 | + } |
| 152 | + err := Validate(&cfg) |
| 153 | + if err != nil && strings.Contains(err.Error(), "allowed_device_requests") { |
| 154 | + t.Fatalf("Validate() produced unexpected allowed_device_requests error: %v", err) |
| 155 | + } |
| 156 | + }) |
| 157 | + |
| 158 | + t.Run("valid_entry_max_count_positive", func(t *testing.T) { |
| 159 | + cfg := Defaults() |
| 160 | + cfg.RequestBody.ContainerCreate.AllowedDeviceRequests = []AllowedDeviceRequest{ |
| 161 | + {Driver: "nvidia.com/gpu", AllowedCapabilities: [][]string{{"compute"}}, MaxCount: intPtr(4)}, |
| 162 | + } |
| 163 | + err := Validate(&cfg) |
| 164 | + if err != nil && strings.Contains(err.Error(), "allowed_device_requests") { |
| 165 | + t.Fatalf("Validate() produced unexpected allowed_device_requests error: %v", err) |
| 166 | + } |
| 167 | + }) |
| 168 | + |
| 169 | + t.Run("valid_entry_nil_max_count", func(t *testing.T) { |
| 170 | + cfg := Defaults() |
| 171 | + cfg.RequestBody.ContainerCreate.AllowedDeviceRequests = []AllowedDeviceRequest{ |
| 172 | + {Driver: "nvidia.com/gpu", AllowedCapabilities: [][]string{{"compute", "utility"}, {"video"}}, MaxCount: nil}, |
| 173 | + } |
| 174 | + err := Validate(&cfg) |
| 175 | + if err != nil && strings.Contains(err.Error(), "allowed_device_requests") { |
| 176 | + t.Fatalf("Validate() produced unexpected allowed_device_requests error: %v", err) |
| 177 | + } |
| 178 | + }) |
| 179 | +} |
0 commit comments