<s> import os <EOL> import tempfile <EOL> import unittest <EOL> from spinnaker . configurator import Configurator <EOL> from spinnaker . validate_configuration import ValidateConfig <EOL> from spinnaker . yaml_util import YamlBindings <EOL> class ValidateConfigurationTest ( unittest . TestCase ) : <EOL> def test_is_reference_good ( self ) : <EOL> bindings = YamlBindings ( ) <EOL> validator = ValidateConfig ( <EOL> configurator = Configurator ( bindings = bindings ) ) <EOL> self . assertTrue ( validator . is_reference ( '<STR_LIT>' ) ) <EOL> self . assertTrue ( validator . is_reference ( '<STR_LIT>' ) ) <EOL> def test_is_reference_bad ( self ) : <EOL> bindings = YamlBindings ( ) <EOL> validator = ValidateConfig ( <EOL> configurator = Configurator ( bindings = bindings ) ) <EOL> self . assertFalse ( validator . is_reference ( '<STR_LIT:str>' ) ) <EOL> self . assertFalse ( validator . is_reference ( '<STR_LIT:true>' ) ) <EOL> self . assertFalse ( validator . is_reference ( '<STR_LIT:0>' ) ) <EOL> self . assertFalse ( validator . is_reference ( '<STR_LIT>' ) ) <EOL> def test_true_false_good ( self ) : <EOL> bindings = YamlBindings ( ) <EOL> bindings . import_dict ( <EOL> { '<STR_LIT:t>' : True , '<STR_LIT:f>' : False , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:default>' : '<STR_LIT>' } ) <EOL> validator = ValidateConfig ( <EOL> configurator = Configurator ( bindings = bindings ) ) <EOL> self . assertTrue ( validator . verify_true_false ( '<STR_LIT:t>' ) ) <EOL> self . assertTrue ( validator . verify_true_false ( '<STR_LIT:f>' ) ) <EOL> self . assertTrue ( validator . verify_true_false ( '<STR_LIT>' ) ) <EOL> self . assertTrue ( validator . verify_true_false ( '<STR_LIT:default>' ) ) <EOL> def test_true_false_bad ( self ) : <EOL> bindings = YamlBindings ( ) <EOL> bindings . import_dict ( <EOL> { '<STR_LIT:t>' : '<STR_LIT:true>' , '<STR_LIT:f>' : '<STR_LIT:false>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:default>' : '<STR_LIT>' } ) <EOL> validator = ValidateConfig ( <EOL> configurator = Configurator ( bindings = bindings ) ) <EOL> self . assertFalse ( validator . verify_true_false ( '<STR_LIT:t>' ) ) <EOL> self . assertFalse ( validator . verify_true_false ( '<STR_LIT:f>' ) ) <EOL> self . assertFalse ( validator . verify_true_false ( '<STR_LIT>' ) ) <EOL> self . assertFalse ( validator . verify_true_false ( '<STR_LIT:default>' ) ) <EOL> self . assertEqual ( <NUM_LIT:4> , len ( validator . errors ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( validator . warnings ) ) <EOL> self . assertEqual ( <EOL> [ "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ] , <EOL> validator . errors ) <EOL> def test_true_false_not_resolved ( self ) : <EOL> bindings = YamlBindings ( ) <EOL> bindings . import_dict ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> validator = ValidateConfig ( <EOL> configurator = Configurator ( bindings = bindings ) ) <EOL> self . assertFalse ( validator . verify_true_false ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( '<STR_LIT>' , validator . errors [ <NUM_LIT:0> ] ) <EOL> def host_test_helper ( self , tests , valid , required = False ) : <EOL> bindings = YamlBindings ( ) <EOL> bindings . import_dict ( tests ) <EOL> validator = ValidateConfig ( <EOL> configurator = Configurator ( bindings = bindings ) ) <EOL> for key , value in tests . items ( ) : <EOL> msg = '<STR_LIT>' . format ( <EOL> key = key , valid = '<STR_LIT>' if valid else '<STR_LIT>' ) <EOL> self . assertEqual ( valid , validator . verify_host ( key , required ) , msg ) <EOL> return validator <EOL> def test_verify_host_good ( self ) : <EOL> tests = { <EOL> '<STR_LIT>' : '<STR_LIT:localhost>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> self . host_test_helper ( tests , True ) <EOL> def test_verify_host_bad ( self ) : <EOL> tests = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> validator = self . host_test_helper ( tests , False ) <EOL> self . assertTrue ( validator . errors [ <NUM_LIT:0> ] . startswith ( '<STR_LIT>' ) ) <EOL> def test_verify_host_missing ( self ) : <EOL> tests = { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> validator = self . host_test_helper ( tests , False , required = True ) <EOL> self . assertEquals ( '<STR_LIT>' , validator . errors [ <NUM_LIT:0> ] ) <EOL> self . assertFalse ( validator . verify_host ( '<STR_LIT>' , True ) ) <EOL> self . assertEquals ( '<STR_LIT>' , validator . errors [ len ( tests ) ] ) <EOL> def test_verify_host_optional_ok ( self ) : <EOL> tests = { <EOL> '<STR_LIT>' : '<STR_LIT:localhost>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> self . host_test_helper ( tests , True , required = False ) <EOL> def baseUrl_test_helper ( self , tests , valid , scheme_optional ) : <EOL> bindings = YamlBindings ( ) <EOL> bindings . import_dict ( tests ) <EOL> validator = ValidateConfig ( <EOL> configurator = Configurator ( bindings = bindings ) ) <EOL> for key , value in tests . items ( ) : <EOL> msg = '<STR_LIT>' . format ( <EOL> key = key , valid = '<STR_LIT>' if valid else '<STR_LIT>' ) <EOL> self . assertEqual ( <EOL> valid , <EOL> validator . verify_baseUrl ( key , True , <EOL> scheme_optional = scheme_optional ) , <EOL> msg ) <EOL> def test_verify_baseUrl_only_host_ok ( self ) : <EOL> tests = { <EOL> '<STR_LIT:localhost>' : '<STR_LIT:localhost>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> self . baseUrl_test_helper ( tests , True , scheme_optional = True ) <EOL> def test_verify_baseUrl_only_host_bad ( self ) : <EOL> tests = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> self . baseUrl_test_helper ( tests , False , scheme_optional = True ) <EOL> def test_verify_baseUrl_only_host_port_ok ( self ) : <EOL> tests = { <EOL> '<STR_LIT:localhost>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> self . baseUrl_test_helper ( tests , True , scheme_optional = True ) <EOL> def test_verify_baseUrl_only_host_port_bad ( self ) : <EOL> tests = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> self . baseUrl_test_helper ( tests , False , scheme_optional = True ) <EOL> def test_verify_baseUrl_only_host_port_path_ok ( self ) : <EOL> tests = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:root>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> self . baseUrl_test_helper ( tests , True , scheme_optional = True ) <EOL> def test_verify_baseUrl_only_host_port_path_bad ( self ) : <EOL> tests = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> self . baseUrl_test_helper ( tests , False , scheme_optional = True ) <EOL> def test_verify_baseUrl_scheme_ok ( self ) : <EOL> tests = { <EOL> '<STR_LIT:host>' : '<STR_LIT>' , <EOL> '<STR_LIT:port>' : '<STR_LIT>' , <EOL> '<STR_LIT:path>' : '<STR_LIT>' , <EOL> } <EOL> self . baseUrl_test_helper ( tests , True , scheme_optional = True ) <EOL> def test_verify_baseUrl_scheme_bad ( self ) : <EOL> tests = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> self . baseUrl_test_helper ( tests , False , scheme_optional = True ) <EOL> def test_verify_baseUrl_scheme_required_ok ( self ) : <EOL> tests = { <EOL> '<STR_LIT:host>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> self . baseUrl_test_helper ( tests , True , scheme_optional = False ) <EOL> def test_verify_baseUrl_scheme_required_bad ( self ) : <EOL> tests = { <EOL> '<STR_LIT>' : '<STR_LIT:http>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:host>' : '<STR_LIT:localhost>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> self . baseUrl_test_helper ( tests , False , scheme_optional = False ) <EOL> def test_verify_user_access_only_good ( self ) : <EOL> bindings = YamlBindings ( ) <EOL> validator = ValidateConfig ( <EOL> configurator = Configurator ( bindings = bindings ) ) <EOL> fd , temp = tempfile . mkstemp ( ) <EOL> os . close ( fd ) <EOL> try : <EOL> os . chmod ( temp , <NUM_LIT:0> <NUM_LIT> ) <EOL> self . assertTrue ( validator . verify_user_access_only ( temp ) ) <EOL> os . chmod ( temp , <NUM_LIT:0> <NUM_LIT> ) <EOL> self . assertTrue ( validator . verify_user_access_only ( temp ) ) <EOL> finally : <EOL> os . remove ( temp ) <EOL> def test_verify_user_access_only_bad ( self ) : <EOL> bindings = YamlBindings ( ) <EOL> validator = ValidateConfig ( <EOL> configurator = Configurator ( bindings = bindings ) ) <EOL> fd , temp = tempfile . mkstemp ( ) <EOL> os . close ( fd ) <EOL> try : <EOL> os . chmod ( temp , <NUM_LIT:0> <NUM_LIT> ) <EOL> self . assertFalse ( validator . verify_user_access_only ( temp ) ) <EOL> self . assertEqual ( <EOL> '<STR_LIT>' <EOL> . format ( temp = temp ) , <EOL> validator . errors [ <NUM_LIT:0> ] ) <EOL> os . chmod ( temp , <NUM_LIT:0> <NUM_LIT> ) <EOL> self . assertFalse ( validator . verify_user_access_only ( temp ) ) <EOL> os . chmod ( temp , <NUM_LIT:0> <NUM_LIT> ) <EOL> self . assertFalse ( validator . verify_user_access_only ( temp ) ) <EOL> os . chmod ( temp , <NUM_LIT:0> <NUM_LIT> ) <EOL> self . assertFalse ( validator . verify_user_access_only ( temp ) ) <EOL> os . chmod ( temp , <NUM_LIT:0> <NUM_LIT> ) <EOL> self . assertFalse ( validator . verify_user_access_only ( temp ) ) <EOL> os . chmod ( temp , <NUM_LIT:0> <NUM_LIT> ) <EOL> self . assertFalse ( validator . verify_user_access_only ( temp ) ) <EOL> finally : <EOL> os . remove ( temp ) <EOL> def test_verify_at_least_one_provider_enabled_good ( self ) : <EOL> bindings = YamlBindings ( ) <EOL> bindings . import_dict ( { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { '<STR_LIT>' : False } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : False } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : True } <EOL> } , <EOL> } ) <EOL> validator = ValidateConfig ( <EOL> configurator = Configurator ( bindings = bindings ) ) <EOL> self . assertTrue ( validator . verify_at_least_one_provider_enabled ( ) ) <EOL> def test_verify_at_least_one_provider_enabled_bad ( self ) : <EOL> bindings = YamlBindings ( ) <EOL> bindings . import_dict ( { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { '<STR_LIT>' : False } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : False } <EOL> } , <EOL> '<STR_LIT>' : { '<STR_LIT:test>' : { '<STR_LIT>' : True } } <EOL> } ) <EOL> validator = ValidateConfig ( <EOL> configurator = Configurator ( bindings = bindings ) ) <EOL> self . assertFalse ( validator . verify_at_least_one_provider_enabled ( ) ) <EOL> self . assertEqual ( '<STR_LIT>' , <EOL> validator . errors [ <NUM_LIT:0> ] ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> loader = unittest . TestLoader ( ) <EOL> suite = loader . loadTestsFromTestCase ( ValidateConfigurationTest ) <EOL> unittest . TextTestRunner ( verbosity = <NUM_LIT:2> ) . run ( suite ) </s>
