Example #1
0
    def test_rename_variable_description_in_config_template(self):
        """
        test change of the description within a Config Template variable
        :return:
        """
        p = Project("project")
        ct = ConfigTemplate(name="Config Template", project=p)
        ct.update_template_variable("var_1", "first description")
        ct.update_template_variable("var_2", "second description")
        ct.update_template_variable("var_3", "third description")
        tvs = TemplateValueSet("Template", config_template=ct)
        tvs.update_variable_value("var_1", "first value")

        db.session.add_all([p, ct, tvs])
        db.session.commit()

        self.assertTrue(len(ct.variables.all()) == 3 + 1)
        self.assertTrue(len(tvs.values.all()) == 4)

        var_obj = ct.get_template_variable_by_name("var_1")
        var_obj.description = "changed description"
        db.session.add(var_obj)
        db.session.commit()

        self.assertEqual(
            ct.get_template_variable_by_name("var_1").description,
            "changed description")
Example #2
0
    def test_rename_variable_name_in_config_template(self):
        """
        test rename variable function of the Config Template object and the associated template value set
        :return:
        """
        p = Project("project")
        ct = ConfigTemplate(name="Config Template", project=p)
        ct.update_template_variable("var_1", "first description")
        ct.update_template_variable("var_2", "second description")
        ct.update_template_variable("var_3", "third description")
        tvs = TemplateValueSet("Template", config_template=ct)
        tvs.update_variable_value("var_1", "first value")

        db.session.add_all([p, ct, tvs])
        db.session.commit()

        self.assertTrue(len(ct.variables.all()) == 3 + 1)
        self.assertTrue(len(tvs.values.all()) == 4)

        with self.assertRaises(TemplateVariableNotFoundException):
            ct.rename_variable(old_name="var1", new_name="var_1_renamed")

        ct.rename_variable(old_name="var_1", new_name="var_1_renamed")

        self.assertTrue(len(ct.variables.all()) == 3 + 1)
        self.assertTrue(len(tvs.values.all()) == 4)

        self.assertEqual(
            tvs.get_template_value_by_name_as_string("var_1_renamed"),
            "first value")
Example #3
0
    def test_template_value_set_name_constraint(self):
        p = Project("project")
        ct1 = ConfigTemplate("Template A", project=p)
        ct2 = ConfigTemplate("Template B", project=p)
        tvs1 = TemplateValueSet("values 1", ct1)
        tvs2 = TemplateValueSet("values 2", ct1)
        tvs3 = TemplateValueSet("values 3", ct2)
        tvs4 = TemplateValueSet("values 4", ct2)

        db.session.add_all([p, ct1, ct2, tvs1, tvs2, tvs3, tvs4])
        db.session.commit()

        self.assertTrue(len(TemplateValueSet.query.all()) == 4)

        # test integrity check
        with self.assertRaises(IntegrityError):
            failed_tvs = TemplateValueSet("values 2", ct1)
        db.session.rollback()

        failed_tvs = TemplateValueSet("values 2")
        failed_tvs.config_template = ct1
        db.session.add(failed_tvs)
        with self.assertRaises(IntegrityError):
            db.session.commit()
        db.session.rollback()

        failed_tvs.config_template = ct2

        db.session.add(failed_tvs)
        db.session.commit()

        self.assertTrue(len(TemplateValueSet.query.all()) == 5)
Example #4
0
    def test_create_template_value_set(self):
        p = Project("Project")
        # create first TemplateValueSet
        ct1 = ConfigTemplate("ConfigurationTemplate", project=p)
        tvs1 = TemplateValueSet("ValueSet A", ct1)
        tvs2 = TemplateValueSet("ValueSet B", ct1)

        db.session.add_all([p, ct1, tvs1, tvs2])
        db.session.commit()

        # count results
        self.assertTrue(len(TemplateValueSet.query.all()) == 2)
        self.assertTrue(len(ConfigTemplate.query.all()) == 1)

        # create additional TemplateValueSet
        ct2 = ConfigTemplate("another ConfigurationTemplate", project=p)
        tvs3 = TemplateValueSet("ValueSet A", ct2)
        tvs4 = TemplateValueSet("ValueSet B", ct2)

        db.session.add_all([ct2, tvs3, tvs4])
        db.session.commit()

        # count results
        self.assertTrue(len(TemplateValueSet.query.all()) == 4)
        self.assertTrue(len(ConfigTemplate.query.all()) == 2)

        # verify sample ct1
        received_ct1 = ConfigTemplate.query.filter_by(
            name="ConfigurationTemplate").first()

        self.assertEqual(received_ct1, ct1)
        self.assertTrue(len(received_ct1.template_value_sets.all()) == 2)
        self.assertEqual(received_ct1.template_value_sets.first(), tvs1)
Example #5
0
 def _create_test_data(self):
     # create test data
     template_content = "!\nhostname ${hostname}\n!"
     p = Project(name="project")
     ct = ConfigTemplate(name="template",
                         project=p,
                         template_content=template_content)
     tvs1 = TemplateValueSet(hostname="tvs1", config_template=ct)
     tvs2 = TemplateValueSet(hostname="tvs2", config_template=ct)
     tvs3 = TemplateValueSet(hostname="tvs3", config_template=ct)
     tvs4 = TemplateValueSet(hostname="tvs4", config_template=ct)
     db.session.add_all([p, ct, tvs1, tvs2, tvs3, tvs4])
     db.session.commit()
Example #6
0
    def test_template_value_set_copy_variable_function_afterwards(self):
        p = Project("project")
        ct = ConfigTemplate(name="my template",
                            template_content="not a real config",
                            project=p)
        ct.update_template_variable("var 1")
        ct.update_template_variable("var 2")
        ct.update_template_variable("var 3")

        db.session.add_all([p, ct])
        db.session.commit()

        # verify database content
        self.assertTrue(len(ConfigTemplate.query.all()) == 1)
        self.assertTrue(len(TemplateVariable.query.all()) == 3 + 1)

        # create a new template value set without link to a config template
        tvs = TemplateValueSet(hostname="Value Set")

        # define it afterwards (you need to trigger the copy process manually)
        tvs.config_template = ct
        tvs.copy_variables_from_config_template(
        )  # this method will automatically create a 'hostname' variable value

        self.assertTrue(len(TemplateValue.query.all()) == 4)

        self.assertTrue(tvs.is_value_defined("var_1"))
        self.assertTrue(tvs.is_value_defined("var_2"))
        self.assertTrue(tvs.is_value_defined("var_3"))
        self.assertTrue(tvs.is_value_defined("hostname"))
def add_template_value_set(config_template_id):
    """add a new Template Value Set

    :param config_template_id:
    :return:
    """
    parent_config_template = ConfigTemplate.query.filter(ConfigTemplate.id == config_template_id).first_or_404()
    form = TemplateValueSetForm(request.form)

    if form.validate_on_submit():
        try:
            template_value_set = TemplateValueSet(hostname="", config_template=parent_config_template)

            template_value_set.hostname = form.hostname.data
            template_value_set.config_template = parent_config_template
            template_value_set.copy_variables_from_config_template()

            db.session.add(template_value_set)
            db.session.commit()

            flash("Template Value Set successful created", "success")
            return redirect(url_for(
                "edit_template_value_set",
                template_value_set_id=template_value_set.id,
                config_template_id=parent_config_template.id
            ))

        except IntegrityError as ex:
            if "UNIQUE constraint failed" in str(ex):
                msg = "Template Value Set hostname already in use, please use another one"

            else:
                msg = "Template Value Set was not created (unknown error)"
            flash(msg, "error")
            logger.error(msg, exc_info=True)
            db.session.rollback()

        except Exception:
            msg = "Template Value Set was not created (unknown error)"
            logger.error(msg, exc_info=True)
            flash(msg, "error")
            db.session.rollback()

    return render_template(
        "template_value_set/add_template_value_set.html",
        config_template=parent_config_template,
        project=parent_config_template.project,
        form=form
    )
Example #8
0
    def test_template_value_set_delete_cascade_option(self):
        p = Project("project")
        ct1 = ConfigTemplate(name="Config Template", project=p)

        tvs1 = TemplateValueSet(hostname="first script", config_template=ct1)
        tvs2 = TemplateValueSet(hostname="second script", config_template=ct1)

        self.assertEqual(tvs1.update_variable_value("test 1", "value 1"),
                         "test_1")
        self.assertEqual(tvs1.update_variable_value("test 2", "value 2"),
                         "test_2")
        self.assertEqual(tvs1.update_variable_value("test 3", "value 3"),
                         "test_3")
        self.assertEqual(
            tvs2.update_variable_value("other test 1", "other value 1"),
            "other_test_1")
        self.assertEqual(
            tvs2.update_variable_value("other test 2", "other value 2"),
            "other_test_2")
        self.assertEqual(
            tvs2.update_variable_value("other test 3", "other value 3"),
            "other_test_3")

        db.session.add_all([p, tvs1, tvs2, ct1])
        db.session.commit()

        # test cascade option when deleting objects
        self.assertTrue(
            len(TemplateValue.query.all()) == 6 + 2,
            len(TemplateValue.query.all()))
        self.assertTrue(
            len(TemplateValueSet.query.all()) == 2,
            len(TemplateValueSet.query.all()))
        db.session.delete(tvs1)
        self.assertTrue(len(ConfigTemplate.query.all()) == 1)
        self.assertTrue(
            len(TemplateValue.query.all()) == 4,
            len(TemplateValue.query.all()))
        self.assertTrue(
            len(TemplateValueSet.query.all()) == 1,
            len(TemplateValueSet.query.all()))
        db.session.delete(tvs2)
        self.assertTrue(len(ConfigTemplate.query.all()) == 1)
        self.assertTrue(
            len(TemplateValue.query.all()) == 0,
            len(TemplateValue.query.all()))
        self.assertTrue(
            len(TemplateValueSet.query.all()) == 0,
            len(TemplateValueSet.query.all()))
Example #9
0
    def test_unique_constraint_for_template_value_name_in_template_value_set(
            self):
        var2_name = "var2"
        var2_value = "another value"
        p = Project("project")
        ct = ConfigTemplate("template name", project=p)
        tvs1 = TemplateValueSet("Test configuration template",
                                config_template=ct)
        var1 = TemplateValue(tvs1, "var1", "description")
        var2 = TemplateValue(tvs1, var2_name, var2_value)

        db.session.add(tvs1)
        db.session.add(var1)
        db.session.add(var2)
        db.session.commit()

        # try to add another variable
        var3 = TemplateValue(tvs1, "var2", "with different value")
        db.session.add(var3)
        with self.assertRaises(IntegrityError):
            db.session.commit()
        db.session.rollback()

        # get same variable and verify description
        received_var2 = TemplateValue.query.filter_by(
            var_name_slug=var2_name, template_value_set=tvs1).first()

        self.assertEqual(received_var2, var2)
Example #10
0
    def test_config_template_delete_cascade_option(self):
        p1 = Project(name="Project 1")
        ct1 = ConfigTemplate(name="first script", project=p1)
        ct2 = ConfigTemplate(name="second script", project=p1)
        ct1.project = p1
        ct2.project = p1
        ct1.update_template_variable("test 1")
        ct1.update_template_variable("test 2")
        ct1.update_template_variable("test 3")
        ct2.update_template_variable("other test 1")
        ct2.update_template_variable("other test 2")
        ct2.update_template_variable("other test 3")

        tvs1 = TemplateValueSet("valueset 1", ct1)
        tvs2 = TemplateValueSet("valueset 2", ct1)
        tvs3 = TemplateValueSet("valueset 1", ct2)
        tvs4 = TemplateValueSet("valueset 2", ct2)
        tvs5 = TemplateValueSet("valueset 3", ct2)

        db.session.add_all([p1, ct1, ct2, tvs1, tvs2, tvs3, tvs4, tvs5])
        db.session.commit()

        # test cascade option when deleting objects (hostname is automatically created)
        self.assertTrue(
            len(TemplateVariable.query.all()) == 6 + 2,
            len(TemplateVariable.query.all()))
        self.assertTrue(
            len(TemplateValueSet.query.all()) == 5,
            len(TemplateValueSet.query.all()))
        db.session.delete(ct1)
        self.assertTrue(len(Project.query.all()) == 1)
        self.assertTrue(
            len(TemplateVariable.query.all()) == 3 + 1,
            len(TemplateVariable.query.all()))
        self.assertTrue(
            len(TemplateValueSet.query.all()) == 3,
            len(TemplateValueSet.query.all()))
        db.session.delete(ct2)
        self.assertTrue(len(Project.query.all()) == 1)
        self.assertTrue(
            len(TemplateVariable.query.all()) == 0,
            len(TemplateVariable.query.all()))
        self.assertTrue(
            len(TemplateValueSet.query.all()) == 0,
            len(TemplateValueSet.query.all()))
Example #11
0
    def test_template_value_set_name_validation_function(self):
        p1 = Project(name="Project 1")
        ct1 = ConfigTemplate(name="first script", project=p1)
        ct2 = ConfigTemplate(name="second script", project=p1)

        db.session.add_all([p1, ct1, ct2])
        db.session.commit()

        test_tvs_name = "template value name"
        self.assertTrue(ct1.valid_template_value_set_name(test_tvs_name))

        tvs = TemplateValueSet(test_tvs_name, ct1)
        db.session.add(tvs)
        db.session.commit()

        self.assertFalse(ct1.valid_template_value_set_name(test_tvs_name))
        self.assertTrue(ct2.valid_template_value_set_name(test_tvs_name))
Example #12
0
    def test_variable_name_conversion(self):
        p = Project("project")
        ct = ConfigTemplate("config template", project=p)
        tvs = TemplateValueSet("My Template Value set", config_template=ct)

        db.session.add_all([p, ct, tvs])

        tvs.update_variable_value("First Test")
        self.assertTrue(tvs.is_value_defined("First_Test"))

        tvs.update_variable_value("first_test")
        self.assertTrue(len(TemplateValueSet.query.all()) == 1)
        self.assertTrue(tvs.is_value_defined("first_test"))

        variable_map = (
            ("Value ", "Value"),
            ("the variable name", "the_variable_name"),
            ("%&/__ASDF ", "ASDF"),
        )

        for string, expected_result in variable_map:
            self.assertEqual(tvs.convert_variable_name(string),
                             expected_result)
Example #13
0
def add_template_value_set(config_template_id):
    """add a new Template Value Set

    :param config_template_id:
    :return:
    """
    parent_config_template = ConfigTemplate.query.filter(
        ConfigTemplate.id == config_template_id).first_or_404()
    form = TemplateValueSetForm(request.form)

    if form.validate_on_submit():
        try:
            template_value_set = TemplateValueSet(
                hostname="", config_template=parent_config_template)

            template_value_set.hostname = form.hostname.data
            template_value_set.config_template = parent_config_template
            template_value_set.copy_variables_from_config_template()

            db.session.add(template_value_set)
            db.session.commit()

            flash("Template Value Set successful created", "success")
            return redirect(
                url_for("edit_template_value_set",
                        template_value_set_id=template_value_set.id,
                        config_template_id=parent_config_template.id))

        except IntegrityError as ex:
            if "UNIQUE constraint failed" in str(ex):
                msg = "Template Value Set hostname already in use, please use another one"

            else:
                msg = "Template Value Set was not created (unknown error)"
            flash(msg, "error")
            logger.error(msg, exc_info=True)
            db.session.rollback()

        except Exception:
            msg = "Template Value Set was not created (unknown error)"
            logger.error(msg, exc_info=True)
            flash(msg, "error")
            db.session.rollback()

    return render_template("template_value_set/add_template_value_set.html",
                           config_template=parent_config_template,
                           project=parent_config_template.project,
                           form=form)
Example #14
0
    def test_template_value_set_add_variable_and_lookup(self):
        # create test data
        p = Project(name="project")
        ct = ConfigTemplate(name="template", project=p)
        tvs1 = TemplateValueSet(hostname="tvs1", config_template=ct)
        tvs2 = TemplateValueSet(hostname="tvs2", config_template=ct)
        tvs3 = TemplateValueSet(hostname="tvs3", config_template=ct)
        tvs4 = TemplateValueSet(hostname="tvs4", config_template=ct)
        db.session.add_all([p, ct, tvs1, tvs2, tvs3, tvs4])
        db.session.commit()

        # verify that the variables are not defined
        self.assertFalse(tvs1.is_value_defined("first variable"))
        self.assertFalse(tvs1.is_value_defined("second variable"))
        self.assertFalse(tvs1.is_value_defined("first variable"))

        # add variables to the first configuration template
        tvs1var1_value = "value for first TVS1 variable"
        tvs1var2_value = "value for second TVS1 variable"
        tvs1.update_variable_value("first variable", tvs1var1_value)
        tvs1.update_variable_value("second variable", tvs1var2_value)
        self.assertFalse(tvs2.is_value_defined("first variable"))
        db.session.commit()

        # add variables to the second configuration template
        tvs2var1_value = "value for first TVS2 variable"
        tvs2.update_variable_value("first variable", tvs2var1_value)
        db.session.commit()

        # retrieve variables
        self.assertTrue(len(tvs1.values.all()) == 2 + 1)
        self.assertTrue(len(tvs2.values.all()) == 1 + 1)
        self.assertTrue(
            tvs1.is_value_defined(
                tvs1.convert_variable_name("first variable")))
        self.assertTrue(
            tvs1.is_value_defined(
                tvs1.convert_variable_name("second variable")))
        self.assertTrue(
            tvs2.is_value_defined(
                tvs1.convert_variable_name("first variable")))

        tvs1var1 = tvs1.get_template_value_by_name(
            tvs1.convert_variable_name("first variable"))
        self.assertTrue(type(tvs1var1) is TemplateValue)
        self.assertEqual(tvs1var1.template_value_set, tvs1)
        self.assertEqual(tvs1var1.value, tvs1var1_value)

        tvs1var2 = tvs1.get_template_value_by_name(
            tvs1.convert_variable_name("second variable"))
        self.assertTrue(type(tvs1var2) is TemplateValue)
        self.assertEqual(tvs1var2.template_value_set, tvs1)
        self.assertEqual(tvs1var2.value, tvs1var2_value)

        tvs2var1 = tvs2.get_template_value_by_name(
            tvs1.convert_variable_name("first variable"))
        self.assertTrue(type(tvs2var1) is TemplateValue)
        self.assertEqual(tvs2var1.template_value_set, tvs2)
        self.assertEqual(tvs2var1.value, tvs2var1_value)

        # invalid template lookup
        with self.assertRaises(TemplateValueNotFoundException):
            tvs1.get_template_value_by_name("unknown key")
Example #15
0
def edit_all_config_template_values(project_id, config_template_id):
    """edit all Config Template Values based on a CSV textarea

    :param project_id:
    :param config_template_id:
    :return:
    """
    Project.query.filter(Project.id == project_id).first_or_404()
    config_template = ConfigTemplate.query.filter(
        ConfigTemplate.id == config_template_id).first_or_404()

    form = EditConfigTemplateValuesForm(request.form, config_template)

    # hostname is defined in every Template Value Set
    variable_list = ["hostname"]
    for var in config_template.variables.all():
        # hostname must be located as first entry
        if var.var_name != "hostname":
            variable_list.append(var.var_name)

    if form.validate_on_submit():
        # update values from the CSV file
        reader = csv.DictReader(io.StringIO(form.csv_content.data),
                                delimiter=";")
        csv_lines = form.csv_content.data.splitlines()
        counter = 0
        for line in reader:
            if "hostname" in line.keys():
                if line["hostname"] is None:
                    flash(
                        "Invalid Hostname for Template Value Set: '%s'" %
                        csv_lines[counter], "error")

                elif line["hostname"] == "":
                    flash(
                        "No Hostname defined for Template Value Set: '%s'" %
                        form.csv_content.data.splitlines()[counter], "error")

                else:
                    # try to access an existing TemplateValueSet
                    tvs = TemplateValueSet.query.filter(
                        TemplateValueSet.config_template_id ==
                        config_template_id,
                        TemplateValueSet.hostname == line["hostname"]).first()
                    if not tvs:
                        # element not found, create and add a flush message
                        tvs = TemplateValueSet(hostname=line["hostname"],
                                               config_template=config_template)
                        flash(
                            "Create new Template Value Set for hostname <strong>%s</strong>"
                            % line["hostname"], "success")

                    # update variable values
                    for var in variable_list:
                        if var in line.keys():
                            if line[var]:
                                tvs.update_variable_value(var_name=var,
                                                          value=line[var])

                            else:
                                tvs.update_variable_value(var_name=var,
                                                          value="")
                                logger.debug(
                                    "Cannot find value for variable %s for TVS "
                                    "object %s using CSV line %s" %
                                    (var, repr(tvs), line))
            else:
                # hostname not defined, no creation possible
                flash("No hostname in CSV line found: %s" % line, "warning")
            counter += 1

        return redirect(
            url_for("view_config_template",
                    project_id=project_id,
                    config_template_id=config_template_id))

    else:
        form.csv_content.data = ";".join(variable_list)
        for tvs in config_template.template_value_sets.all():
            values = []
            for var in variable_list:
                values.append(tvs.get_template_value_by_name_as_string(var))
            form.csv_content.data += "\n" + ";".join(values)

    return render_template(
        "config_template/edit_all_config_template_values.html",
        project_id=project_id,
        config_template=config_template,
        form=form)
Example #16
0
    def test_template_value_set_copy_variable_function_during_creation(self):
        p = Project("project")
        ct = ConfigTemplate(name="my template",
                            template_content="not a real config",
                            project=p)
        self.assertEqual(ct.update_template_variable("var 1"), "var_1")
        self.assertEqual(ct.update_template_variable("var 2"), "var_2")
        self.assertEqual(ct.update_template_variable("var 3"), "var_3")

        db.session.add_all([p, ct])
        db.session.commit()

        # verify database content
        self.assertTrue(len(ConfigTemplate.query.all()) == 1)
        self.assertTrue(len(TemplateVariable.query.all()) == 3 + 1)

        # create a new template value set and verify that template variables are copied to the TemplateValueSet object
        tvs = TemplateValueSet(hostname="Value Set", config_template=ct)

        self.assertTrue(len(TemplateValue.query.all()) == 4)

        self.assertTrue(tvs.is_value_defined("hostname"))
        self.assertTrue(tvs.is_value_defined("var_1"))
        self.assertTrue(tvs.is_value_defined("var_2"))
        self.assertTrue(tvs.is_value_defined("var_3"))
        var_1_value = "My Value"
        tvs.update_variable_value("var_1", var_1_value)

        # call it again and verify that nothing has changed
        tvs.copy_variables_from_config_template()

        self.assertTrue(len(TemplateValue.query.all()) == 4)

        self.assertTrue(tvs.is_value_defined("hostname"))
        self.assertTrue(tvs.is_value_defined("var_1"))
        self.assertTrue(tvs.is_value_defined("var_2"))
        self.assertTrue(tvs.is_value_defined("var_3"))

        # test that old values are preserved
        self.assertEqual(tvs.get_template_value_by_name_as_string("var_1"),
                         var_1_value)
        self.assertEqual(tvs.get_template_value_by_name_as_string("hostname"),
                         tvs.hostname)
Example #17
0
    def test_template_value_set_update_value(self):
        p = Project("Project")
        ct1 = ConfigTemplate(name="first script", project=p)

        tvs1 = TemplateValueSet(hostname="tvs1", config_template=ct1)
        tvs2 = TemplateValueSet(hostname="tvs2", config_template=ct1)

        db.session.add(p)
        db.session.add(ct1)
        db.session.add(tvs1)
        db.session.add(tvs2)
        db.session.commit()

        # verify that the variables are not defined
        self.assertFalse(tvs1.is_value_defined("first variable"))
        self.assertFalse(tvs1.is_value_defined("second variable"))
        self.assertFalse(tvs2.is_value_defined("first variable"))

        # add variables to the first configuration template
        tvs1var1_value = "description for first tvs1 variable"
        tvs1.update_variable_value("first variable", tvs1var1_value)
        tvs1.update_variable_value("second variable",
                                   "value for second tvs1 variable")
        tvs2.update_variable_value("first variable",
                                   "value for first tvs2 variable")

        self.assertTrue(len(tvs1.values.all()) == 2 + 1)
        self.assertTrue(len(tvs2.values.all()) == 1 + 1)

        # update value
        tvs1var1_value_mod = "modified description"
        tvs1.update_variable_value("first variable", tvs1var1_value_mod)

        tvs1var1 = tvs1.get_template_value_by_name("first_variable")
        self.assertNotEqual(tvs1var1.value, tvs1var1_value)
        self.assertEqual(tvs1var1.value, tvs1var1_value_mod)