def test_plain_content_with_modes_exists(self, mock_find_and_mount):
        mock_find_and_mount.return_value.__enter__.return_value = self.path

        fl = {
            'path': '/etc/dir1/dir2/file.name',
            'content': 'Y29udGVudA==',
            'mode': 0o602,
            'dirmode': 0o703
        }
        os.makedirs(self.dirpath)
        with open(self.path, 'wb') as fp:
            fp.write(b"I'm not a cat, I'm a lawyer")

        inject_files._inject_one(self.node, self.ports, fl, '/dev/root',
                                 self.http_get)

        with open(self.path, 'rb') as fp:
            self.assertEqual(b'content', fp.read())
        self.assertEqual(0o602, stat.S_IMODE(os.stat(self.path).st_mode))
        # Exising directories do not change their permissions
        self.assertNotEqual(0o703, stat.S_IMODE(os.stat(self.dirpath).st_mode))

        mock_find_and_mount.assert_called_once_with(fl['path'], None,
                                                    '/dev/root')
        self.http_get.assert_not_called()
    def test_plain_content(self, mock_find_and_mount):
        mock_find_and_mount.return_value.__enter__.return_value = self.path

        fl = {'path': '/etc/dir1/dir2/file.name', 'content': 'Y29udGVudA=='}
        inject_files._inject_one(self.node, self.ports, fl, '/dev/root',
                                 self.http_get)

        with open(self.path, 'rb') as fp:
            self.assertEqual(b'content', fp.read())
        mock_find_and_mount.assert_called_once_with(fl['path'], None,
                                                    '/dev/root')
        self.http_get.assert_not_called()
    def test_delete_not_exists(self, mock_find_and_mount):
        mock_find_and_mount.return_value.__enter__.return_value = self.path

        fl = {'path': '/etc/dir1/dir2/file.name', 'deleted': True}
        inject_files._inject_one(self.node, self.ports, fl, '/dev/root',
                                 self.http_get)

        self.assertFalse(os.path.exists(self.path))
        self.assertFalse(os.path.isdir(self.dirpath))
        mock_find_and_mount.assert_called_once_with(fl['path'], None,
                                                    '/dev/root')
        self.http_get.assert_not_called()
    def test_url_formatting(self, mock_find_and_mount):
        mock_find_and_mount.return_value.__enter__.return_value = self.path

        fl = {
            'path': '/etc/dir1/dir2/file.name',
            'content': 'http://example.com/{node[uuid]}/{ports[0][address]}'
        }
        inject_files._inject_one(self.node, self.ports, fl, '/dev/root',
                                 self.http_get)

        with open(self.path, 'rb') as fp:
            self.assertEqual(b'content', fp.read())

        mock_find_and_mount.assert_called_once_with(fl['path'], None,
                                                    '/dev/root')
        self.http_get.assert_called_once_with('http://example.com/1234/aabb')