Lorenz Brun | d67eb4f | 2023-04-11 16:03:31 +0200 | [diff] [blame^] | 1 | package bootparam |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | func TestConsoles(t *testing.T) { |
| 6 | cases := []struct { |
| 7 | name string |
| 8 | cmdline string |
| 9 | consoles []string |
| 10 | }{ |
| 11 | {"Empty", "", []string{}}, |
| 12 | {"None", "notconsole=test", []string{}}, |
| 13 | {"Single", "asdf=ttyS1 console=ttyS0,115200", []string{"ttyS0"}}, |
| 14 | {"MultipleSame", "console=ttyS0 noop console=ttyS0", []string{"ttyS0"}}, |
| 15 | {"MultipleDiff", "console=tty27 console=ttyACM0", []string{"tty27", "ttyACM0"}}, |
| 16 | {"WithDev", "console=/dev/ttyXYZ0", []string{"ttyXYZ0"}}, |
| 17 | {"BrokenBadDev", "console=/etc/passwd", []string{}}, |
| 18 | {"BrokenNoValue", "console=", []string{}}, |
| 19 | } |
| 20 | for _, c := range cases { |
| 21 | t.Run(c.name, func(t *testing.T) { |
| 22 | p, _, err := Unmarshal(c.cmdline) |
| 23 | if err != nil { |
| 24 | t.Fatalf("Failed to parse cmdline %q: %v", c.cmdline, err) |
| 25 | } |
| 26 | consoles := p.Consoles() |
| 27 | wantConsoles := make(map[string]bool) |
| 28 | for _, con := range c.consoles { |
| 29 | wantConsoles[con] = true |
| 30 | } |
| 31 | for con := range wantConsoles { |
| 32 | if !consoles[con] { |
| 33 | t.Errorf("Expected console %s to be returned but it wasn't", con) |
| 34 | } |
| 35 | } |
| 36 | for con := range consoles { |
| 37 | if !wantConsoles[con] { |
| 38 | t.Errorf("Didn't expect console %s to be returned but it was", con) |
| 39 | } |
| 40 | } |
| 41 | }) |
| 42 | } |
| 43 | } |