-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_binding.py
139 lines (102 loc) · 4.57 KB
/
test_binding.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# Copyright 2021 the original author or authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
from os import path
from typing import Optional
from bindings import binding, bindings
class TestBinding(unittest.TestCase):
def test__get__missing(self):
b = binding.DictBinding("test-name", {})
self.assertIsNone(b.get("test-missing-key"))
def test__get__valid(self):
b = binding.DictBinding("test-name", {
"test-secret-key": b"test-secret-value\n",
})
self.assertEqual("test-secret-value", b.get("test-secret-key"))
def test__get_provider__missing(self):
b = binding.DictBinding("test-name", {})
self.assertIsNone(b.get_provider())
def test__get_provider__valid(self):
b = binding.DictBinding("test-name", {
"provider": b"test-provider-1",
})
self.assertEqual("test-provider-1", b.get_provider())
def test__get_type__invalid(self):
b = binding.DictBinding("test-name", {})
self.assertRaises(ValueError, lambda: b.get_type())
def test__get_type__valid(self):
b = binding.DictBinding("test-name", {
"type": b"test-type-1",
})
self.assertEqual("test-type-1", b.get_type())
def test__CacheBinding__missing(self):
s = StubBinding()
b = bindings.CacheBinding(s)
self.assertIsNone(b.get_as_bytes("test-unknown-key"))
self.assertIsNone(b.get_as_bytes("test-unknown-key"))
self.assertEqual(2, s.get_as_bytes_count)
def test__CacheBinding__valid(self):
s = StubBinding()
b = bindings.CacheBinding(s)
self.assertIsNotNone(b.get_as_bytes("test-secret-key"))
self.assertIsNotNone(b.get_as_bytes("test-secret-key"))
self.assertEqual(1, s.get_as_bytes_count)
def test__CacheBinding__get_name(self):
s = StubBinding()
b = bindings.CacheBinding(s)
self.assertEqual("test-name", b.get_name())
self.assertEqual("test-name", b.get_name())
self.assertEqual(2, s.get_name_count)
def test__ConfigTreeBinding__missing(self):
b = binding.ConfigTreeBinding(path.join("tests", "testdata", "test-k8s"))
self.assertIsNone(b.get_as_bytes("test-missing-key"))
def test__ConfigTreeBinding__directory(self):
b = binding.ConfigTreeBinding(path.join("tests", "testdata", "test-k8s"))
self.assertIsNone(b.get_as_bytes(".hidden-data"))
def test__ConfigTreeBinding__invalid(self):
b = binding.ConfigTreeBinding(path.join("tests", "testdata", "test-k8s"))
self.assertIsNone(b.get_as_bytes("test^invalid^key"))
def test__ConfigTreeBinding__valid(self):
b = binding.ConfigTreeBinding(path.join("tests", "testdata", "test-k8s"))
self.assertEqual(b"test-secret-value\n", b.get_as_bytes("test-secret-key"))
def test__ConfigTreeBinding__get_name(self):
b = binding.ConfigTreeBinding(path.join("tests", "testdata", "test-k8s"))
self.assertEqual("test-k8s", b.get_name())
def test__DictBinding__missing(self):
b = binding.DictBinding("test-name", {})
self.assertIsNone(b.get_as_bytes("test-missing-key"))
def test__DictBinding__invalid(self):
b = binding.DictBinding("test-name", {})
self.assertIsNone(b.get_as_bytes("test^secret^key"))
def test__DictBinding__valid(self):
b = binding.DictBinding("test-name", {
"test-secret-key": b"test-secret-value\n",
})
self.assertEqual(b"test-secret-value\n", b.get_as_bytes("test-secret-key"))
def test__DictBinding__get_name(self):
b = binding.DictBinding("test-name", {})
self.assertEqual("test-name", b.get_name())
class StubBinding(binding.Binding):
get_as_bytes_count = 0
get_name_count = 0
def get_as_bytes(self, key: str) -> Optional[bytes]:
self.get_as_bytes_count += 1
if key == "test-secret-key":
return bytes()
return None
def get_name(self) -> str:
self.get_name_count += 1
return "test-name"
if __name__ == "__main__":
unittest.main()