Example #1
0
 def setUp(self):
     super(SecondaryConnections, self).setUp()
     self.factory = YamlSshFactory()
     jinja_template_path(system=False)
     self.device_type = self.factory.make_device_type()
     self.conf = {
         'arch': 'amd64',
         'extends': 'qemu.yaml',
         'mac_addr': '52:54:00:12:34:59',
         'memory': '256',
     }
Example #2
0
 def setUp(self):
     super(SecondaryConnections, self).setUp()
     self.factory = YamlSshFactory()
     jinja_template_path(system=False)
     self.device_type = self.factory.make_device_type()
     self.conf = {
         'arch': 'amd64',
         'extends': 'qemu.jinja2',
         'mac_addr': '52:54:00:12:34:59',
         'memory': '256',
     }
Example #3
0
 def setUp(self):
     super(PipelineDeviceTags, self).setUp()
     self.factory = YamlFactory()
     jinja_template_path(system=False)
     self.device_type = self.factory.make_device_type()
     self.conf = {
         'arch': 'amd64',
         'extends': 'qemu.yaml',
         'mac_addr': '52:54:00:12:34:59',
         'memory': '256',
     }
Example #4
0
 def setUp(self):
     super(MasterTest, self).setUp()
     self.factory = YamlFactory()
     jinja_template_path(system=False)
     self.device_type = self.factory.make_device_type()
     self.conf = {
         'arch': 'amd64',
         'extends': 'qemu.jinja2',
         'mac_addr': '52:54:00:12:34:59',
         'memory': '256',
     }
     self.worker, _ = Worker.objects.get_or_create(hostname='localhost')
     self.remote, _ = Worker.objects.get_or_create(hostname='remote')
     # exclude remote from the list
     self.dispatchers = [self.worker.hostname]
Example #5
0
    def test_jinja_string_templates(self):
        jinja2_path = jinja_template_path(system=False)
        self.assertTrue(os.path.exists(jinja2_path))
        device_dictionary = {
            'usb_label': 'SanDisk_Ultra',
            'sata_label': 'ST160LM003',
            'usb_uuid': "usb-SanDisk_Ultra_20060775320F43006019-0:0",
            'sata_uuid': "ata-ST160LM003_HN-M160MBB_S2SYJ9KC102184",
            'connection_command': 'telnet localhost 6002',
            'console_device': 'ttyfake1',
            'baud_rate': 56
        }
        data = devicedictionary_to_jinja2(device_dictionary, 'cubietruck.jinja2')
        template = prepare_jinja_template('cubie', data, system_path=False, path=jinja2_path)
        device_configuration = template.render()
        yaml_data = yaml.load(device_configuration)
        self.assertTrue(validate_device(yaml_data))
        self.assertIn('timeouts', yaml_data)
        self.assertIn('parameters', yaml_data)
        self.assertIn('bootz', yaml_data['parameters'])
        self.assertIn('media', yaml_data['parameters'])
        self.assertIn('usb', yaml_data['parameters']['media'])
        self.assertIn(device_dictionary['usb_label'], yaml_data['parameters']['media']['usb'])
        self.assertIn('uuid', yaml_data['parameters']['media']['usb'][device_dictionary['usb_label']])
        self.assertEqual(
            yaml_data['parameters']['media']['usb'][device_dictionary['usb_label']]['uuid'],
            device_dictionary['usb_uuid']
        )
        self.assertIn('commands', yaml_data)
        self.assertIn('connect', yaml_data['commands'])
        self.assertEqual(
            device_dictionary['connection_command'],
            yaml_data['commands']['connect'])
        ramdisk_args = yaml_data['actions']['boot']['methods']['u-boot']['ramdisk']
        self.assertIn('commands', ramdisk_args)
        self.assertIn('boot', ramdisk_args['commands'])
        self.assertIn(
            "setenv bootargs 'console=ttyfake1,56n8 root=/dev/ram0  ip=dhcp'",
            ramdisk_args['commands'])

        device_dictionary.update(
            {
                'hard_reset_command': "/usr/bin/pduclient --daemon localhost --hostname pdu --command reboot --port 08",
                'power_off_command': "/usr/bin/pduclient --daemon localhost --hostname pdu --command off --port 08",
                'power_on_command': "/usr/bin/pduclient --daemon localhost --hostname pdu --command on --port 08"
            }
        )

        data = devicedictionary_to_jinja2(device_dictionary, 'beaglebone-black.jinja2')
        template = prepare_jinja_template('bbb', data, system_path=False, path=jinja2_path)
        device_configuration = template.render()
        yaml_data = yaml.load(device_configuration)
        self.assertTrue(validate_device(yaml_data))
        device = PipelineDevice(yaml_data, 'bbb')
        self.assertIn('power_state', device)
        # bbb has power_on_command defined above
        self.assertEqual(device.power_state, 'off')
        self.assertTrue(hasattr(device, 'power_state'))
        self.assertFalse(hasattr(device, 'hostname'))
        self.assertIn('hostname', device)
Example #6
0
    def test_jinja_string_templates(self):
        jinja2_path = jinja_template_path(system=False)
        self.assertTrue(os.path.exists(jinja2_path))
        device_dictionary = {
            'usb_label': 'SanDisk_Ultra',
            'sata_label': 'ST160LM003',
            'usb_uuid': "usb-SanDisk_Ultra_20060775320F43006019-0:0",
            'sata_uuid': "ata-ST160LM003_HN-M160MBB_S2SYJ9KC102184",
            'connection_command': 'telnet localhost 6002',
            'console_device': 'ttyfake1',
            'baud_rate': 56
        }
        data = devicedictionary_to_jinja2(device_dictionary, 'cubietruck.jinja2')
        template = prepare_jinja_template('cubie', data, system_path=False, path=jinja2_path)
        device_configuration = template.render()
        yaml_data = yaml.load(device_configuration)
        self.assertTrue(validate_device(yaml_data))
        self.assertIn('timeouts', yaml_data)
        self.assertIn('parameters', yaml_data)
        self.assertIn('bootz', yaml_data['parameters'])
        self.assertIn('media', yaml_data['parameters'])
        self.assertIn('usb', yaml_data['parameters']['media'])
        self.assertIn(device_dictionary['usb_label'], yaml_data['parameters']['media']['usb'])
        self.assertIn('uuid', yaml_data['parameters']['media']['usb'][device_dictionary['usb_label']])
        self.assertEqual(
            yaml_data['parameters']['media']['usb'][device_dictionary['usb_label']]['uuid'],
            device_dictionary['usb_uuid']
        )
        self.assertIn('commands', yaml_data)
        self.assertIn('connect', yaml_data['commands'])
        self.assertEqual(
            device_dictionary['connection_command'],
            yaml_data['commands']['connect'])
        ramdisk_args = yaml_data['actions']['boot']['methods']['u-boot']['ramdisk']
        self.assertIn('commands', ramdisk_args)
        self.assertIn('boot', ramdisk_args['commands'])
        self.assertIn(
            "setenv bootargs 'console=ttyfake1,56n8 root=/dev/ram0  ip=dhcp'",
            ramdisk_args['commands'])

        device_dictionary.update(
            {
                'hard_reset_command': "/usr/bin/pduclient --daemon localhost --hostname pdu --command reboot --port 08",
                'power_off_command': "/usr/bin/pduclient --daemon localhost --hostname pdu --command off --port 08",
                'power_on_command': "/usr/bin/pduclient --daemon localhost --hostname pdu --command on --port 08"
            }
        )

        data = devicedictionary_to_jinja2(device_dictionary, 'beaglebone-black.jinja2')
        template = prepare_jinja_template('bbb', data, system_path=False, path=jinja2_path)
        device_configuration = template.render()
        yaml_data = yaml.load(device_configuration)
        self.assertTrue(validate_device(yaml_data))
        device = PipelineDevice(yaml_data, 'bbb')
        self.assertIn('power_state', device)
        # bbb has power_on_command defined above
        self.assertEqual(device.power_state, 'off')
        self.assertTrue(hasattr(device, 'power_state'))
        self.assertFalse(hasattr(device, 'hostname'))
        self.assertIn('hostname', device)
Example #7
0
 def test_device_type_parser(self):
     jinja2_path = jinja_template_path(system=False)
     self.assertTrue(os.path.exists(jinja2_path))
     data = load_devicetype_template('beaglebone-black', system_path=False)
     self.assertIsNotNone(data)
     self.assertIn('actions', data)
     self.assertIn('deploy', data['actions'])
     self.assertIn('boot', data['actions'])
Example #8
0
 def test_device_type_parser(self):
     jinja2_path = jinja_template_path(system=False)
     self.assertTrue(os.path.exists(jinja2_path))
     data = load_devicetype_template('beaglebone-black', system_path=False)
     self.assertIsNotNone(data)
     self.assertIn('actions', data)
     self.assertIn('deploy', data['actions'])
     self.assertIn('boot', data['actions'])
Example #9
0
 def setUp(self):
     super(TestPipelineMenu, self).setUp()
     self.factory = YamlMenuFactory()
     self.jinja_path = jinja_template_path(system=False)
     self.device_type = self.factory.make_device_type(name='mustang-uefi')
     self.conf = {
         'extends': 'mustang-uefi.jinja2',
         'tftp_mac': '52:54:00:12:34:59',
     }
Example #10
0
 def setUp(self):
     super(TestPipelineMenu, self).setUp()
     self.factory = YamlMenuFactory()
     self.jinja_path = jinja_template_path(system=False)
     self.device_type = self.factory.make_device_type(name='mustang-uefi')
     self.conf = {
         'extends': 'mustang-uefi.yaml',
         'tftp_mac': '52:54:00:12:34:59',
     }
Example #11
0
    def test_jinja_postgres_loader(self):
        # path used for the device_type template
        jinja2_path = jinja_template_path(system=False)
        self.assertTrue(os.path.exists(jinja2_path))
        device_type = 'cubietruck'
        # pretend this was already imported into the database and use for comparison later.
        device_dictionary = {
            'usb_label': 'SanDisk_Ultra',
            'sata_label': 'ST160LM003',
            'usb_uuid': "usb-SanDisk_Ultra_20060775320F43006019-0:0",
            'sata_uuid': "ata-ST160LM003_HN-M160MBB_S2SYJ9KC102184",
            'connection_command': 'telnet localhost 6002'
        }

        # create a DeviceDictionary for this test
        cubie = DeviceDictionary(hostname='cubie')
        cubie.parameters = device_dictionary
        cubie.save()
        jinja_data = devicedictionary_to_jinja2(cubie.parameters, '%s.jinja2' % device_type)
        dict_loader = jinja2.DictLoader({'cubie.jinja2': jinja_data})
        type_loader = jinja2.FileSystemLoader([os.path.join(jinja2_path, 'device-types')])
        env = jinja2.Environment(
            loader=jinja2.ChoiceLoader([dict_loader, type_loader]),
            trim_blocks=True)
        template = env.get_template("%s.jinja2" % 'cubie')
        # pylint gets this wrong from jinja
        device_configuration = template.render()  # pylint: disable=no-member

        chk_template = prepare_jinja_template('cubie', jinja_data, system_path=False, path=jinja2_path)
        self.assertEqual(template.render(), chk_template.render())  # pylint: disable=no-member
        yaml_data = yaml.load(device_configuration)
        self.assertTrue(validate_device(yaml_data))
        self.assertIn('timeouts', yaml_data)
        self.assertIn('parameters', yaml_data)
        self.assertIn('bootz', yaml_data['parameters'])
        self.assertIn('media', yaml_data['parameters'])
        self.assertIn('usb', yaml_data['parameters']['media'])
        self.assertIn(device_dictionary['usb_label'], yaml_data['parameters']['media']['usb'])
        self.assertIn('uuid', yaml_data['parameters']['media']['usb'][device_dictionary['usb_label']])
        self.assertEqual(
            yaml_data['parameters']['media']['usb'][device_dictionary['usb_label']]['uuid'],
            device_dictionary['usb_uuid']
        )
        self.assertIn('commands', yaml_data)
        self.assertIn('connect', yaml_data['commands'])
        self.assertEqual(
            device_dictionary['connection_command'],
            yaml_data['commands']['connect'])
        device = PipelineDevice(yaml_data, 'cubie')
        self.assertIn('power_state', device)
        # cubie1 has no power_on_command defined
        self.assertEqual(device.power_state, '')
        self.assertTrue(hasattr(device, 'power_state'))
        self.assertFalse(hasattr(device, 'hostname'))
        self.assertIn('hostname', device)
Example #12
0
    def test_jinja_postgres_loader(self):
        # path used for the device_type template
        jinja2_path = jinja_template_path(system=False)
        self.assertTrue(os.path.exists(jinja2_path))
        device_type = 'cubietruck'
        # pretend this was already imported into the database and use for comparison later.
        device_dictionary = {
            'usb_label': 'SanDisk_Ultra',
            'sata_label': 'ST160LM003',
            'usb_uuid': "usb-SanDisk_Ultra_20060775320F43006019-0:0",
            'sata_uuid': "ata-ST160LM003_HN-M160MBB_S2SYJ9KC102184",
            'connection_command': 'telnet localhost 6002'
        }

        # create a DeviceDictionary for this test
        cubie = DeviceDictionary(hostname='cubie')
        cubie.parameters = device_dictionary
        cubie.save()
        jinja_data = devicedictionary_to_jinja2(cubie.parameters, '%s.jinja2' % device_type)
        dict_loader = jinja2.DictLoader({'cubie.jinja2': jinja_data})
        type_loader = jinja2.FileSystemLoader([os.path.join(jinja2_path, 'device-types')])
        env = jinja2.Environment(
            loader=jinja2.ChoiceLoader([dict_loader, type_loader]),
            trim_blocks=True)
        template = env.get_template("%s.jinja2" % 'cubie')
        # pylint gets this wrong from jinja
        device_configuration = template.render()  # pylint: disable=no-member

        chk_template = prepare_jinja_template('cubie', jinja_data, system_path=False, path=jinja2_path)
        self.assertEqual(template.render(), chk_template.render())  # pylint: disable=no-member
        yaml_data = yaml.load(device_configuration)
        self.assertTrue(validate_device(yaml_data))
        self.assertIn('timeouts', yaml_data)
        self.assertIn('parameters', yaml_data)
        self.assertIn('bootz', yaml_data['parameters'])
        self.assertIn('media', yaml_data['parameters'])
        self.assertIn('usb', yaml_data['parameters']['media'])
        self.assertIn(device_dictionary['usb_label'], yaml_data['parameters']['media']['usb'])
        self.assertIn('uuid', yaml_data['parameters']['media']['usb'][device_dictionary['usb_label']])
        self.assertEqual(
            yaml_data['parameters']['media']['usb'][device_dictionary['usb_label']]['uuid'],
            device_dictionary['usb_uuid']
        )
        self.assertIn('commands', yaml_data)
        self.assertIn('connect', yaml_data['commands'])
        self.assertEqual(
            device_dictionary['connection_command'],
            yaml_data['commands']['connect'])
        device = PipelineDevice(yaml_data, 'cubie')
        self.assertIn('power_state', device)
        # cubie1 has no power_on_command defined
        self.assertEqual(device.power_state, '')
        self.assertTrue(hasattr(device, 'power_state'))
        self.assertFalse(hasattr(device, 'hostname'))
        self.assertIn('hostname', device)
Example #13
0
 def setUp(self):
     super(MasterTest, self).setUp()
     self.factory = YamlFactory()
     jinja_template_path(system=False)
     self.device_type = self.factory.make_device_type()
     self.conf = {
         'arch': 'amd64',
         'extends': 'qemu.jinja2',
         'mac_addr': '52:54:00:12:34:59',
         'memory': '256',
     }
     self.worker, _ = Worker.objects.get_or_create(hostname='localhost')
     self.remote, _ = Worker.objects.get_or_create(hostname='remote')
     # exclude remote from the list
     self.dispatchers = [self.worker.hostname]
     logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
     logger = logging.getLogger('unittests')
     logger.disabled = True
     logger.propagate = False
     logger = logging.getLogger('dispatcher')
     logging.disable(logging.DEBUG)
     logger.disabled = True
     logger.propagate = False
Example #14
0
    def get_pipeline_device_config(self, device_hostname):
        """
        Name
        ----
        `get_pipeline_device_config` (`device_hostname`)

        Description
        -----------
        Get the pipeline device configuration for given device hostname.

        Arguments
        ---------
        `device_hostname`: string
            Device hostname for which the configuration is required.

        Return value
        ------------
        This function returns an XML-RPC binary data of output file.
        """
        if not device_hostname:
            raise xmlrpclib.Fault(
                400, "Bad request: Device hostname was not "
                "specified.")

        element = DeviceDictionary.get(device_hostname)
        if element is None:
            raise xmlrpclib.Fault(404, "Specified device not found.")

        data = devicedictionary_to_jinja2(element.parameters,
                                          element.parameters['extends'])
        string_loader = jinja2.DictLoader({'%s.yaml' % device_hostname: data})
        type_loader = jinja2.FileSystemLoader(
            [os.path.join(jinja_template_path(), 'device-types')])
        env = jinja2.Environment(loader=jinja2.ChoiceLoader(
            [string_loader, type_loader]),
                                 trim_blocks=True)
        template = env.get_template("%s.yaml" % device_hostname)
        device_configuration = template.render()

        # validate against the device schema
        validate_device(device_configuration)

        return xmlrpclib.Binary(device_configuration.encode('UTF-8'))
Example #15
0
    def get_pipeline_device_config(self, device_hostname):
        """
        Name
        ----
        `get_pipeline_device_config` (`device_hostname`)

        Description
        -----------
        Get the pipeline device configuration for given device hostname.

        Arguments
        ---------
        `device_hostname`: string
            Device hostname for which the configuration is required.

        Return value
        ------------
        This function returns an XML-RPC binary data of output file.
        """
        if not device_hostname:
            raise xmlrpclib.Fault(400, "Bad request: Device hostname was not "
                                  "specified.")

        element = DeviceDictionary.get(device_hostname)
        if element is None:
            raise xmlrpclib.Fault(404, "Specified device not found.")

        data = devicedictionary_to_jinja2(element.parameters,
                                          element.parameters['extends'])
        string_loader = jinja2.DictLoader({'%s.yaml' % device_hostname: data})
        type_loader = jinja2.FileSystemLoader(
            [os.path.join(jinja_template_path(), 'device-types')])
        env = jinja2.Environment(loader=jinja2.ChoiceLoader([string_loader,
                                                             type_loader]),
                                 trim_blocks=True)
        template = env.get_template("%s.yaml" % device_hostname)
        device_configuration = template.render()
        return xmlrpclib.Binary(device_configuration.encode('UTF-8'))
Example #16
0
 def test_device_type_templates(self):
     """
     Ensure each template renders valid YAML
     """
     jinja2_path = jinja_template_path(system=False)
     for template_name in os.listdir(os.path.join(jinja2_path, 'device-types')):
         if not template_name.endswith('jinja2'):
             continue
         type_loader = jinja2.FileSystemLoader([os.path.join(jinja2_path, 'device-types')])
         env = jinja2.Environment(
             loader=jinja2.ChoiceLoader([type_loader]),
             trim_blocks=True)
         try:
             template = env.get_template(template_name)
         except jinja2.TemplateNotFound as exc:
             self.fail('%s: %s' % (template_name, exc))
         data = None
         try:
             data = template.render()
             yaml_data = yaml.load(data)
         except yaml.YAMLError as exc:
             print(data)  # for easier debugging - use the online yaml parser
             self.fail("%s: %s" % (template_name, exc))
         self.assertIsInstance(yaml_data, dict)
Example #17
0
 def test_device_type_templates(self):
     """
     Ensure each template renders valid YAML
     """
     jinja2_path = jinja_template_path(system=False)
     for template_name in os.listdir(os.path.join(jinja2_path, 'device-types')):
         if not template_name.endswith('jinja2'):
             continue
         type_loader = jinja2.FileSystemLoader([os.path.join(jinja2_path, 'device-types')])
         env = jinja2.Environment(
             loader=jinja2.ChoiceLoader([type_loader]),
             trim_blocks=True)
         try:
             template = env.get_template(template_name)
         except jinja2.TemplateNotFound as exc:
             self.fail('%s: %s' % (template_name, exc))
         data = None
         try:
             data = template.render()
             yaml_data = yaml.load(data)
         except yaml.YAMLError as exc:
             print(data)  # for easier debugging - use the online yaml parser
             self.fail("%s: %s" % (template_name, exc))
         self.assertIsInstance(yaml_data, dict)