1+ import pytest
2+ from unittest .mock import Mock
3+ from openstack import exceptions
4+ from openstack_mcp_server .tools .keystone_tools import KeystoneTools , Region
5+
6+ class TestKeystoneTools :
7+ """Test cases for KeystoneTools class."""
8+
9+ def get_keystone_tools (self ) -> KeystoneTools :
10+ """Get an instance of KeystoneTools."""
11+ return KeystoneTools ()
12+
13+ def test_get_regions_success (self , mock_get_openstack_conn_keystone ):
14+ """Test getting keystone regions successfully."""
15+ mock_conn = mock_get_openstack_conn_keystone
16+
17+ # Create mock region objects
18+ mock_region1 = Mock ()
19+ mock_region1 .id = "RegionOne"
20+ mock_region1 .description = "Region One description"
21+
22+ mock_region2 = Mock ()
23+ mock_region2 .id = "RegionTwo"
24+ mock_region2 .description = "Region Two description"
25+
26+ # Configure mock region.regions()
27+ mock_conn .identity .regions .return_value = [mock_region1 , mock_region2 ]
28+
29+ # Test get_regions()
30+ keystone_tools = self .get_keystone_tools ()
31+ result = keystone_tools .get_regions ()
32+
33+ # Verify results
34+ assert result == [Region (id = "RegionOne" , description = "Region One description" ),
35+ Region (id = "RegionTwo" , description = "Region Two description" )]
36+
37+ # Verify mock calls
38+ mock_conn .identity .regions .assert_called_once ()
39+
40+ def test_get_regions_empty_list (self , mock_get_openstack_conn_keystone ):
41+ """Test getting keystone regions when there are no regions."""
42+ mock_conn = mock_get_openstack_conn_keystone
43+
44+ # Empty region list
45+ mock_conn .identity .regions .return_value = []
46+
47+ # Test get_regions()
48+ keystone_tools = self .get_keystone_tools ()
49+ result = keystone_tools .get_regions ()
50+
51+ # Verify results
52+ assert result == []
53+
54+ # Verify mock calls
55+ mock_conn .identity .regions .assert_called_once ()
56+
57+ def test_create_region_success (self , mock_get_openstack_conn_keystone ):
58+ """Test creating a keystone region successfully."""
59+ mock_conn = mock_get_openstack_conn_keystone
60+
61+ # Create mock region object
62+ mock_region = Mock ()
63+ mock_region .id = "RegionOne"
64+ mock_region .description = "Region One description"
65+
66+ # Configure mock region.create_region()
67+ mock_conn .identity .create_region .return_value = mock_region
68+
69+ # Test create_region()
70+ keystone_tools = self .get_keystone_tools ()
71+ result = keystone_tools .create_region (id = "RegionOne" , description = "Region One description" )
72+
73+ # Verify results
74+ assert result == Region (id = "RegionOne" , description = "Region One description" )
75+
76+ # Verify mock calls
77+ mock_conn .identity .create_region .assert_called_once_with (id = "RegionOne" , description = "Region One description" )
78+
79+ def test_create_region_without_description (self , mock_get_openstack_conn_keystone ):
80+ """Test creating a keystone region without a description."""
81+ mock_conn = mock_get_openstack_conn_keystone
82+
83+ # Create mock region object
84+ mock_region = Mock ()
85+ mock_region .id = "RegionOne"
86+ mock_region .description = None
87+
88+ # Configure mock region.create_region()
89+ mock_conn .identity .create_region .return_value = mock_region
90+
91+ # Test create_region()
92+ keystone_tools = self .get_keystone_tools ()
93+ result = keystone_tools .create_region (id = "RegionOne" )
94+
95+ # Verify results
96+ assert result == Region (id = "RegionOne" )
97+
98+ def test_create_region_invalid_id_format (self , mock_get_openstack_conn_keystone ):
99+ """Test creating a keystone region with an invalid ID format."""
100+ mock_conn = mock_get_openstack_conn_keystone
101+
102+ # Configure mock region.create_region() to raise an exception
103+ mock_conn .identity .create_region .side_effect = exceptions .BadRequestException (
104+ "Invalid input for field 'id': Expected string, got integer"
105+ )
106+
107+ # Test create_region()
108+ keystone_tools = self .get_keystone_tools ()
109+
110+ # Verify results
111+ with pytest .raises (exceptions .BadRequestException , match = "Invalid input for field 'id': Expected string, got integer" ):
112+ keystone_tools .create_region (id = 1 , description = "Region One description" )
113+
114+ # Verify mock calls
115+ mock_conn .identity .create_region .assert_called_once_with (id = 1 , description = "Region One description" )
116+
0 commit comments