@@ -65,36 +65,68 @@ def test_build_model_from_xml(self):
6565
6666 @mock .patch .object (model_root .ModelRoot , 'get_all_compute_nodes' )
6767 @mock .patch .object (model_root .ModelRoot , 'get_node_instances' )
68- def test_get_model_to_list (self , mock_instances , mock_nodes ):
69- fake_compute_node = mock .MagicMock (
70- uuid = 'fake_node_uuid' ,
71- fields = ['uuid' ])
72- fake_instance = mock .MagicMock (
73- uuid = 'fake_instance_uuid' ,
74- fields = ['uuid' ])
68+ @mock .patch ('watcher.decision_engine.model.model_root.LOG' )
69+ def test_get_model_to_list (self , mock_log , mock_instances , mock_nodes ):
70+ fake_compute_node = element .ComputeNode (
71+ uuid = 'fake_node_uuid' , hostname = "hostname0" )
72+ fake_instance = element .Instance (
73+ uuid = 'fake_instance_uuid' )
7574
7675 mock_nodes .return_value = {'fake_node_uuid' : fake_compute_node }
7776 mock_instances .return_value = [fake_instance ]
7877
79- expected_keys = ['server_uuid' , 'node_uuid' ]
78+ expected_keys = [
79+ 'server_uuid' , 'node_uuid' , 'node_state' , 'node_hostname' ,
80+ 'node_status' , 'server_flavor_extra_specs' , 'server_locked' ,
81+ 'server_pinned_az' , 'server_state' , 'server_watcher_exclude'
82+ ]
8083
8184 result = model_root .ModelRoot ().to_list ()
8285 self .assertEqual (1 , len (result ))
8386
8487 result_keys = result [0 ].keys ()
8588 self .assertEqual (sorted (expected_keys ), sorted (result_keys ))
8689
90+ mock_log .debug .assert_any_call (
91+ 'Attribute %s for Instance: %s is not provided' , 'name' ,
92+ 'fake_instance_uuid'
93+ )
94+ mock_log .debug .assert_any_call (
95+ 'Attribute %s for Compute: %s is not provided' , 'memory' ,
96+ 'hostname0'
97+ )
98+
8799 # test compute node has no instance
88100 mock_instances .return_value = []
89101
90- expected_keys = ['node_uuid' ]
102+ expected_keys = ['node_hostname' , 'node_status' ,
103+ 'node_state' , 'node_uuid' ]
91104
92105 result = model_root .ModelRoot ().to_list ()
93106 self .assertEqual (1 , len (result ))
94107
95108 result_keys = result [0 ].keys ()
96109 self .assertEqual (expected_keys , list (result_keys ))
97110
111+ def test_model_to_list_all_fields (self ):
112+ struct_str = self .load_data ('scenario_1.xml' )
113+ model = model_root .ModelRoot .from_xml (struct_str )
114+ model_list = model .to_list ()
115+ self .assertIsInstance (model_list , list )
116+ self .assertEqual (len (model_list ), 8 )
117+ self .assertEqual (
118+ model_list [0 ]['server_uuid' ],
119+ 'd000ef1f-dc19-4982-9383-087498bfde03' )
120+
121+ def test_model_to_xml_all_fields (self ):
122+ struct_str = self .load_data ('scenario_2_with_metrics.xml' )
123+
124+ model = model_root .ModelRoot .from_xml (struct_str )
125+ model_xml = model .to_xml ()
126+ self .assertIsInstance (model_xml , str )
127+ self .assertIn ('Instance' , model_xml )
128+ self .assertIn ('Compute' , model_xml )
129+
98130 def test_get_node_by_instance_uuid (self ):
99131 model = model_root .ModelRoot ()
100132 uuid_ = f"{ uuidutils .generate_uuid ()} "
0 commit comments