|
1 |
| -package cmd |
| 1 | +package cmd_test |
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "github.com/LINBIT/virter/cmd" |
| 5 | + "github.com/rck/unit" |
4 | 6 | "reflect"
|
5 | 7 | "testing"
|
6 | 8 | )
|
7 | 9 |
|
8 |
| -func TestParseArgMap(t *testing.T) { |
| 10 | +func TestFromFlag(t *testing.T) { |
9 | 11 | cases := []struct {
|
| 12 | + name string |
10 | 13 | input string
|
11 |
| - expect map[string]string |
| 14 | + expect cmd.DiskArg |
12 | 15 | expectError bool
|
13 | 16 | }{
|
14 | 17 | {
|
15 |
| - input: "", |
16 |
| - expect: map[string]string{}, |
| 18 | + name: "empty arg is error", |
| 19 | + input: "", |
| 20 | + expectError: true, |
17 | 21 | }, {
|
| 22 | + name: "full spec parses", |
18 | 23 | input: "name=test,size=5GiB,format=qcow2,bus=virtio",
|
19 |
| - expect: map[string]string{ |
20 |
| - "name": "test", |
21 |
| - "size": "5GiB", |
22 |
| - "format": "qcow2", |
23 |
| - "bus": "virtio", |
| 24 | + expect: cmd.DiskArg{ |
| 25 | + Name: "test", |
| 26 | + Size: cmd.Size{KiB: uint64(5 * unit.G / unit.K)}, |
| 27 | + Format: "qcow2", |
| 28 | + Bus: "virtio", |
24 | 29 | },
|
25 | 30 | }, {
|
26 |
| - input: "name=test", |
27 |
| - expect: map[string]string{ |
28 |
| - "name": "test", |
| 31 | + name: "only required args parses", |
| 32 | + input: "name=test,size=10G", |
| 33 | + expect: cmd.DiskArg{ |
| 34 | + Name: "test", |
| 35 | + Size: cmd.Size{KiB: uint64(10 * unit.G / unit.K)}, |
| 36 | + Format: "qcow2", |
| 37 | + Bus: "virtio", |
29 | 38 | },
|
30 | 39 | }, {
|
31 |
| - input: ",,,", |
32 |
| - expect: map[string]string{}, |
| 40 | + name: "empty but different is error", |
| 41 | + input: ",,,", |
| 42 | + expectError: true, |
33 | 43 | }, {
|
34 |
| - input: ",name=test,", |
35 |
| - expect: map[string]string{ |
36 |
| - "name": "test", |
| 44 | + name: "only required with empty kv-pairs parses", |
| 45 | + input: ",name=test,,,size=1G", |
| 46 | + expect: cmd.DiskArg{ |
| 47 | + Name: "test", |
| 48 | + Size: cmd.Size{KiB: uint64(1 * unit.G / unit.K)}, |
| 49 | + Format: "qcow2", |
| 50 | + Bus: "virtio", |
37 | 51 | },
|
38 | 52 | }, {
|
| 53 | + name: "nonsence input is error", |
39 | 54 | input: "x,y,z",
|
40 | 55 | expectError: true,
|
41 | 56 | }, {
|
42 |
| - input: "name=", |
43 |
| - expect: map[string]string{ |
44 |
| - "name": "", |
45 |
| - }, |
| 57 | + name: "missing size is error", |
| 58 | + input: "name=test", |
| 59 | + expectError: true, |
46 | 60 | }, {
|
47 |
| - input: "=test", |
| 61 | + name: "missing key is error", |
| 62 | + input: "=test,name=bla,size=1G", |
48 | 63 | expectError: true,
|
49 | 64 | }, {
|
| 65 | + name: "multi-value is error", |
50 | 66 | input: "name=test=hello",
|
51 | 67 | expectError: true,
|
52 | 68 | }, {
|
53 |
| - input: "name=test hello", |
54 |
| - expect: map[string]string{ |
55 |
| - "name": "test hello", |
| 69 | + name: "whitepace is okay", |
| 70 | + input: "name=test hello,size=1G", |
| 71 | + expect: cmd.DiskArg{ |
| 72 | + Name: "test hello", |
| 73 | + Size: cmd.Size{KiB: uint64(1 * unit.G / unit.K)}, |
| 74 | + Format: "qcow2", |
| 75 | + Bus: "virtio", |
56 | 76 | },
|
57 | 77 | }, {
|
58 |
| - input: "name=test,name=hello", |
59 |
| - expect: map[string]string{ |
60 |
| - "name": "hello", |
| 78 | + name: "repeated keys are okay", |
| 79 | + input: "name=test,name=hello,size=1G", |
| 80 | + expect: cmd.DiskArg{ |
| 81 | + Name: "hello", |
| 82 | + Size: cmd.Size{KiB: uint64(1 * unit.G / unit.K)}, |
| 83 | + Format: "qcow2", |
| 84 | + Bus: "virtio", |
61 | 85 | },
|
62 | 86 | },
|
63 | 87 | }
|
64 | 88 |
|
65 |
| - for _, c := range cases { |
66 |
| - actual, err := parseArgMap(c.input) |
67 |
| - if !c.expectError && err != nil { |
68 |
| - t.Errorf("on input '%s':", c.input) |
69 |
| - t.Fatalf("unexpected error: %v", err) |
70 |
| - } |
71 |
| - if c.expectError && err == nil { |
72 |
| - t.Errorf("on input '%s':", c.input) |
73 |
| - t.Fatal("expected error, got nil") |
74 |
| - } |
| 89 | + t.Parallel() |
| 90 | + for i := range cases { |
| 91 | + c := cases[i] |
| 92 | + t.Run(c.name, func(t *testing.T) { |
| 93 | + actual := cmd.DiskArg{} |
| 94 | + err := actual.Set(c.input) |
| 95 | + if err != nil { |
| 96 | + if !c.expectError { |
| 97 | + t.Errorf("on input '%s':", c.input) |
| 98 | + t.Fatalf("unexpected error: %v", err) |
| 99 | + } |
| 100 | + return |
| 101 | + } |
| 102 | + |
| 103 | + if c.expectError { |
| 104 | + t.Errorf("on input '%s':", c.input) |
| 105 | + t.Fatal("expected error, got nil") |
| 106 | + } |
75 | 107 |
|
76 |
| - if !reflect.DeepEqual(actual, c.expect) { |
77 |
| - t.Errorf("on input '%s':a", c.input) |
78 |
| - t.Errorf("unexpected map contents") |
79 |
| - t.Errorf("expected: %+v", c.expect) |
80 |
| - t.Errorf("actual: %+v", actual) |
81 |
| - } |
| 108 | + if !reflect.DeepEqual(actual, c.expect) { |
| 109 | + t.Errorf("on input '%s'", c.input) |
| 110 | + t.Errorf("unexpected arg contents") |
| 111 | + t.Errorf("expected: %+v", c.expect) |
| 112 | + t.Errorf("actual: %+v", actual) |
| 113 | + } |
| 114 | + }) |
82 | 115 | }
|
83 | 116 | }
|
0 commit comments