|
| 1 | +package handlers_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/containers/podman/v6/pkg/api/handlers" |
| 8 | +) |
| 9 | + |
| 10 | +// TestExecCreateConfigDetachKeys verifies that DetachKeys *string correctly |
| 11 | +// distinguishes "field absent" (nil) from "explicitly set to empty string" (*""). |
| 12 | +// This is the regression test for the bug where DetachKeys:"" was silently |
| 13 | +// ignored because the zero value of string is indistinguishable from absent. |
| 14 | +func TestExecCreateConfigDetachKeys(t *testing.T) { |
| 15 | + tests := []struct { |
| 16 | + name string |
| 17 | + body string |
| 18 | + wantNil bool |
| 19 | + wantVal string |
| 20 | + }{ |
| 21 | + { |
| 22 | + name: "empty body - DetachKeys should be nil", |
| 23 | + body: `{}`, |
| 24 | + wantNil: true, |
| 25 | + }, |
| 26 | + { |
| 27 | + name: "field absent - DetachKeys should be nil", |
| 28 | + body: `{"AttachStdout":true}`, |
| 29 | + wantNil: true, |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "field set to empty string - DetachKeys should be non-nil empty", |
| 33 | + body: `{"DetachKeys":""}`, |
| 34 | + wantNil: false, |
| 35 | + wantVal: "", |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "field set to value - DetachKeys should be non-nil with value", |
| 39 | + body: `{"DetachKeys":"ctrl-p,ctrl-q"}`, |
| 40 | + wantNil: false, |
| 41 | + wantVal: "ctrl-p,ctrl-q", |
| 42 | + }, |
| 43 | + } |
| 44 | + |
| 45 | + for _, tt := range tests { |
| 46 | + t.Run(tt.name, func(t *testing.T) { |
| 47 | + var cfg handlers.ExecCreateConfig |
| 48 | + if err := json.Unmarshal([]byte(tt.body), &cfg); err != nil { |
| 49 | + t.Fatalf("unmarshal: %v", err) |
| 50 | + } |
| 51 | + if tt.wantNil { |
| 52 | + if cfg.DetachKeys != nil { |
| 53 | + t.Errorf("expected DetachKeys to be nil, got %q", *cfg.DetachKeys) |
| 54 | + } |
| 55 | + } else { |
| 56 | + if cfg.DetachKeys == nil { |
| 57 | + t.Fatal("expected DetachKeys to be non-nil") |
| 58 | + } |
| 59 | + if *cfg.DetachKeys != tt.wantVal { |
| 60 | + t.Errorf("DetachKeys = %q, want %q", *cfg.DetachKeys, tt.wantVal) |
| 61 | + } |
| 62 | + } |
| 63 | + }) |
| 64 | + } |
| 65 | +} |
0 commit comments