コード例 #1
0
def test_1950_deprecation_warning_nullable_diamond_inheritance(
        snippetcompiler, assign: bool):
    snippetcompiler.setup_for_snippet(
        """
entity A:
    number? n%s
end

entity B extends A:
end

entity C extends A, B:
end


implement A using std::none
implement B using std::none
implement C using std::none

C()
        """ % (" = null" if assign else ""), )
    message: str = "No value for attribute __config__::C.n. Assign null instead of leaving unassigned. ({dir}/main.cf:17)"
    with warnings.catch_warnings(record=True) as w:
        compiler.do_compile()
        assert len(w) == 0 if assign else 1
        if not assign:
            assert issubclass(w[0].category, CompilerDeprecationWarning)
            assert str(w[0].message) == message.format(
                dir=snippetcompiler.project_dir)
コード例 #2
0
def test_issue_2378_scheduler(snippetcompiler):
    snippetcompiler.setup_for_snippet("""
entity A:
    string name
end

A.b [0:] -- B

entity B:
    string name
end


b = B(name="b")

a3 = A(name="a3")

a1 = A(name="a1")
a2 = A(name="a2")

implement A using std::none
implement B using std::none

a3.b = std::key_sort(a1.b, "name")
a1.b = a2.b
a2.b += b
""")
    compiler.do_compile()
コード例 #3
0
def test_optional_loop_list(snippetcompiler):
    snippetcompiler.setup_for_snippet("""
entity Thing:
    string name
end

implement Thing using std::none

Thing.other [0:] -- Thing.that [0:]

implementation setother for Thing:
    t = Thing(name="it")
    t.that = self
end

implement Thing using setother when std::count(other) == 1

t = Thing(name="a")
t.other = Thing(name="b")
""")
    with pytest.raises(AttributeException) as e:
        compiler.do_compile()

    print(ExplainerFactory().explain_and_format(e.value))
    assert (ExplainerFactory().explain_and_format(e.value) == """
Exception explanation
=====================
The compiler could not figure out how to execute this model.

During compilation, the compiler has to decide when it expects a relation to have all its elements.
In this compiler run, it guessed that the relation 'other' on the instance __config__::Thing (instantiated at %(dir)s/main.cf:17) would be complete with the values [__config__::Thing (instantiated at %(dir)s/main.cf:18)], but the value __config__::Thing (instantiated at %(dir)s/main.cf:11) was added at %(dir)s/main.cf:12:14

This can mean one of two things:

1. The model is incorrect. Most often, this is due to something of the form:

    implementation mydefault for MyEntity:
      self.relation += "default"
    end

    implement MyEntity using mydefault when std::count(relation) == 0


   This is always wrong, because the relation can not at the same time have length 0 and contain the value "default"

2. The model is too complicated for the compiler to resolve.

The procedure to solve this is the following

1. Ensure the model is correct by checking that the problematic assignment at %(dir)s/main.cf:12:14 is not conditional on the value it assigns.
2. Report a bug to the inmanta issue tracker at https://github.com/inmanta/inmanta/issues or directly contact inmanta. This is a priority issue to us, so you will be helped rapidly and by reporting the problem, we can fix it properly.
3. [applies] If the exception is on the reverse relation, try to give a hint by explicitly using the problematic relation: self.other = t
4. Simplify the model by reducing the number of implements calls that pass a list into a plugin function in their when clause.

"""

            # noqa: E501
            % {
                "dir": snippetcompiler.project_dir
            })
コード例 #4
0
def test_671_bounds_check(snippetcompiler):
    snippetcompiler.setup_for_snippet(
        """ entity Test:

end

entity Foo:

end

Test.foos [2] -- Foo

t = Test()
t.foos += Foo()
t.foos += Foo()

a = t.foos

implementation none for std::Entity:
end

implement Test using none
implement Foo using none
""",
        autostd=False,
    )
    compiler.do_compile()
コード例 #5
0
def test_lazy_constructor(snippetcompiler):
    snippetcompiler.setup_for_snippet(
        """
entity One:
end

entity Two:
end

One.two [1] -- Two.one [1]

one = One(two=two)
two = Two(one=one)

implementation none for std::Entity:

end

implement One using none
implement Two using none
""",
        autostd=False,
    )

    compiler.do_compile()
コード例 #6
0
def test_shadow_warning(snippetcompiler):
    snippetcompiler.setup_for_snippet(
        """
x = 0
if true:
    x = 1
    if true:
        if true:
            x = 3
        end
    end
end
        """
    )
    message: str = "Variable `x` shadowed: originally declared at {dir}/main.cf:%d, shadowed at {dir}/main.cf:%d"
    message = message.format(dir=snippetcompiler.project_dir)
    with warnings.catch_warnings(record=True) as w:
        compiler.do_compile()
        assert len(w) == 2
        w1 = w[0]
        w3 = w[1]
        assert issubclass(w1.category, VariableShadowWarning)
        assert str(w1.message) == message % (2, 4)
        assert issubclass(w3.category, VariableShadowWarning)
        assert str(w3.message) == message % (4, 7)
コード例 #7
0
def test_deprecation_minus_relation(snippetcompiler):
    with warnings.catch_warnings(record=True) as w:
        snippetcompiler.setup_for_snippet("""
entity Host:
    string  name
end

entity File:
    string path
end

Host.files-hehe [0:] -- File.host-hoho [1]
            """)
        message1: str = (
            f"The use of '-' in identifiers is deprecated. Consider renaming files-hehe. "
            f"(reported in files-hehe ({snippetcompiler.project_dir}/main.cf:10:6))"
        )
        message2: str = (
            f"The use of '-' in identifiers is deprecated. Consider renaming host-hoho. "
            f"(reported in host-hoho ({snippetcompiler.project_dir}/main.cf:10:30))"
        )
        compiler.do_compile()
        assert len(w) == 2
        assert issubclass(w[0].category, CompilerDeprecationWarning)
        assert str(w[0].message) == message1
        assert str(w[1].message) == message2
コード例 #8
0
def test_issue_93(snippetcompiler):
    snippetcompiler.setup_for_snippet("""
entity Test1:

end
implement Test1 using std::none

entity Test2:
    string attribute="1234"
end
implement Test2 using std::none

Test1 test1 [1] -- [0:] Test2 test2

t = Test1()
t2a = Test2(test1=t)
t2b = Test2(test1=t)

std::print(t.test2.attribute)
        """)

    try:
        compiler.do_compile()
        raise AssertionError("Should get exception")
    except RuntimeException as e:
        assert e.location.lnr == 18
コード例 #9
0
def test_issue_120_bad_import_extra(snippetcompiler):
    snippetcompiler.setup_for_snippet("""import slorpf""")
    try:
        compiler.do_compile()
        raise AssertionError("Should get exception")
    except ModuleNotFoundException as e:
        assert e.location.lnr == 1
コード例 #10
0
def test_issue_132_relation_on_default(snippetcompiler):
    snippetcompiler.setup_for_snippet("""
typedef CFG as std::File(mode=755)
CFG cfg [1] -- [1] std::File stuff
""")
    with pytest.raises(TypingException):
        compiler.do_compile()
コード例 #11
0
ファイル: test_compilation.py プロジェクト: n-pochet/inmanta
 def test_compile(self):
     with pytest.raises(RuntimeException) as e:
         compiler.do_compile()
     text = str(e.value)
     print(text)
     assert text.startswith("Exception in plugin test::badtype caused by Invalid type for value 'a'," +
                            " should be type test::Item (reported in test::badtype(c1.items) (")
コード例 #12
0
def test_import_hypen_in_name(snippetcompiler):
    with pytest.raises(CompilerException) as e:
        snippetcompiler.setup_for_snippet("""
import st-d
            """)
        compiler.do_compile()
    assert "st-d is not a valid module name: hyphens are not allowed, please use underscores instead." == e.value.msg
コード例 #13
0
def test_allowed_line_breaks(snippetcompiler):
    snippetcompiler.setup_for_snippet(
        r'''
test_string_1 = "TESTSTRING"
test_string_2 = "TEST\nSTR\rING"
test_string_3 = r"TESTSTRING"
test_string_4 = r"TEST\nSTR\rING"

test_string_5 = 'TESTSTRING'
test_string_6 = 'TEST\nSTR\rING'
test_string_7 = r'TESTSTRING'
test_string_8 = r'TEST\nSTR\rING'


test_string_9 =  """TESTSTRING"""
test_string_10 = """TEST\nSTR\rING"""
test_string_11 = """TEST
STRING"""


std::print(test_string_1)
std::print(test_string_2)
std::print(test_string_3)
std::print(test_string_4)
std::print(test_string_5)
std::print(test_string_6)
std::print(test_string_7)
std::print(test_string_8)
std::print(test_string_9)
std::print(test_string_10)
std::print(test_string_11)

'''
    )
    compiler.do_compile()
コード例 #14
0
ファイル: test_compilation.py プロジェクト: n-pochet/inmanta
 def test_issue_120_bad_import(self):
     self.setup_for_snippet("""import ip::ip""")
     try:
         compiler.do_compile()
         raise AssertionError("Should get exception")
     except ModuleNotFoundException as e:
         assert e.location.lnr == 1
コード例 #15
0
def test_dataflow_exception(snippetcompiler):
    snippetcompiler.setup_for_snippet("""
x = 0
x = 1
        """, )
    Config.set("compiler", "datatrace_enable", "true")
    try:
        compiler.do_compile()
    except DoubleSetException as e:
        assert e.msg.strip() == (
            """
value set twice:
	old value: 0
		set at {dir}/main.cf:2
	new value: 1
		set at {dir}/main.cf:3

data trace:
x
├── 0
│   SET BY `x = 0`
│   AT {dir}/main.cf:2
└── 1
    SET BY `x = 1`
    AT {dir}/main.cf:3
            """.strip().format(  # noqa: W191, E101
                dir=snippetcompiler.project_dir))
コード例 #16
0
def test_438_parent_scopes_accessible_2(snippetcompiler):

    snippetcompiler.setup_for_snippet(
        """
entity Host:
    string name
end

entity HostConfig:
    string result
end

HostConfig.host [1] -- Host

implementation hostDefaults for Host:
    test="foo"
    HostConfig(host=self)
end

implement Host using hostDefaults

implementation test for HostConfig:
    self.result = test
end

implement HostConfig using test

Host(name="bar")
""",
        autostd=False,
    )
    with pytest.raises(NotFoundException):
        compiler.do_compile()
コード例 #17
0
ファイル: test_index.py プロジェクト: e7ChatsApp/inmanta-core
def test_issue_122_index_inheritance(snippetcompiler):
    snippetcompiler.setup_for_snippet("""
entity Repository extends std::File:
    string name
    bool gpgcheck=false
    bool enabled=true
    string baseurl
    string gpgkey=""
    number metadata_expire=7200
    bool send_event=true
end

implementation redhatRepo for Repository:
    self.mode = 644
    self.owner = "root"
    self.group = "root"

    self.path = "/etc/yum.repos.d/{{ name }}.repo"
    self.content = "{{name}}"
end

implement Repository using redhatRepo

h1 = std::Host(name="test", os=std::linux)

Repository(host=h1, name="demo", baseurl="http://example.com")
Repository(host=h1, name="demo", baseurl="http://example.com")
        """)

    try:
        compiler.do_compile()
        raise AssertionError("Should get exception")
    except TypingException as e:
        assert e.location.lnr == 25
コード例 #18
0
ファイル: test_compilation.py プロジェクト: n-pochet/inmanta
def test_400_typeloops(snippetcompiler):
    snippetcompiler.setup_for_snippet("""
    entity Test extends Test:

    end
    """)
    with pytest.raises(TypingException):
        compiler.do_compile()
コード例 #19
0
def test_674_not_nullable_type_in_plugin_arguments(snippetcompiler):
    snippetcompiler.setup_for_snippet(
        """
import test_674

test_674::test_not_nullable("Hello World!")
        """, )
    compiler.do_compile()
コード例 #20
0
def test_if_scope_new(snippetcompiler):
    snippetcompiler.setup_for_snippet("""
a = 1
if 1 == 1:
    a = 2
end
        """)
    compiler.do_compile()
コード例 #21
0
def test_issue_73(snippetcompiler):
    snippetcompiler.setup_for_snippet(
        """
vm1 = std::floob()
"""
    )
    with pytest.raises(TypeNotFoundException):
        compiler.do_compile()
コード例 #22
0
ファイル: test_compilation.py プロジェクト: n-pochet/inmanta
    def test_dict_collide(self):
        self.setup_for_snippet("""
a = "a"
b = { "a" : a, "a" : "b", "c" : 3}
""")

        with pytest.raises(DuplicateException):
            compiler.do_compile()
コード例 #23
0
def test_1774_plugin_returning_entity_in_dict(snippetcompiler):
    snippetcompiler.setup_for_snippet(
        """
import test_1774

test_1774::test_dict(test_1774::Test())
        """, )
    compiler.do_compile()
コード例 #24
0
ファイル: test_index.py プロジェクト: e7ChatsApp/inmanta-core
def test_index_on_subtype2(snippetcompiler):
    snippetcompiler.setup_for_snippet("""
        host = std::Host(name="a",os=std::linux)
        a=std::DefaultDirectory(host=host,path="/etc")
        b=std::Directory(host=host,path="/etc",mode=755 ,group="root",owner="root" )
    """)
    with pytest.raises(DuplicateException):
        compiler.do_compile()
コード例 #25
0
def test_674_not_nullable_list_type_in_plugin_arguments(snippetcompiler):
    snippetcompiler.setup_for_snippet(
        """
import test_674

test_674::test_not_nullable_list([1,2])
        """, )
    compiler.do_compile()
コード例 #26
0
ファイル: test_compilation.py プロジェクト: n-pochet/inmanta
    def test_issue_134_colliding_umplementations(self):

        self.setup_for_snippet("""
implementation test for std::Entity:
end
implementation test for std::Entity:
end""")
        with pytest.raises(DuplicateException):
            compiler.do_compile()
コード例 #27
0
def test_optional_loop_forward_tty(snippetcompiler):
    snippetcompiler.setup_for_snippet("""
entity Thing:
    string name
end

implement Thing using std::none

Thing.other [0:1] -- Thing

implementation setother for Thing:
    self.other = Thing(name="it")
end

implement Thing using setother when not (other is defined)

Thing(name="a")
""")
    with pytest.raises(AttributeException) as e:
        compiler.do_compile()

    value = ExplainerFactory().explain_and_format(e.value, plain=False)

    assert (value == """
\033[1mException explanation
=====================\033[0m
The compiler could not figure out how to execute this model.

During compilation, the compiler has to decide when it expects an optional relation to remain undefined. In this compiler run, it guessed that the relation '\033[4mother\033[0m' on the instance \033[4m__config__::Thing (instantiated at %(dir)s/main.cf:16)\033[0m would never get a value assigned, but the value \033[4m__config__::Thing (instantiated at %(dir)s/main.cf:11)\033[0m was assigned at \033[4m%(dir)s/main.cf:11\033[0m

This can mean one of two things:

1. The model is incorrect. Most often, this is due to something of the form:

    \033[1mimplementation mydefault for MyEntity:
        self.relation = "default"
    end

    implement MyEntity using mydefault when not (relation is defined)\033[0m

  This is always wrong, because the relation can not at the same time be undefined and have the value "default".

2. The model is too complicated for the compiler to resolve.

The procedure to solve this is the following:

1. Ensure the model is correct by checking that the problematic assignment at \033[4m%(dir)s/main.cf:11\033[0m is not conditional on the value it assigns.
2. Set a relation precedence policy on the Inmanta project (See: https://docs.inmanta.com/community/latest/troubleshooting.html#compilation-fails).
3. Report a bug to the inmanta issue tracker at https://github.com/inmanta/inmanta/issues or directly contact inmanta. This is a priority issue to us, so you will be helped rapidly and by reporting the problem, we can fix it properly.
4. [does not apply here] If the exception is on the reverse relation, try to give a hint by explicitly using the problematic relation.
5. Simplify the model by relying less on `is defined` but use a boolean instead.
"""

            # noqa: E501
            % {
                "dir": snippetcompiler.project_dir
            })
コード例 #28
0
ファイル: test_index.py プロジェクト: e7ChatsApp/inmanta-core
def test_index_on_subtype_diamond_3(snippetcompiler):
    snippetcompiler.setup_for_snippet(diamond + """
    index A(at)
    index B(at)

    a = A(at="a")
    b = B(at="ab")
    """)
    compiler.do_compile()
コード例 #29
0
ファイル: test_index.py プロジェクト: e7ChatsApp/inmanta-core
def test_issue_212_bad_index_defintion(snippetcompiler):
    snippetcompiler.setup_for_snippet("""
entity Test1:
    string x
end
index Test1(x,y)
""")
    with pytest.raises(RuntimeException):
        compiler.do_compile()
コード例 #30
0
ファイル: test_defaults.py プロジェクト: inmanta/inmanta-core
def test_1725_default_type_check_with_plugin(snippetcompiler):
    snippetcompiler.setup_for_snippet(
        """
entity Test:
    std::date d = "2020-01-22"
end
        """,
    )
    compiler.do_compile()