|
| 1 | +package opts |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "reflect" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/alibaba/pouch/apis/types" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +func TestParseDeviceMapping(t *testing.T) { |
| 14 | + type args struct { |
| 15 | + device []string |
| 16 | + } |
| 17 | + tests := []struct { |
| 18 | + name string |
| 19 | + args args |
| 20 | + want []*types.DeviceMapping |
| 21 | + wantErr bool |
| 22 | + }{ |
| 23 | + { |
| 24 | + name: "deviceMapping1", |
| 25 | + args: args{ |
| 26 | + device: []string{"/dev/deviceName:/dev/deviceName:mrw"}, |
| 27 | + }, |
| 28 | + want: []*types.DeviceMapping{{ |
| 29 | + PathOnHost: "/dev/deviceName", |
| 30 | + PathInContainer: "/dev/deviceName", |
| 31 | + CgroupPermissions: "mrw", |
| 32 | + }}, |
| 33 | + |
| 34 | + wantErr: false, |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "deviceMapping2", |
| 38 | + args: args{ |
| 39 | + device: []string{"/dev/deviceName:"}, |
| 40 | + }, |
| 41 | + want: []*types.DeviceMapping{{ |
| 42 | + PathOnHost: "/dev/deviceName", |
| 43 | + PathInContainer: "/dev/deviceName", |
| 44 | + CgroupPermissions: "rwm", |
| 45 | + }}, |
| 46 | + wantErr: false, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "deviceMappingWrong1", |
| 50 | + args: args{ |
| 51 | + device: []string{"/dev/deviceName:/dev/deviceName:rrw"}, |
| 52 | + }, |
| 53 | + wantErr: true, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "deviceMappingWrong2", |
| 57 | + args: args{ |
| 58 | + device: []string{"/dev/deviceName:/dev/deviceName:arw"}, |
| 59 | + }, |
| 60 | + wantErr: true, |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "deviceMappingWrong3", |
| 64 | + args: args{ |
| 65 | + device: []string{"/dev/deviceName:/dev/deviceName:mrw:mrw"}, |
| 66 | + }, |
| 67 | + wantErr: true, |
| 68 | + }, |
| 69 | + } |
| 70 | + for _, tt := range tests { |
| 71 | + t.Run(tt.name, func(t *testing.T) { |
| 72 | + got, err := ParseDeviceMappings(tt.args.device) |
| 73 | + if (err != nil) != tt.wantErr { |
| 74 | + t.Errorf("parseDeviceMappings() error = %v, wantErr %v", err, tt.wantErr) |
| 75 | + return |
| 76 | + } |
| 77 | + if !reflect.DeepEqual(got, tt.want) { |
| 78 | + t.Errorf("parseDeviceMappings() = %v, want %v", got, tt.want) |
| 79 | + } |
| 80 | + }) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +type tDeviceModeCase struct { |
| 85 | + input string |
| 86 | + expected bool |
| 87 | +} |
| 88 | + |
| 89 | +type tParseDeviceCase struct { |
| 90 | + input string |
| 91 | + expected *types.DeviceMapping |
| 92 | + err error |
| 93 | +} |
| 94 | + |
| 95 | +func Test_parseDevice(t *testing.T) { |
| 96 | + for _, tc := range []tParseDeviceCase{ |
| 97 | + { |
| 98 | + input: "/dev/zero:/dev/zero:rwm", |
| 99 | + expected: &types.DeviceMapping{ |
| 100 | + PathOnHost: "/dev/zero", |
| 101 | + PathInContainer: "/dev/zero", |
| 102 | + CgroupPermissions: "rwm", |
| 103 | + }, |
| 104 | + err: nil, |
| 105 | + }, { |
| 106 | + input: "/dev/zero:rwm", |
| 107 | + expected: &types.DeviceMapping{ |
| 108 | + PathOnHost: "/dev/zero", |
| 109 | + PathInContainer: "rwm", |
| 110 | + CgroupPermissions: "rwm", |
| 111 | + }, |
| 112 | + err: nil, |
| 113 | + }, { |
| 114 | + input: "/dev/zero", |
| 115 | + expected: &types.DeviceMapping{ |
| 116 | + PathOnHost: "/dev/zero", |
| 117 | + PathInContainer: "/dev/zero", |
| 118 | + CgroupPermissions: "rwm", |
| 119 | + }, |
| 120 | + err: nil, |
| 121 | + }, { |
| 122 | + input: "/dev/zero:/dev/testzero:rwm", |
| 123 | + expected: &types.DeviceMapping{ |
| 124 | + PathOnHost: "/dev/zero", |
| 125 | + PathInContainer: "/dev/testzero", |
| 126 | + CgroupPermissions: "rwm", |
| 127 | + }, |
| 128 | + err: nil, |
| 129 | + }, { |
| 130 | + input: "/dev/zero:/dev/testzero:rwm:tooLong", |
| 131 | + expected: nil, |
| 132 | + err: fmt.Errorf("invalid device specification: /dev/zero:/dev/testzero:rwm:tooLong"), |
| 133 | + }, |
| 134 | + } { |
| 135 | + output, err := parseDevice(tc.input) |
| 136 | + assert.Equal(t, tc.err, err, tc.input) |
| 137 | + assert.Equal(t, tc.expected, output, tc.input) |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +func TestValidateDeviceMode(t *testing.T) { |
| 142 | + for _, modeCase := range []tDeviceModeCase{ |
| 143 | + { |
| 144 | + input: "rwm", |
| 145 | + expected: true, |
| 146 | + }, { |
| 147 | + input: "r", |
| 148 | + expected: true, |
| 149 | + }, { |
| 150 | + input: "rw", |
| 151 | + expected: true, |
| 152 | + }, { |
| 153 | + input: "rr", |
| 154 | + expected: false, |
| 155 | + }, { |
| 156 | + input: "rxm", |
| 157 | + expected: false, |
| 158 | + }, |
| 159 | + } { |
| 160 | + isValid := ValidateDeviceMode(modeCase.input) |
| 161 | + assert.Equal(t, modeCase.expected, isValid, modeCase.input) |
| 162 | + } |
| 163 | +} |
0 commit comments