<s> import copy <EOL> from sahara import context <EOL> from sahara . db . templates import api as template_api <EOL> from sahara . db . templates import utils as u <EOL> from sahara . tests . unit . conductor import base <EOL> from sahara . tests . unit . db . templates import common as c <EOL> class Config ( c . Config ) : <EOL> def __init__ ( self , option_values = None ) : <EOL> option_values = option_values or { } <EOL> if "<STR_LIT:name>" not in option_values : <EOL> option_values [ "<STR_LIT:name>" ] = "<STR_LIT>" <EOL> super ( Config , self ) . __init__ ( option_values ) <EOL> class TemplateDeleteTestCase ( base . ConductorManagerTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( TemplateDeleteTestCase , self ) . setUp ( ) <EOL> self . logger = c . Logger ( ) <EOL> template_api . set_logger ( self . logger ) <EOL> def test_node_group_template_delete_by_id ( self ) : <EOL> self . logger . clear_log ( ) <EOL> self . setup_context ( tenant_id = None ) <EOL> ctx = context . ctx ( ) <EOL> t = self . api . node_group_template_create ( ctx , c . SAMPLE_NGT ) <EOL> option_values = { "<STR_LIT>" : t [ "<STR_LIT>" ] , <EOL> "<STR_LIT:id>" : t [ "<STR_LIT:id>" ] } <EOL> template_api . set_conf ( Config ( option_values ) ) <EOL> template_api . do_node_group_template_delete_by_id ( ) <EOL> msg = '<STR_LIT>' . format ( <EOL> info = u . name_and_id ( t ) ) <EOL> self . assertIn ( msg , self . logger . infos ) <EOL> t = self . api . node_group_template_get ( ctx , t [ "<STR_LIT:id>" ] ) <EOL> self . assertIsNone ( t ) <EOL> def test_node_group_template_delete_by_id_skipped ( self ) : <EOL> self . logger . clear_log ( ) <EOL> self . setup_context ( tenant_id = None ) <EOL> ctx = context . ctx ( ) <EOL> template_values = copy . copy ( c . SAMPLE_NGT ) <EOL> template_values [ "<STR_LIT>" ] = False <EOL> t = self . api . node_group_template_create ( ctx , template_values ) <EOL> option_values = { "<STR_LIT>" : t [ "<STR_LIT>" ] , <EOL> "<STR_LIT:id>" : t [ "<STR_LIT:id>" ] } <EOL> template_api . set_conf ( Config ( option_values ) ) <EOL> template_api . do_node_group_template_delete_by_id ( ) <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( info = u . name_and_id ( t ) ) ) <EOL> self . assertIn ( msg , self . logger . warnings ) <EOL> t = self . api . node_group_template_get ( ctx , t [ "<STR_LIT:id>" ] ) <EOL> self . assertIsNotNone ( t ) <EOL> def test_node_group_template_delete_bad_id ( self ) : <EOL> self . logger . clear_log ( ) <EOL> option_values = { "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT:id>" : "<STR_LIT>" } <EOL> template_api . set_conf ( Config ( option_values ) ) <EOL> template_api . do_node_group_template_delete_by_id ( ) <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( id = option_values [ "<STR_LIT:id>" ] ) ) <EOL> self . assertIn ( msg , self . logger . warnings ) <EOL> def test_node_group_template_delete_by_name ( self ) : <EOL> self . logger . clear_log ( ) <EOL> ctx = context . ctx ( ) <EOL> t = self . api . node_group_template_create ( ctx , c . SAMPLE_NGT ) <EOL> option_values = { "<STR_LIT>" : t [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : t [ "<STR_LIT:name>" ] } <EOL> template_api . set_conf ( Config ( option_values ) ) <EOL> template_api . do_node_group_template_delete ( ) <EOL> msg = '<STR_LIT>' . format ( <EOL> info = u . name_and_id ( t ) ) <EOL> self . assertIn ( msg , self . logger . infos ) <EOL> t = self . api . node_group_template_get ( ctx , t [ "<STR_LIT:id>" ] ) <EOL> self . assertIsNone ( t ) <EOL> def test_node_group_template_delete_by_name_skipped ( self ) : <EOL> self . logger . clear_log ( ) <EOL> ctx = context . ctx ( ) <EOL> template_values = copy . copy ( c . SAMPLE_NGT ) <EOL> template_values [ "<STR_LIT>" ] = False <EOL> t = self . api . node_group_template_create ( ctx , template_values ) <EOL> option_values = { "<STR_LIT>" : t [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : t [ "<STR_LIT:name>" ] } <EOL> template_api . set_conf ( Config ( option_values ) ) <EOL> template_api . do_node_group_template_delete ( ) <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( name = t [ "<STR_LIT:name>" ] ) ) <EOL> self . assertIn ( msg , self . logger . warnings ) <EOL> t = self . api . node_group_template_get ( ctx , t [ "<STR_LIT:id>" ] ) <EOL> self . assertIsNotNone ( t ) <EOL> def test_node_group_template_delete_in_use ( self ) : <EOL> self . logger . clear_log ( ) <EOL> ctx = context . ctx ( ) <EOL> t = self . api . node_group_template_create ( ctx , c . SAMPLE_NGT ) <EOL> cluster_values = copy . deepcopy ( c . SAMPLE_CLUSTER ) <EOL> cluster_values [ "<STR_LIT>" ] [ <NUM_LIT:0> ] [ "<STR_LIT>" ] = t [ "<STR_LIT:id>" ] <EOL> cl = self . api . cluster_create ( ctx , cluster_values ) <EOL> cluster_temp_values = copy . deepcopy ( c . SAMPLE_CLT ) <EOL> cluster_temp_values [ "<STR_LIT>" ] = cluster_values [ "<STR_LIT>" ] <EOL> clt = self . api . cluster_template_create ( ctx , cluster_temp_values ) <EOL> msgs = [ "<STR_LIT>" <EOL> "<STR_LIT>" . format ( <EOL> info = u . name_and_id ( t ) , clusters = [ cl [ "<STR_LIT:name>" ] ] ) ] <EOL> msgs += [ "<STR_LIT>" <EOL> "<STR_LIT>" . format ( <EOL> info = u . name_and_id ( t ) , cluster_temps = [ clt [ "<STR_LIT:name>" ] ] ) ] <EOL> msgs += [ "<STR_LIT>" . format ( <EOL> info = u . name_and_id ( t ) ) ] <EOL> option_values = { "<STR_LIT>" : t [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : t [ "<STR_LIT:name>" ] } <EOL> template_api . set_conf ( Config ( option_values ) ) <EOL> template_api . do_node_group_template_delete ( ) <EOL> for msg in msgs : <EOL> self . assertIn ( msg , self . logger . warnings ) <EOL> self . logger . clear_log ( ) <EOL> option_values = { "<STR_LIT>" : t [ "<STR_LIT>" ] , <EOL> "<STR_LIT:id>" : t [ "<STR_LIT:id>" ] } <EOL> template_api . set_conf ( Config ( option_values ) ) <EOL> template_api . do_node_group_template_delete_by_id ( ) <EOL> for msg in msgs : <EOL> self . assertIn ( msg , self . logger . warnings ) <EOL> self . logger . clear_log ( ) <EOL> def test_cluster_template_delete_by_id ( self ) : <EOL> self . logger . clear_log ( ) <EOL> self . setup_context ( tenant_id = None ) <EOL> ctx = context . ctx ( ) <EOL> t = self . api . cluster_template_create ( ctx , c . SAMPLE_CLT ) <EOL> option_values = { "<STR_LIT>" : t [ "<STR_LIT>" ] , <EOL> "<STR_LIT:id>" : t [ "<STR_LIT:id>" ] } <EOL> template_api . set_conf ( Config ( option_values ) ) <EOL> template_api . do_cluster_template_delete_by_id ( ) <EOL> msg = '<STR_LIT>' . format ( <EOL> info = u . name_and_id ( t ) ) <EOL> self . assertIn ( msg , self . logger . infos ) <EOL> t = self . api . cluster_template_get ( ctx , t [ "<STR_LIT:id>" ] ) <EOL> self . assertIsNone ( t ) <EOL> def test_cluster_template_delete_by_id_skipped ( self ) : <EOL> self . logger . clear_log ( ) <EOL> ctx = context . ctx ( ) <EOL> template_values = copy . copy ( c . SAMPLE_CLT ) <EOL> template_values [ "<STR_LIT>" ] = False <EOL> t = self . api . cluster_template_create ( ctx , template_values ) <EOL> option_values = { "<STR_LIT>" : t [ "<STR_LIT>" ] , <EOL> "<STR_LIT:id>" : t [ "<STR_LIT:id>" ] } <EOL> template_api . set_conf ( Config ( option_values ) ) <EOL> template_api . do_cluster_template_delete_by_id ( ) <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( info = u . name_and_id ( t ) ) ) <EOL> self . assertIn ( msg , self . logger . warnings ) <EOL> t = self . api . cluster_template_get ( ctx , t [ "<STR_LIT:id>" ] ) <EOL> self . assertIsNotNone ( t ) <EOL> def test_cluster_template_delete_bad_id ( self ) : <EOL> self . logger . clear_log ( ) <EOL> option_values = { "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT:id>" : "<STR_LIT>" } <EOL> template_api . set_conf ( Config ( option_values ) ) <EOL> template_api . do_cluster_template_delete_by_id ( ) <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( id = option_values [ "<STR_LIT:id>" ] ) ) <EOL> self . assertIn ( msg , self . logger . warnings ) <EOL> def test_cluster_template_delete_by_name ( self ) : <EOL> self . logger . clear_log ( ) <EOL> ctx = context . ctx ( ) <EOL> t = self . api . cluster_template_create ( ctx , c . SAMPLE_NGT ) <EOL> option_values = { "<STR_LIT>" : t [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : t [ "<STR_LIT:name>" ] } <EOL> template_api . set_conf ( Config ( option_values ) ) <EOL> template_api . do_cluster_template_delete ( ) <EOL> msg = '<STR_LIT>' . format ( <EOL> info = u . name_and_id ( t ) ) <EOL> self . assertIn ( msg , self . logger . infos ) <EOL> t = self . api . cluster_template_get ( ctx , t [ "<STR_LIT:id>" ] ) <EOL> self . assertIsNone ( t ) <EOL> def test_cluster_template_delete_by_name_skipped ( self ) : <EOL> self . logger . clear_log ( ) <EOL> ctx = context . ctx ( ) <EOL> template_values = copy . copy ( c . SAMPLE_NGT ) <EOL> template_values [ "<STR_LIT>" ] = False <EOL> t = self . api . cluster_template_create ( ctx , template_values ) <EOL> option_values = { "<STR_LIT>" : t [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : t [ "<STR_LIT:name>" ] } <EOL> template_api . set_conf ( Config ( option_values ) ) <EOL> template_api . do_cluster_template_delete ( ) <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( name = t [ "<STR_LIT:name>" ] ) ) <EOL> self . assertIn ( msg , self . logger . warnings ) <EOL> t = self . api . cluster_template_get ( ctx , t [ "<STR_LIT:id>" ] ) <EOL> self . assertIsNotNone ( t ) <EOL> def test_cluster_template_delete_in_use ( self ) : <EOL> self . logger . clear_log ( ) <EOL> ctx = context . ctx ( ) <EOL> t = self . api . cluster_template_create ( ctx , c . SAMPLE_CLT ) <EOL> cluster_values = copy . deepcopy ( c . SAMPLE_CLUSTER ) <EOL> cluster_values [ "<STR_LIT>" ] = t [ "<STR_LIT:id>" ] <EOL> del cluster_values [ "<STR_LIT>" ] <EOL> cl = self . api . cluster_create ( ctx , cluster_values ) <EOL> msgs = [ "<STR_LIT>" <EOL> "<STR_LIT>" . format ( <EOL> info = u . name_and_id ( t ) , clusters = [ cl [ "<STR_LIT:name>" ] ] ) ] <EOL> msgs += [ "<STR_LIT>" . format ( <EOL> info = u . name_and_id ( t ) ) ] <EOL> option_values = { "<STR_LIT>" : t [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : t [ "<STR_LIT:name>" ] } <EOL> template_api . set_conf ( Config ( option_values ) ) <EOL> template_api . do_cluster_template_delete ( ) <EOL> for msg in msgs : <EOL> self . assertIn ( msg , self . logger . warnings ) <EOL> self . logger . clear_log ( ) <EOL> option_values = { "<STR_LIT>" : t [ "<STR_LIT>" ] , <EOL> "<STR_LIT:id>" : t [ "<STR_LIT:id>" ] } <EOL> template_api . set_conf ( Config ( option_values ) ) <EOL> template_api . do_cluster_template_delete_by_id ( ) <EOL> for msg in msgs : <EOL> self . assertIn ( msg , self . logger . warnings ) <EOL> self . logger . clear_log ( ) <EOL> def _make_templates ( self , ctx , name , plugin_name , plugin_version ) : <EOL> values = copy . copy ( c . SAMPLE_NGT ) <EOL> values [ "<STR_LIT:name>" ] = "<STR_LIT>" + name <EOL> values [ "<STR_LIT>" ] = plugin_name <EOL> values [ "<STR_LIT>" ] = plugin_version <EOL> ngt = self . api . node_group_template_create ( ctx , values ) <EOL> values = copy . deepcopy ( c . SAMPLE_CLT ) <EOL> values [ "<STR_LIT:name>" ] = "<STR_LIT>" + name <EOL> values [ "<STR_LIT>" ] = plugin_name <EOL> values [ "<STR_LIT>" ] = plugin_version <EOL> values [ "<STR_LIT>" ] [ <NUM_LIT:0> ] [ "<STR_LIT>" ] = ngt [ "<STR_LIT:id>" ] <EOL> clt = self . api . cluster_template_create ( ctx , values ) <EOL> return ngt , clt <EOL> def test_do_delete ( self ) : <EOL> self . logger . clear_log ( ) <EOL> ctx = context . ctx ( ) <EOL> ngt , clt = self . _make_templates ( ctx , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> ngt2 , clt2 = self . _make_templates ( ctx , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> safe_ngt , safe_clt = self . _make_templates ( ctx , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> option_values = { "<STR_LIT>" : ngt [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : [ ngt [ "<STR_LIT>" ] ] , <EOL> "<STR_LIT>" : [ ngt [ "<STR_LIT>" ] ] } <EOL> template_api . set_conf ( Config ( option_values ) ) <EOL> template_api . do_delete ( ) <EOL> msgs = [ "<STR_LIT>" . format ( <EOL> info = u . name_and_id ( clt ) ) ] <EOL> msgs += [ "<STR_LIT>" . format ( <EOL> info = u . name_and_id ( ngt ) ) ] <EOL> self . assertEqual ( msgs , self . logger . infos ) <EOL> self . assertIsNone ( self . api . node_group_template_get ( ctx , ngt [ "<STR_LIT:id>" ] ) ) <EOL> self . assertIsNone ( self . api . cluster_template_get ( ctx , clt [ "<STR_LIT:id>" ] ) ) <EOL> self . assertIsNotNone ( self . api . node_group_template_get ( ctx , <EOL> ngt2 [ "<STR_LIT:id>" ] ) ) <EOL> self . assertIsNotNone ( self . api . cluster_template_get ( ctx , <EOL> clt2 [ "<STR_LIT:id>" ] ) ) <EOL> self . assertIsNotNone ( self . api . node_group_template_get ( ctx , <EOL> safe_ngt [ "<STR_LIT:id>" ] ) ) <EOL> self . assertIsNotNone ( self . api . cluster_template_get ( ctx , <EOL> safe_clt [ "<STR_LIT:id>" ] ) ) <EOL> self . logger . clear_log ( ) <EOL> option_values = { "<STR_LIT>" : ngt2 [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : [ ngt2 [ "<STR_LIT>" ] ] , <EOL> "<STR_LIT>" : None } <EOL> template_api . set_conf ( Config ( option_values ) ) <EOL> template_api . do_delete ( ) <EOL> msgs = [ "<STR_LIT>" . format ( <EOL> info = u . name_and_id ( clt2 ) ) ] <EOL> msgs += [ "<STR_LIT>" . format ( <EOL> info = u . name_and_id ( ngt2 ) ) ] <EOL> self . assertEqual ( msgs , self . logger . infos ) <EOL> self . assertIsNone ( self . api . node_group_template_get ( ctx , ngt2 [ "<STR_LIT:id>" ] ) ) <EOL> self . assertIsNone ( self . api . cluster_template_get ( ctx , clt2 [ "<STR_LIT:id>" ] ) ) <EOL> self . assertIsNotNone ( self . api . node_group_template_get ( ctx , <EOL> safe_ngt [ "<STR_LIT:id>" ] ) ) <EOL> self . assertIsNotNone ( self . api . cluster_template_get ( ctx , <EOL> safe_clt [ "<STR_LIT:id>" ] ) ) </s>
