|
1 | | -import { K8sNode } from '../korrel8r/k8s'; |
| 1 | +import { URIRef, Query } from '../korrel8r/types'; |
| 2 | +import { K8sDomain } from '../korrel8r/k8s'; |
| 3 | + |
| 4 | +const k8s = new K8sDomain(); |
2 | 5 |
|
3 | 6 | beforeAll(() => { |
4 | 7 | // Mock API discovery resources. |
@@ -75,85 +78,80 @@ describe('K8sNode.fromURL', () => { |
75 | 78 | }, |
76 | 79 | { |
77 | 80 | url: 'k8s/ns/default/pods/bad-deployment-000000000-00000', |
78 | | - query: 'k8s:Pod.v1.:{"namespace":"default","name":"bad-deployment-000000000-00000"}', |
| 81 | + query: 'k8s:Pod.v1:{"namespace":"default","name":"bad-deployment-000000000-00000"}', |
79 | 82 | }, |
80 | 83 | { |
81 | 84 | url: 'k8s/ns/default/pods/bad-deployment-000000000-00000/events', |
82 | 85 | query: |
83 | | - 'k8s:Event.v1.:{"fields":{"involvedObject.namespace":"default","involvedObject.name":"bad-deployment-000000000-00000","involvedObject.apiVersion":"v1","involvedObject.kind":"Pod"}}', |
| 86 | + 'k8s:Event.v1:{"fields":{"involvedObject.namespace":"default","involvedObject.name":"bad-deployment-000000000-00000","involvedObject.apiVersion":"v1","involvedObject.kind":"Pod"}}', |
84 | 87 | }, |
85 | | - { url: `/k8s/ns/default/pods/foo`, query: `k8s:Pod.v1.:{"namespace":"default","name":"foo"}` }, |
86 | | - { url: `/k8s/ns/default/pods`, query: `k8s:Pod.v1.:{"namespace":"default"}` }, |
87 | | - { url: `/k8s/cluster/projects/foo`, query: `k8s:Namespace.v1.:{"name":"foo"}` }, |
| 88 | + { url: `/k8s/ns/default/pods/foo`, query: `k8s:Pod.v1:{"namespace":"default","name":"foo"}` }, |
| 89 | + { url: `/k8s/ns/default/pods`, query: `k8s:Pod.v1:{"namespace":"default"}` }, |
| 90 | + { url: `/k8s/cluster/projects/foo`, query: `k8s:Namespace.v1:{"name":"foo"}` }, |
88 | 91 | { |
89 | 92 | url: `/k8s/ns/x/operators.coreos.com~v1alpha1~ClusterServiceVersion/y`, |
90 | 93 | query: `k8s:ClusterServiceVersion.v1alpha1.operators.coreos.com:{"namespace":"x","name":"y"}`, |
91 | 94 | }, |
92 | | - { url: `/search/all-namespaces?kind=core~v1~Pod`, query: `k8s:Pod.v1.:{}` }, |
93 | | - { url: `/k8s/all-namespaces/core~v1~Pod`, query: `k8s:Pod.v1.:{}` }, |
94 | | - { url: `/k8s/cluster/nodes/oscar7`, query: `k8s:Node.v1.:{"name":"oscar7"}` }, |
95 | | - { url: '/k8s/ns/netobserv/core~v1~Pod', query: 'k8s:Pod.v1.:{"namespace":"netobserv"}' }, |
96 | | - ])('converts $url', ({ url, query }) => expect(K8sNode.fromURL(url).toQuery()).toEqual(query)); |
| 95 | + { url: `/search/all-namespaces?kind=core~v1~Pod`, query: `k8s:Pod.v1:{}` }, |
| 96 | + { url: `/k8s/all-namespaces/core~v1~Pod`, query: `k8s:Pod.v1:{}` }, |
| 97 | + { url: `/k8s/cluster/nodes/oscar7`, query: `k8s:Node.v1:{"name":"oscar7"}` }, |
| 98 | + { url: '/k8s/ns/netobserv/core~v1~Pod', query: 'k8s:Pod.v1:{"namespace":"netobserv"}' }, |
| 99 | + ])('converts $url', ({ url, query }) => |
| 100 | + expect(k8s.linkToQuery(new URIRef(url))).toEqual(Query.parse(query)), |
| 101 | + ); |
97 | 102 | }); |
98 | 103 |
|
99 | 104 | describe('K8sNode.fromQuery', () => { |
100 | 105 | it.each([ |
101 | 106 | // Variations on query parameters. |
102 | 107 | // Note "fields" are ignored. |
103 | 108 | { |
104 | | - query: 'k8s:Pod.v1.:{"namespace":"default","name":"bad-deployment-000000000-00000"}', |
105 | | - url: 'k8s/ns/default/pods/bad-deployment-000000000-00000', |
| 109 | + query: 'k8s:Pod.v1:{"namespace":"default","name":"bad-deployment-000000000-00000"}', |
| 110 | + u: 'k8s/ns/default/pods/bad-deployment-000000000-00000', |
106 | 111 | }, |
107 | 112 | { |
108 | 113 | query: `k8s:Pod:{"namespace":"x","name":"y","labels":{"a":"b","c":"d"},"fields": {"x":"y"}}`, |
109 | | - url: `k8s/ns/x/pods/y?labels=${encodeURIComponent('a=b,c=d')}`, |
| 114 | + u: `k8s/ns/x/pods/y?labels=${encodeURIComponent('a=b,c=d')}`, |
110 | 115 | }, |
111 | 116 | { |
112 | 117 | query: |
113 | | - 'k8s:Event.v1.:{"fields":{"involvedObject.namespace":"default","involvedObject.name":"bad-deployment-000000000-00000","involvedObject.apiVersion":"v1","involvedObject.kind":"Pod"}}', |
114 | | - url: 'k8s/ns/default/pods/bad-deployment-000000000-00000/events', |
| 118 | + 'k8s:Event.v1:{"fields":{"involvedObject.namespace":"default","involvedObject.name":"bad-deployment-000000000-00000","involvedObject.apiVersion":"v1","involvedObject.kind":"Pod"}}', |
| 119 | + u: 'k8s/ns/default/pods/bad-deployment-000000000-00000/events', |
115 | 120 | }, |
116 | 121 | { |
117 | 122 | query: `k8s:Pod:{"namespace":"x","name":"y","labels":{"a":"b"}}`, |
118 | | - url: `k8s/ns/x/pods/y?labels=${encodeURIComponent('a=b')}`, |
| 123 | + u: `k8s/ns/x/pods/y?labels=${encodeURIComponent('a=b')}`, |
119 | 124 | }, |
120 | 125 | { |
121 | 126 | query: `k8s:Pod:{"namespace":"x","labels":{"a":"b"}}`, |
122 | | - url: `k8s/ns/x/pods?labels=${encodeURIComponent('a=b')}`, |
| 127 | + u: `k8s/ns/x/pods?labels=${encodeURIComponent('a=b')}`, |
123 | 128 | }, |
124 | | - { query: `k8s:Pod.v1:{"namespace":"x","name":"y"}`, url: `k8s/ns/x/pods/y` }, |
125 | | - { query: `k8s:Pod.v1:{"namespace":"x"}`, url: `k8s/ns/x/pods` }, |
| 129 | + { query: `k8s:Pod.v1:{"namespace":"x","name":"y"}`, u: `k8s/ns/x/pods/y` }, |
| 130 | + { query: `k8s:Pod.v1:{"namespace":"x"}`, u: `k8s/ns/x/pods` }, |
126 | 131 | { |
127 | 132 | query: `k8s:Pod.v1:{"labels":{"a":"b"}}`, |
128 | | - url: `search/all-namespaces?labels=${encodeURIComponent('a=b')}&kind=core~v1~Pod`, |
| 133 | + u: `search/all-namespaces?labels=${encodeURIComponent('a=b')}&kind=core~v1~Pod`, |
129 | 134 | }, |
130 | 135 | { |
131 | 136 | query: `k8s:Pod.v1:{"namespace":"x","labels":{"a":"b"}}`, |
132 | | - url: `k8s/ns/x/pods?labels=${encodeURIComponent('a=b')}`, |
| 137 | + u: `k8s/ns/x/pods?labels=${encodeURIComponent('a=b')}`, |
133 | 138 | }, |
134 | 139 |
|
135 | 140 | // Variations on korrel8r class spec. |
136 | | - { query: `k8s:Role:{"namespace":"x", "name":"y"}`, url: `k8s/ns/x/roles/y` }, |
137 | | - { query: `k8s:Role.:{"namespace":"x", "name":"y"}`, url: `k8s/ns/x/roles/y` }, |
138 | | - { query: `k8s:Role.v1:{"namespace":"x", "name":"y"}`, url: `k8s/ns/x/roles/y` }, |
139 | | - { query: `k8s:Role.v1.:{"namespace":"x", "name":"y"}`, url: `k8s/ns/x/roles/y` }, |
| 141 | + { query: `k8s:Role:{"namespace":"x", "name":"y"}`, u: `k8s/ns/x/roles/y` }, |
| 142 | + { query: `k8s:Role.:{"namespace":"x", "name":"y"}`, u: `k8s/ns/x/roles/y` }, |
| 143 | + { query: `k8s:Role.v1:{"namespace":"x", "name":"y"}`, u: `k8s/ns/x/roles/y` }, |
| 144 | + { query: `k8s:Role.v1:{"namespace":"x", "name":"y"}`, u: `k8s/ns/x/roles/y` }, |
140 | 145 | { |
141 | 146 | query: `k8s:Role.v1.rbac.authorization.k8s.io:{"namespace":"x", "name":"y"}`, |
142 | | - url: `k8s/ns/x/roles/y`, |
| 147 | + u: `k8s/ns/x/roles/y`, |
143 | 148 | }, |
144 | | - { query: `k8s:Pod:{}`, url: 'k8s/all-namespaces/pods' }, |
145 | | - ])('converts $query to $url', ({ url, query }) => { |
146 | | - expect(K8sNode.fromQuery(query).toURL()).toEqual(url); |
| 149 | + { query: `k8s:Pod:{}`, u: 'k8s/all-namespaces/pods' }, |
| 150 | + ])('converts $query to $url', ({ u: u, query }) => { |
| 151 | + expect(k8s.queryToLink(Query.parse(query))).toEqual(u); |
147 | 152 | }); |
148 | 153 |
|
149 | | - it.each([ |
150 | | - { query: `foo:bar:baz`, err: 'Expected k8s query' }, |
151 | | - { query: `k8s:Pod`, err: 'Expected k8s query' }, |
152 | | - { query: `k8s:nosuch:{}`, err: 'Unknown k8s kind:' }, |
153 | | - { query: `k8s:Role.v1.bad.group:{}`, err: `Unknown k8s kind:` }, |
154 | | - ])('raises error on $query', ({ query, err }) => |
155 | | - expect(() => { |
156 | | - K8sNode.fromQuery(query); |
157 | | - }).toThrow(err), |
| 154 | + it.each([{ query: `foo:bar:baz` }])('raises error on $query', ({ query }) => |
| 155 | + expect(() => k8s.queryToLink(Query.parse(query))).toThrow(TypeError), |
158 | 156 | ); |
159 | 157 | }); |
0 commit comments