Exemplo n.º 1
0
    def test_append_convert_to_list(self):
        # Append to an existing grain, converting to a list
        self.setGrains({'a': 'aval', 'foo': {'bar': 'val'}})
        self.assertGrainFileContent("a: aval\n"
                                  + "foo:\n"
                                  + "  bar: val\n"
        )
        ret = grains.append(
            name='foo',
            value='baz',
            convert=True)
        self.assertEqual(ret['result'], True)
        self.assertEqual(ret['comment'], 'Value baz was added to grain foo')
        self.assertEqual(ret['changes'], {'added': 'baz'})
        self.assertEqual(
            grains.__grains__,
            {'a': 'aval', 'foo': [{'bar': 'val'}, 'baz']})
        self.assertGrainFileContent("a: aval\n"
                                  + "foo:\n"
                                  + "- bar: val\n"
                                  + "- baz\n"
        )

        # Append to an existing grain, converting to a list a multi-value dict
        self.setGrains({'a': 'aval', 'foo': {'bar': 'val', 'other': 'value'}})
        self.assertGrainFileContent("a: aval\n"
                                  + "foo:\n"
                                  + "  bar: val\n"
                                  + "  other: value\n"
        )
        ret = grains.append(
            name='foo',
            value='baz',
            convert=True)
        self.assertEqual(ret['result'], True)
        self.assertEqual(ret['comment'], 'Value baz was added to grain foo')
        self.assertEqual(ret['changes'], {'added': 'baz'})
        self.assertEqual(
            grains.__grains__,
            {'a': 'aval', 'foo': [{'bar': 'val', 'other': 'value'}, 'baz']})
        self.assertGrainFileContent("a: aval\n"
                                  + "foo:\n"
                                  + "- bar: val\n"
                                  + "  other: value\n"
                                  + "- baz\n"
        )
Exemplo n.º 2
0
 def test_append_convert_to_list(self):
     # Append to an existing grain, converting to a list
     grains.__grains__ = grainsmod.__grains__ = {"a": "aval", "foo": {"bar": "val"}}
     ret = grains.append(name="foo", value="baz", convert=True)
     self.assertEqual(ret["result"], True)
     self.assertEqual(ret["comment"], "Value baz was added to grain foo")
     self.assertEqual(ret["changes"], {"added": "baz"})
     self.assertEqual(grains.__grains__, {"a": "aval", "foo": [{"bar": "val"}, "baz"]})
Exemplo n.º 3
0
 def test_append_already(self):
     # Append to an existing list
     grains.__grains__ = grainsmod.__grains__ = {"a": "aval", "foo": ["bar"]}
     ret = grains.append(name="foo", value="bar")
     self.assertEqual(ret["result"], True)
     self.assertEqual(ret["comment"], "Value bar is already in the list " + "for grain foo")
     self.assertEqual(ret["changes"], {})
     self.assertEqual(grains.__grains__, {"a": "aval", "foo": ["bar"]})
Exemplo n.º 4
0
 def test_append(self):
     # Append to an existing list
     grains.__grains__ = grainsmod.__grains__ = {"a": "aval", "foo": ["bar"]}
     ret = grains.append(name="foo", value="baz")
     self.assertEqual(ret["result"], True)
     self.assertEqual(ret["comment"], "Value baz was added to grain foo")
     self.assertEqual(ret["changes"], {"added": "baz"})
     self.assertEqual(grains.__grains__, {"a": "aval", "foo": ["bar", "baz"]})
Exemplo n.º 5
0
 def test_append_fails_inexistent(self):
     # Append to a non existing grain
     grains.__grains__ = grainsmod.__grains__ = {"a": "aval"}
     ret = grains.append(name="foo", value="bar")
     # Note from dr4Ke: should be false, IMO
     self.assertEqual(ret["result"], False)
     self.assertEqual(ret["comment"], "Grain foo does not exist")
     self.assertEqual(ret["changes"], {})
     self.assertEqual(grains.__grains__, {"a": "aval"})
Exemplo n.º 6
0
 def test_append_fails_not_a_list(self):
     # Fail to append to an existing grain, not a list
     grains.__grains__ = grainsmod.__grains__ = {"a": "aval", "foo": {"bar": "val"}}
     ret = grains.append(name="foo", value="baz")
     # Note from dr4Ke: should be false, IMO
     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": "val"}})
Exemplo n.º 7
0
 def test_append_fails_not_a_list(self):
     # Fail to append to an existing grain, not a list
     self.setGrains({'a': 'aval', 'foo': {'bar': 'val'}})
     ret = grains.append(
         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': 'val'}})
Exemplo n.º 8
0
 def test_append_fails_inexistent(self):
     # Append to a non existing grain
     self.setGrains({'a': 'aval'})
     ret = grains.append(
         name='foo',
         value='bar')
     self.assertEqual(ret['result'], False)
     self.assertEqual(ret['comment'], 'Grain foo does not exist')
     self.assertEqual(ret['changes'], {})
     self.assertEqual(
         grains.__grains__,
         {'a': 'aval'})
Exemplo n.º 9
0
 def test_append_fails_not_a_list(self):
     # Fail to append to an existing grain, not a list
     with self.setGrains({"a": "aval", "foo": {"bar": "val"}}):
         ret = grains.append(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": "val"
             }
         })
Exemplo n.º 10
0
 def test_append(self):
     # Append to an existing list
     self.setGrains({'a': 'aval', 'foo': ['bar']})
     ret = grains.append(name='foo', value='baz')
     self.assertEqual(ret['result'], True)
     self.assertEqual(ret['comment'], 'Value baz was added to grain foo')
     self.assertEqual(ret['changes'], {'added': 'baz'})
     self.assertEqual(grains.__grains__, {
         'a': 'aval',
         'foo': ['bar', 'baz']
     })
     self.assertGrainFileContent("a: aval\n" + "foo:\n" + "- bar\n" +
                                 "- baz\n")
Exemplo n.º 11
0
 def test_append_fails_not_a_list(self):
     # Fail to append to an existing grain, not a list
     self.setGrains({'a': 'aval', 'foo': {'bar': 'val'}})
     ret = grains.append(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': 'val'
         }
     })
Exemplo n.º 12
0
 def test_append_fails_not_a_list(self):
     # Fail to append to an existing grain, not a list
     grains.__grains__ = grainsmod.__grains__ = {'a': 'aval', 'foo': {'bar': 'val'}}
     ret = grains.append(
         name='foo',
         value='baz')
     # Note from dr4Ke: should be false, IMO
     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': 'val'}})
Exemplo n.º 13
0
    def test_append(self):
        '''
        Test to append a value to a list in the grains config file
        '''
        ret = {'changes': {}, 'name': self.name, 'result': True,
               'comment': 'Value edam is already in the list for grain cheese'}

        ret1 = {'changes': {'added': self.value}, 'name': self.name,
                'result': None,
                'comment': 'Value edam in grain cheese is set to be added'}

        comment = ('Grain cheese is set to be converted to list'
                   ' and value edam will be added')
        ret2 = {'changes': {'added': self.value}, 'name': self.name,
                'result': None,
                'comment': comment}

        ret3 = {'changes': {}, 'name': self.name, 'result': False,
                'comment': 'Grain cheese does not exist'}

        ret4 = {'changes': {}, 'name': self.name, 'result': False,
                'comment': 'Grain cheese is not a valid list'}

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

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

        with patch.dict(grains.__grains__, {self.name: 'edam'}):
            with patch.dict(grains.__opts__, {'test': True}):
                self.assertDictEqual(grains.append(self.name, self.value,
                                                   convert=True), ret2)

        self.assertDictEqual(grains.append(self.name, self.value), ret3)

        with patch.dict(grains.__grains__, {self.name: 'eves'}):
            self.assertDictEqual(grains.append(self.name, self.value), ret4)
Exemplo n.º 14
0
 def test_append_already(self):
     # Append to an existing list
     grains.__grains__ = grainsmod.__grains__ = {'a': 'aval', 'foo': ['bar']}
     ret = grains.append(
         name='foo',
         value='bar')
     self.assertEqual(ret['result'], True)
     self.assertEqual(ret['comment'], 'Value bar is already in the list '
                                    + 'for grain foo')
     self.assertEqual(ret['changes'], {})
     self.assertEqual(
         grains.__grains__,
         {'a': 'aval', 'foo': ['bar']})
Exemplo n.º 15
0
 def test_append(self):
     # Append to an existing list
     grains.__grains__ = grainsmod.__grains__ = {
         'a': 'aval',
         'foo': ['bar']
     }
     ret = grains.append(name='foo', value='baz')
     self.assertEqual(ret['result'], True)
     self.assertEqual(ret['comment'], 'Value baz was added to grain foo')
     self.assertEqual(ret['changes'], {'added': 'baz'})
     self.assertEqual(grains.__grains__, {
         'a': 'aval',
         'foo': ['bar', 'baz']
     })
Exemplo n.º 16
0
 def test_append(self):
     # Append to an existing list
     with self.setGrains({"a": "aval", "foo": ["bar"]}):
         ret = grains.append(name="foo", value="baz")
         self.assertEqual(ret["result"], True)
         self.assertEqual(ret["comment"],
                          "Value baz was added to grain foo")
         self.assertEqual(ret["changes"], {"added": "baz"})
         self.assertEqual(grains.__grains__, {
             "a": "aval",
             "foo": ["bar", "baz"]
         })
         self.assertGrainFileContent("a: aval\n" + "foo:\n" + "- bar\n" +
                                     "- baz\n")
Exemplo n.º 17
0
 def test_append_convert_to_list_empty(self):
     # Append to an existing list
     with self.setGrains({'foo': None}):
         ret = grains.append(name='foo',
                             value='baz',
                             convert=True)
         self.assertEqual(ret['result'], True)
         self.assertEqual(ret['comment'], 'Value baz was added to grain foo')
         self.assertEqual(ret['changes'], {'added': 'baz'})
         self.assertEqual(
             grains.__grains__,
             {'foo': ['baz']})
         self.assertGrainFileContent("foo:\n"
                                   + "- baz\n")
Exemplo n.º 18
0
 def test_append_nested(self):
     # Append to an existing nested list
     with self.setGrains({'a': 'aval', 'foo': {'list': ['bar']}}):
         ret = grains.append(name='foo,list', value='baz', delimiter=',')
         self.assertEqual(ret['result'], True)
         self.assertEqual(ret['comment'],
                          'Value baz was added to grain foo:list')
         self.assertEqual(ret['changes'], {'added': 'baz'})
         self.assertEqual(grains.__grains__, {
             'a': 'aval',
             'foo': {
                 'list': ['bar', 'baz']
             }
         })
         self.assertGrainFileContent("a: aval\n" + "foo:\n" + "  list:\n" +
                                     "  - bar\n" + "  - baz\n")
Exemplo n.º 19
0
 def test_append_already(self):
     # Append to an existing list
     self.setGrains({'a': 'aval', 'foo': ['bar']})
     ret = grains.append(
         name='foo',
         value='bar')
     self.assertEqual(ret['result'], True)
     self.assertEqual(ret['comment'], 'Value bar is already in the list '
                                    + 'for 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.º 20
0
 def test_append(self):
     # Append to an existing list
     self.setGrains({'a': 'aval', 'foo': ['bar']})
     ret = grains.append(
         name='foo',
         value='baz')
     self.assertEqual(ret['result'], True)
     self.assertEqual(ret['comment'], 'Value baz was added to grain foo')
     self.assertEqual(ret['changes'], {'added': 'baz'})
     self.assertEqual(
         grains.__grains__,
         {'a': 'aval', 'foo': ['bar', 'baz']})
     self.assertGrainFileContent("a: aval\n"
                               + "foo:\n"
                               + "- bar\n"
                               + "- baz\n"
     )
Exemplo n.º 21
0
 def test_append_already(self):
     # Append to an existing list
     with self.setGrains({'a': 'aval', 'foo': ['bar']}):
         ret = grains.append(
             name='foo',
             value='bar')
         self.assertEqual(ret['result'], True)
         self.assertEqual(ret['comment'], 'Value bar is already in the list '
                                        + 'for 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_append_convert_to_list(self):
     # Append to an existing grain, converting to a list
     grains.__grains__ = grainsmod.__grains__ = {
         'a': 'aval',
         'foo': {
             'bar': 'val'
         }
     }
     ret = grains.append(name='foo', value='baz', convert=True)
     self.assertEqual(ret['result'], True)
     self.assertEqual(ret['comment'], 'Value baz was added to grain foo')
     self.assertEqual(ret['changes'], {'added': 'baz'})
     self.assertEqual(grains.__grains__, {
         'a': 'aval',
         'foo': [{
             'bar': 'val'
         }, 'baz']
     })
Exemplo n.º 23
0
    def test_append(self):
        '''
        Test to append a value to a list in the grains config file
        '''
        ret = {
            'changes': {},
            'name': self.name,
            'result': True,
            'comment': 'Value edam is already in the list for grain cheese'
        }

        ret1 = {
            'changes': {
                'added': self.value
            },
            'name': self.name,
            'result': None,
            'comment': 'Value edam in grain cheese is set to be added'
        }

        comment = ('Grain cheese is set to be converted to list'
                   ' and value edam will be added')
        ret2 = {
            'changes': {
                'added': self.value
            },
            'name': self.name,
            'result': None,
            'comment': comment
        }

        ret3 = {
            'changes': {},
            'name': self.name,
            'result': False,
            'comment': 'Grain cheese does not exist'
        }

        ret4 = {
            'changes': {},
            'name': self.name,
            'result': False,
            'comment': 'Grain cheese is not a valid list'
        }

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

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

        with patch.dict(grains.__grains__, {self.name: 'edam'}):
            with patch.dict(grains.__opts__, {'test': True}):
                self.assertDictEqual(
                    grains.append(self.name, self.value, convert=True), ret2)

        self.assertDictEqual(grains.append(self.name, self.value), ret3)

        with patch.dict(grains.__grains__, {self.name: 'eves'}):
            self.assertDictEqual(grains.append(self.name, self.value), ret4)