@patch('salt.modules.hosts.__get_hosts_filename',
           MagicMock(return_value='/etc/hosts'))
    @patch('os.path.isfile', MagicMock(return_value=True))
    def test_set_host_true_remove(self):
        data = ['\n'.join((
            '1.1.1.1 foo.foofoo foo',
            '2.2.2.2 bar.barbar bar',
            '3.3.3.3 asdf.asdfadsf asdf',
            '1.1.1.1 foofoo.foofoo foofoo',
        ))]

        class TmpStringIO(StringIO):
            def __init__(self, fn, mode='r'):
                initial_value = data[0]
                if 'w' in mode:
                    initial_value = ''
                StringIO.__init__(self, initial_value)

            def __enter__(self):
                return self

            def __exit__(self, exc_type, exc_value, traceback):
                self.close()

            def close(self):
                data[0] = self.getvalue()
                StringIO.close(self)

        expected = '\n'.join((
            '2.2.2.2 bar.barbar bar',
            '3.3.3.3 asdf.asdfadsf asdf',
        )) + '\n'

        with patch('salt.utils.fopen', TmpStringIO):
            mock_opt = MagicMock(return_value=None)
            with patch.dict(hosts.__salt__, {'config.option': mock_opt}):
                self.assertTrue(hosts.set_host('1.1.1.1', ' '))
        self.assertEqual(data[0], expected)

    # 'rm_host' function tests: 2
Test if an empty hosts value removes existing entries
dataset/ETHPy150Open saltstack/salt/tests/unit/modules/hosts_test.py/HostsTestCase.test_set_host_true_remove