|
| 1 | +package com.ambiata.ivory.alien.hdfs |
| 2 | + |
| 3 | +import scala.io.Codec |
| 4 | +import scalaz.{Store => _, _}, Scalaz._, \&/._, effect.IO |
| 5 | +import scodec.bits.ByteVector |
| 6 | +import org.specs2._, org.specs2.matcher._ |
| 7 | +import org.scalacheck.Arbitrary, Arbitrary._ |
| 8 | +import org.apache.hadoop.conf.Configuration |
| 9 | +import org.apache.hadoop.fs.Path |
| 10 | +import com.ambiata.saws.core._ |
| 11 | +import com.ambiata.mundane.control._ |
| 12 | +import com.ambiata.mundane.io._ |
| 13 | +import com.ambiata.mundane.testing._, ResultTIOMatcher._ |
| 14 | +import java.io.{File, FileOutputStream, ByteArrayInputStream} |
| 15 | +import java.util.UUID |
| 16 | + |
| 17 | + |
| 18 | +// FIX Workout how this test can be pulled out and shared with posix/s3/hdfs. |
| 19 | +class HdfsStoreSpec extends Specification with ScalaCheck { def is = args.execute(threadsNb = 10) ^ s2""" |
| 20 | + Hdfs Store Usage |
| 21 | + ================ |
| 22 | + |
| 23 | + list path $list |
| 24 | + filter listed paths $filter |
| 25 | + find path in root (thirdish) $find |
| 26 | + find path in root (first) $findfirst |
| 27 | + find path in root (last) $findlast |
| 28 | + |
| 29 | + exists $exists |
| 30 | + not exists $notExists |
| 31 | + |
| 32 | + delete $delete |
| 33 | + deleteAll $deleteAll |
| 34 | + |
| 35 | + move $move |
| 36 | + move and read $moveRead |
| 37 | + copy $copy |
| 38 | + copy and read $copyRead |
| 39 | + mirror $mirror |
| 40 | + |
| 41 | + moveTo $moveTo |
| 42 | + copyTo $copyTo |
| 43 | + mirrorTo $mirrorTo |
| 44 | + |
| 45 | + checksum $checksum |
| 46 | + |
| 47 | + read / write bytes $bytes |
| 48 | + |
| 49 | + read / write strings $strings |
| 50 | + |
| 51 | + read / write utf8 strings $utf8Strings |
| 52 | + |
| 53 | + read / write lines $lines |
| 54 | + |
| 55 | + read / write utf8 lines $utf8Lines |
| 56 | + |
| 57 | + """ |
| 58 | + |
| 59 | + implicit val params = |
| 60 | + Parameters(workers = 20, minTestsOk = 40, maxSize = 10) |
| 61 | + |
| 62 | + val conf = new Configuration |
| 63 | + |
| 64 | + implicit def HdfsStoreArbitary: Arbitrary[HdfsStore] = |
| 65 | + Arbitrary(arbitrary[Int].map(math.abs).map(n => |
| 66 | + HdfsStore(conf, FilePath.root </> "tmp" </> s"HdfsStoreSpec.${UUID.randomUUID}.${n}"))) |
| 67 | + |
| 68 | + def list = |
| 69 | + prop((store: HdfsStore, paths: Paths) => clean(store, paths) { filepaths => |
| 70 | + store.list(FilePath.root) must beOkValue(filepaths) }) |
| 71 | + |
| 72 | + def filter = |
| 73 | + prop((store: HdfsStore, paths: Paths) => clean(store, paths) { filepaths => |
| 74 | + val first = filepaths.head |
| 75 | + val last = filepaths.last |
| 76 | + val expected = if (first == last) List(first) else List(first, last) |
| 77 | + store.filter(FilePath.root, x => x == first || x == last) must beOkLike(paths => paths must contain(allOf(expected:_*))) }) |
| 78 | + |
| 79 | + def find = |
| 80 | + prop((store: HdfsStore, paths: Paths) => paths.entries.length >= 3 ==> { clean(store, paths) { filepaths => |
| 81 | + val third = filepaths.drop(2).head |
| 82 | + store.find(FilePath.root, _ == third) must beOkValue(Some(third)) } }) |
| 83 | + |
| 84 | + def findfirst = |
| 85 | + prop((store: HdfsStore, paths: Paths) => clean(store, paths) { filepaths => |
| 86 | + store.find(FilePath.root, x => x == filepaths.head) must beOkValue(Some(filepaths.head)) }) |
| 87 | + |
| 88 | + def findlast = |
| 89 | + prop((store: HdfsStore, paths: Paths) => clean(store, paths) { filepaths => |
| 90 | + store.find(FilePath.root, x => x == filepaths.last) must beOkValue(Some(filepaths.last)) }) |
| 91 | + |
| 92 | + def exists = |
| 93 | + prop((store: HdfsStore, paths: Paths) => clean(store, paths) { filepaths => |
| 94 | + filepaths.traverseU(store.exists) must beOkLike(_.forall(identity)) }) |
| 95 | + |
| 96 | + def notExists = |
| 97 | + prop((store: HdfsStore, paths: Paths) => store.exists(FilePath.root </> "i really don't exist") must beOkValue(false)) |
| 98 | + |
| 99 | + def delete = |
| 100 | + prop((store: HdfsStore, paths: Paths) => clean(store, paths) { filepaths => |
| 101 | + val first = filepaths.head |
| 102 | + (store.delete(first) >> filepaths.traverseU(store.exists)) must beOkLike(x => !x.head && x.tail.forall(identity)) }) |
| 103 | + |
| 104 | + def deleteAll = |
| 105 | + prop((store: HdfsStore, paths: Paths) => clean(store, paths) { filepaths => |
| 106 | + (store.deleteAll(FilePath.root) >> filepaths.traverseU(store.exists)) must beOkLike(x => !x.tail.exists(identity)) }) |
| 107 | + |
| 108 | + def move = |
| 109 | + prop((store: HdfsStore, m: Entry, n: Entry) => clean(store, Paths(m :: Nil)) { _ => |
| 110 | + (store.move(m.full.toFilePath, n.full.toFilePath) >> |
| 111 | + store.exists(m.full.toFilePath).zip(store.exists(n.full.toFilePath))) must beOkValue(false -> true) }) |
| 112 | + |
| 113 | + def moveRead = |
| 114 | + prop((store: HdfsStore, m: Entry, n: Entry) => clean(store, Paths(m :: Nil)) { _ => |
| 115 | + (store.move(m.full.toFilePath, n.full.toFilePath) >> |
| 116 | + store.utf8.read(n.full.toFilePath)) must beOkValue(m.value.toString) }) |
| 117 | + |
| 118 | + def copy = |
| 119 | + prop((store: HdfsStore, m: Entry, n: Entry) => clean(store, Paths(m :: Nil)) { _ => |
| 120 | + (store.copy(m.full.toFilePath, n.full.toFilePath) >> |
| 121 | + store.exists(m.full.toFilePath).zip(store.exists(n.full.toFilePath))) must beOkValue(true -> true) }) |
| 122 | + |
| 123 | + def copyRead = |
| 124 | + prop((store: HdfsStore, m: Entry, n: Entry) => clean(store, Paths(m :: Nil)) { _ => |
| 125 | + (store.copy(m.full.toFilePath, n.full.toFilePath) >> |
| 126 | + store.utf8.read(m.full.toFilePath).zip(store.utf8.read(n.full.toFilePath))) must beOkLike({ case (in, out) => in must_== out }) }) |
| 127 | + |
| 128 | + def mirror = |
| 129 | + prop((store: HdfsStore, paths: Paths) => clean(store, paths) { filepaths => |
| 130 | + store.mirror(FilePath.root, FilePath.root </> "mirror") >> store.list(FilePath.root </> "mirror") must beOkValue(filepaths.map("mirror" </> _)) }) |
| 131 | + |
| 132 | + def moveTo = |
| 133 | + prop((store: HdfsStore, alternate: HdfsStore, m: Entry, n: Entry) => clean(store, alternate, Paths(m :: Nil)) { _ => |
| 134 | + (store.moveTo(alternate, m.full.toFilePath, n.full.toFilePath) >> |
| 135 | + store.exists(m.full.toFilePath).zip(alternate.exists(n.full.toFilePath))) must beOkValue(false -> true) }) |
| 136 | + |
| 137 | + def copyTo = |
| 138 | + prop((store: HdfsStore, alternate: HdfsStore, m: Entry, n: Entry) => clean(store, alternate, Paths(m :: Nil)) { _ => |
| 139 | + (store.copyTo(alternate, m.full.toFilePath, n.full.toFilePath) >> |
| 140 | + store.exists(m.full.toFilePath).zip(alternate.exists(n.full.toFilePath))) must beOkValue(true -> true) }) |
| 141 | + |
| 142 | + def mirrorTo = |
| 143 | + prop((store: HdfsStore, alternate: HdfsStore, paths: Paths) => clean(store, alternate, paths) { filepaths => |
| 144 | + store.mirrorTo(alternate, FilePath.root, FilePath.root </> "mirror") >> alternate.list(FilePath.root </> "mirror") must beOkValue(filepaths.map("mirror" </> _)) }) |
| 145 | + |
| 146 | + def checksum = |
| 147 | + prop((store: HdfsStore, m: Entry) => clean(store, Paths(m :: Nil)) { _ => |
| 148 | + store.checksum(m.full.toFilePath, MD5) must beOkValue(Checksum.string(m.value.toString, MD5)) }) |
| 149 | + |
| 150 | + def bytes = |
| 151 | + prop((store: HdfsStore, m: Entry, bytes: Array[Byte]) => clean(store, Paths(m :: Nil)) { _ => |
| 152 | + (store.bytes.write(m.full.toFilePath, ByteVector(bytes)) >> store.bytes.read(m.full.toFilePath)) must beOkValue(ByteVector(bytes)) }) |
| 153 | + |
| 154 | + def strings = |
| 155 | + prop((store: HdfsStore, m: Entry, s: String) => clean(store, Paths(m :: Nil)) { _ => |
| 156 | + (store.strings.write(m.full.toFilePath, s, Codec.UTF8) >> store.strings.read(m.full.toFilePath, Codec.UTF8)) must beOkValue(s) }) |
| 157 | + |
| 158 | + def utf8Strings = |
| 159 | + prop((store: HdfsStore, m: Entry, s: String) => clean(store, Paths(m :: Nil)) { _ => |
| 160 | + (store.utf8.write(m.full.toFilePath, s) >> store.utf8.read(m.full.toFilePath)) must beOkValue(s) }) |
| 161 | + |
| 162 | + def lines = |
| 163 | + prop((store: HdfsStore, m: Entry, s: List[Int]) => clean(store, Paths(m :: Nil)) { _ => |
| 164 | + (store.lines.write(m.full.toFilePath, s.map(_.toString), Codec.UTF8) >> store.lines.read(m.full.toFilePath, Codec.UTF8)) must beOkValue(s.map(_.toString)) }) |
| 165 | + |
| 166 | + def utf8Lines = |
| 167 | + prop((store: HdfsStore, m: Entry, s: List[Int]) => clean(store, Paths(m :: Nil)) { _ => |
| 168 | + (store.linesUtf8.write(m.full.toFilePath, s.map(_.toString)) >> store.linesUtf8.read(m.full.toFilePath)) must beOkValue(s.map(_.toString)) }) |
| 169 | + |
| 170 | + def files(paths: Paths): List[FilePath] = |
| 171 | + paths.entries.map(e => e.full.toFilePath).sortBy(_.path) |
| 172 | + |
| 173 | + def create(store: HdfsStore, paths: Paths): ResultT[IO, Unit] = |
| 174 | + paths.entries.traverseU(e => |
| 175 | + Hdfs.writeWith[Unit](new Path((store.base </> e.full).path), out => ResultT.safe[IO, Unit] { out.write( e.value.toString.getBytes("UTF-8")) }).run(conf)).void |
| 176 | + |
| 177 | + def clean[A](store: HdfsStore, paths: Paths)(run: List[FilePath] => A): A = { |
| 178 | + create(store, paths).run.unsafePerformIO |
| 179 | + try run(files(paths)) |
| 180 | + finally store.deleteAll(FilePath.root).run.unsafePerformIO |
| 181 | + } |
| 182 | + |
| 183 | + def clean[A](store: HdfsStore, alternate: HdfsStore, paths: Paths)(run: List[FilePath] => A): A = { |
| 184 | + create(store, paths).run.unsafePerformIO |
| 185 | + try run(files(paths)) |
| 186 | + finally (store.deleteAll(FilePath.root) >> alternate.deleteAll(FilePath.root)).run.unsafePerformIO |
| 187 | + } |
| 188 | +} |
0 commit comments