|
| 1 | +/* |
| 2 | +Copyright 2024 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package hostpath |
| 18 | + |
| 19 | +import ( |
| 20 | + "reflect" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/kubernetes-csi/csi-driver-host-path/pkg/state" |
| 24 | +) |
| 25 | + |
| 26 | +func TestOptionsFromParameters(t *testing.T) { |
| 27 | + mountedVolume := state.Volume{ |
| 28 | + VolAccessType: state.MountAccess, |
| 29 | + } |
| 30 | + blockVolume := state.Volume{ |
| 31 | + VolAccessType: state.BlockAccess, |
| 32 | + } |
| 33 | + |
| 34 | + cases := []struct { |
| 35 | + testName string |
| 36 | + volume state.Volume |
| 37 | + params map[string]string |
| 38 | + result []string |
| 39 | + success bool |
| 40 | + }{ |
| 41 | + { |
| 42 | + testName: "no options passed (mounted)", |
| 43 | + params: nil, |
| 44 | + result: nil, |
| 45 | + success: true, |
| 46 | + volume: mountedVolume, |
| 47 | + }, |
| 48 | + { |
| 49 | + testName: "no options passed (block)", |
| 50 | + params: nil, |
| 51 | + result: nil, |
| 52 | + success: true, |
| 53 | + volume: blockVolume, |
| 54 | + }, |
| 55 | + { |
| 56 | + testName: "ignoreFailedRead=false (mounted)", |
| 57 | + params: map[string]string{ |
| 58 | + ignoreFailedReadParameterName: "false", |
| 59 | + }, |
| 60 | + result: nil, |
| 61 | + success: true, |
| 62 | + volume: mountedVolume, |
| 63 | + }, |
| 64 | + { |
| 65 | + testName: "ignoreFailedRead=true (mounted)", |
| 66 | + params: map[string]string{ |
| 67 | + ignoreFailedReadParameterName: "true", |
| 68 | + }, |
| 69 | + result: []string{ |
| 70 | + "--ignore-failed-read", |
| 71 | + }, |
| 72 | + success: true, |
| 73 | + volume: mountedVolume, |
| 74 | + }, |
| 75 | + { |
| 76 | + testName: "invalid ignoreFailedRead (mounted)", |
| 77 | + params: map[string]string{ |
| 78 | + ignoreFailedReadParameterName: "ABC", |
| 79 | + }, |
| 80 | + result: nil, |
| 81 | + success: false, |
| 82 | + volume: mountedVolume, |
| 83 | + }, |
| 84 | + { |
| 85 | + testName: "ignoreFailedRead=true (block)", |
| 86 | + params: map[string]string{ |
| 87 | + ignoreFailedReadParameterName: "true", |
| 88 | + }, |
| 89 | + result: nil, |
| 90 | + success: true, |
| 91 | + volume: blockVolume, |
| 92 | + }, |
| 93 | + { |
| 94 | + testName: "invalid ignoreFailedRead (block)", |
| 95 | + params: map[string]string{ |
| 96 | + ignoreFailedReadParameterName: "ABC", |
| 97 | + }, |
| 98 | + result: nil, |
| 99 | + success: true, |
| 100 | + volume: blockVolume, |
| 101 | + }, |
| 102 | + } |
| 103 | + |
| 104 | + for _, tt := range cases { |
| 105 | + t.Run(tt.testName, func(t *testing.T) { |
| 106 | + result, err := optionsFromParameters(tt.volume, tt.params) |
| 107 | + if tt.success && err != nil { |
| 108 | + t.Fatalf("expected success, found error: %s", err.Error()) |
| 109 | + } |
| 110 | + if !tt.success && err == nil { |
| 111 | + t.Fatalf("expected failure, but succeeded with %q", result) |
| 112 | + } |
| 113 | + |
| 114 | + if tt.success && !reflect.DeepEqual(tt.result, result) { |
| 115 | + t.Fatalf("expected %q but received %q", tt.result, result) |
| 116 | + } |
| 117 | + }) |
| 118 | + } |
| 119 | +} |
0 commit comments