Example #1
0
    def test_variables_loose(self):
        bootstrap = Bootstrap(ztps_default_config=True)
        config = random_string()
        url = config
        var_dict = {'a': 'A', 'b': 'A', 'xxx': '999', 'dummy': 'DUMMY'}
        bootstrap.ztps.set_definition_response(actions=[{
            'action': 'test_action',
            'attributes': {
                'url': url,
                'substitution_mode': 'loose',
                'variables': var_dict
            }
        }])
        bootstrap.ztps.set_action_response('test_action',
                                           get_action('add_config'))
        contents = '$a 1234 $b 4  321 $xxx$a'
        expected_contents = Template(contents).safe_substitute(var_dict)
        bootstrap.ztps.set_file_response(config, contents)
        bootstrap.start_test()

        try:
            self.failUnless(os.path.isfile(bootstrap.startup_config))
            self.failUnless(
                [expected_contents] == file_log(bootstrap.startup_config))
            self.failUnless(bootstrap.success())
        except AssertionError as assertion:
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise_exception(assertion)
        finally:
            bootstrap.end_test()
Example #2
0
    def test_no_downgrade(self):
        bootstrap = Bootstrap(ztps_default_config=True)
        version1 = '4.18.1F'
        version2 = '4.17.2F'
        bootstrap.eapi.version = version1
        bootstrap.ztps.set_definition_response(
            actions=[{
                'action': 'test_action',
                'attributes': {
                    'downgrade': False,
                    'url': random_string(),
                    'version': version2
                }
            }, {
                'action': 'startup_config_action'
            }])
        bootstrap.ztps.set_action_response('test_action',
                                           get_action('install_image'))
        bootstrap.ztps.set_action_response('startup_config_action',
                                           startup_config_action())
        bootstrap.start_test()

        image_file = '%s/EOS-%s.swi' % (bootstrap.flash, version2)
        try:
            self.failUnless(bootstrap.success())
            self.failUnless('install_image: nothing to do: downgrade disabled'
                            in bootstrap.output)
        except AssertionError as assertion:
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise_exception(assertion)
        finally:
            remove_file(image_file)
            bootstrap.end_test()
    def test_duplicate_actions(self):
        bootstrap = Bootstrap()
        bootstrap.ztps.set_config_response()
        bootstrap.ztps.set_node_check_response()
        bootstrap.ztps.set_definition_response(
            actions=[{'action' : 'startup_config_action'},
                     {'action' : 'print_action'},
                     {'action' : 'print_action'}])
        bootstrap.ztps.set_action_response('startup_config_action',
                                           startup_config_action())

        text = random_string()
        bootstrap.ztps.set_action_response('print_action',
                                           print_action(text))
        bootstrap.start_test()

        try:
            self.failUnless(bootstrap.eapi_node_information_collected())
            self.failUnless(bootstrap.success())
            self.failUnless(bootstrap.output.count('Downloading action '
                                                   'print_action') == 1)
            self.failUnless(bootstrap.output.count('Executing action '
                                                   'print_action') == 2)
            self.failUnless(bootstrap.output.count(text) == 2)
            self.failIf(bootstrap.error)
        except AssertionError as assertion:
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise assertion
        finally:
            bootstrap.end_test()
Example #4
0
    def test_success(self):
        bootstrap = Bootstrap(ztps_default_config=True)
        config = random_string()
        url = config
        bootstrap.ztps.set_definition_response(actions=[{
            'action': 'test_action',
            'attributes': {
                'url': url
            }
        }])
        bootstrap.ztps.set_action_response('test_action',
                                           get_action('add_config'))
        contents = random_string()
        bootstrap.ztps.set_file_response(config, contents)
        bootstrap.start_test()

        try:
            self.failUnless(os.path.isfile(bootstrap.startup_config))
            self.failUnless(
                contents.split() == file_log(bootstrap.startup_config))
            self.failUnless(bootstrap.success())
        except AssertionError as assertion:
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise_exception(assertion)
        finally:
            bootstrap.end_test()
Example #5
0
    def test_replace(self):
        bootstrap = Bootstrap(ztps_default_config=True)

        source = random_string()
        destination = random_string()

        url = 'http://%s/%s' % (bootstrap.server, source)
        attributes = {'src_url': url, 'dst_url': destination}

        # 'replace' is the default
        if bool(random.getrandbits(1)):
            attributes['overwrite'] = 'replace'

        mode = None
        if True or bool(random.getrandbits(1)):
            mode = random_permissions()
            attributes['mode'] = mode

        bootstrap.ztps.set_definition_response(
            actions=[{
                'action': 'startup_config_action'
            }, {
                'action': 'test_action',
                'attributes': attributes
            }])

        bootstrap.ztps.set_action_response('startup_config_action',
                                           startup_config_action())

        persistent_dir = '/tmp'
        action = get_action('copy_file')
        action = action.replace('/mnt/flash/.ztp-files', persistent_dir)
        bootstrap.ztps.set_action_response('test_action', action)

        contents = random_string()
        bootstrap.ztps.set_file_response(source, contents)

        destination_path = '%s/%s' % (persistent_dir, source)
        bootstrap.start_test()

        try:
            self.failUnless(os.path.isfile(destination_path))
            self.failUnless([contents] == file_log(destination_path))

            self.failUnless(os.path.isfile(bootstrap.rc_eos))
            log = file_log(bootstrap.rc_eos)
            self.failUnless('#!/bin/bash' in log)
            self.failUnless('sudo cp %s %s' %
                            (destination_path, destination) in log)
            if mode:
                self.failUnless('sudo chmod %s %s' %
                                (mode, destination) in log)
            self.failUnless(bootstrap.success())
        except AssertionError as assertion:
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise_exception(assertion)
        finally:
            remove_file(destination_path)
            bootstrap.end_test()
    def test_variables_loose(self):
        bootstrap = Bootstrap(ztps_default_config=True)
        config = random_string()
        url = config
        var_dict = { 'a' : 'A',
                     'b' : 'A',
                     'xxx' : '999',
                     'dummy': 'DUMMY'}
        bootstrap.ztps.set_definition_response(
            actions=[{'action' : 'test_action',
                      'attributes': {'url' : url,
                                     'substitution_mode': 'loose',
                                     'variables': var_dict}}])
        bootstrap.ztps.set_action_response('test_action',
                                           get_action('add_config'))
        contents = '$a 1234 $b 4  321 $xxx$a'
        expected_contents = Template(contents).safe_substitute(var_dict)
        bootstrap.ztps.set_file_response(config, contents)
        bootstrap.start_test()

        try:
            self.failUnless(os.path.isfile(bootstrap.startup_config))
            self.failUnless([expected_contents] ==
                            file_log(bootstrap.startup_config))
            self.failUnless(bootstrap.success())
        except AssertionError as assertion:
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise_exception(assertion)
        finally:
            bootstrap.end_test()
Example #7
0
    def test_keep_backup(self):
        bootstrap = Bootstrap(ztps_default_config=True)

        source = random_string()
        destination = '/tmp/%s' % random_string()

        url = 'http://%s/%s' % (bootstrap.server, source)
        bootstrap.ztps.set_definition_response(
            actions=[{
                'action': 'startup_config_action'
            }, {
                'action': 'test_action',
                'attributes': {
                    'src_url': url,
                    'dst_url': destination,
                    'overwrite': 'backup'
                }
            }])

        bootstrap.ztps.set_action_response('startup_config_action',
                                           startup_config_action())

        action = get_action('copy_file')

        # Make the destinaton persistent
        action = action.replace('PERSISTENT_STORAGE = [',
                                'PERSISTENT_STORAGE = [\'%s\', ' % destination)
        bootstrap.ztps.set_action_response('test_action', action)

        contents = random_string()
        bootstrap.ztps.set_file_response(source, contents)

        destination_path = '%s/%s' % (destination, source)
        backup_contents = random_string()
        os.makedirs(destination)
        file_descriptor = open(destination_path, 'w')
        file_descriptor.write(backup_contents)
        file_descriptor.close()

        bootstrap.start_test()

        backup_path = '%s.backup' % destination_path
        try:
            self.failUnless(os.path.isfile(destination_path))
            self.failUnless([contents] == file_log(destination_path))

            self.failUnless(os.path.isfile(backup_path))
            self.failUnless([backup_contents] == file_log(backup_path))

            self.failIf(os.path.isfile(bootstrap.rc_eos))
            self.failUnless(bootstrap.success())
        except AssertionError as assertion:
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise_exception(assertion)
        finally:
            remove_file(destination_path)
            remove_file(backup_path)
            shutil.rmtree(destination)
            bootstrap.end_test()
    def test_success(self):
        bootstrap = Bootstrap(ztps_default_config=True)
        config = random_string()
        url = config
        bootstrap.ztps.set_definition_response(
            actions=[{'action' : 'startup_config_action'},
                     {'action' : 'test_action',
                      'attributes': {'url' : url}}])

        bootstrap.ztps.set_action_response(
            'startup_config_action', startup_config_action())
        bootstrap.ztps.set_action_response('test_action',
                                           get_action('run_bash_script'))

        print_string = random_string()
        contents = '''#!/usr/bin/env python
print "%s"''' % print_string
        bootstrap.ztps.set_file_response(config, contents)
        bootstrap.start_test()

        try:
            self.failUnless(os.path.isfile(bootstrap.startup_config))
            self.failUnless(print_string in bootstrap.output)
            self.failUnless(bootstrap.success())
        except AssertionError as assertion:
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise_exception(assertion)
        finally:
            bootstrap.end_test()
    def test_append(self):
        bootstrap = Bootstrap(ztps_default_config=True)
        config = random_string()
        url = 'http://%s/%s' % (bootstrap.server, config)
        bootstrap.ztps.set_definition_response(
            actions=[{'action' : 'startup_config_action'},
                     {'action' : 'test_action',
                      'attributes': {'url' : url}}])
        bootstrap.ztps.set_action_response('test_action',
                                           get_action('add_config'))

        startup_config_text = random_string()
        bootstrap.ztps.set_action_response(
            'startup_config_action',
            startup_config_action(lines=[startup_config_text]))
        contents = random_string()
        bootstrap.ztps.set_file_response(config, contents)
        bootstrap.start_test()

        try:
            self.failUnless(os.path.isfile(STARTUP_CONFIG))
            log = file_log(STARTUP_CONFIG)
            self.failUnless(contents in log)
            self.failUnless(startup_config_text in log)
            self.failUnless(bootstrap.success())
        except AssertionError as assertion:
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise assertion
        finally:
            bootstrap.end_test()
    def test_success(self):
        bootstrap = Bootstrap(ztps_default_config=True)
        version = random_string()
        image = random_string()
        url = 'http://%s/%s' % (bootstrap.server, image)
        bootstrap.ztps.set_definition_response(
            actions=[{'action' : 'test_action',
                      'attributes' : {
                        'url' : url,
                        'version' : version}}])

        boot_file = '/tmp/boot-config'
        action = get_action('install_image')
        action = action.replace('/mnt/flash/boot-config',
                                boot_file)
        bootstrap.ztps.set_action_response('test_action',
                                           action)
        bootstrap.ztps.set_file_response(image, print_action())
        bootstrap.start_test()

        image_file = '%s/%s.swi' % (FLASH, version)
        try:
            self.failUnless('! boot system flash:/%s.swi' % version
                            in file_log(STARTUP_CONFIG))
            self.failUnless(os.path.isfile(image_file))
            self.failUnless(['SWI=flash:/%s.swi' % version] ==
                            file_log(boot_file))
            self.failUnless(bootstrap.success())
        except AssertionError:
            raise
        finally:
            remove_file(image_file)
            remove_file(boot_file)
            bootstrap.end_test()
    def test_multi_lines(self):
        bootstrap = Bootstrap(ztps_default_config=True)
        config = random_string()
        url = 'http://%s/%s' % (bootstrap.server, config)
        bootstrap.ztps.set_definition_response(
            actions=[{'action' : 'startup_config_action'},
                     {'action' : 'test_action',
                      'attributes': {'url' : url}}])
        bootstrap.ztps.set_action_response('test_action',
                                           get_action('add_config'))

        startup_config_lines = [random_string(), random_string(),
                                random_string(), random_string()]
        bootstrap.ztps.set_action_response(
            'startup_config_action',
            startup_config_action(lines=startup_config_lines))
        contents = '\n'.join([random_string(), random_string(),
                              random_string(), random_string(),
                              random_string(), random_string()])
        bootstrap.ztps.set_file_response(config, contents)
        bootstrap.start_test()

        try:
            self.failUnless(os.path.isfile(bootstrap.startup_config))
            log = file_log(bootstrap.startup_config)
            all_lines = startup_config_lines + contents.split()
            for line in all_lines:
                self.failUnless(line in log)
            self.failUnless(bootstrap.success())
        except AssertionError as assertion:
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise_exception(assertion)
        finally:
            bootstrap.end_test()
    def test_success(self):
        bootstrap = Bootstrap(ztps_default_config=True)
        config = random_string()
        url = config
        bootstrap.ztps.set_definition_response(
            actions=[{'action' : 'startup_config_action'},
                     {'action' : 'test_action',
                      'attributes': {'url' : url}}])

        bootstrap.ztps.set_action_response(
            'startup_config_action', startup_config_action())
        bootstrap.ztps.set_action_response('test_action',
                                           get_action('run_cli_commands'))

        contents = random_string()
        bootstrap.ztps.set_file_response(config, contents)
        bootstrap.start_test()

        try:
            self.failUnless(os.path.isfile(bootstrap.startup_config))
            self.failUnless(eapi_log()[-1] == contents)
            self.failUnless(bootstrap.success())
        except AssertionError as assertion:
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise_exception(assertion)
        finally:
            bootstrap.end_test()
Example #13
0
    def test_attribute(self):
        bootstrap = Bootstrap()
        bootstrap.ztps.set_config_response()
        bootstrap.ztps.set_node_check_response()
        text = random_string()
        bootstrap.ztps.set_definition_response(
            actions=[{
                'action': 'startup_config_action'
            }, {
                'action': 'print_action',
                'attributes': {
                    'print_action-attr': text
                }
            }])
        bootstrap.ztps.set_action_response('startup_config_action',
                                           startup_config_action())
        bootstrap.ztps.set_action_response('print_action',
                                           print_action(use_attribute=True))
        bootstrap.start_test()

        try:
            self.failUnless(bootstrap.eapi_node_information_collected())
            self.failUnless(bootstrap.success())
            self.failUnless(text in bootstrap.output)
            self.failIf(bootstrap.error)
        except AssertionError as assertion:
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise_exception(assertion)
        finally:
            bootstrap.end_test()
    def test_multiple_actions(self):
        bootstrap = Bootstrap()
        bootstrap.ztps.set_config_response()
        bootstrap.ztps.set_node_check_response()
        bootstrap.ztps.set_definition_response(
            actions=[{'action' : 'startup_config_action'},
                     {'action' : 'print_action_1'},
                     {'action' : 'print_action_2'}])
        bootstrap.ztps.set_action_response('startup_config_action',
                                           startup_config_action())

        text_1 = random_string()
        bootstrap.ztps.set_action_response('print_action_1',
                                           print_action(text_1))
        text_2 = random_string()
        bootstrap.ztps.set_action_response('print_action_2',
                                           print_action(text_2))
        bootstrap.start_test()

        try:
            self.failUnless(bootstrap.eapi_node_information_collected())
            self.failUnless(bootstrap.success())
            self.failUnless(text_1 in bootstrap.output)
            self.failUnless(text_2 in bootstrap.output)
            self.failIf(bootstrap.error)
        except AssertionError as assertion:
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise assertion
        finally:
            bootstrap.end_test()
Example #15
0
    def test_multiple_actions(self):
        bootstrap = Bootstrap()
        bootstrap.ztps.set_config_response()
        bootstrap.ztps.set_node_check_response()
        bootstrap.ztps.set_definition_response(
            actions=[{
                'action': 'startup_config_action'
            }, {
                'action': 'print_action_1'
            }, {
                'action': 'print_action_2'
            }])
        bootstrap.ztps.set_action_response('startup_config_action',
                                           startup_config_action())

        text_1 = random_string()
        bootstrap.ztps.set_action_response('print_action_1',
                                           print_action(text_1))
        text_2 = random_string()
        bootstrap.ztps.set_action_response('print_action_2',
                                           print_action(text_2))
        bootstrap.start_test()

        try:
            self.failUnless(bootstrap.eapi_node_information_collected())
            self.failUnless(bootstrap.success())
            self.failUnless(text_1 in bootstrap.output)
            self.failUnless(text_2 in bootstrap.output)
            self.failIf(bootstrap.error)
        except AssertionError as assertion:
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise_exception(assertion)
        finally:
            bootstrap.end_test()
Example #16
0
    def test_success(self):
        bootstrap = Bootstrap(ztps_default_config=True)
        version = random_string()
        image = random_string()
        url = 'http://%s/%s' % (bootstrap.server, image)
        bootstrap.ztps.set_definition_response(
            actions=[{'action' : 'test_action',
                      'attributes' : {
                        'url' : url,
                        'version' : version}},
                     {'action' :'startup_config_action'}])

        action = get_action('install_image')
        bootstrap.ztps.set_action_response('test_action',
                                           action)
        bootstrap.ztps.set_action_response('startup_config_action',
                                           startup_config_action())
        bootstrap.ztps.set_file_response(image, print_action())
        bootstrap.start_test()

        image_file = '%s/EOS-%s.swi' % (FLASH, version)
        try:
            self.failUnless(os.path.isfile(image_file))
            self.failUnless(bootstrap.success())
            self.failUnless(eapi_log()[-1] == 
                            'install source flash:EOS-%s.swi' % version)
        except AssertionError as assertion:
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise_exception(assertion)
        finally:
            remove_file(image_file)
            bootstrap.end_test()
Example #17
0
    def test_no_op(self):
        bootstrap = Bootstrap(ztps_default_config=True)
        version = random_string()
        bootstrap.eapi.version = version
        bootstrap.ztps.set_definition_response(
            actions=[{
                'action': 'test_action',
                'attributes': {
                    'url': random_string(),
                    'version': version
                }
            }, {
                'action': 'startup_config_action'
            }])
        bootstrap.ztps.set_action_response('test_action',
                                           get_action('install_image'))
        bootstrap.ztps.set_action_response('startup_config_action',
                                           startup_config_action())
        bootstrap.start_test()

        try:
            self.failUnless(bootstrap.success())
        except AssertionError as assertion:
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise_exception(assertion)
        finally:
            bootstrap.end_test()
    def test_success(self):
        bootstrap = Bootstrap(ztps_default_config=True)

        smarthost = "%s:2525" % str(bootstrap.server).split(':')[0]

        bootstrap.ztps.set_definition_response(
            actions=[{'action' : 'startup_config_action'},
                     {'action' : 'test_action',
                      'attributes' : {
                        'smarthost': smarthost,
                        'sender': 'ztps@localhost',
                        'receivers': ['ztps@localhost']}}])


        bootstrap.ztps.set_action_response('startup_config_action',
                                           startup_config_action())

        action = get_action('send_email')
        bootstrap.ztps.set_action_response('test_action',
                                           action)

        bootstrap.start_test()
        try:
            self.failUnless(bootstrap.success())
        except Exception as exception: #pylint: disable=W0703
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise_exception(exception)
        finally:
            bootstrap.end_test()
    def test_success(self):
        bootstrap = Bootstrap(ztps_default_config=True)
        config = random_string()
        url = config
        bootstrap.ztps.set_definition_response(
            actions=[{
                'action': 'startup_config_action'
            }, {
                'action': 'test_action',
                'attributes': {
                    'url': url
                }
            }])

        bootstrap.ztps.set_action_response('startup_config_action',
                                           startup_config_action())
        bootstrap.ztps.set_action_response('test_action',
                                           get_action('run_bash_script'))

        print_string = random_string()
        contents = '''#!/usr/bin/env python
print "%s"''' % print_string
        bootstrap.ztps.set_file_response(config, contents)
        bootstrap.start_test()

        try:
            self.failUnless(os.path.isfile(bootstrap.startup_config))
            self.failUnless(print_string in bootstrap.output)
            self.failUnless(bootstrap.success())
        except AssertionError as assertion:
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise_exception(assertion)
        finally:
            bootstrap.end_test()
Example #20
0
    def test_success(self):
        bootstrap = Bootstrap(ztps_default_config=True)

        smarthost = "%s:2525" % str(bootstrap.server).split(':')[0]

        bootstrap.ztps.set_definition_response(
            actions=[{
                'action': 'startup_config_action'
            }, {
                'action': 'test_action',
                'attributes': {
                    'smarthost': smarthost,
                    'sender': 'ztps@localhost',
                    'receivers': ['ztps@localhost']
                }
            }])

        bootstrap.ztps.set_action_response('startup_config_action',
                                           startup_config_action())

        action = get_action('send_email')
        bootstrap.ztps.set_action_response('test_action', action)

        bootstrap.start_test()
        try:
            self.failUnless(bootstrap.success())
        except Exception as exception:  #pylint: disable=W0703
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise_exception(exception)
        finally:
            bootstrap.end_test()
    def test_attribute(self):
        bootstrap = Bootstrap()
        bootstrap.ztps.set_config_response()
        bootstrap.ztps.set_node_check_response()
        text = random_string()
        bootstrap.ztps.set_definition_response(
            actions=[{'action' : 'startup_config_action'},
                     {'action' : 'print_action',
                      'attributes' : {'print_action-attr':text}}])
        bootstrap.ztps.set_action_response('startup_config_action',
                                           startup_config_action())
        bootstrap.ztps.set_action_response('print_action',
                                           print_action(use_attribute=True))
        bootstrap.start_test()

        try:
            self.failUnless(bootstrap.eapi_node_information_collected())
            self.failUnless(bootstrap.success())
            self.failUnless(text in bootstrap.output)
            self.failIf(bootstrap.error)
        except AssertionError as assertion:
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise assertion
        finally:
            bootstrap.end_test()
    def test_no_downgrade(self):
        bootstrap = Bootstrap(ztps_default_config=True)
        version1 = '4.18.1F'
        version2 = '4.17.2F'
        bootstrap.eapi.version = version1
        bootstrap.ztps.set_definition_response(
            actions=[{'action' : 'test_action',
                      'attributes': {
                        'downgrade' : False,
                        'url' : random_string(),
                        'version' : version2}},
                     {'action' :'startup_config_action'}])
        bootstrap.ztps.set_action_response('test_action',
                                           get_action('install_image'))
        bootstrap.ztps.set_action_response('startup_config_action',
                                           startup_config_action())
        bootstrap.start_test()

        image_file = '%s/EOS-%s.swi' % (bootstrap.flash, version2)
        try:
            self.failUnless(bootstrap.success())
            self.failUnless('install_image: nothing to do: downgrade disabled'
                            in bootstrap.output)
        except AssertionError as assertion:
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise_exception(assertion)
        finally:
            remove_file(image_file)
            bootstrap.end_test()
    def test_keep_backup(self):
        bootstrap = Bootstrap(ztps_default_config=True)

        source = random_string()
        destination = '/tmp/%s' % random_string()

        url = 'http://%s/%s' % (bootstrap.server, source)
        bootstrap.ztps.set_definition_response(
            actions=[{'action' : 'startup_config_action'},
                     {'action' : 'test_action',
                      'attributes' : {'src_url' : url,
                                      'dst_url' : destination,
                                      'overwrite' : 'backup'}}])

        bootstrap.ztps.set_action_response(
            'startup_config_action', startup_config_action())

        action = get_action('copy_file')

        # Make the destinaton persistent
        action = action.replace('PERSISTENT_STORAGE = [',
                                'PERSISTENT_STORAGE = [\'%s\', ' %
                                destination)
        bootstrap.ztps.set_action_response('test_action',
                                           action)

        contents = random_string()
        bootstrap.ztps.set_file_response(source, contents)

        destination_path = '%s/%s' % (destination, source)
        backup_contents = random_string()
        os.makedirs(destination)
        file_descriptor = open(destination_path, 'w')
        file_descriptor.write(backup_contents)
        file_descriptor.close()

        bootstrap.start_test()

        backup_path = '%s.backup' % destination_path
        try:
            self.failUnless(os.path.isfile(destination_path))
            self.failUnless([contents] ==
                            file_log(destination_path))

            self.failUnless(os.path.isfile(backup_path))
            self.failUnless([backup_contents] ==
                            file_log(backup_path))

            self.failIf(os.path.isfile(bootstrap.rc_eos))
            self.failUnless(bootstrap.success())
        except AssertionError as assertion:
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise_exception(assertion)
        finally:
            remove_file(destination_path)
            remove_file(backup_path)
            shutil.rmtree(destination)
            bootstrap.end_test()
    def test_replace(self):
        bootstrap = Bootstrap(ztps_default_config=True)

        source = random_string()
        destination = random_string()

        url = 'http://%s/%s' % (bootstrap.server, source)
        attributes = {'src_url' : url,
                      'dst_url' : destination}

        # 'replace' is the default
        if bool(random.getrandbits(1)):
            attributes['overwrite'] = 'replace'

        mode = None
        if True or bool(random.getrandbits(1)):
            mode = random_permissions()
            attributes['mode'] = mode

        bootstrap.ztps.set_definition_response(
            actions=[{'action' : 'startup_config_action'},
                     {'action' : 'test_action',
                      'attributes' : attributes}])

        bootstrap.ztps.set_action_response(
            'startup_config_action', startup_config_action())

        persistent_dir = '/tmp'
        action = get_action('copy_file')
        action = action.replace('/mnt/flash/.ztp-files',
                                persistent_dir)
        bootstrap.ztps.set_action_response('test_action',
                                           action)

        contents = random_string()
        bootstrap.ztps.set_file_response(source, contents)

        destination_path = '%s/%s' % (persistent_dir, source)
        bootstrap.start_test()

        try:
            self.failUnless(os.path.isfile(destination_path))
            self.failUnless([contents] ==
                            file_log(destination_path))

            self.failUnless(os.path.isfile(RC_EOS))
            log = file_log(RC_EOS)
            self.failUnless('#!/bin/bash' in log)
            self.failUnless('sudo cp %s %s' %
                            (destination_path, destination) in log)
            if mode:
                self.failUnless('sudo chmod %s %s' %
                                (mode, destination) in log)
            self.failUnless(bootstrap.success())
        except AssertionError:
            raise
        finally:
            remove_file(destination_path)
            bootstrap.end_test()
    def test_replace(self):
        bootstrap = Bootstrap(ztps_default_config=True)

        source = random_string()
        destination = '/tmp/%s' % random_string()

        url = 'http://%s/%s' % (bootstrap.server, source)
        attributes = {'src_url' : url,
                      'dst_url' : destination}

        # 'replace' is the default
        if bool(random.getrandbits(1)):
            attributes['overwrite'] = 'replace'

        mode = None
        if True or bool(random.getrandbits(1)):
            mode = random_permissions()
            attributes['mode'] = mode

        bootstrap.ztps.set_definition_response(
            actions=[{'action' : 'startup_config_action'},
                     {'action' : 'test_action',
                      'attributes' : attributes}])

        bootstrap.ztps.set_action_response(
            'startup_config_action', startup_config_action())

        action = get_action('copy_file')

        # Make the destinaton persistent
        action = action.replace('PERSISTENT_STORAGE = [',
                                'PERSISTENT_STORAGE = [\'%s\', ' %
                                destination)
        bootstrap.ztps.set_action_response('test_action',
                                           action)

        contents = random_string()
        bootstrap.ztps.set_file_response(source, contents)

        destination_path = '%s/%s' % (destination, source)
        bootstrap.start_test()

        try:
            self.failUnless(os.path.isfile(destination_path))
            self.failUnless([contents] ==
                            file_log(destination_path))
            self.failIf(os.path.isfile(bootstrap.rc_eos))
            if mode:
                self.failUnless(mode ==
                                oct(os.stat(destination_path)[ST_MODE])[-3:])
            self.failUnless(bootstrap.success())
        except AssertionError as assertion:
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise_exception(assertion)
        finally:
            remove_file(destination_path)
            shutil.rmtree(destination)
            bootstrap.end_test()
Example #26
0
    def test_replace(self):
        bootstrap = Bootstrap(ztps_default_config=True)

        source = random_string()
        destination = '/tmp/%s' % random_string()

        url = 'http://%s/%s' % (bootstrap.server, source)
        attributes = {'src_url': url, 'dst_url': destination}

        # 'replace' is the default
        if bool(random.getrandbits(1)):
            attributes['overwrite'] = 'replace'

        mode = None
        if True or bool(random.getrandbits(1)):
            mode = random_permissions()
            attributes['mode'] = mode

        bootstrap.ztps.set_definition_response(
            actions=[{
                'action': 'startup_config_action'
            }, {
                'action': 'test_action',
                'attributes': attributes
            }])

        bootstrap.ztps.set_action_response('startup_config_action',
                                           startup_config_action())

        action = get_action('copy_file')

        # Make the destinaton persistent
        action = action.replace('PERSISTENT_STORAGE = [',
                                'PERSISTENT_STORAGE = [\'%s\', ' % destination)
        bootstrap.ztps.set_action_response('test_action', action)

        contents = random_string()
        bootstrap.ztps.set_file_response(source, contents)

        destination_path = '%s/%s' % (destination, source)
        bootstrap.start_test()

        try:
            self.failUnless(os.path.isfile(destination_path))
            self.failUnless([contents] == file_log(destination_path))
            self.failIf(os.path.isfile(bootstrap.rc_eos))
            if mode:
                self.failUnless(
                    mode == oct(os.stat(destination_path)[ST_MODE])[-3:])
            self.failUnless(bootstrap.success())
        except AssertionError as assertion:
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise_exception(assertion)
        finally:
            remove_file(destination_path)
            shutil.rmtree(destination)
            bootstrap.end_test()
Example #27
0
    def test_keep_backup(self):
        bootstrap = Bootstrap(ztps_default_config=True)

        source = random_string()
        destination = random_string()

        url = 'http://%s/%s' % (bootstrap.server, source)
        bootstrap.ztps.set_definition_response(
            actions=[{
                'action': 'startup_config_action'
            }, {
                'action': 'test_action',
                'attributes': {
                    'src_url': url,
                    'dst_url': destination,
                    'overwrite': 'backup'
                }
            }])

        bootstrap.ztps.set_action_response('startup_config_action',
                                           startup_config_action())

        persistent_dir = '/tmp'
        action = get_action('copy_file')
        action = action.replace('/mnt/flash/.ztp-files', persistent_dir)
        bootstrap.ztps.set_action_response('test_action', action)

        contents = random_string()
        bootstrap.ztps.set_file_response(source, contents)

        bootstrap.start_test()

        destination_path = '%s/%s' % (persistent_dir, source)
        try:
            self.failUnless(os.path.isfile(destination_path))
            self.failUnless([contents] == file_log(destination_path))

            self.failUnless(os.path.isfile(bootstrap.rc_eos))
            log = file_log(bootstrap.rc_eos)
            self.failUnless('#!/bin/bash' in log)
            self.failUnless('sudo cp %s %s' %
                            (destination_path, destination) in log)
            self.failUnless('[ -f %s ] && sudo mv %s %s.backup' %
                            (destination, destination, destination) in log)
            self.failUnless(bootstrap.success())
        except AssertionError as assertion:
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise_exception(assertion)
        finally:
            remove_file(destination_path)
            bootstrap.end_test()
    def test_keep_backup(self):
        bootstrap = Bootstrap(ztps_default_config=True)

        source = random_string()
        destination = random_string()

        url = 'http://%s/%s' % (bootstrap.server, source)
        bootstrap.ztps.set_definition_response(
            actions=[{'action' : 'startup_config_action'},
                     {'action' : 'test_action',
                      'attributes' : {'src_url' : url,
                                      'dst_url' : destination,
                                      'overwrite' : 'backup'}}])

        bootstrap.ztps.set_action_response(
            'startup_config_action', startup_config_action())

        persistent_dir = '/tmp'
        action = get_action('copy_file')
        action = action.replace('/mnt/flash/.ztp-files',
                                persistent_dir)
        bootstrap.ztps.set_action_response('test_action',
                                           action)

        contents = random_string()
        bootstrap.ztps.set_file_response(source, contents)

        bootstrap.start_test()

        destination_path = '%s/%s' % (persistent_dir, source)
        try:
            self.failUnless(os.path.isfile(destination_path))
            self.failUnless([contents] ==
                            file_log(destination_path))

            self.failUnless(os.path.isfile(bootstrap.rc_eos))
            log = file_log(bootstrap.rc_eos)
            self.failUnless('#!/bin/bash' in log)
            self.failUnless('sudo cp %s %s' %
                            (destination_path, destination) in log)
            self.failUnless('[ -f %s ] && sudo mv %s %s.backup' %
                            (destination, destination,
                             destination) in log)
            self.failUnless(bootstrap.success())
        except AssertionError as assertion:
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise_exception(assertion)
        finally:
            remove_file(destination_path)
            bootstrap.end_test()
Example #29
0
    def test_success(self):
        bootstrap = Bootstrap(ztps_default_config=True)

        source = random_string()
        destination = '/tmp/%s' % random_string()
        ztps_server = 'http://%s' % bootstrap.server
        url = 'http://%s/%s' % (bootstrap.server, source)

        bootstrap.ztps.set_definition_response(
            actions=[{
                'action': 'startup_config_action'
            }, {
                'action': 'test_action',
                'attributes': {
                    'src_url': url,
                    'dst_url': destination,
                    'ztps_server': ztps_server
                }
            }])

        bootstrap.ztps.set_action_response('startup_config_action',
                                           startup_config_action())

        action = get_action('copy_file')

        # Make the destinaton persistent
        action = action.replace('PERSISTENT_STORAGE = [',
                                'PERSISTENT_STORAGE = [\'%s\', ' % destination)
        bootstrap.ztps.set_action_response('test_action', action)

        contents = random_string()
        bootstrap.ztps.set_file_response(source, contents)

        bootstrap.start_test()

        destination_path = '%s/%s' % (destination, source)

        try:
            self.failUnless(os.path.isfile(destination_path))
            self.failUnless([contents] == file_log(destination_path))
            self.failIf(os.path.isfile(RC_EOS))
            self.failUnless(bootstrap.success())
        except AssertionError as assertion:
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise_exception(assertion)
        finally:
            remove_file(destination_path)
            shutil.rmtree(destination)
            bootstrap.end_test()
Example #30
0
    def test_multi_lines(self):
        bootstrap = Bootstrap(ztps_default_config=True)
        config = random_string()
        url = 'http://%s/%s' % (bootstrap.server, config)
        bootstrap.ztps.set_definition_response(
            actions=[{
                'action': 'startup_config_action'
            }, {
                'action': 'test_action',
                'attributes': {
                    'url': url
                }
            }])
        bootstrap.ztps.set_action_response('test_action',
                                           get_action('add_config'))

        startup_config_lines = [
            random_string(),
            random_string(),
            random_string(),
            random_string()
        ]
        bootstrap.ztps.set_action_response(
            'startup_config_action',
            startup_config_action(lines=startup_config_lines))
        contents = '\n'.join([
            random_string(),
            random_string(),
            random_string(),
            random_string(),
            random_string(),
            random_string()
        ])
        bootstrap.ztps.set_file_response(config, contents)
        bootstrap.start_test()

        try:
            self.failUnless(os.path.isfile(bootstrap.startup_config))
            log = file_log(bootstrap.startup_config)
            all_lines = startup_config_lines + contents.split()
            for line in all_lines:
                self.failUnless(line in log)
            self.failUnless(bootstrap.success())
        except AssertionError as assertion:
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise_exception(assertion)
        finally:
            bootstrap.end_test()
Example #31
0
    def test_ztps_path_success(self):
        bootstrap = Bootstrap(ztps_default_config=True)
        plugin = url = random_string()
        ztps_server = 'http://%s' % bootstrap.server
        bootstrap.ztps.set_definition_response(
            actions=[{
                'action': 'startup_config_action'
            }, {
                'action': 'test_action',
                'attributes': {
                    'url': url,
                    'ztps_server': ztps_server
                }
            }])

        bootstrap.ztps.set_action_response('startup_config_action',
                                           startup_config_action())

        plugin_dir = '/tmp'
        persistent_dir = '/tmp/persistent'
        action = get_action('install_cli_plugin')
        action = action.replace('/usr/lib/python2.7/site-packages/CliPlugin',
                                plugin_dir)

        action = action.replace('/mnt/flash/.ztp-plugins', persistent_dir)
        bootstrap.ztps.set_action_response('test_action', action)

        contents = random_string()
        bootstrap.ztps.set_file_response(plugin, contents)
        bootstrap.start_test()

        try:
            self.failUnless(os.path.isfile(bootstrap.rc_eos))
            log = file_log(bootstrap.rc_eos)
            self.failUnless('#!/bin/bash' in log)
            self.failUnless('sudo cp %s/%s %s' %
                            (persistent_dir, plugin, plugin_dir) in log)

            self.failUnless(contents in file_log('%s/%s' %
                                                 (persistent_dir, plugin)))
            self.failUnless(bootstrap.success())
        except AssertionError as assertion:
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise_exception(assertion)
        finally:
            shutil.rmtree(persistent_dir)
            bootstrap.end_test()
    def test_append_server(self):
        bootstrap = Bootstrap(ztps_default_config=True)

        source = random_string()
        destination = '/tmp/%s' % random_string()
        ztps_server = 'http://%s' % bootstrap.server

        bootstrap.ztps.set_definition_response(
            actions=[{'action' : 'startup_config_action'},
                     {'action' : 'test_action',
                     'attributes' : {'src_url'    : source,
                                     'dst_url'    : destination,
                                     'ztps_server': ztps_server}}])

        bootstrap.ztps.set_action_response(
            'startup_config_action', startup_config_action())

        action = get_action('copy_file')

        # Make the destinaton persistent
        action = action.replace('PERSISTENT_STORAGE = [',
                                'PERSISTENT_STORAGE = [\'%s\', ' %
                                destination)
        bootstrap.ztps.set_action_response('test_action',
                                           action)

        contents = random_string()
        bootstrap.ztps.set_file_response(source, contents)

        bootstrap.start_test()

        destination_path = '%s/%s' % (destination, source)

        try:
            self.failUnless(os.path.isfile(destination_path))
            self.failUnless([contents] ==
                            file_log(destination_path))
            self.failIf(os.path.isfile(bootstrap.rc_eos))
            self.failUnless(bootstrap.success())
        except AssertionError as assertion:
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise_exception(assertion)
        finally:
            remove_file(destination_path)
            shutil.rmtree(destination)
            bootstrap.end_test()
    def test_success(self):
        bootstrap = Bootstrap(ztps_default_config=True)

        source = random_string()
        destination = random_string()

        url = 'http://%s/%s' % (bootstrap.server, source)
        bootstrap.ztps.set_definition_response(
            actions=[{'action' : 'startup_config_action'},
                     {'action' : 'test_action',
                      'attributes' : {'src_url' : url,
                                      'dst_url' : destination}}])

        bootstrap.ztps.set_action_response(
            'startup_config_action', startup_config_action())

        persistent_dir = '/tmp'
        action = get_action('copy_file')
        action = action.replace('/mnt/flash/.ztp-files',
                                persistent_dir)
        bootstrap.ztps.set_action_response('test_action',
                                           action)

        contents = random_string()
        bootstrap.ztps.set_file_response(source, contents)

        bootstrap.start_test()

        destination_path = '%s/%s' % (persistent_dir, source)

        try:
            self.failUnless(os.path.isfile(destination_path))
            self.failUnless([contents] ==
                            file_log(destination_path))

            self.failUnless(os.path.isfile(RC_EOS))
            log = file_log(RC_EOS)
            self.failUnless('#!/bin/bash' in log)
            self.failUnless('sudo cp %s %s' %
                            (destination_path, destination) in log)
            self.failUnless(bootstrap.success())
        except AssertionError:
            raise
        finally:
            remove_file(destination_path)
            bootstrap.end_test()
    def test_ztps_path_success(self):
        bootstrap = Bootstrap(ztps_default_config=True)
        plugin = url = random_string()
        ztps_server = 'http://%s' % bootstrap.server
        bootstrap.ztps.set_definition_response(
            actions=[{'action' : 'startup_config_action'},
                     {'action' : 'test_action',
                      'attributes' : {'url' : url,
                                      'ztps_server': ztps_server}}])

        bootstrap.ztps.set_action_response(
            'startup_config_action', startup_config_action())

        plugin_dir = '/tmp'
        persistent_dir = '/tmp/persistent'
        action = get_action('install_cli_plugin')
        action = action.replace('/usr/lib/python2.7/site-packages/CliPlugin',
                                plugin_dir)

        action = action.replace('/mnt/flash/.ztp-plugins',
                                persistent_dir)
        bootstrap.ztps.set_action_response('test_action',
                                           action)

        contents = random_string()
        bootstrap.ztps.set_file_response(plugin, contents)
        bootstrap.start_test()

        try:
            self.failUnless(os.path.isfile(RC_EOS))
            log = file_log(RC_EOS)
            self.failUnless('#!/bin/bash' in log)
            self.failUnless('sudo cp %s/%s %s' %
                            (persistent_dir, plugin, plugin_dir) in log)

            self.failUnless(contents in
                            file_log('%s/%s' % (persistent_dir, plugin)))
            self.failUnless(bootstrap.success())
        except AssertionError as assertion:
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise_exception(assertion)
        finally:
            shutil.rmtree(persistent_dir)
            bootstrap.end_test()
Example #35
0
    def test_action_success_log(self):
        log = '/tmp/ztps-log-%s-debug' % os.getpid()

        bootstrap = Bootstrap()
        bootstrap.ztps.set_config_response(logging=[
            {
                'destination': 'file:%s' % log,
                'level': 'DEBUG'
            },
        ])
        bootstrap.ztps.set_node_check_response()

        text_onstart = random_string()
        text_onsuccess = random_string()
        text_onfailure = random_string()
        bootstrap.ztps.set_definition_response(
            actions=[{
                'action': 'startup_config_action',
                'onstart': text_onstart,
                'onsuccess': text_onsuccess,
                'onfailure': text_onfailure,
            }])
        bootstrap.ztps.set_action_response('startup_config_action',
                                           startup_config_action())
        bootstrap.start_test()

        try:
            self.failUnless(bootstrap.eapi_node_information_collected())
            self.failUnless(bootstrap.success())
            self.failIf(bootstrap.error)
            log = ''.join(file_log(log))
            self.failUnless('startup_config_action: %s' % text_onstart in log)
            self.failUnless('startup_config_action: %s' %
                            text_onsuccess in log)
            self.failUnless('startup_config_action: %s' %
                            text_onfailure not in log)
        except AssertionError as assertion:
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise_exception(assertion)
        finally:
            bootstrap.end_test()
    def test_success(self):
        bootstrap = Bootstrap()
        bootstrap.ztps.set_config_response()
        bootstrap.ztps.set_node_check_response()
        bootstrap.ztps.set_definition_response(
            actions=[{'action' : 'startup_config_action'}])
        bootstrap.ztps.set_action_response('startup_config_action',
                                           startup_config_action())
        bootstrap.start_test()

        try:
            self.failUnless(bootstrap.eapi_node_information_collected())
            self.failUnless(bootstrap.success())
            self.failIf(bootstrap.error)
        except AssertionError as assertion:
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise assertion
        finally:
            bootstrap.end_test()
    def test_success(self):
        bootstrap = Bootstrap(ztps_default_config=True)
        config = random_string()
        url = config
        bootstrap.ztps.set_definition_response(
            actions=[{'action' : 'test_action',
                      'attributes': {'url' : url}}])
        bootstrap.ztps.set_action_response('test_action',
                                           get_action('replace_config'))
        contents = random_string()
        bootstrap.ztps.set_file_response(config, contents)
        bootstrap.start_test()

        try:
            self.failUnless(os.path.isfile(STARTUP_CONFIG))
            self.failUnless(contents.split() == file_log(STARTUP_CONFIG))
            self.failUnless(bootstrap.success())
        except AssertionError:
            raise
        finally:
            bootstrap.end_test()
    def test_ztps_path_success(self):
        bootstrap = Bootstrap(ztps_default_config=True)
        plugin = url = random_string()
        ztps_server = "http://%s" % bootstrap.server
        bootstrap.ztps.set_definition_response(
            actions=[
                {"action": "startup_config_action"},
                {"action": "test_action", "attributes": {"url": url, "ztps_server": ztps_server}},
            ]
        )

        bootstrap.ztps.set_action_response("startup_config_action", startup_config_action())

        plugin_dir = "/tmp"
        persistent_dir = "/tmp/persistent"
        action = get_action("install_cli_plugin")
        action = action.replace("/usr/lib/python2.7/site-packages/CliPlugin", plugin_dir)

        action = action.replace("/mnt/flash/.ztp-plugins", persistent_dir)
        bootstrap.ztps.set_action_response("test_action", action)

        contents = random_string()
        bootstrap.ztps.set_file_response(plugin, contents)
        bootstrap.start_test()

        try:
            self.failUnless(os.path.isfile(bootstrap.rc_eos))
            log = file_log(bootstrap.rc_eos)
            self.failUnless("#!/bin/bash" in log)
            self.failUnless("sudo cp %s/%s %s" % (persistent_dir, plugin, plugin_dir) in log)

            self.failUnless(contents in file_log("%s/%s" % (persistent_dir, plugin)))
            self.failUnless(bootstrap.success())
        except AssertionError as assertion:
            print "Output: %s" % bootstrap.output
            print "Error: %s" % bootstrap.error
            raise_exception(assertion)
        finally:
            shutil.rmtree(persistent_dir)
            bootstrap.end_test()
    def test_variables(self):
        bootstrap = Bootstrap(ztps_default_config=True)
        config = random_string()
        url = config
        var_dict = {'a': 'A', 'b': 'A', 'xxx': '999', 'dummy': 'DUMMY'}
        bootstrap.ztps.set_definition_response(
            actions=[{
                'action': 'startup_config_action'
            }, {
                'action': 'test_action',
                'attributes': {
                    'url': url,
                    'substitution_mode': 'strict',
                    'variables': var_dict
                }
            }])

        bootstrap.ztps.set_action_response('startup_config_action',
                                           startup_config_action())
        bootstrap.ztps.set_action_response('test_action',
                                           get_action('run_bash_script'))

        print_string = '$a 1234 $b 4 321 $xxx$a'
        contents = '''#!/usr/bin/env bash
echo %s''' % print_string
        bootstrap.ztps.set_file_response(config, contents)
        bootstrap.start_test()

        expected_contents = Template(print_string).substitute(var_dict)

        try:
            self.failUnless(os.path.isfile(bootstrap.startup_config))
            self.failUnless(expected_contents in bootstrap.output)
            self.failUnless(bootstrap.success())
        except AssertionError as assertion:
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise_exception(assertion)
        finally:
            bootstrap.end_test()
    def test_no_op(self):
        bootstrap = Bootstrap(ztps_default_config=True)
        version = random_string()
        bootstrap.eapi.version = version
        bootstrap.ztps.set_definition_response(
            actions=[{'action' : 'test_action',
                      'attributes': {
                        'url' : random_string(),
                        'version' : version}},
                     {'action' :'startup_config_action'}])
        bootstrap.ztps.set_action_response('test_action',
                                           get_action('install_image'))
        bootstrap.ztps.set_action_response('startup_config_action',
                                           startup_config_action())
        bootstrap.start_test()

        try:
            self.failUnless(bootstrap.success())
        except AssertionError:
            raise
        finally:
            bootstrap.end_test()
Example #41
0
    def test_success(self):
        bootstrap = Bootstrap()
        bootstrap.ztps.set_config_response()
        bootstrap.ztps.set_node_check_response()
        bootstrap.ztps.set_definition_response(
            actions=[{
                'action': 'startup_config_action'
            }])
        bootstrap.ztps.set_action_response('startup_config_action',
                                           startup_config_action())
        bootstrap.start_test()

        try:
            self.failUnless(bootstrap.eapi_node_information_collected())
            self.failUnless(bootstrap.success())
            self.failIf(bootstrap.error)
        except AssertionError as assertion:
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise_exception(assertion)
        finally:
            bootstrap.end_test()
    def test_success(self):
        bootstrap = Bootstrap()

        startup_config = random_string()

        bootstrap.ztps.set_config_response()
        bootstrap.ztps.set_node_check_response()
        bootstrap.ztps.set_definition_response(
            actions=[{'action' : 'test_action'}])
        bootstrap.ztps.set_action_response(
            'test_action',
            startup_config_action([startup_config]))

        open(bootstrap.rc_eos, 'w').write(random_string())
        open(bootstrap.startup_config, 'w').write(startup_config + 
                                                  random_string())
        open(bootstrap.boot_extensions, 'w').write(random_string())
        os.mkdir(bootstrap.boot_extensions_folder)
        open('%s/my_extension' % 
             bootstrap.boot_extensions_folder, 'w').write(random_string())

        bootstrap.start_test()

        try:
            self.failUnless(bootstrap.eapi_node_information_collected())
            self.failUnless(bootstrap.success())
            self.failIf(bootstrap.error)

            self.failIf(os.path.isfile(bootstrap.rc_eos))
            self.failUnless(open(bootstrap.startup_config).read() ==
                           startup_config)
            self.failIf(os.path.isfile(bootstrap.boot_extensions))
            self.failIf(os.path.isdir(bootstrap.boot_extensions_folder))
        except AssertionError as assertion:
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise_exception(assertion)
        finally:
            bootstrap.end_test()
    def test_variables(self):
        bootstrap = Bootstrap(ztps_default_config=True)
        config = random_string()
        url = config
        var_dict = { 'a' : 'A',
                     'b' : 'A',
                     'xxx' : '999',
                     'dummy': 'DUMMY'}
        bootstrap.ztps.set_definition_response(
            actions=[{'action' : 'startup_config_action'},
                     {'action' : 'test_action',
                      'attributes': {'url' : url,
                                     'substitution_mode': 'strict',
                                     'variables': var_dict}}])

        bootstrap.ztps.set_action_response(
            'startup_config_action', startup_config_action())
        bootstrap.ztps.set_action_response('test_action',
                                           get_action('run_bash_script'))

        print_string = '$a 1234 $b 4 321 $xxx$a'
        contents = '''#!/usr/bin/env bash
echo %s''' % print_string
        bootstrap.ztps.set_file_response(config, contents)
        bootstrap.start_test()

        expected_contents = Template(print_string).substitute(var_dict)

        try:
            self.failUnless(os.path.isfile(bootstrap.startup_config))
            self.failUnless(expected_contents in bootstrap.output)
            self.failUnless(bootstrap.success())
        except AssertionError as assertion:
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise_exception(assertion)
        finally:
            bootstrap.end_test()
Example #44
0
    def test_url_success(self):
        bootstrap = Bootstrap(ztps_default_config=True)
        config = random_string()
        url = 'http://%s/%s' % (bootstrap.server, config)
        bootstrap.ztps.set_definition_response(
            actions=[{'action' : 'test_action',
                      'attributes': {'url' : url}}])
        bootstrap.ztps.set_action_response('test_action',
                                           get_action('replace_config'))
        contents = random_string()
        bootstrap.ztps.set_file_response(config, contents)
        bootstrap.start_test()

        try:
            self.failUnless(os.path.isfile(STARTUP_CONFIG))
            self.failUnless(contents.split() == file_log(STARTUP_CONFIG))
            self.failUnless(bootstrap.success())
        except AssertionError as assertion:
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise_exception(assertion)
        finally:
            bootstrap.end_test()
    def test_action_success_log(self):
        log = '/tmp/ztps-log-%s-debug' % os.getpid()

        bootstrap = Bootstrap()
        bootstrap.ztps.set_config_response(logging=[
                {'destination' : 'file:%s' % log,
                 'level' : 'DEBUG'},])
        bootstrap.ztps.set_node_check_response()

        text_onstart = random_string()
        text_onsuccess = random_string()
        text_onfailure = random_string()
        bootstrap.ztps.set_definition_response(
            actions=[{'action' : 'startup_config_action',
                      'onstart' : text_onstart,
                      'onsuccess' : text_onsuccess,
                      'onfailure' : text_onfailure,
                      }])
        bootstrap.ztps.set_action_response('startup_config_action',
                                           startup_config_action())
        bootstrap.start_test()

        try:
            self.failUnless(bootstrap.eapi_node_information_collected())
            self.failUnless(bootstrap.success())
            self.failIf(bootstrap.error)
            log = ''.join(file_log(log))
            self.failUnless('startup_config_action:%s' % text_onstart in log)
            self.failUnless('startup_config_action:%s' % text_onsuccess in log)
            self.failUnless('startup_config_action:%s' % 
                            text_onfailure not in log)
        except AssertionError as assertion:
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise assertion
        finally:
            bootstrap.end_test()
Example #46
0
    def test_append(self):
        bootstrap = Bootstrap(ztps_default_config=True)
        config = random_string()
        url = 'http://%s/%s' % (bootstrap.server, config)
        bootstrap.ztps.set_definition_response(
            actions=[{
                'action': 'startup_config_action'
            }, {
                'action': 'test_action',
                'attributes': {
                    'url': url
                }
            }])
        bootstrap.ztps.set_action_response('test_action',
                                           get_action('add_config'))

        startup_config_text = random_string()
        bootstrap.ztps.set_action_response(
            'startup_config_action',
            startup_config_action(lines=[startup_config_text]))
        contents = random_string()
        bootstrap.ztps.set_file_response(config, contents)
        bootstrap.start_test()

        try:
            self.failUnless(os.path.isfile(STARTUP_CONFIG))
            log = file_log(STARTUP_CONFIG)
            self.failUnless(contents in log)
            self.failUnless(startup_config_text in log)
            self.failUnless(bootstrap.success())
        except AssertionError as assertion:
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise_exception(assertion)
        finally:
            bootstrap.end_test()
Example #47
0
    def test_success(self):
        bootstrap = Bootstrap(ztps_default_config=True)
        version = random_string()
        image = random_string()
        url = 'http://%s/%s' % (bootstrap.server, image)
        bootstrap.ztps.set_definition_response(
            actions=[{
                'action': 'test_action',
                'attributes': {
                    'url': url,
                    'version': version
                }
            }, {
                'action': 'startup_config_action'
            }])

        action = get_action('install_image')
        bootstrap.ztps.set_action_response('test_action', action)
        bootstrap.ztps.set_action_response('startup_config_action',
                                           startup_config_action())
        bootstrap.ztps.set_file_response(image, print_action())
        bootstrap.start_test()

        image_file = '%s/EOS-%s.swi' % (bootstrap.flash, version)
        try:
            self.failUnless(os.path.isfile(image_file))
            self.failUnless(bootstrap.success())
            self.failUnless(
                eapi_log()[-1] == 'install source flash:EOS-%s.swi' % version)
        except AssertionError as assertion:
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise_exception(assertion)
        finally:
            remove_file(image_file)
            bootstrap.end_test()
Example #48
0
    def test_duplicate_actions(self):
        bootstrap = Bootstrap()
        bootstrap.ztps.set_config_response()
        bootstrap.ztps.set_node_check_response()
        bootstrap.ztps.set_definition_response(
            actions=[{
                'action': 'startup_config_action'
            }, {
                'action': 'print_action'
            }, {
                'action': 'print_action'
            }])
        bootstrap.ztps.set_action_response('startup_config_action',
                                           startup_config_action())

        text = random_string()
        bootstrap.ztps.set_action_response('print_action', print_action(text))
        bootstrap.start_test()

        try:
            self.failUnless(bootstrap.eapi_node_information_collected())
            self.failUnless(bootstrap.success())
            self.failUnless(
                bootstrap.output.count('Downloading action '
                                       'print_action') == 1)
            self.failUnless(
                bootstrap.output.count('Executing action '
                                       'print_action') == 2)
            self.failUnless(bootstrap.output.count(text) == 2)
            self.failIf(bootstrap.error)
        except AssertionError as assertion:
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise_exception(assertion)
        finally:
            bootstrap.end_test()
    def test_success(self):
        bootstrap = Bootstrap(ztps_default_config=True)

        extension = random_string()
        url = extension

        extension_force = random_string()
        url_force = 'http://%s/%s' % (bootstrap.server, extension_force)

        bootstrap.ztps.set_definition_response(
            actions=[{
                'action': 'startup_config_action'
            }, {
                'action': 'test_action',
                'attributes': {
                    'url': url
                }
            }, {
                'action': 'test_action_force',
                'attributes': {
                    'url': url_force,
                    'force': True
                }
            }])

        bootstrap.ztps.set_action_response('startup_config_action',
                                           startup_config_action())

        extensions_dir = '/tmp/extensions'
        boot_extensions = '/tmp/boot-extensions'
        action = get_action('install_extension')
        action = action.replace('/mnt/flash/.extensions', extensions_dir)
        action = action.replace('/mnt/flash/boot-extensions', boot_extensions)

        bootstrap.ztps.set_action_response('test_action', action)
        contents = random_string()
        bootstrap.ztps.set_file_response(extension, contents)

        bootstrap.ztps.set_action_response('test_action_force', action)
        contents_force = random_string()
        bootstrap.ztps.set_file_response(extension_force, contents_force)

        bootstrap.start_test()

        try:
            ext_filename = '%s/%s' % (extensions_dir, extension)
            self.failUnless(os.path.isfile(ext_filename))
            self.failUnless([contents] == file_log(ext_filename))
            self.failUnless(extension in file_log(boot_extensions))

            ext_filename_force = '%s/%s' % (extensions_dir, extension_force)
            self.failUnless(os.path.isfile(ext_filename_force))

            self.failUnless([contents_force] == file_log(ext_filename_force))
            self.failUnless('%s force' %
                            extension_force in file_log(boot_extensions))

            self.failUnless(bootstrap.success())
        except AssertionError as assertion:
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise_exception(assertion)
        finally:
            shutil.rmtree(extensions_dir)
            remove_file(boot_extensions)
            bootstrap.end_test()
    def test_success(self):
        bootstrap = Bootstrap(ztps_default_config=True)

        extension = random_string()
        url = extension

        extension_force = random_string()
        url_force = 'http://%s/%s' % (bootstrap.server, extension_force)

        bootstrap.ztps.set_definition_response(
            actions=[{'action' : 'startup_config_action'},
                     {'action' : 'test_action',
                      'attributes' : {'url' : url}},
                     {'action' : 'test_action_force',
                      'attributes' :
                      {'url' : url_force,
                       'force' : True}}
                     ])

        bootstrap.ztps.set_action_response(
            'startup_config_action', startup_config_action())

        extensions_dir = '/tmp/extensions'
        boot_extensions = '/tmp/boot-extensions'
        action = get_action('install_extension')
        action = action.replace('/mnt/flash/.extensions',
                                extensions_dir)
        action = action.replace('/mnt/flash/boot-extensions',
                                boot_extensions)

        bootstrap.ztps.set_action_response('test_action',
                                           action)
        contents = random_string()
        bootstrap.ztps.set_file_response(extension, contents)

        bootstrap.ztps.set_action_response('test_action_force',
                                           action)
        contents_force = random_string()
        bootstrap.ztps.set_file_response(extension_force, contents_force)

        bootstrap.start_test()

        try:
            ext_filename = '%s/%s' % (extensions_dir, extension)
            self.failUnless(os.path.isfile(ext_filename))
            self.failUnless([contents] ==
                            file_log(ext_filename))
            self.failUnless(extension in file_log(boot_extensions))

            ext_filename_force = '%s/%s' % (extensions_dir, extension_force)
            self.failUnless(os.path.isfile(ext_filename_force))

            self.failUnless([contents_force] ==
                            file_log(ext_filename_force))
            self.failUnless('%s force' % extension_force in
                            file_log(boot_extensions))

            self.failUnless(bootstrap.success())
        except AssertionError as assertion:
            print 'Output: %s' % bootstrap.output
            print 'Error: %s' % bootstrap.error
            raise_exception(assertion)
        finally:
            shutil.rmtree(extensions_dir)
            remove_file(boot_extensions)
            bootstrap.end_test()