# Path: gocdapi/go.py
# class Go(object):
#     """
#     Class that represents Go Server environment.
#     """
#     def __init__(self, baseurl, username=None, password=None):
#         """Inits Go objects.
# 
#         Args:
#             baseurl (str): The base url of Go Server
#             username (str): The username to login in the Go Server
#             password (str): The username's password
#         """
#         self.baseurl = baseurl
#         self.username = username
#         self.password = password
# 
#     @property
#     def agents(self):
#         """Return agents of the Go Server
#         """
#         return Agents(self)
# 
#     @property
#     def pipeline_groups(self):
#         """Return Pipeline Groups of the Go Server
#         """
#         return PipelineGroups(self)
# 
#     @property
#     def pipelines(self):
#         """Return Pipeline Groups of the Go Server
#         """
#         return dict(self._iter_through_pipelines())
# 
#     @property
#     def admin(self):
#         """Return an Admin object
#         """
#         return Admin(self)
# 
#     def pipeline_exist(self, name):
#         """ Check if pipeline exists.
# 
#         Args:
#             name (str): name of the pipeline to find
# 
#         Returns:
#             bool: True if pipeline with the given name exists
#         """
#         return name in self.pipelines
# 
#     def get_pipeline(self, name):
#         """ Return pipeline if exists.
# 
#         Args:
#             name (str): name of the pipeline to get
# 
#         Returns:
#             pipeline: Pipeline with the given name or None if it doesn't exist.
#         """
#         return self.pipelines.get(name, None)
# 
#     def _iter_through_pipelines(self):
#         """ Return a iterable containing every pair pipeline_name, pipeline
# 
#         Generator: Yields a pair pipeline_name, pipeline
#         """
#         for pipeline_group in self.pipeline_groups.values():
#             for pipeline_name, pipeline in pipeline_group.iteritems():
#                 yield pipeline_name, pipeline
# 
# Path: gocdapi_tests/systests/pipeline_configs.py
# EMPTY_PIPELINE = '''\
# <pipeline name="%s">
#   <materials>
#     <git url="git://127.0.0.1/precommit.git"/>
#   </materials>
#   <stage name="stage1">
#     <environmentvariables>
#       <variable name="PYTHONUNBUFFERED">
#         <value>1</value>
#       </variable>
#     </environmentvariables>
#     <jobs>
#       <job name="job1">
#         <tasks>
#           <exec command="python">
#             <arg>foo.py</arg>
#             <runif status="passed"/>
#           </exec>
#         </tasks>
#       </job>
#     </jobs>
#   </stage>
#   <stage name="stage2">
#     <environmentvariables>
#       <variable name="PYTHONUNBUFFERED">
#         <value>True</value>
#       </variable>
#     </environmentvariables>
#     <jobs>
#       <job name="job1">
#         <tasks>
#           <exec command="python">
#             <arg>bar.py</arg>
#             <runif status="passed"/>
#           </exec>
#         </tasks>
#       </job>
#     </jobs>
#   </stage>
# </pipeline>
# '''.strip()


import logging
import time
import unittest
from gocdapi.go import Go
from gocdapi_tests.systests.pipeline_configs import EMPTY_PIPELINE


log = logging.getLogger(__name__)

DEFAULT_GO_URL = "http://localhost"
DEFAULT_GO_PORT = 8153
DEFAULT_GO_PIPELINE_GROUP = "GO_SYSTESTS"


class BaseSystemTest(unittest.TestCase):
    def pipeline_group_name(self):
        return DEFAULT_GO_PIPELINE_GROUP

    def setUp(self):
        self.go = Go('%s:%d' % (DEFAULT_GO_URL, DEFAULT_GO_PORT))
        self._delete_test_pipelines_group(self.pipeline_group_name())
        self._create_test_pipelines_group(self.pipeline_group_name())
        self.go.admin.create_pipeline_from_xml(self.pipeline_group_name(), EMPTY_PIPELINE % "pipe_for_tests")

    def tearDown(self):
        self._delete_test_pipelines_group(self.pipeline_group_name())

    def _delete_test_pipelines_group(self, name):
        if name in self.go.pipeline_groups:

# Path: gocdapi/go.py
# class Go(object):
#     """
#     Class that represents Go Server environment.
#     """
#     def __init__(self, baseurl, username=None, password=None):
#         """Inits Go objects.
# 
#         Args:
#             baseurl (str): The base url of Go Server
#             username (str): The username to login in the Go Server
#             password (str): The username's password
#         """
#         self.baseurl = baseurl
#         self.username = username
#         self.password = password
# 
#     @property
#     def agents(self):
#         """Return agents of the Go Server
#         """
#         return Agents(self)
# 
#     @property
#     def pipeline_groups(self):
#         """Return Pipeline Groups of the Go Server
#         """
#         return PipelineGroups(self)
# 
#     @property
#     def pipelines(self):
#         """Return Pipeline Groups of the Go Server
#         """
#         return dict(self._iter_through_pipelines())
# 
#     @property
#     def admin(self):
#         """Return an Admin object
#         """
#         return Admin(self)
# 
#     def pipeline_exist(self, name):
#         """ Check if pipeline exists.
# 
#         Args:
#             name (str): name of the pipeline to find
# 
#         Returns:
#             bool: True if pipeline with the given name exists
#         """
#         return name in self.pipelines
# 
#     def get_pipeline(self, name):
#         """ Return pipeline if exists.
# 
#         Args:
#             name (str): name of the pipeline to get
# 
#         Returns:
#             pipeline: Pipeline with the given name or None if it doesn't exist.
#         """
#         return self.pipelines.get(name, None)
# 
#     def _iter_through_pipelines(self):
#         """ Return a iterable containing every pair pipeline_name, pipeline
# 
#         Generator: Yields a pair pipeline_name, pipeline
#         """
#         for pipeline_group in self.pipeline_groups.values():
#             for pipeline_name, pipeline in pipeline_group.iteritems():
#                 yield pipeline_name, pipeline
# 
# Path: gocdapi_tests/systests/pipeline_configs.py
# EMPTY_PIPELINE = '''\
# <pipeline name="%s">
#   <materials>
#     <git url="git://127.0.0.1/precommit.git"/>
#   </materials>
#   <stage name="stage1">
#     <environmentvariables>
#       <variable name="PYTHONUNBUFFERED">
#         <value>1</value>
#       </variable>
#     </environmentvariables>
#     <jobs>
#       <job name="job1">
#         <tasks>
#           <exec command="python">
#             <arg>foo.py</arg>
#             <runif status="passed"/>
#           </exec>
#         </tasks>
#       </job>
#     </jobs>
#   </stage>
#   <stage name="stage2">
#     <environmentvariables>
#       <variable name="PYTHONUNBUFFERED">
#         <value>True</value>
#       </variable>
#     </environmentvariables>
#     <jobs>
#       <job name="job1">
#         <tasks>
#           <exec command="python">
#             <arg>bar.py</arg>
#             <runif status="passed"/>
#           </exec>
#         </tasks>
#       </job>
#     </jobs>
#   </stage>
# </pipeline>
# '''.strip()


# Path: gocdapi_tests/systests/base.py
import logging
import time
import unittest
from gocdapi.go import Go
from gocdapi_tests.systests.pipeline_configs import EMPTY_PIPELINE



log = logging.getLogger(__name__)

DEFAULT_GO_URL = "http://localhost"
DEFAULT_GO_PORT = 8153
DEFAULT_GO_PIPELINE_GROUP = "GO_SYSTESTS"


class BaseSystemTest(unittest.TestCase):
    def pipeline_group_name(self):
        return DEFAULT_GO_PIPELINE_GROUP

    def setUp(self):
        self.go = Go('%s:%d' % (DEFAULT_GO_URL, DEFAULT_GO_PORT))
        self._delete_test_pipelines_group(self.pipeline_group_name())
        self._create_test_pipelines_group(self.pipeline_group_name())
        self.go.admin.create_pipeline_from_xml(self.pipeline_group_name(), EMPTY_PIPELINE % "pipe_for_tests")

    def tearDown(self):
        self._delete_test_pipelines_group(self.pipeline_group_name())

    def _delete_test_pipelines_group(self, name):
        if name in self.go.pipeline_groups:

            self.go.admin.delete_pipeline_group(name)