|
| 1 | +package nginx |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "k8s.io/kubernetes/pkg/api" |
| 7 | + "k8s.io/kubernetes/pkg/api/unversioned" |
| 8 | + "k8s.io/kubernetes/pkg/apis/extensions" |
| 9 | +) |
| 10 | + |
| 11 | +var configMap = api.ConfigMap{ |
| 12 | + ObjectMeta: api.ObjectMeta{ |
| 13 | + Name: "test", |
| 14 | + Namespace: "default", |
| 15 | + }, |
| 16 | + TypeMeta: unversioned.TypeMeta{ |
| 17 | + Kind: "ConfigMap", |
| 18 | + APIVersion: "v1", |
| 19 | + }, |
| 20 | +} |
| 21 | +var ingress = extensions.Ingress{ |
| 22 | + ObjectMeta: api.ObjectMeta{ |
| 23 | + Name: "test", |
| 24 | + Namespace: "kube-system", |
| 25 | + }, |
| 26 | + TypeMeta: unversioned.TypeMeta{ |
| 27 | + Kind: "Ingress", |
| 28 | + APIVersion: "extensions/v1beta1", |
| 29 | + }, |
| 30 | +} |
| 31 | + |
| 32 | +// |
| 33 | +// GetMapKeyAsBool |
| 34 | +// |
| 35 | +func TestGetMapKeyAsBool(t *testing.T) { |
| 36 | + configMap := configMap |
| 37 | + configMap.Data = map[string]string{ |
| 38 | + "key": "True", |
| 39 | + } |
| 40 | + |
| 41 | + b, err := GetMapKeyAsBool(configMap.Data, "key", &configMap) |
| 42 | + if err != nil { |
| 43 | + t.Errorf("Unexpected error: %v", err) |
| 44 | + } |
| 45 | + if b != true { |
| 46 | + t.Errorf("Result should be true") |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func TestGetMapKeyAsBoolNotFound(t *testing.T) { |
| 51 | + configMap := configMap |
| 52 | + configMap.Data = map[string]string{} |
| 53 | + |
| 54 | + _, err := GetMapKeyAsBool(configMap.Data, "key", &configMap) |
| 55 | + if err != ErrorKeyNotFound { |
| 56 | + t.Errorf("ErrorKeyNotFound was expected, got: %v", err) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func TestGetMapKeyAsBoolErrorMessage(t *testing.T) { |
| 61 | + cfgm := configMap |
| 62 | + cfgm.Data = map[string]string{ |
| 63 | + "key": "string", |
| 64 | + } |
| 65 | + |
| 66 | + // Test with configmap |
| 67 | + _, err := GetMapKeyAsBool(cfgm.Data, "key", &cfgm) |
| 68 | + if err == nil { |
| 69 | + t.Error("An error was expected") |
| 70 | + } |
| 71 | + expected := `ConfigMap default/test 'key' contains invalid bool: strconv.ParseBool: parsing "string": invalid syntax, ignoring` |
| 72 | + if err.Error() != expected { |
| 73 | + t.Errorf("The error message does not match expectations:\nGot: %v\nExpected: %v", err, expected) |
| 74 | + } |
| 75 | + |
| 76 | + // Test with ingress object |
| 77 | + ingress := ingress |
| 78 | + ingress.Annotations = map[string]string{ |
| 79 | + "key": "other_string", |
| 80 | + } |
| 81 | + _, err = GetMapKeyAsBool(ingress.Annotations, "key", &ingress) |
| 82 | + if err == nil { |
| 83 | + t.Error("An error was expected") |
| 84 | + } |
| 85 | + expected = `Ingress kube-system/test 'key' contains invalid bool: strconv.ParseBool: parsing "other_string": invalid syntax, ignoring` |
| 86 | + if err.Error() != expected { |
| 87 | + t.Errorf("The error message does not match expectations:\nGot: %v\nExpected: %v", err, expected) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +// |
| 92 | +// GetMapKeyAsInt |
| 93 | +// |
| 94 | +func TestGetMapKeyAsInt(t *testing.T) { |
| 95 | + configMap := configMap |
| 96 | + configMap.Data = map[string]string{ |
| 97 | + "key": "123456789", |
| 98 | + } |
| 99 | + |
| 100 | + i, err := GetMapKeyAsInt(configMap.Data, "key", &configMap) |
| 101 | + if err != nil { |
| 102 | + t.Errorf("Unexpected error: %v", err) |
| 103 | + } |
| 104 | + var expected int64 = 123456789 |
| 105 | + if i != expected { |
| 106 | + t.Errorf("Unexpected return value:\nGot: %v\nExpected: %v", i, expected) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func TestGetMapKeyAsIntNotFound(t *testing.T) { |
| 111 | + configMap := configMap |
| 112 | + configMap.Data = map[string]string{} |
| 113 | + |
| 114 | + _, err := GetMapKeyAsInt(configMap.Data, "key", &configMap) |
| 115 | + if err != ErrorKeyNotFound { |
| 116 | + t.Errorf("ErrorKeyNotFound was expected, got: %v", err) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +func TestGetMapKeyAsIntErrorMessage(t *testing.T) { |
| 121 | + cfgm := configMap |
| 122 | + cfgm.Data = map[string]string{ |
| 123 | + "key": "string", |
| 124 | + } |
| 125 | + |
| 126 | + // Test with configmap |
| 127 | + _, err := GetMapKeyAsInt(cfgm.Data, "key", &cfgm) |
| 128 | + if err == nil { |
| 129 | + t.Error("An error was expected") |
| 130 | + } |
| 131 | + expected := `ConfigMap default/test 'key' contains invalid integer: strconv.ParseInt: parsing "string": invalid syntax, ignoring` |
| 132 | + if err.Error() != expected { |
| 133 | + t.Errorf("The error message does not match expectations:\nGot: %v\nExpected: %v", err, expected) |
| 134 | + } |
| 135 | + |
| 136 | + // Test with ingress object |
| 137 | + ingress := ingress |
| 138 | + ingress.Annotations = map[string]string{ |
| 139 | + "key": "other_string", |
| 140 | + } |
| 141 | + _, err = GetMapKeyAsInt(ingress.Annotations, "key", &ingress) |
| 142 | + if err == nil { |
| 143 | + t.Error("An error was expected") |
| 144 | + } |
| 145 | + expected = `Ingress kube-system/test 'key' contains invalid integer: strconv.ParseInt: parsing "other_string": invalid syntax, ignoring` |
| 146 | + if err.Error() != expected { |
| 147 | + t.Errorf("The error message does not match expectations:\nGot: %v\nExpected: %v", err, expected) |
| 148 | + } |
| 149 | +} |
0 commit comments