14
14
15
15
import os
16
16
import unittest
17
+ from unittest .mock import AsyncMock , MagicMock , patch
17
18
18
19
from kubernetes_asyncio .client import api_client
19
20
from kubernetes_asyncio .dynamic import DynamicClient
21
+ from kubernetes_asyncio .dynamic .discovery import Discoverer
20
22
from kubernetes_asyncio .e2e_test import base
21
23
22
24
@@ -56,7 +58,7 @@ async def test_cache_decoder_resource_and_subresource(self):
56
58
deploy1 = await client .resources .get (kind = 'Deployment' , api_version = "apps/v1" )
57
59
58
60
# do Discoverer.__init__
59
- # async with api_client.ApiClient(configuration=self.config) as apic:
61
+ async with api_client .ApiClient (configuration = self .config ) as apic :
60
62
client2 = await DynamicClient (apic )
61
63
# the resources of client will use _cache['resources'] decode from cache file
62
64
deploy2 = await client2 .resources .get (kind = 'Deployment' , api_version = "apps/v1" )
@@ -65,5 +67,29 @@ async def test_cache_decoder_resource_and_subresource(self):
65
67
# test Resource is the same
66
68
self .assertDictEqual (deploy1 .to_dict (), deploy2 .to_dict ())
67
69
68
- # test Subresource is the same
69
- self .assertDictEqual (deploy1 .status .to_dict (), deploy2 .status .to_dict ())
70
+ @patch ('kubernetes_asyncio.dynamic.discovery.Discoverer.get_resources_for_api_version' , new_callable = AsyncMock )
71
+ async def test_get_resources_for_api_version (self , mock_get_resources ):
72
+ """Test case for get_resources_for_api_version"""
73
+ mock_get_resources .return_value = {
74
+ 'resources' : [{'name' : 'pods' , 'kind' : 'Pod' }],
75
+ 'subresources' : {
76
+ 'virtualmachineinstances' : {
77
+ 'sev/fetchcertchain' : {'name' : 'virtualmachineinstances/sev/fetchcertchain' }
78
+ }
79
+ }
80
+ }
81
+
82
+ # Create a mock client with the necessary attributes
83
+ mock_client = MagicMock ()
84
+ mock_client .configuration .host = "https://mock-host"
85
+
86
+ discoverer = Discoverer (client = mock_client )
87
+ response = await discoverer .get_resources_for_api_version ('api' , 'v1' , 'pods' , True )
88
+ self .assertEqual (response ['resources' ][0 ]['name' ], 'pods' )
89
+ self .assertEqual (response ['resources' ][0 ]['kind' ], 'Pod' )
90
+ self .assertIn ('virtualmachineinstances' , response ['subresources' ])
91
+ self .assertIn ('sev/fetchcertchain' , response ['subresources' ]['virtualmachineinstances' ])
92
+
93
+
94
+ if __name__ == '__main__' :
95
+ unittest .main ()
0 commit comments