#!/opt/csw/bin/python2.6

import Cheetah
import mox
import unittest

from lib.python import integrate_catalogs

OUT_1 = u"""#!/bin/bash
# Catalog modification (not integration yet): unstable-fake -> kiel-fake
# Generated by fake_prog


if ! grep buildfarm ~/.netrc
then
  touch ~/.netrc
  chmod 0600 ~/.netrc
  echo >> ~/.netrc     "machine buildfarm.opencsw.org login ${LOGNAME} password $(cat /etc/opt/csw/releases/auth/${LOGNAME})"
fi

set -e

readonly CURL="curl --netrc"
readonly REST_URL=http://buildfarm.opencsw.org/releases/

function _add_to_cat {
  ${CURL} -X PUT ${REST_URL}catalogs/$1/$2/$3/$4/ ; echo
}

function _del_from_cat {
  ${CURL} -X DELETE ${REST_URL}catalogs/$1/$2/$3/$4/ ; echo
}

"""

OUT_2 = OUT_1 + """function new_pkg_fake_catalogname_1 {
  # adding fake_basename_1.pkg
  _add_to_cat kiel-fake i386 SunOS5.10 6110aad210240504ede48f9cd8b4501c
}
function undo_new_pkg_fake_catalogname_1 {
  # UNDO adding fake_basename_1.pkg
  _del_from_cat kiel-fake i386 SunOS5.10 6110aad210240504ede48f9cd8b4501c
}

function new_pkg_fake_catalogname_2 {
  # adding fake_basename_2.pkg
  _add_to_cat kiel-fake sparc SunOS5.10 6110aad210240504ede48f9cd8b4501d
}
function undo_new_pkg_fake_catalogname_2 {
  # UNDO adding fake_basename_2.pkg
  _del_from_cat kiel-fake sparc SunOS5.10 6110aad210240504ede48f9cd8b4501d
}
function upgrade_fake_catalogname_2 {
  # fake_catalogname_2 upgrade from 1.1.31,REV=2013.01.07 to 1.0,REV=2013.01.08
  _del_from_cat kiel-fake i386 SunOS5.10 6110aad210240504ede48f9cd8b4501c
  _add_to_cat kiel-fake i386 SunOS5.10 6110aad210240504ede48f9cd8b4501d
}
function undo_upgrade_fake_catalogname_2 {
  # UNDO of fake_catalogname_2 upgrade from 1.1.31,REV=2013.01.07 to 1.0,REV=2013.01.08
  _del_from_cat kiel-fake i386 SunOS5.10 6110aad210240504ede48f9cd8b4501d
  _add_to_cat kiel-fake i386 SunOS5.10 6110aad210240504ede48f9cd8b4501c
}

new_pkg_fake_catalogname_1
 # bundles:
new_pkg_fake_catalogname_2
upgrade_fake_catalogname_2 # version 1.1.31,REV=2013.01.07 to 1.0,REV=2013.01.08 # bundles:
"""

class TemplateUnitTest(mox.MoxTestBase):

  def testTemplateMinimal(self):
    self.maxDiff = None
    bundles_by_catalogname = {}
    bundles_by_md5 = {}
    diffs_by_catalogname = {}
    namespace = {
        "bundles_by_catalogname": bundles_by_catalogname,
        "bundles_by_md5": bundles_by_md5,
        "diffs_by_catalogname": diffs_by_catalogname,
        "catrel_to": "kiel-fake",
        "catrel_from": "unstable-fake",
        "prog": "fake_prog",
    }
    t = Cheetah.Template.Template(integrate_catalogs.CATALOG_MOD_TMPL, searchList=[namespace])
    # Compare line by line for better output.
    self.assertEqual(OUT_1.splitlines(), unicode(t).splitlines())

  def testTemplateUpgrade(self):
    self.maxDiff = None
    bundles_by_catalogname = {
        "fake_catalogname_1" : [],
        "fake_catalogname_2" : [],
    }
    bundles_by_md5 = {
        "fake_md5": [],
    }
    catalogs = {
        ("unstable-fake", "i386", "SunOS5.8"): [],
        ("kiel-fake", "i386", "SunOS5.8"): [],
        ("unstable-fake", "i386", "SunOS5.9"): [],
        ("kiel-fake", "i386", "SunOS5.9"): [],
        ("unstable-fake", "i386", "SunOS5.10"): [
            {
                "basename": "fake_basename_1.pkg",
                "catalogname": "fake_catalogname_1",
                "md5_sum": "6110aad210240504ede48f9cd8b4501c",
                "version": "1.1.30,REV=2013.01.08",
            },
            {
                "basename": "fake_basename_2.pkg",
                "catalogname": "fake_catalogname_2",
                "md5_sum": "6110aad210240504ede48f9cd8b4501d",
                "version": "1.0,REV=2013.01.08",
            },
        ],
        ("kiel-fake", "i386", "SunOS5.10"): [
            {
                "basename": "fake_basename_2.pkg",
                "catalogname": "fake_catalogname_2",
                "md5_sum": "6110aad210240504ede48f9cd8b4501c",
                "version": "1.1.31,REV=2013.01.07",
            },
        ],
        ("unstable-fake", "i386", "SunOS5.11"): [],
        ("kiel-fake", "i386", "SunOS5.11"): [],

        ("unstable-fake", "sparc", "SunOS5.8"): [],
        ("kiel-fake", "sparc", "SunOS5.8"): [],
        ("unstable-fake", "sparc", "SunOS5.9"): [],
        ("kiel-fake", "sparc", "SunOS5.9"): [],
        ("unstable-fake", "sparc", "SunOS5.10"): [
            {
                "basename": "fake_basename_2.pkg",
                "catalogname": "fake_catalogname_2",
                "md5_sum": "6110aad210240504ede48f9cd8b4501d",
                "version": "1.0,REV=2013.01.08",
            },
        ],
        ("kiel-fake", "sparc", "SunOS5.10"): [],
        ("unstable-fake", "sparc", "SunOS5.11"): [],
        ("kiel-fake", "sparc", "SunOS5.11"): [],
    }
    diffs_by_catalogname = integrate_catalogs.ComposeDiffsByCatalogname(
        catalogs, "unstable-fake", "kiel-fake", True, True)
    namespace = {
        "bundles_by_catalogname": bundles_by_catalogname,
        "bundles_by_md5": bundles_by_md5,
        "diffs_by_catalogname": diffs_by_catalogname,
        "catrel_to": "kiel-fake",
        "catrel_from": "unstable-fake",
        "prog": "fake_prog",
    }
    t = Cheetah.Template.Template(integrate_catalogs.CATALOG_MOD_TMPL, searchList=[namespace])
    # Compare line by line for better output.
    self.assertEqual(OUT_2.splitlines(), unicode(t).splitlines())


if __name__ == '__main__':
  unittest.main()
