Skip to content

Commit bee74ae

Browse files
committed
add tests
1 parent 608ee8b commit bee74ae

File tree

1 file changed

+145
-0
lines changed

1 file changed

+145
-0
lines changed

src/object_test.ts

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,9 @@ describe('KubernetesObject', () => {
10711071
kc.loadFromOptions(testConfigOptions);
10721072
client = KubernetesObjectApi.makeApiClient(kc);
10731073
(client as any).apiVersionResourceCache.v1 = JSON.parse(resourceBodies.core);
1074+
(client as any).apiVersionResourceCache['networking.k8s.io/v1'] = JSON.parse(
1075+
resourceBodies.networking,
1076+
);
10741077
});
10751078

10761079
it('should modify resources with defaults', async () => {
@@ -1520,6 +1523,148 @@ describe('KubernetesObject', () => {
15201523
}
15211524
});
15221525

1526+
it('properly serialize resources', async () => {
1527+
const netPol = {
1528+
apiVersion: 'networking.k8s.io/v1',
1529+
kind: 'NetworkPolicy',
1530+
metadata: {
1531+
name: 'k8s-js-client-test',
1532+
namespace: 'default',
1533+
},
1534+
spec: {
1535+
podSelector: {
1536+
matchLabels: {
1537+
app: 'my-app',
1538+
},
1539+
},
1540+
policyTypes: ['Ingress'],
1541+
ingress: [
1542+
{
1543+
_from: [
1544+
{
1545+
podSelector: { matchLabels: { app: 'foo' } },
1546+
},
1547+
],
1548+
ports: [{ port: 123 }],
1549+
},
1550+
],
1551+
},
1552+
};
1553+
const serializedNetPol = {
1554+
apiVersion: 'networking.k8s.io/v1',
1555+
kind: 'NetworkPolicy',
1556+
metadata: {
1557+
name: 'k8s-js-client-test',
1558+
namespace: 'default',
1559+
},
1560+
spec: {
1561+
podSelector: {
1562+
matchLabels: {
1563+
app: 'my-app',
1564+
},
1565+
},
1566+
policyTypes: ['Ingress'],
1567+
ingress: [
1568+
{
1569+
from: [
1570+
{
1571+
podSelector: { matchLabels: { app: 'foo' } },
1572+
},
1573+
],
1574+
ports: [{ port: 123 }],
1575+
},
1576+
],
1577+
},
1578+
};
1579+
const methods = [
1580+
{
1581+
m: client.create,
1582+
v: 'POST',
1583+
p: '/apis/networking.k8s.io/v1/namespaces/default/networkpolicies',
1584+
c: 201,
1585+
b: `{
1586+
"kind": "NetworkPolicy",
1587+
"apiVersion": "networking.k8s.io/v1",
1588+
"metadata": {
1589+
"name": "k8s-js-client-test",
1590+
"namespace": "default",
1591+
"selfLink": "/api/v1/namespaces/default/services/k8s-js-client-test",
1592+
"uid": "6a43eddc-26bf-424e-ab30-cde3041a706a",
1593+
"resourceVersion": "32373",
1594+
"creationTimestamp": "2020-05-11T17:34:25Z"
1595+
},
1596+
"spec": {
1597+
"policyTypes": ["Ingress"],
1598+
"podSelector": {
1599+
"matchLabels": {
1600+
"app": "my-app"
1601+
}
1602+
},
1603+
"ingress": [
1604+
{
1605+
"from": [{
1606+
"podSelector": {
1607+
"matchLabels": {
1608+
"app": "foo"
1609+
}
1610+
}
1611+
}],
1612+
"ports": [{"port": 123}]
1613+
}
1614+
]
1615+
}
1616+
}`,
1617+
},
1618+
{
1619+
m: client.patch,
1620+
v: 'PATCH',
1621+
p: '/apis/networking.k8s.io/v1/namespaces/default/networkpolicies/k8s-js-client-test',
1622+
c: 200,
1623+
b: `{
1624+
"kind": "NetworkPolicy",
1625+
"apiVersion": "networking.k8s.io/v1",
1626+
"metadata": {
1627+
"name": "k8s-js-client-test",
1628+
"namespace": "default",
1629+
"selfLink": "/api/v1/namespaces/default/services/k8s-js-client-test",
1630+
"uid": "6a43eddc-26bf-424e-ab30-cde3041a706a",
1631+
"resourceVersion": "32373",
1632+
"creationTimestamp": "2020-05-11T17:34:25Z"
1633+
},
1634+
"spec": {
1635+
"policyTypes": ["Ingress"],
1636+
"podSelector": {
1637+
"matchLabels": {
1638+
"app": "my-app"
1639+
}
1640+
},
1641+
"ingress": [
1642+
{
1643+
"from": [{
1644+
"podSelector": {
1645+
"matchLabels": {
1646+
"app": "foo"
1647+
}
1648+
}
1649+
}],
1650+
"ports": [{"port": 123}]
1651+
}
1652+
]
1653+
}
1654+
}`,
1655+
},
1656+
];
1657+
for (const m of methods) {
1658+
const scope = nock('https://d.i.y')
1659+
.intercept(m.p, m.v, serializedNetPol)
1660+
.reply(m.c, m.b, contentTypeJsonHeader);
1661+
// TODO: Figure out why Typescript barfs if we do m.call
1662+
const hack_m = m.m as any;
1663+
await hack_m.call(client, netPol);
1664+
scope.done();
1665+
}
1666+
});
1667+
15231668
it('should replace a resource', async () => {
15241669
const s = {
15251670
apiVersion: 'v1',

0 commit comments

Comments
 (0)