<s> import uuid <EOL> from oslo_serialization import jsonutils <EOL> import six <EOL> from nailgun import objects <EOL> from nailgun . test import base <EOL> class TestNetTemplateMalformedNICMappingApply ( base . BaseIntegrationTest ) : <EOL> def setUp ( self ) : <EOL> super ( TestNetTemplateMalformedNICMappingApply , self ) . setUp ( ) <EOL> self . cluster = self . env . create ( <EOL> release_kwargs = { '<STR_LIT>' : False , '<STR_LIT:version>' : '<STR_LIT>' } , <EOL> cluster_kwargs = { '<STR_LIT>' : False } <EOL> ) <EOL> self . node = self . create_controller ( ) <EOL> self . modified_template = self . mutilate_nic_mapping ( <EOL> self . _prepare_template ( ) , self . node ) <EOL> def create_controller ( self ) : <EOL> node = self . env . create_node ( roles = [ '<STR_LIT>' ] ) <EOL> node . roles = [ '<STR_LIT>' ] <EOL> self . db . flush ( ) <EOL> return node <EOL> def _prepare_template ( self ) : <EOL> template = self . env . read_fixtures ( [ '<STR_LIT>' ] ) [ <NUM_LIT:0> ] <EOL> template . pop ( '<STR_LIT>' ) <EOL> return template <EOL> def add_node_to_cluster ( self , cluster , node , expect_errors = False ) : <EOL> return self . app . put ( <EOL> base . reverse ( <EOL> '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT>' : node . id } <EOL> ) , <EOL> params = jsonutils . dumps ( { '<STR_LIT>' : cluster . id } ) , <EOL> headers = self . default_headers , <EOL> expect_errors = expect_errors <EOL> ) <EOL> def upload_template ( self , cluster , template , expect_errors = False ) : <EOL> return self . app . put ( <EOL> base . reverse ( <EOL> '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT>' : cluster . id } , <EOL> ) , <EOL> jsonutils . dumps ( template ) , <EOL> headers = self . default_headers , <EOL> expect_errors = expect_errors <EOL> ) <EOL> def mutilate_nic_mapping ( self , template , node ) : <EOL> node_nic_names = [ nic . name for nic in node . nic_interfaces ] <EOL> nic_mapping = template [ '<STR_LIT>' ] [ '<STR_LIT:default>' ] [ '<STR_LIT>' ] <EOL> new_mapping = { } <EOL> for substitute , iface_name in six . iteritems ( nic_mapping [ '<STR_LIT:default>' ] ) : <EOL> if iface_name in node_nic_names : <EOL> new_mapping [ substitute ] = uuid . uuid4 ( ) . hex <EOL> else : <EOL> new_mapping [ substitute ] = iface_name <EOL> nic_mapping [ '<STR_LIT:default>' ] = new_mapping <EOL> return template <EOL> def check_err_resp ( self , resp ) : <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> self . assertIn ( '<STR_LIT>' , resp . json_body [ '<STR_LIT:message>' ] ) <EOL> def test_fail_for_cluster_w_nodes ( self ) : <EOL> self . add_node_to_cluster ( self . cluster , self . node ) <EOL> resp = self . upload_template ( self . cluster , self . modified_template , <EOL> expect_errors = True ) <EOL> self . check_err_resp ( resp ) <EOL> def test_fail_if_set_node_via_single_handler ( self ) : <EOL> self . upload_template ( self . cluster , self . modified_template ) <EOL> resp = self . add_node_to_cluster ( self . cluster , self . node , <EOL> expect_errors = True ) <EOL> self . check_err_resp ( resp ) <EOL> def test_fail_if_set_node_via_collect_handler ( self ) : <EOL> self . upload_template ( self . cluster , self . modified_template ) <EOL> resp = self . app . put ( <EOL> base . reverse ( "<STR_LIT>" ) , <EOL> params = jsonutils . dumps ( <EOL> [ { '<STR_LIT:id>' : self . node . id , '<STR_LIT>' : self . cluster . id } ] <EOL> ) , <EOL> headers = self . default_headers , <EOL> expect_errors = True <EOL> ) <EOL> self . check_err_resp ( resp ) <EOL> def test_fail_if_set_node_via_cluster_handler ( self ) : <EOL> self . upload_template ( self . cluster , self . modified_template ) <EOL> self . node . nodegroup = objects . Cluster . get_default_group ( self . cluster ) <EOL> resp = self . app . put ( <EOL> base . reverse ( <EOL> "<STR_LIT>" , <EOL> kwargs = { '<STR_LIT>' : self . cluster . id } <EOL> ) , <EOL> params = jsonutils . dumps ( { '<STR_LIT>' : [ self . node . id ] } ) , <EOL> headers = self . default_headers , <EOL> expect_errors = True <EOL> ) <EOL> self . check_err_resp ( resp ) </s>
