def testNewEntryPermissions(self):
    dirPermissions=0707
    dumpPermissions=0500
    sfl = SilentFakeLogger()
    j = JDS.JsonDumpStorage(root=self.testDir,dirPermissions=dirPermissions,dumpPermissions=dumpPermissions,logger=sfl)
    u = str(socorro_uuid.uuid1())
    f1, f2 = j.newEntry(u)
    f1.close()
    f2.close()
    jpath = j.getJson(u)
    gotPermissions = stat.S_IMODE(os.stat(jpath)[0])
    assert stat.S_IMODE(os.stat(jpath)[0]) == dumpPermissions, "%s: Expected %o, got %o" % (jpath, dumpPermissions, gotPermissions)

    dpath = j.getDump(u)
    gotPermissions = stat.S_IMODE(os.stat(dpath)[0])
    assert stat.S_IMODE(os.stat(dpath)[0]) == dumpPermissions, "%s: Expected %o, got %o" % (dpath, dumpPermissions, gotPermissions)

    udir = os.path.split(dpath)[0]
    datePath = os.path.abspath(os.path.join(udir,os.readlink(os.path.splitext(dpath)[0])))
    namePath = os.path.abspath(os.path.splitext(dpath)[0])
    topPath = os.path.abspath(self.testDir)
    dailies = os.listdir(topPath)

    def assertPermVisitor(p):
      gotPerm = stat.S_IMODE(os.stat(p)[0])
      assert dirPermissions == gotPerm, "%s: Expected %0o, got %0o"%(p,dirPermissions,gotPerm)
    for d in dailies:
      # visitPath quietly ignores a file as the leaf
      socorro_fs.visitPath(os.path.join(topPath,d),datePath,assertPermVisitor)
      socorro_fs.visitPath(os.path.join(topPath,d),namePath,assertPermVisitor)
  def testCopyFromGid(self):
    j = JDS.JsonDumpStorage(root=self.testDir,dumpGID = self.newGid)
    os.makedirs(self.testMoveFrom)
    u = str(suuid.uuid1())
    jopath = os.path.join(self.testMoveFrom,u+j.jsonSuffix)
    dopath = os.path.join(self.testMoveFrom,u+j.dumpSuffix)
    fj = open(jopath,'w')
    fd = open(dopath,'w')
    fj.close()
    fd.close()
    j.copyFrom(u,jopath,dopath,'w', DT.datetime(2008,8,8,8,8),createLinks = True)

    jpath = j.getJson(u)
    gotGid = os.stat(jpath)[stat.ST_GID]
    assert self.newGid ==  gotGid, "%s: Expected %o, got %o" % (jpath, self.newGid, gotGid)

    dpath = j.getDump(u)
    gotGid = os.stat(dpath)[stat.ST_GID]
    assert self.newGid == gotGid, "%s: Expected %o, got %o" % (dpath, self.newGid, gotGid)

    udir = os.path.split(dpath)[0]
    datePath = os.path.join(udir,os.readlink(os.path.abspath(os.path.splitext(dpath)[0])))

    x = p.split(jpath)[0]
    y = os.path.abspath(datePath)
    for tp in (x,y):
      while tp != j.nameBranch and tp != os.path.abspath(j.dateBranch):
        gotGid = os.stat(tp)[stat.ST_GID]
        assert self.newGid == gotGid, "%s: Expected %o, got %o"%(tp,077,gotGid)
        tp = p.split(tp)[0]
  def testNewEntryGid(self):
    j = JDS.JsonDumpStorage(root=self.testDir,dumpGID=self.newGid)
    u = str(suuid.uuid1())
    f1,f2 = j.newEntry(u)
    f1.close()
    f2.close()

    jpath = j.getJson(u)
    gotGid = os.stat(jpath)[stat.ST_GID]
    assert self.newGid ==  gotGid, "%s: Expected %o, got %o" % (jpath, self.newGid, gotGid)

    dpath = j.getDump(u)
    gotGid = os.stat(dpath)[stat.ST_GID]
    assert self.newGid == gotGid, "%s: Expected %o, got %o" % (dpath, self.newGid, gotGid)

    udir = os.path.split(dpath)[0]
    datePath = os.path.join(udir,os.readlink(os.path.abspath(os.path.splitext(dpath)[0])))

    x = p.split(jpath)[0]
    y = os.path.abspath(datePath)
    for tp in (x,y):
      while tp != j.nameBranch and tp != os.path.abspath(j.dateBranch):
        gotGid = os.stat(tp)[stat.ST_GID]
        assert self.newGid == gotGid, "%s: Expected %o, got %o"%(tp,077,gotGid)
        tp = p.split(tp)[0]
  def testCopyFromPermissions(self):
    dirPermissions=0777
    dumpPermissions=0755
    j = JDS.JsonDumpStorage(root=self.testDir,dirPermissions=dirPermissions,dumpPermissions=dumpPermissions)
    os.makedirs(self.testMoveFrom)
    u = str(suuid.uuid1())
    jopath = os.path.join(self.testMoveFrom,u+j.jsonSuffix)
    dopath = os.path.join(self.testMoveFrom,u+j.dumpSuffix)
    fj = open(jopath,'w')
    fd = open(dopath,'w')
    fj.close()
    fd.close()
    j.copyFrom(u,jopath,dopath,'w', DT.datetime(2008,8,8,8,8),createLinks = True)

    jpath = j.getJson(u)
    gotPermissions = stat.S_IMODE(os.stat(jpath)[0])
    assert dumpPermissions == stat.S_IMODE(os.stat(jpath)[0]), "%s: Expected %o, got %o" % (jpath, dumpPermissions, gotPermissions)

    dpath = j.getDump(u)
    gotPermissions = stat.S_IMODE(os.stat(dpath)[0])
    assert dumpPermissions == stat.S_IMODE(os.stat(dpath)[0]), "%s: Expected %o, got %o" % (dpath, dumpPermissions, gotPermissions)

    udir = os.path.split(dpath)[0]
    datePath = os.path.join(udir,os.readlink(os.path.abspath(os.path.splitext(dpath)[0])))

    x = p.split(jpath)[0]
    y = os.path.abspath(datePath)
    for tp in (x,y):
      while tp != j.nameBranch and tp != os.path.abspath(j.dateBranch):
        gotPermissions = stat.S_IMODE(os.stat(tp)[0])
        assert dirPermissions == gotPermissions, "%s: Expected %o, got %o"%(tp,dirPermissions,gotPermissions)
        tp = p.split(tp)[0]
  def testNewEntryPermissions(self):
    dirPermissions=0777
    dumpPermissions=0755
    j = JDS.JsonDumpStorage(root=self.testDir,dirPermissions=dirPermissions,dumpPermissions=dumpPermissions)
    u = str(suuid.uuid1())
    f1, f2 = j.newEntry(u)
    f1.close()
    f2.close()

    jpath = j.getJson(u)
    gotPermissions = stat.S_IMODE(os.stat(jpath)[0])
    assert stat.S_IMODE(os.stat(jpath)[0]) == dumpPermissions, "%s: Expected %o, got %o" % (jpath, dumpPermissions, gotPermissions)

    dpath = j.getDump(u)
    gotPermissions = stat.S_IMODE(os.stat(dpath)[0])
    assert stat.S_IMODE(os.stat(dpath)[0]) == dumpPermissions, "%s: Expected %o, got %o" % (dpath, dumpPermissions, gotPermissions)

    udir = os.path.split(dpath)[0]
    datePath = os.path.join(udir,os.readlink(os.path.abspath(os.path.splitext(dpath)[0])))

    x = p.split(jpath)[0]
    y = os.path.abspath(datePath)
    for tp in (x,y):
      while tp != j.nameBranch and tp != os.path.abspath(j.dateBranch):
        gotPermissions = stat.S_IMODE(os.stat(tp)[0])
        assert dirPermissions == gotPermissions, "%s: Expected %o, got %o"%(tp,dirPermissions,gotPermissions)
        tp = p.split(tp)[0]
    def testCopyFromPermissions(self):
        dirPermissions = 0777
        dumpPermissions = 0755
        sfl = SilentFakeLogger()
        j = JDS.JsonDumpStorage(
            root=self.testDir, dirPermissions=dirPermissions, dumpPermissions=dumpPermissions, logger=sfl
        )
        os.makedirs(self.testMoveFrom)
        u = str(socorro_uuid.uuid1())
        jopath = os.path.join(self.testMoveFrom, u + j.jsonSuffix)
        dopath = os.path.join(self.testMoveFrom, u + j.dumpSuffix)
        fj = open(jopath, "w")
        fd = open(dopath, "w")
        fj.close()
        fd.close()
        j.copyFrom(u, jopath, dopath, "w", DT.datetime(2008, 8, 8, 8, 8), createLinks=True)

        jpath = j.getJson(u)
        gotPermissions = stat.S_IMODE(os.stat(jpath)[0])
        assert dumpPermissions == stat.S_IMODE(os.stat(jpath)[0]), "%s: Expected %o, got %o" % (
            jpath,
            dumpPermissions,
            gotPermissions,
        )

        dpath = j.getDump(u)
        gotPermissions = stat.S_IMODE(os.stat(dpath)[0])
        assert dumpPermissions == stat.S_IMODE(os.stat(dpath)[0]), "%s: Expected %o, got %o" % (
            dpath,
            dumpPermissions,
            gotPermissions,
        )

        udir = os.path.split(dpath)[0]
        datePath = os.path.abspath(os.path.join(udir, os.readlink(os.path.splitext(dpath)[0])))
        namePath = os.path.abspath(os.path.splitext(dpath)[0])
        topPath = os.path.abspath(self.testDir)
        dailies = os.listdir(topPath)

        def assertPermVisitor(p):
            gotPerm = stat.S_IMODE(os.stat(p)[0])
            assert dirPermissions == gotPerm, "%s: Expected %0o, got %0o" % (p, dirPermissions, gotPerm)

        for d in dailies:
            # visitPath quietly ignores a file as the leaf
            socorro_fs.visitPath(os.path.join(topPath, d), datePath, assertPermVisitor)
            socorro_fs.visitPath(os.path.join(topPath, d), namePath, assertPermVisitor)
  def testCopyFromPermissions(self):
    dirPermissions=0777
    dumpPermissions=0755
    sfl = SilentFakeLogger()
    j = JDS.JsonDumpStorage(root=self.testDir,dirPermissions=dirPermissions,dumpPermissions=dumpPermissions,logger=sfl)
    os.makedirs(self.testMoveFrom)
    u = str(socorro_uuid.uuid1())
    jopath = os.path.join(self.testMoveFrom,u+j.jsonSuffix)
    dopath = os.path.join(self.testMoveFrom,u+j.dumpSuffix)
    fj = open(jopath,'w')
    fd = open(dopath,'w')
    fj.close()
    fd.close()
    j.copyFrom(u,jopath,dopath,'w', DT.datetime(2008,8,8,8,8, tzinfo=UTC),createLinks = True)

    jpath = j.getJson(u)
    gotPermissions = stat.S_IMODE(os.stat(jpath)[0])
    assert dumpPermissions == stat.S_IMODE(os.stat(jpath)[0]), "%s: Expected %o, got %o" % (jpath, dumpPermissions, gotPermissions)

    dpath = j.getDump(u)
    gotPermissions = stat.S_IMODE(os.stat(dpath)[0])
    assert dumpPermissions == stat.S_IMODE(os.stat(dpath)[0]), "%s: Expected %o, got %o" % (dpath, dumpPermissions, gotPermissions)

    udir = os.path.split(dpath)[0]
    datePath = os.path.abspath(os.path.join(udir,os.readlink(os.path.splitext(dpath)[0])))
    namePath = os.path.abspath(os.path.splitext(dpath)[0])
    topPath = os.path.abspath(self.testDir)
    dailies = os.listdir(topPath)

    def assertPermVisitor(p):
      gotPerm = stat.S_IMODE(os.stat(p)[0])
      assert dirPermissions == gotPerm, "%s: Expected %0o, got %0o"%(p,dirPermissions,gotPerm)
    for d in dailies:
      # visitPath quietly ignores a file as the leaf
      socorro_fs.visitPath(os.path.join(topPath,d),datePath,assertPermVisitor)
      socorro_fs.visitPath(os.path.join(topPath,d),namePath,assertPermVisitor)