def test_unzip(self, destdir): class MockZipInfo(object): def __init__(self, name, perms): self.filename = name self.external_attr = perms << 16 self.mox.StubOutClassWithMocks(zipfile, 'ZipFile') self.mox.StubOutWithMock(os, 'chmod') files = ['test/', 'test/a', 'test/b/', 'test/b/c'] perms = [0755, 0644, 0755, 0550] infos = [MockZipInfo(f, p) for (f, p) in zip(files, perms)] mockzip = zipfile.ZipFile('path/to/test.zip') mockzip.namelist().AndReturn(files) mockzip.infolist().AndReturn(infos) for n in xrange(4): mockzip.extract(infos[n], destdir) os.chmod(os.path.join(destdir, files[n]), perms[n]) self.mox.ReplayAll() if destdir == '.': unzipped = fileutils.unzip('path/to/test.zip', True) else: unzipped = fileutils.unzip('path/to/test.zip', True, destdir) assert unzipped == os.path.join(destdir, 'test') self.mox.VerifyAll()
def test_get_server_dir(self, server): ext = self.mox.CreateMock(External) self.mox.StubOutWithMock(omego.upgrade, 'Artifacts') self.mox.StubOutWithMock(fileutils, 'get_as_local_path') self.mox.StubOutWithMock(fileutils, 'unzip') args = self.Args({ 'server': None, 'skipunzip': False, 'overwrite': 'error', 'unzipdir': None, 'httpuser': '******', 'httppassword': '******' }) if server == 'local': args.server = 'local-server-dir' fileutils.get_as_local_path( args.server, args.overwrite, progress=0, httpuser=args.httpuser, httppassword=args.httppassword).AndReturn( ('directory', 'local-server-dir')) expected = 'local-server-dir' elif server == 'remote': args.server = 'http://example.org/remote/server.zip' fileutils.get_as_local_path( args.server, args.overwrite, progress=0, httpuser=args.httpuser, httppassword=args.httppassword).AndReturn( ('file', 'server.zip')) fileutils.unzip('server.zip', match_dir=True, destdir=args.unzipdir).AndReturn('server') expected = 'server' else: artifact_args = copy.copy(args) artifact_args.sym = '' omego.upgrade.Artifacts(artifact_args).AndReturn( self.MockArtifacts()) expected = 'server-dir' self.mox.ReplayAll() upgrade = self.PartialMockUnixInstall(args, ext) s = upgrade.get_server_dir() assert s == expected self.mox.VerifyAll()
def test_download(self): url = 'http://example.org/test/component-0.0.0.zip' a = self.MockArtifacts('testcomponent', url) self.mox.StubOutWithMock(fileutils, 'get_as_local_path') self.mox.StubOutWithMock(fileutils, 'unzip') fileutils.get_as_local_path( url, 'error', progress=0, httpuser=None, httppassword=None).AndReturn( ('file', 'component-0.0.0.zip')) fileutils.unzip('component-0.0.0.zip', match_dir=True, destdir='unzip/dir').AndReturn('component-0.0.0') self.mox.ReplayAll() assert a.download('testcomponent') == 'component-0.0.0' self.mox.VerifyAll()
def test_download(self): url = 'http://example.org/test/component-0.0.0.zip' a = self.MockArtifacts('testcomponent', url) self.mox.StubOutWithMock(fileutils, 'get_as_local_path') self.mox.StubOutWithMock(fileutils, 'unzip') fileutils.get_as_local_path(url, 'error', progress=0, httpuser=None, httppassword=None).AndReturn( ('file', 'component-0.0.0.zip')) fileutils.unzip('component-0.0.0.zip', match_dir=True, destdir='unzip/dir').AndReturn('component-0.0.0') self.mox.ReplayAll() assert a.download('testcomponent') == 'component-0.0.0' self.mox.VerifyAll()
def test_get_server_dir(self, server): ext = self.mox.CreateMock(External) self.mox.StubOutWithMock(omego.upgrade, 'Artifacts') self.mox.StubOutWithMock(fileutils, 'get_as_local_path') self.mox.StubOutWithMock(fileutils, 'unzip') args = self.Args({'server': None, 'skipunzip': False, 'overwrite': 'error', 'unzipdir': None, 'httpuser': '******', 'httppassword': '******'}) if server == 'local': args.server = 'local-server-dir' fileutils.get_as_local_path( args.server, args.overwrite, progress=0, httpuser=args.httpuser, httppassword=args.httppassword ).AndReturn(('directory', 'local-server-dir')) expected = 'local-server-dir' elif server == 'remote': args.server = 'http://example.org/remote/server.zip' fileutils.get_as_local_path( args.server, args.overwrite, progress=0, httpuser=args.httpuser, httppassword=args.httppassword ).AndReturn(('file', 'server.zip')) fileutils.unzip( 'server.zip', match_dir=True, destdir=args.unzipdir ).AndReturn('server') expected = 'server' else: artifact_args = copy.copy(args) artifact_args.sym = '' omego.upgrade.Artifacts(artifact_args).AndReturn( self.MockArtifacts()) expected = 'server-dir' self.mox.ReplayAll() upgrade = self.PartialMockUnixInstall(args, ext) s = upgrade.get_server_dir() assert s == expected self.mox.VerifyAll()