1414
1515import os
1616import unittest
17+ from unittest .mock import AsyncMock , MagicMock , patch
1718
1819from kubernetes_asyncio .client import api_client
1920from kubernetes_asyncio .dynamic import DynamicClient
21+ from kubernetes_asyncio .dynamic .discovery import Discoverer
2022from kubernetes_asyncio .e2e_test import base
2123
2224
@@ -56,7 +58,7 @@ async def test_cache_decoder_resource_and_subresource(self):
5658 deploy1 = await client .resources .get (kind = 'Deployment' , api_version = "apps/v1" )
5759
5860 # 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 :
6062 client2 = await DynamicClient (apic )
6163 # the resources of client will use _cache['resources'] decode from cache file
6264 deploy2 = await client2 .resources .get (kind = 'Deployment' , api_version = "apps/v1" )
@@ -65,5 +67,29 @@ async def test_cache_decoder_resource_and_subresource(self):
6567 # test Resource is the same
6668 self .assertDictEqual (deploy1 .to_dict (), deploy2 .to_dict ())
6769
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