Skip to main content

Source code file content

Revision: 2910

15654935 pkg facet/variant should also display those implicitly set 15713232 pkg change-facet/change-variant could be faster
» Project Revision History

» Checkout URL

pkg-gate / src / tests / cli / t_pkg_varcet.py

Size: 22422 bytes, 1 line
#!/usr/bin/python
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#

# Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.

import testutils
if __name__ == "__main__":
        testutils.setup_environment("../../../proto")
import pkg5unittest

import os
import pkg.fmri as fmri
import pkg.portable as portable
import pkg.misc as misc
import pkg.p5p
import shutil
import stat
import tempfile
import unittest


class TestPkgVarcet(pkg5unittest.SingleDepotTestCase):

        # Don't discard repository or setUp() every test.
        persistent_setup = True

        pkg_foo = """
            open foo@1.0
            add file tmp/non-debug path=usr/bin/foo mode=0755 owner=root group=root
            add file tmp/man path=usr/man/man1/foo.1 mode=0444 owner=root group=root
            close
            open foo@2.0
            add set name=variant.icecream value=neapolitan value=strawberry
            add file tmp/debug path=usr/bin/foo mode=0755 owner=root group=root variant.debug.foo=true
            add file tmp/non-debug path=usr/bin/foo mode=0755 owner=root group=root variant.debug.foo=false
            add file tmp/neapolitan path=etc/icecream mode=0644 owner=root group=root variant.icecream=neapolitan
            add file tmp/strawberry path=etc/icecream mode=0644 owner=root group=root variant.icecream=strawberry
            add file tmp/man path=usr/man/man1/foo.1 mode=0444 owner=root group=root facet.doc.man=true
            add file tmp/doc path=usr/share/foo/README mode=0444 owner=root group=root facet.doc.txt=true
            add file tmp/pdf path=usr/share/foo/README.pdf mode=0444 owner=root group=root facet.doc.pdf=true
            open foo@3.0
            add set name=pkg.facet value=doc.man value=doc.txt value=doc.pdf
            add set name=pkg.variant value=icecream value=debug.foo
            add set name=variant.icecream value=neapolitan value=strawberry
            add file tmp/debug path=usr/bin/foo mode=0755 owner=root group=root variant.debug.foo=true
            add file tmp/non-debug path=usr/bin/foo mode=0755 owner=root group=root variant.debug.foo=false
            add file tmp/neapolitan path=etc/icecream mode=0644 owner=root group=root variant.icecream=neapolitan
            add file tmp/strawberry path=etc/icecream mode=0644 owner=root group=root variant.icecream=strawberry
            add file tmp/man path=usr/man/man1/foo.1 mode=0444 owner=root group=root facet.doc.man=true
            add file tmp/doc path=usr/share/foo/README mode=0444 owner=root group=root facet.doc.txt=true
            add file tmp/pdf path=usr/share/foo/README.pdf mode=0444 owner=root group=root facet.doc.pdf=true
            close """

        pkg_unknown = """
            open unknown@1.0
            add set name=variant.unknown value=bar value=foo
            add file tmp/non-debug path=usr/bin/bar mode=0755 owner=root group=root variant.unknown=bar
            add file tmp/non-debug path=usr/bin/foo mode=0755 owner=root group=root variant.unknown=foo
            close """

        misc_files = ["tmp/debug", "tmp/non-debug", "tmp/neapolitan",
            "tmp/strawberry", "tmp/doc", "tmp/man", "tmp/pdf"]

        def setUp(self):
                pkg5unittest.SingleDepotTestCase.setUp(self)
                self.make_misc_files(self.misc_files)
                self.pkgsend_bulk(self.rurl, [
                    getattr(self, p)
                    for p in dir(self)
                    if p.startswith("pkg_") and isinstance(getattr(self, p),
                        basestring)
                ])

        def __assert_varcet_matches_default(self, cmd, expected, errout=None,
            exit=0, opts=misc.EmptyI, names=misc.EmptyI, su_wrap=False):
                if errout is None and exit != 0:
                        # Assume there should be error output for non-zero exit
                        # if not explicitly indicated.
                        errout = True

                self.pkg("%s %s -H %s" % (cmd, " ".join(opts), " ".join(names)),
                    exit=exit, su_wrap=su_wrap)
                self.assertEqualDiff(expected, self.reduceSpaces(self.output))
                if errout:
                        self.assert_(self.errout != "")
                else:
                        self.assertEqualDiff("", self.errout)

        def __assert_varcet_matches_tsv(self, cmd, expected, errout=None,
            exit=0, opts=misc.EmptyI, names=misc.EmptyI, su_wrap=False):
                self.pkg("%s %s -H -F tsv %s" % (cmd, " ".join(opts),
                    " ".join(names)), exit=exit, su_wrap=su_wrap)
                self.assertEqualDiff(expected, self.output)
                if errout:
                        self.assert_(self.errout != "")
                else:
                        self.assertEqualDiff("", self.errout)

        def __assert_varcet_fails(self, cmd, operands, errout=True, exit=1,
            su_wrap=False):
                self.pkg("%s %s" % (cmd, operands), exit=exit, su_wrap=su_wrap) 
                if errout:
                        self.assert_(self.errout != "")
                else:
                        self.assertEqualDiff("", self.errout)

        def __assert_facet_matches_default(self, *args, **kwargs):
                self.__assert_varcet_matches_default("facet", *args,
                    **kwargs)

        def __assert_facet_matches_tsv(self, *args, **kwargs):
                self.__assert_varcet_matches_tsv("facet", *args,
                    **kwargs)

        def __assert_facet_matches(self, exp_def, **kwargs):
                exp_tsv = exp_def.replace(" ", "\t")
                self.__assert_varcet_matches_default("facet", exp_def,
                    **kwargs)
                self.__assert_varcet_matches_tsv("facet", exp_tsv,
                    **kwargs)

        def __assert_facet_fails(self, *args, **kwargs):
                self.__assert_varcet_fails("facet", *args, **kwargs)

        def __assert_variant_matches_default(self, *args, **kwargs):
                self.__assert_varcet_matches_default("variant", *args,
                    **kwargs)

        def __assert_variant_matches_tsv(self, *args, **kwargs):
                self.__assert_varcet_matches_tsv("variant", *args,
                    **kwargs)

        def __assert_variant_matches(self, exp_def, **kwargs):
                exp_tsv = exp_def.replace(" ", "\t")
                self.__assert_varcet_matches_default("variant", exp_def,
                    **kwargs)
                self.__assert_varcet_matches_tsv("variant", exp_tsv,
                    **kwargs)

        def __assert_variant_fails(self, *args, **kwargs):
                self.__assert_varcet_fails("variant", *args, **kwargs)

        def __test_foo_facet_upgrade(self, pkg):
                #
                # Next, verify output after upgrading package to faceted
                # version.
                #
                self.pkg("update %s" % pkg)

                # Verify output for no options and no patterns.
                exp_def = """\
facet.doc.* False
facet.doc.html False
facet.doc.man False
facet.doc.txt True
"""
                self.__assert_facet_matches(exp_def)

                # Unmatched because facet is not explicitly set.
                self.__assert_facet_fails("doc.pdf")
                self.__assert_facet_fails("'*pdf'")

                # Matched case for explicitly set.
                exp_def = """\
facet.doc.* False
facet.doc.txt True
"""
                names = ("'facet.doc.[*]'", "doc.txt")
                self.__assert_facet_matches(exp_def, names=names)

                # Verify -a output.
                exp_def = """\
facet.doc.* False
facet.doc.html False
facet.doc.man False
facet.doc.pdf False
facet.doc.txt True
"""
                opts = ("-a",)
                self.__assert_facet_matches(exp_def, opts=opts)

                # Matched case for explicitly set and those in packages.
                exp_def = """\
facet.doc.* False
facet.doc.pdf False
facet.doc.txt True
"""
                names = ("'facet.doc.[*]'", "*pdf", "facet.doc.txt")
                opts = ("-a",)
                self.__assert_facet_matches(exp_def, opts=opts, names=names)

                # Verify -i output.
                exp_def = """\
facet.doc.man False
facet.doc.pdf False
facet.doc.txt True
"""
                opts = ("-i",)
                self.__assert_facet_matches(exp_def, opts=opts)

                # Unmatched because facet is not used in package.
                self.__assert_facet_fails("-i doc.html")
                self.__assert_facet_fails("-i '*html'")

                # Matched case in packages.
                exp_def = """\
facet.doc.man False
facet.doc.pdf False
"""
                names = ("'facet.*[!t]'",)
                opts = ("-i",)
                self.__assert_facet_matches(exp_def, opts=opts, names=names)

                exp_def = """\
facet.doc.pdf False
"""
                names = ("'*pdf'",)
                opts = ("-i",)
                self.__assert_facet_matches(exp_def, opts=opts, names=names)

                # Now uninstall package and verify output (to ensure any
                # potentially cached information has been updated).
                self.pkg("uninstall foo")

                exp_def = """\
facet.doc.* False
facet.doc.html False
facet.doc.man False
facet.doc.txt True
"""

                # Output should be the same for both -a and default cases with
                # no packages installed.
                for opts in ((), ("-a",)):
                        self.__assert_facet_matches(exp_def, opts=opts)

                # No output expected for -i.
                opts = ("-i",)
                self.__assert_facet_matches("", opts=opts)

        def test_00_facet(self):
                """Verify facet subcommand works as expected."""

                # create an image
                variants = { "variant.icecream": "strawberry" }
                self.image_create(self.rurl, variants=variants)

                # Verify invalid options handled gracefully.
                self.__assert_facet_fails("-z", exit=2)
                self.__assert_facet_fails("-fi", exit=2)

                #
                # First, verify output before setting any facets or installing
                # any packages.
                #

                # Output should be the same for all cases with no facets set and
                # no packages installed.
                for opts in ((), ("-i",), ("-a",)):
                        # No operands specified case.
                        self.__assert_facet_matches("", opts=opts)

                        # Unprivileged user case.
                        self.__assert_facet_matches("", opts=opts, su_wrap=True)

                        # Unmatched case.
                        self.__assert_facet_matches_default("", opts=opts,
                            names=("bogus",), exit=1)

                        # Unmatched case tsv; subtly different as no error
                        # output is expected.
                        self.__assert_facet_matches_tsv("", opts=opts,
                            names=("bogus",), exit=1, errout=False)

                #
                # Next, verify output after setting facets.
                #

                # Set some facets.
                self.pkg("change-facet 'doc.*=False' doc.man=False "
                    "facet.doc.html=False facet.doc.txt=True")

                exp_def = """\
facet.doc.* False
facet.doc.html False
facet.doc.man False
facet.doc.txt True
"""

                # Output should be the same for both -a and default cases with
                # no packages installed.
                for opts in ((), ("-a",)):
                        self.__assert_facet_matches(exp_def, opts=opts)

                #
                # Next, verify output after installing unfaceted package.
                #
                self.pkg("install foo@1.0")

                # Verify output for no options and no patterns.
                exp_def = """\
facet.doc.* False
facet.doc.html False
facet.doc.man False
facet.doc.txt True
"""
                self.__assert_facet_matches(exp_def)

                # Verify -a output.
                opts = ("-a",)
                self.__assert_facet_matches(exp_def, opts=opts)

                # Verify -i output.
                opts = ("-i",)
                self.__assert_facet_matches("", opts=opts)

                # Test upgraded package that does not declare all
                # facets/variants.
                self.__test_foo_facet_upgrade("foo@2.0")

                # Reinstall and then retest with upgraded package that declares
                # all facets/variants.
                self.pkg("install foo@1.0")
                self.__test_foo_facet_upgrade("foo@3.0")

        def __test_foo_variant_upgrade(self, pkg, variants):
                #
                # Next, verify output after upgrading package to varianted
                # version.
                #
                self.pkg("update %s" % pkg)

                # Verify output for no options and no patterns.
                exp_def = """\
variant.arch %(variant.arch)s
variant.icecream strawberry
variant.opensolaris.zone global
""" % variants
                self.__assert_variant_matches(exp_def)

                # Unmatched because variant is not explicitly set.
                self.__assert_variant_fails("debug.foo")
                self.__assert_variant_fails("'*foo'")

                # Matched case for explicitly set.
                exp_def = """\
variant.arch %(variant.arch)s
variant.opensolaris.zone global
""" % variants
                names = ("arch", "'variant.*zone'")
                self.__assert_variant_matches(exp_def, names=names)

                # Verify -a output.
                exp_def = """\
variant.arch %(variant.arch)s
variant.debug.foo false
variant.icecream strawberry
variant.opensolaris.zone global
""" % variants
                opts = ("-a",)
                self.__assert_variant_matches(exp_def, opts=opts)

                # Matched case for explicitly set and those in packages.
                exp_def = """\
variant.arch %(variant.arch)s
variant.debug.foo false
variant.opensolaris.zone global
""" % variants
                names = ("'variant.debug.*'", "arch", "'*zone'")
                opts = ("-a",)
                self.__assert_variant_matches(exp_def, opts=opts, names=names)

                # Verify -i output.
                exp_def = """\
variant.debug.foo false
variant.icecream strawberry
""" % variants
                opts = ("-i",)
                self.__assert_variant_matches(exp_def, opts=opts)

                # Unmatched because variant is not used in package.
                self.__assert_variant_fails("-i opensolaris.zone")
                self.__assert_variant_fails("-i '*arch'")

                # Verify -v and -av output.
                exp_def = """\
variant.debug.foo false
variant.debug.foo true
variant.icecream neapolitan
variant.icecream strawberry
"""
                for opts in (("-v",), ("-av",)):
                        self.__assert_variant_matches(exp_def, opts=opts)

                exp_def = """\
variant.icecream neapolitan
variant.icecream strawberry
""" % variants
                names = ("'ice*'",)
                opts = ("-av",)
                self.__assert_variant_matches(exp_def, opts=opts, names=names)

                # Matched case in packages.
                exp_def = """\
variant.icecream strawberry
""" % variants
                names = ("'variant.*[!o]'",)
                opts = ("-i",)
                self.__assert_variant_matches(exp_def, opts=opts, names=names)

                exp_def = """\
variant.debug.foo false
""" % variants
                names = ("*foo",)
                opts = ("-i",)
                self.__assert_variant_matches(exp_def, opts=opts, names=names)

                # Now uninstall package and verify output (to ensure any
                # potentially cached information has been updated).
                self.pkg("uninstall foo")

                exp_def = """\
variant.arch %(variant.arch)s
variant.icecream strawberry
variant.opensolaris.zone global
""" % variants

                # Output should be the same for both -a and default cases with
                # no packages installed.
                for opts in ((), ("-a",)):
                        self.__assert_variant_matches(exp_def, opts=opts)

                # No output expected for -v, -av, -i, or -iv.
                for opts in (("-v",), ("-av",), ("-i",), ("-iv",)):
                        self.__assert_variant_matches("", opts=opts)

        def test_01_variant(self):
                """Verify variant subcommand works as expected."""

                # create an image
                self.image_create(self.rurl)

                # Get variant data.
                api_obj = self.get_img_api_obj()
                variants = dict(v[:-1] for v in api_obj.gen_variants(
                    api_obj.VARIANT_IMAGE))

                # Verify invalid options handled gracefully.
                self.__assert_variant_fails("-z", exit=2)
                self.__assert_variant_fails("-ai", exit=2)
                self.__assert_variant_fails("-aiv", exit=2)

                #
                # First, verify output before setting any variants or installing
                # any packages.
                #

                # Output should be the same for -a and default cases with no
                # variants set and no packages installed.
                exp_def = """\
variant.arch %(variant.arch)s
variant.opensolaris.zone global
""" % variants

                for opts in ((), ("-a",)):
                        # No operands specified case.
                        self.__assert_variant_matches(exp_def, opts=opts)

                        # Unprivileged user case.
                        self.__assert_variant_matches(exp_def, opts=opts,
                            su_wrap=True)

                        # Unmatched case.
                        self.__assert_variant_matches_default("", opts=opts,
                            names=("bogus",), exit=1)

                        # Unmatched case tsv; subtly different as no error
                        # output is expected.
                        self.__assert_variant_matches_tsv("", opts=opts,
                            names=("bogus",), exit=1, errout=False)

                # No output expected for with no variants set and no packages
                # installed for -v, -av, -i, and -iv.
                for opts in (("-v",), ("-av",), ("-i",), ("-iv",)):
                        self.__assert_variant_matches("", opts=opts)

                #
                # Next, verify output after setting variants.
                #

                # Set some variants.
                self.pkg("change-variant variant.icecream=strawberry")

                exp_def = """\
variant.arch %(variant.arch)s
variant.icecream strawberry
variant.opensolaris.zone global
""" % variants

                # Output should be the same for both -a and default cases with
                # no packages installed.
                for opts in ((), ("-a",)):
                        self.__assert_variant_matches(exp_def, opts=opts)

                #
                # Next, verify output after installing unvarianted package.
                #
                self.pkg("install foo@1.0")

                # Verify output for no options and no patterns.
                exp_def = """\
variant.arch %(variant.arch)s
variant.icecream strawberry
variant.opensolaris.zone global
""" % variants
                self.__assert_variant_matches(exp_def)

                # Verify -a output.
                opts = ("-a",)
                self.__assert_variant_matches(exp_def, opts=opts)

                # Verify -v, -av, -i, and -iv output.
                for opts in (("-v",), ("-av",), ("-i",), ("-iv",)):
                        self.__assert_variant_matches("", opts=opts)

                # Test upgraded package that does not declare all
                # facets/variants.
                self.__test_foo_variant_upgrade("foo@2.0", variants)

                # Reinstall and then retest with upgraded package that declares
                # all facets/variants.
                self.pkg("install foo@1.0")
                self.__test_foo_variant_upgrade("foo@3.0", variants)

                # Next, verify output after installing package with unknown
                # variant.
                self.pkg("install unknown@1.0")

                # Verify output for no options and no patterns.
                exp_def = """\
variant.arch %(variant.arch)s
variant.icecream strawberry
variant.opensolaris.zone global
""" % variants
                self.__assert_variant_matches(exp_def)

                # Verify -a output.
                exp_def = """\
variant.arch %(variant.arch)s
variant.icecream strawberry
variant.opensolaris.zone global
variant.unknown 
""" % variants
                self.__assert_variant_matches(exp_def, opts=("-a",))

                # Verify -i output.
                exp_def = """\
variant.unknown 
""" % variants
                self.__assert_variant_matches(exp_def, opts=("-i",))

                # Verify -v, -av, and -iv output.
                for opts in (("-v",), ("-av",), ("-iv",)):
                        exp_def = """\
variant.unknown bar
variant.unknown foo
""" % variants
                        self.__assert_variant_matches(exp_def, opts=opts)


if __name__ == "__main__":
        unittest.main()
 
 
Close
loading
Please Confirm
Close