# Path: crmsh/parse.py
# def parse(self, cmd):
#     "Called by do_parse(). Raises ParseError if parsing fails."
#     raise NotImplementedError
# 
# Path: crmsh/utils.py
# def lines2cli(s):
#     '''
#     Convert a string into a list of lines. Replace continuation
#     characters. Strip white space, left and right. Drop empty lines.
#     '''
#     cl = []
#     l = s.split('\n')
#     cum = []
#     for p in l:
#         p = p.strip()
#         if p.endswith('\\'):
#             p = p.rstrip('\\')
#             cum.append(p)
#         else:
#             cum.append(p)
#             cl.append(''.join(cum).strip())
#             cum = []
#     if cum:  # in case s ends with backslash
#         cl.append(''.join(cum))
#     return [x for x in cl if x]
# 
# Path: crmsh/xmlutil.py
# def xml_tostring(*args, **kwargs):
#     """
#     Python 2/3 conversion utility:
#     etree.tostring returns a bytestring, but
#     we need actual Python strings.
#     """
#     return etree.tostring(*args, **kwargs).decode('utf-8')


    from unittest import mock
from builtins import zip
from crmsh import parse
from crmsh.utils import lines2cli
from crmsh.xmlutil import xml_tostring
from lxml import etree
    import mock
import unittest
import shlex

        out = self._parse('node node-1:unknown')
        self.assertFalse(out)

        out = self._parse('node node-1 description="foo bar" attributes foo=bar')
        self.assertEqual(out.get('description'), 'foo bar')
        self.assertEqual(['bar'], out.xpath('instance_attributes/nvpair[@name="foo"]/@value'))

        out = self._parse('node node-1 attributes foo=bar utilization wiz=bang')
        self.assertEqual(['bar'], out.xpath('instance_attributes/nvpair[@name="foo"]/@value'))
        self.assertEqual(['bang'], out.xpath('utilization/nvpair[@name="wiz"]/@value'))

    @mock.patch('logging.Logger.error')
    def test_resources(self, mock_error):
        out = self._parse('primitive www ocf:heartbeat:apache op monitor timeout=10s')
        self.assertEqual(out.get('id'), 'www')
        self.assertEqual(out.get('class'), 'ocf')
        self.assertEqual(['monitor'], out.xpath('//op/@name'))

        out = self._parse('rsc_template public_vm ocf:heartbeat:Xen op start timeout=300s op stop timeout=300s op monitor interval=30s timeout=60s op migrate_from timeout=600s op migrate_to timeout=600s')
        self.assertEqual(out.get('id'), 'public_vm')
        self.assertEqual(out.get('class'), 'ocf')
        #print out

        out = self._parse('primitive st stonith:ssh params hostlist=node1 meta target-role=Started requires=nothing op start timeout=60s op monitor interval=60m timeout=60s')
        self.assertEqual(out.get('id'), 'st')

        out2 = self._parse('primitive st stonith:ssh hostlist=node1 meta target-role=Started requires=nothing op start timeout=60s op monitor interval=60m timeout=60s')
        self.assertEqual(out2.get('id'), 'st')


# Path: crmsh/parse.py
# def parse(self, cmd):
#     "Called by do_parse(). Raises ParseError if parsing fails."
#     raise NotImplementedError
# 
# Path: crmsh/utils.py
# def lines2cli(s):
#     '''
#     Convert a string into a list of lines. Replace continuation
#     characters. Strip white space, left and right. Drop empty lines.
#     '''
#     cl = []
#     l = s.split('\n')
#     cum = []
#     for p in l:
#         p = p.strip()
#         if p.endswith('\\'):
#             p = p.rstrip('\\')
#             cum.append(p)
#         else:
#             cum.append(p)
#             cl.append(''.join(cum).strip())
#             cum = []
#     if cum:  # in case s ends with backslash
#         cl.append(''.join(cum))
#     return [x for x in cl if x]
# 
# Path: crmsh/xmlutil.py
# def xml_tostring(*args, **kwargs):
#     """
#     Python 2/3 conversion utility:
#     etree.tostring returns a bytestring, but
#     we need actual Python strings.
#     """
#     return etree.tostring(*args, **kwargs).decode('utf-8')


# Path: test/unittests/test_parse.py
    from unittest import mock
from builtins import zip
from crmsh import parse
from crmsh.utils import lines2cli
from crmsh.xmlutil import xml_tostring
from lxml import etree
    import mock
import unittest
import shlex


        out = self._parse('node node-1:unknown')
        self.assertFalse(out)

        out = self._parse('node node-1 description="foo bar" attributes foo=bar')
        self.assertEqual(out.get('description'), 'foo bar')
        self.assertEqual(['bar'], out.xpath('instance_attributes/nvpair[@name="foo"]/@value'))

        out = self._parse('node node-1 attributes foo=bar utilization wiz=bang')
        self.assertEqual(['bar'], out.xpath('instance_attributes/nvpair[@name="foo"]/@value'))
        self.assertEqual(['bang'], out.xpath('utilization/nvpair[@name="wiz"]/@value'))

    @mock.patch('logging.Logger.error')
    def test_resources(self, mock_error):
        out = self._parse('primitive www ocf:heartbeat:apache op monitor timeout=10s')
        self.assertEqual(out.get('id'), 'www')
        self.assertEqual(out.get('class'), 'ocf')
        self.assertEqual(['monitor'], out.xpath('//op/@name'))

        out = self._parse('rsc_template public_vm ocf:heartbeat:Xen op start timeout=300s op stop timeout=300s op monitor interval=30s timeout=60s op migrate_from timeout=600s op migrate_to timeout=600s')
        self.assertEqual(out.get('id'), 'public_vm')
        self.assertEqual(out.get('class'), 'ocf')
        #print out

        out = self._parse('primitive st stonith:ssh params hostlist=node1 meta target-role=Started requires=nothing op start timeout=60s op monitor interval=60m timeout=60s')
        self.assertEqual(out.get('id'), 'st')

        out2 = self._parse('primitive st stonith:ssh hostlist=node1 meta target-role=Started requires=nothing op start timeout=60s op monitor interval=60m timeout=60s')
        self.assertEqual(out2.get('id'), 'st')


        self.assertEqual(xml_tostring(out), xml_tostring(out2))