Exemplo n.º 1
0
 def test_list_present_nested(self):
     with self.setGrains({"a": "aval", "foo": {"is": {"nested": ["bar"]}}}):
         ret = grains.list_present(name="foo,is,nested",
                                   value="baz",
                                   delimiter=",")
         self.assertEqual(ret["result"], True)
         self.assertEqual(ret["comment"],
                          "Append value baz to grain foo:is:nested")
         self.assertEqual(
             ret["changes"],
             {"new": {
                 "foo": {
                     "is": {
                         "nested": ["bar", "baz"]
                     }
                 }
             }})
         self.assertEqual(
             grains.__grains__,
             {
                 "a": "aval",
                 "foo": {
                     "is": {
                         "nested": ["bar", "baz"]
                     }
                 }
             },
         )
         self.assertGrainFileContent("a: aval\n" + "foo:\n" + "  is:\n" +
                                     "    nested:\n" + "    - bar\n" +
                                     "    - baz\n")
Exemplo n.º 2
0
 def test_list_present_not_a_list(self):
     grains.__grains__ = grainsmod.__grains__ = {'a': 'aval', 'foo': 'bar'}
     ret = grains.list_present(name='foo', value='baz')
     self.assertEqual(ret['result'], False)
     self.assertEqual(ret['comment'], 'Grain foo is not a valid list')
     self.assertEqual(ret['changes'], {})
     self.assertEqual(grains.__grains__, {'a': 'aval', 'foo': 'bar'})
Exemplo n.º 3
0
 def test_list_present_already(self):
     grains.__grains__ = grainsmod.__grains__ = {"a": "aval", "foo": ["bar"]}
     ret = grains.list_present(name="foo", value="bar")
     self.assertEqual(ret["result"], True)
     self.assertEqual(ret["comment"], "Value bar is already in grain foo")
     self.assertEqual(ret["changes"], {})
     self.assertEqual(grains.__grains__, {"a": "aval", "foo": ["bar"]})
Exemplo n.º 4
0
 def test_list_present_not_a_list(self):
     grains.__grains__ = grainsmod.__grains__ = {"a": "aval", "foo": "bar"}
     ret = grains.list_present(name="foo", value="baz")
     self.assertEqual(ret["result"], False)
     self.assertEqual(ret["comment"], "Grain foo is not a valid list")
     self.assertEqual(ret["changes"], {})
     self.assertEqual(grains.__grains__, {"a": "aval", "foo": "bar"})
Exemplo n.º 5
0
 def test_list_present_inexistent(self):
     grains.__grains__ = grainsmod.__grains__ = {"a": "aval"}
     ret = grains.list_present(name="foo", value="baz")
     self.assertEqual(ret["result"], True)
     self.assertEqual(ret["comment"], "Append value baz to grain foo")
     self.assertEqual(ret["changes"], {"new": {"foo": ["baz"]}})
     self.assertEqual(grains.__grains__, {"a": "aval", "foo": ["baz"]})
Exemplo n.º 6
0
 def test_list_present_nested(self):
     with self.setGrains({'a': 'aval', 'foo': {'is': {'nested': ['bar']}}}):
         ret = grains.list_present(name='foo,is,nested',
                                   value='baz',
                                   delimiter=',')
         self.assertEqual(ret['result'], True)
         self.assertEqual(ret['comment'],
                          'Append value baz to grain foo:is:nested')
         self.assertEqual(
             ret['changes'],
             {'new': {
                 'foo': {
                     'is': {
                         'nested': ['bar', 'baz']
                     }
                 }
             }})
         self.assertEqual(grains.__grains__, {
             'a': 'aval',
             'foo': {
                 'is': {
                     'nested': ['bar', 'baz']
                 }
             }
         })
         self.assertGrainFileContent("a: aval\n" + "foo:\n" + "  is:\n" +
                                     "    nested:\n" + "    - bar\n" +
                                     "    - baz\n")
Exemplo n.º 7
0
 def test_list_present_inexistent(self):
     grains.__grains__ = grainsmod.__grains__ = {'a': 'aval'}
     ret = grains.list_present(name='foo', value='baz')
     self.assertEqual(ret['result'], True)
     self.assertEqual(ret['comment'], 'Append value baz to grain foo')
     self.assertEqual(ret['changes'], {'new': {'foo': ['baz']}})
     self.assertEqual(grains.__grains__, {'a': 'aval', 'foo': ['baz']})
Exemplo n.º 8
0
 def test_list_present_inexistent(self):
     with self.setGrains({'a': 'aval'}):
         ret = grains.list_present(name='foo', value='baz')
         self.assertEqual(ret['result'], True)
         self.assertEqual(ret['comment'], 'Append value baz to grain foo')
         self.assertEqual(ret['changes'], {'new': {'foo': ['baz']}})
         self.assertEqual(grains.__grains__, {'a': 'aval', 'foo': ['baz']})
         self.assertGrainFileContent("a: aval\n" + "foo:\n" + "- baz\n")
Exemplo n.º 9
0
 def test_list_present_not_a_list(self):
     with self.setGrains({'a': 'aval', 'foo': 'bar'}):
         ret = grains.list_present(name='foo', value='baz')
         self.assertEqual(ret['result'], False)
         self.assertEqual(ret['comment'], 'Grain foo is not a valid list')
         self.assertEqual(ret['changes'], {})
         self.assertEqual(grains.__grains__, {'a': 'aval', 'foo': 'bar'})
         self.assertGrainFileContent("a: aval\n" + "foo: bar\n")
Exemplo n.º 10
0
 def test_list_present_not_a_list(self):
     with self.setGrains({"a": "aval", "foo": "bar"}):
         ret = grains.list_present(name="foo", value="baz")
         self.assertEqual(ret["result"], False)
         self.assertEqual(ret["comment"], "Grain foo is not a valid list")
         self.assertEqual(ret["changes"], {})
         self.assertEqual(grains.__grains__, {"a": "aval", "foo": "bar"})
         self.assertGrainFileContent("a: aval\n" + "foo: bar\n")
Exemplo n.º 11
0
 def test_list_present_inexistent(self):
     with self.setGrains({"a": "aval"}):
         ret = grains.list_present(name="foo", value="baz")
         self.assertEqual(ret["result"], True)
         self.assertEqual(ret["comment"], "Append value baz to grain foo")
         self.assertEqual(ret["changes"], {"new": {"foo": ["baz"]}})
         self.assertEqual(grains.__grains__, {"a": "aval", "foo": ["baz"]})
         self.assertGrainFileContent("a: aval\n" + "foo:\n" + "- baz\n")
Exemplo n.º 12
0
 def test_list_present_already(self):
     self.setGrains({'a': 'aval', 'foo': ['bar']})
     ret = grains.list_present(name='foo', value='bar')
     self.assertEqual(ret['result'], True)
     self.assertEqual(ret['comment'], 'Value bar is already in grain foo')
     self.assertEqual(ret['changes'], {})
     self.assertEqual(grains.__grains__, {'a': 'aval', 'foo': ['bar']})
     self.assertGrainFileContent("a: aval\n" + "foo:\n" + "- bar\n")
Exemplo n.º 13
0
 def test_list_present_unknown_failure(self):
     grains.__grains__ = grainsmod.__grains__ = {"a": "aval", "foo": ["bar"]}
     # Unknown reason failure
     grainsmod.__salt__["grains.append"] = MagicMock()
     ret = grains.list_present(name="foo", value="baz")
     self.assertEqual(ret["result"], False)
     self.assertEqual(ret["comment"], "Failed append value baz to grain foo")
     self.assertEqual(ret["changes"], {})
     self.assertEqual(grains.__grains__, {"a": "aval", "foo": ["bar"]})
Exemplo n.º 14
0
 def test_list_present_already(self):
     with self.setGrains({"a": "aval", "foo": ["bar"]}):
         ret = grains.list_present(name="foo", value="bar")
         self.assertEqual(ret["result"], True)
         self.assertEqual(ret["comment"],
                          "Value bar is already in grain foo")
         self.assertEqual(ret["changes"], {})
         self.assertEqual(grains.__grains__, {"a": "aval", "foo": ["bar"]})
         self.assertGrainFileContent("a: aval\n" + "foo:\n" + "- bar\n")
Exemplo n.º 15
0
 def test_list_present_already(self):
     grains.__grains__ = grainsmod.__grains__ = {
         'a': 'aval',
         'foo': ['bar']
     }
     ret = grains.list_present(name='foo', value='bar')
     self.assertEqual(ret['result'], True)
     self.assertEqual(ret['comment'], 'Value bar is already in grain foo')
     self.assertEqual(ret['changes'], {})
     self.assertEqual(grains.__grains__, {'a': 'aval', 'foo': ['bar']})
Exemplo n.º 16
0
 def test_list_present_unknown_failure(self):
     self.setGrains({'a': 'aval', 'foo': ['bar']})
     # Unknown reason failure
     grainsmod.__salt__['grains.append'] = MagicMock()
     ret = grains.list_present(name='foo', value='baz')
     self.assertEqual(ret['result'], False)
     self.assertEqual(ret['comment'],
                      'Failed append value baz to grain foo')
     self.assertEqual(ret['changes'], {})
     self.assertEqual(grains.__grains__, {'a': 'aval', 'foo': ['bar']})
     self.assertGrainFileContent("a: aval\n" + "foo:\n" + "- bar\n")
Exemplo n.º 17
0
 def test_list_present_not_a_list(self):
     self.setGrains({'a': 'aval', 'foo': 'bar'})
     ret = grains.list_present(
         name='foo',
         value='baz')
     self.assertEqual(ret['result'], False)
     self.assertEqual(ret['comment'], 'Grain foo is not a valid list')
     self.assertEqual(ret['changes'], {})
     self.assertEqual(
         grains.__grains__,
         {'a': 'aval', 'foo': 'bar'})
     self.assertGrainFileContent("a: aval\n"
                               + "foo: bar\n"
     )
Exemplo n.º 18
0
    def test_list_present(self):
        '''
        Test to ensure the value is present in the list type grain
        '''
        ret = {'changes': {}, 'name': self.name, 'result': False,
               'comment': 'Grain cheese is not a valid list'}

        with patch.dict(grains.__grains__, {self.name: self.value}):
            self.assertDictEqual(grains.list_present(self.name, self.value),
                                 ret)

        ret = {'changes': {}, 'name': self.name, 'result': True,
               'comment': 'Value edam is already in grain cheese'}

        with patch.dict(grains.__grains__, {self.name: [self.value]}):
            self.assertDictEqual(grains.list_present(self.name, self.value),
                                 ret)

        ret = {'changes': {'new': None}, 'name': self.name, 'result': None,
               'comment': 'Grain cheese is set to be added'}

        ret1 = {'changes': {'new': ['eves']}, 'name': self.name, 'result': None,
                'comment': 'Value edam is set to be appended to grain cheese'}

        ret2 = {'changes': {}, 'name': self.name, 'result': False,
                'comment': 'Failed append value edam to grain cheese'}

        with patch.dict(grains.__opts__, {'test': True}):
            self.assertDictEqual(grains.list_present(self.name, self.value),
                                 ret)

            with patch.dict(grains.__grains__, {self.name: ['eves']}):
                self.assertDictEqual(grains.list_present(self.name, self.value),
                                     ret1)

                with patch.dict(grains.__opts__, {'test': False}):
                    mock = MagicMock(return_value={self.name: 'eves'})
                    with patch.dict(grains.__salt__, {'grains.append': mock}):
                        self.assertDictEqual(grains.list_present(self.name,
                                                                 self.value),
                                             ret2)

        ret = {'changes': {'new': self.value}, 'name': self.name,
               'result': True, 'comment': 'Append value edam to grain cheese'}

        def add_grain(name, value):
            '''
            Add key: value to __grains__ dict.
            '''
            grains.__grains__[name].append(value)
            return value

        with patch.dict(grains.__opts__, {'test': False}):
            with patch.dict(grains.__grains__, {self.name: []}):
                with patch.dict(grains.__salt__, {'grains.append': add_grain}):
                    self.assertDictEqual(grains.list_present(self.name,
                                                             self.value), ret)
Exemplo n.º 19
0
 def test_list_present_inexistent(self):
     self.setGrains({'a': 'aval'})
     ret = grains.list_present(
         name='foo',
         value='baz')
     self.assertEqual(ret['result'], True)
     self.assertEqual(ret['comment'], 'Append value baz to grain foo')
     self.assertEqual(ret['changes'], {'new': {'foo': ['baz']}})
     self.assertEqual(
         grains.__grains__,
         {'a': 'aval', 'foo': ['baz']})
     self.assertGrainFileContent("a: aval\n"
                               + "foo:\n"
                               + "- baz\n"
     )
Exemplo n.º 20
0
 def test_list_present_already(self):
     self.setGrains({'a': 'aval', 'foo': ['bar']})
     ret = grains.list_present(
         name='foo',
         value='bar')
     self.assertEqual(ret['result'], True)
     self.assertEqual(ret['comment'], 'Value bar is already in grain foo')
     self.assertEqual(ret['changes'], {})
     self.assertEqual(
         grains.__grains__,
         {'a': 'aval', 'foo': ['bar']})
     self.assertGrainFileContent("a: aval\n"
                               + "foo:\n"
                               + "- bar\n"
     )
Exemplo n.º 21
0
    def test_list_present_unknown_failure(self):
        with self.setGrains({"a": "aval", "foo": ["bar"]}):
            # Unknown reason failure

            with patch.dict(grainsmod.__salt__,
                            {"grains.append": MagicMock()}):
                ret = grains.list_present(name="foo", value="baz")
                self.assertEqual(ret["result"], False)
                self.assertEqual(ret["comment"],
                                 "Failed append value baz to grain foo")
                self.assertEqual(ret["changes"], {})
                self.assertEqual(grains.__grains__, {
                    "a": "aval",
                    "foo": ["bar"]
                })
                self.assertGrainFileContent("a: aval\n" + "foo:\n" + "- bar\n")
Exemplo n.º 22
0
 def test_list_present_unknown_failure(self):
     self.setGrains({'a': 'aval', 'foo': ['bar']})
     # Unknown reason failure
     grainsmod.__salt__['grains.append'] = MagicMock()
     ret = grains.list_present(
         name='foo',
         value='baz')
     self.assertEqual(ret['result'], False)
     self.assertEqual(ret['comment'], 'Failed append value baz to grain foo')
     self.assertEqual(ret['changes'], {})
     self.assertEqual(
         grains.__grains__,
         {'a': 'aval', 'foo': ['bar']})
     self.assertGrainFileContent("a: aval\n"
                               + "foo:\n"
                               + "- bar\n"
     )
Exemplo n.º 23
0
 def test_list_present_nested(self):
     self.setGrains({'a': 'aval', 'foo': {'is': {'nested': ['bar']}}})
     ret = grains.list_present(
         name='foo,is,nested',
         value='baz',
         delimiter=',')
     self.assertEqual(ret['result'], True)
     self.assertEqual(ret['comment'], 'Append value baz to grain foo:is:nested')
     self.assertEqual(ret['changes'], {'new': {'foo': {'is': {'nested': ['bar', 'baz']}}}})
     self.assertEqual(
         grains.__grains__,
         {'a': 'aval', 'foo': {'is': {'nested': ['bar', 'baz']}}})
     self.assertGrainFileContent("a: aval\n"
                               + "foo:\n"
                               + "  is:\n"
                               + "    nested:\n"
                               + "    - bar\n"
                               + "    - baz\n"
     )
Exemplo n.º 24
0
    def test_list_present(self):
        '''
        Test to ensure the value is present in the list type grain
        '''
        ret = {
            'changes': {},
            'name': self.name,
            'result': False,
            'comment': 'Grain cheese is not a valid list'
        }

        with patch.dict(grains.__grains__, {self.name: self.value}):
            self.assertDictEqual(grains.list_present(self.name, self.value),
                                 ret)

        ret = {
            'changes': {},
            'name': self.name,
            'result': True,
            'comment': 'Value edam is already in grain cheese'
        }

        with patch.dict(grains.__grains__, {self.name: [self.value]}):
            self.assertDictEqual(grains.list_present(self.name, self.value),
                                 ret)

        ret = {
            'changes': {
                'new': None
            },
            'name': self.name,
            'result': None,
            'comment': 'Grain cheese is set to be added'
        }

        ret1 = {
            'changes': {
                'new': ['eves']
            },
            'name': self.name,
            'result': None,
            'comment': 'Value edam is set to be appended to grain cheese'
        }

        ret2 = {
            'changes': {},
            'name': self.name,
            'result': False,
            'comment': 'Failed append value edam to grain cheese'
        }

        with patch.dict(grains.__opts__, {'test': True}):
            self.assertDictEqual(grains.list_present(self.name, self.value),
                                 ret)

            with patch.dict(grains.__grains__, {self.name: ['eves']}):
                self.assertDictEqual(
                    grains.list_present(self.name, self.value), ret1)

                with patch.dict(grains.__opts__, {'test': False}):
                    mock = MagicMock(return_value={self.name: 'eves'})
                    with patch.dict(grains.__salt__, {'grains.append': mock}):
                        self.assertDictEqual(
                            grains.list_present(self.name, self.value), ret2)

        ret = {
            'changes': {
                'new': self.value
            },
            'name': self.name,
            'result': True,
            'comment': 'Append value edam to grain cheese'
        }

        def add_grain(name, value):
            '''
            Add key: value to __grains__ dict.
            '''
            grains.__grains__[name].append(value)
            return value

        with patch.dict(grains.__opts__, {'test': False}):
            with patch.dict(grains.__grains__, {self.name: []}):
                with patch.dict(grains.__salt__, {'grains.append': add_grain}):
                    self.assertDictEqual(
                        grains.list_present(self.name, self.value), ret)