src/tests/cli/t_pkg_image_create.py
changeset 1352 5c92c9d342ef
parent 1143 71becfca5cfd
child 1353 b9535ddac1c6
equal deleted inserted replaced
1351:75f7a28beaf5 1352:5c92c9d342ef
    28 import testutils
    28 import testutils
    29 if __name__ == "__main__":
    29 if __name__ == "__main__":
    30         testutils.setup_environment("../../../proto")
    30         testutils.setup_environment("../../../proto")
    31 
    31 
    32 import os
    32 import os
       
    33 import pkg.portable
       
    34 import pkg.catalog
    33 import shutil
    35 import shutil
    34 import unittest
    36 import unittest
    35 
    37 
    36 
    38 
    37 class TestPkgImageCreateBasics(testutils.SingleDepotTestCase):
    39 class TestPkgImageCreateBasics(testutils.ManyDepotTestCase):
    38         # Only start/stop the depot once (instead of for every test)
    40         # Only start/stop the depots once (instead of for every test)
    39         persistent_depot = True
    41         persistent_depot = True
       
    42 
       
    43         def setUp(self):
       
    44                 testutils.ManyDepotTestCase.setUp(self, 2)
       
    45 
       
    46                 self.durl1 = self.dcs[1].get_depot_url()
       
    47                 self.durl2 = self.dcs[2].get_depot_url()
    40 
    48 
    41         def test_basic(self):
    49         def test_basic(self):
    42                 """ Create an image, verify it. """
    50                 """ Create an image, verify it. """
    43 
    51 
    44                 durl = self.dc.get_depot_url()
    52                 self.image_create(self.durl1)
    45                 self.image_create(durl)
       
    46                 self.pkg("verify")
    53                 self.pkg("verify")
    47 
    54 
    48         def test_image_create_bad_opts(self):
    55         def test_image_create_bad_opts(self):
    49                 """Test some bad cli options."""
    56                 """Test some bad cli options."""
    50 
    57 
    51                 self.pkg("image-create -@", exit=2)
    58                 self.pkg("image-create -@", exit=2)
    52                 self.pkg("image-create --bozo", exit=2)
    59                 self.pkg("image-create --bozo", exit=2)
    53                 self.pkg("image-create", exit=2)
    60                 self.pkg("image-create", exit=2)
    54 
    61 
       
    62         def __add_install_file(self, imgdir, fmri):
       
    63                 """Take an image path and fmri. Write a file to disk that
       
    64                 indicates that the package named by the fmri has been
       
    65                 installed.  Assumes package was installed from a preferred
       
    66                 publisher."""
       
    67 
       
    68                 def install_file(fmri):
       
    69                         return "%s/pkg/%s/installed" % (imgdir,
       
    70                             fmri.get_dir_path())
       
    71 
       
    72                 f = file(install_file(fmri), "w")
       
    73                 f.writelines(["VERSION_1\n_PRE_", fmri.publisher])
       
    74                 f.close()
       
    75 
       
    76                 fi = file("%s/state/installed/%s" % (imgdir,
       
    77                     fmri.get_link_path()), "w")
       
    78                 fi.close()
       
    79 
    55         def test_766(self):
    80         def test_766(self):
    56                 """Bug 766: image-create without publisher prefix specified."""
    81                 """Bug 766: image-create without publisher prefix specified."""
    57 
       
    58                 durl = self.dc.get_depot_url()
       
    59 
    82 
    60                 pkgsend_data = """
    83                 pkgsend_data = """
    61                 open [email protected]
    84                 open [email protected]
    62                 close
    85                 close
    63                 """
    86                 """
    64                 self.pkgsend_bulk(durl, pkgsend_data)
    87                 self.pkgsend_bulk(self.durl1, pkgsend_data)
    65 
    88 
    66                 self.assertRaises(testutils.UnexpectedExitCodeException,
    89                 self.assertRaises(testutils.UnexpectedExitCodeException,
    67                     self.image_create, durl, "")
    90                     self.image_create, self.durl1, "")
    68 
    91 
    69         def test_3588(self):
    92         def test_3588(self):
    70                 """Bug 3588: Make sure we can't create an image where one
    93                 """Ensure that image creation works as expected when an image
    71                 already exists"""
    94                 already exists."""
    72                 durl = self.dc.get_depot_url()
    95 
       
    96                 # These tests are interdependent.
       
    97                 #
       
    98                 # Bug 3588: Make sure we can't create an image where one
       
    99                 # already exists
    73                 self.pkg("image-create -p mydepot=%s %s/3588_image" % (
   100                 self.pkg("image-create -p mydepot=%s %s/3588_image" % (
    74                     durl, self.get_img_path()))
   101                     self.durl1, self.get_img_path()))
    75                 self.pkg("image-create -p mydepot=%s %s/3588_image" %
   102                 self.pkg("image-create -p mydepot=%s %s/3588_image" % (
    76                                   (durl, self.get_img_path()), exit=1)
   103                     self.durl1, self.get_img_path()), exit=1)
    77 
   104 
    78         def test_3588_1(self):
   105                 # Make sure we can create an image where one
    79                 """Bug 3588: Make sure we can create an image where one
   106                 # already exists with the -f (force) flag
    80                 already exists with the -f (force) flag"""
       
    81                 durl = self.dc.get_depot_url()
       
    82                 self.pkg("image-create -p mydepot=%s %s/3588_image_1" % (
   107                 self.pkg("image-create -p mydepot=%s %s/3588_image_1" % (
    83                     durl, self.get_img_path()))
   108                     self.durl1, self.get_img_path()))
    84                 self.pkg("image-create -f -p mydepot=%s %s/3588_image_1" %
   109                 self.pkg("image-create -f -p mydepot=%s %s/3588_image_1" %
    85                          (durl, self.get_img_path()))
   110                          (self.durl1, self.get_img_path()))
    86 
   111 
    87         def test_3588_2(self):
   112                 # Bug 3588: Make sure we can't create an image where a
    88                 """Bug 3588: Make sure we can't create an image where a
   113                 # non-empty directory exists
    89                 non-empty directory exists"""
       
    90                 durl = self.dc.get_depot_url()
       
    91                 p = os.path.join(self.get_img_path(), "3588_2_image")
   114                 p = os.path.join(self.get_img_path(), "3588_2_image")
    92                 os.mkdir(p)
   115                 os.mkdir(p)
    93                 os.system("touch %s/%s" % (p, "somefile"))
   116                 os.system("touch %s/%s" % (p, "somefile"))
    94                 self.pkg("image-create -p mydepot=%s %s" % (durl, p), exit=1)
   117                 self.pkg("image-create -p mydepot=%s %s" % (self.durl1, p),
    95                 self.pkg("image-create -f -p mydepot=%s %s" % (durl, p))
   118                     exit=1)
       
   119                 self.pkg("image-create -f -p mydepot=%s %s" % (self.durl1, p))
    96 
   120 
    97         def test_4_options(self):
   121         def test_4_options(self):
    98                 """Verify that all of the options for specifying publisher
   122                 """Verify that all of the options for specifying publisher
    99                 information work as expected for image-create."""
   123                 information work as expected for image-create."""
   100 
   124 
   101                 img_path = os.path.join(self.get_test_prefix(), "test_4_img")
   125                 img_path = os.path.join(self.get_test_prefix(), "test_4_img")
   102                 for opt in ("-a", "-p", "--publisher"):
   126                 for opt in ("-a", "-p", "--publisher"):
   103                         durl = self.dc.get_depot_url()
   127                         self.pkg("image-create %s mydepot=%s %s" % (opt,
   104                         self.pkg("image-create %s mydepot=%s %s" % (opt, durl,
   128                             self.durl1, img_path))
   105                             img_path))
       
   106                         shutil.rmtree(img_path)
   129                         shutil.rmtree(img_path)
   107 
   130 
   108         def test_5_bad_values_no_image(self):
   131         def test_5_bad_values_no_image(self):
   109                 """Verify that an invalid publisher URI or other piece of
   132                 """Verify that an invalid publisher URI or other piece of
   110                 information provided to image-create will not result in an
   133                 information provided to image-create will not result in an
   117         def test_6_relative_root_create(self):
   140         def test_6_relative_root_create(self):
   118                 """Verify that an image with a relative path for the root is
   141                 """Verify that an image with a relative path for the root is
   119                 created correctly."""
   142                 created correctly."""
   120 
   143 
   121                 pwd = os.getcwd()
   144                 pwd = os.getcwd()
   122                 durl = self.dc.get_depot_url()
       
   123                 img_path = "test_6_image"
   145                 img_path = "test_6_image"
   124                 abs_img_path = os.path.join(self.get_test_prefix(), img_path)
   146                 abs_img_path = os.path.join(self.get_test_prefix(), img_path)
   125 
   147 
   126                 # Now verify that the image root isn't duplicated within the
   148                 # Now verify that the image root isn't duplicated within the
   127                 # specified image root if the specified root doesn't already
   149                 # specified image root if the specified root doesn't already
   128                 # exist.
   150                 # exist.
   129                 os.chdir(self.get_test_prefix())
   151                 os.chdir(self.get_test_prefix())
   130                 self.pkg("image-create -p mydepot=%s %s" % (durl, img_path))
   152                 self.pkg("image-create -p mydepot=%s %s" % (self.durl1,
       
   153                     img_path))
   131                 os.chdir(pwd)
   154                 os.chdir(pwd)
   132                 self.assertFalse(os.path.exists(os.path.join(abs_img_path, img_path)))
   155                 self.assertFalse(os.path.exists(os.path.join(abs_img_path,
       
   156                     img_path)))
   133                 shutil.rmtree(abs_img_path)
   157                 shutil.rmtree(abs_img_path)
   134 
   158 
   135                 # Now verify that the image root isn't duplicated within the
   159                 # Now verify that the image root isn't duplicated within the
   136                 # specified image root if the specified root already exists.
   160                 # specified image root if the specified root already exists.
   137                 os.chdir(self.get_test_prefix())
   161                 os.chdir(self.get_test_prefix())
   138                 os.mkdir(img_path)
   162                 os.mkdir(img_path)
   139                 self.pkg("image-create -p mydepot=%s %s" % (durl, img_path))
   163                 self.pkg("image-create -p mydepot=%s %s" % (self.durl1,
       
   164                     img_path))
   140                 os.chdir(pwd)
   165                 os.chdir(pwd)
   141                 self.assertFalse(os.path.exists(os.path.join(abs_img_path, img_path)))
   166                 self.assertFalse(os.path.exists(os.path.join(abs_img_path,
       
   167                     img_path)))
   142                 shutil.rmtree(abs_img_path)
   168                 shutil.rmtree(abs_img_path)
   143 
   169 
   144         def test_7_image_create_no_refresh(self):
   170         def test_7_image_create_no_refresh(self):
   145                 """Verify that image-create --no-refresh works as expected.
   171                 """Verify that image-create --no-refresh works as expected.
   146                 See bug 8777 for related issue."""
   172                 See bug 8777 for related issue."""
   147 
       
   148                 durl = self.dc.get_depot_url()
       
   149 
   173 
   150                 pkgsend_data = """
   174                 pkgsend_data = """
   151                 open [email protected]
   175                 open [email protected]
   152                 close
   176                 close
   153                 """
   177                 """
   154                 self.pkgsend_bulk(durl, pkgsend_data)
   178                 self.pkgsend_bulk(self.durl1, pkgsend_data)
   155 
   179 
   156                 # First, check to be certain that an image-create --no-refresh
   180                 # First, check to be certain that an image-create --no-refresh
   157                 # will succeed.
   181                 # will succeed.
   158                 self.image_create(durl, prefix="norefresh",
   182                 self.image_create(self.durl2, prefix="norefresh",
   159                     additional_args="--no-refresh")
   183                     additional_args="--no-refresh")
   160                 self.pkg("list --no-refresh -a | grep baz", exit=1)
   184                 self.pkg("list --no-refresh -a | grep baz", exit=1)
   161 
   185 
   162                 # Finally, verify that using set-publisher will cause a refresh
   186                 # Finally, verify that using set-publisher will cause a refresh
   163                 # which in turn should cause 'baz' to be listed.
   187                 # which in turn should cause 'baz' to be listed *if* the origin
   164                 self.pkg("set-publisher -O %s norefresh" % durl)
   188                 # has changed (setting it to the same value again won't work).
       
   189                 self.pkg("set-publisher -O %s norefresh" % self.durl1)
   165                 self.pkg("list --no-refresh -a | grep baz")
   190                 self.pkg("list --no-refresh -a | grep baz")
   166 
   191 
       
   192         def test_8_image_upgrade(self):
       
   193                 """Verify that a version 0 image can be used by a client that
       
   194                 normally creates version 1 images, and that it will be upgraded
       
   195                 correctly when a privileged user uses it."""
       
   196 
       
   197                 # Publish some sample packages.
       
   198                 pkgsend_data = """
       
   199                 open [email protected]
       
   200                 close
       
   201                 open [email protected]
       
   202                 close
       
   203                 """
       
   204                 self.pkgsend_bulk(self.durl1, pkgsend_data)
       
   205 
       
   206                 # First, create a new image.
       
   207                 self.image_create(self.durl1)
       
   208 
       
   209                 # Next, install the packages.
       
   210                 self.pkg("install quux corge")
       
   211 
       
   212                 # Next, convert the v1 image to a v0 image.
       
   213                 img_root = os.path.join(self.get_img_path(), "var", "pkg")
       
   214                 cat_path = os.path.join(img_root, "catalog")
       
   215                 pub_path = os.path.join(img_root, "publisher")
       
   216                 v1_cat_path = os.path.join(pub_path, "test", "catalog")
       
   217                 v0_cat_path = os.path.join(cat_path, "test")
       
   218 
       
   219                 # For conversion, the v0 catalogs need to be moved to the
       
   220                 # v0 location.
       
   221                 os.makedirs(v0_cat_path)
       
   222                 for fname in ("catalog", "attrs"):
       
   223                         src = os.path.join(v1_cat_path, fname)
       
   224                         dest = os.path.join(v0_cat_path, fname)
       
   225                         pkg.portable.rename(src, dest)
       
   226 
       
   227                 # The existing installed state has to be converted to v0.
       
   228                 state_dir = os.path.join(img_root, "state")
       
   229                 inst_state_dir = os.path.join(state_dir, "installed")
       
   230                 cat = pkg.catalog.Catalog(meta_root=inst_state_dir)
       
   231                 for f in cat.fmris():
       
   232                         self.__add_install_file(img_root, f)
       
   233                 cat = None
       
   234 
       
   235                 # Now dump the new publisher directory, the 'known' state
       
   236                 # directory, and any catalog files in the 'installed'
       
   237                 # directory.
       
   238                 known_state_dir = os.path.join(state_dir, "known")
       
   239                 for path in (pub_path, known_state_dir):
       
   240                         shutil.rmtree(path)
       
   241 
       
   242                 for fname in sorted(os.listdir(inst_state_dir)):
       
   243                         if fname.startswith("catalog"):
       
   244                                 pkg.portable.remove(os.path.join(inst_state_dir,
       
   245                                     fname))
       
   246 
       
   247                 # Next, verify that the new client can read v0 images as an
       
   248                 # an unprivileged user.
       
   249                 self.pkg("info pkg://test/quux corge", su_wrap=True)
       
   250 
       
   251                 # Next, verify that the new client can upgrade v0 images to
       
   252                 # v1 images.
       
   253                 self.pkg("info quux pkg://test/corge")
       
   254 
       
   255                 # Finally, verify that the old structures and state information
       
   256                 # are gone.
       
   257                 self.assertFalse(os.path.exists(cat_path))
       
   258                 self.assertTrue(os.path.exists(pub_path))
       
   259 
       
   260                 for pl in sorted(os.listdir(inst_state_dir)):
       
   261                         # If there any other files but catalog files here, then
       
   262                         # the old state information didn't get properly removed.
       
   263                         assert pl.startswith("catalog.")
   167 
   264 
   168 class TestImageCreateNoDepot(testutils.CliTestCase):
   265 class TestImageCreateNoDepot(testutils.CliTestCase):
   169         persistent_depot = True
   266         persistent_depot = True
   170         def test_bad_image_create(self):
   267         def test_bad_image_create(self):
   171                 """ Create image from non-existent server """
   268                 """ Create image from non-existent server """