<commit_before>package akashic.storage

import java.io.File
import java.nio.file.{Paths, Files}

import akashic.storage.server
import akashic.storage.admin.User
import org.apache.commons.codec.binary.Base64
import org.apache.commons.io.{IOUtils, FileUtils}
import org.apache.http.client.methods.{HttpGet, HttpPost}
import org.apache.http.impl.client.HttpClients
import org.apache.http.message.BasicHeader
import scala.xml.XML
import scala.sys.process.Process

class McTest extends ServerTestBase {
  case class FixtureParam()
  override protected def withFixture(test: OneArgTest) = {
    test(FixtureParam())
  }

  val alias = "akashic-storage"
  val confDir = "/tmp/akashic-storage-mc"

  def mc(cmd: String) = {
    // val cmdline = s"mc --config-folder=${confDir} ${cmd}"
    val cmdline = s"mc --debug ${cmd}"
    println(cmdline)
    Process(cmdline)
  }

  override def beforeEach() {
    super.beforeEach()

    val url = s"http://${server.address}/admin/user"
    val authHeader = new BasicHeader("Authorization", s"Basic ${Base64.encodeBase64URLSafeString("admin:passwd".getBytes)}")

    val postReq = new HttpPost(url)
    postReq.addHeader(authHeader)

    val postRes = HttpClients.createDefault.execute(postReq)
    assert(postRes.getStatusLine.getStatusCode === 200)
    val newUser = User.fromXML(XML.load(postRes.getEntity.getContent))
    println(newUser)

    val confDirPath = Paths.get(confDir)
    if (!Files.exists(confDirPath)) {
      Files.createDirectory(confDirPath)
    }
    FileUtils.cleanDirectory(confDirPath.toFile)

    assert(mc(s"config host add ${alias} http://${server.address} ${newUser.accessKey} ${newUser.secretKey} S3v2").! === 0)
    // assert(mc(s"config alias add ${alias} http://${server.address}").! === 0)
  }

  test("add buckets") { _ =>
    assert(mc(s"ls ${alias}").!!.split("\n").length === 1)
    assert(mc(s"mb ${alias}/abc").! === 0)
    assert(mc(s"ls ${alias}").!!.split("\n").length === 1)
    assert(mc(s"mb ${alias}/def").! === 0)
    assert(mc(s"ls ${alias}").!!.split("\n").length === 2)
  }

  test("ls bucket") { _ =>
    mc(s"mb ${alias}/abc").!
    val f = getTestFile("test.txt")
    // in sbt test we need to add --quiet flag otherwise
    // mc: <ERROR> Unable to get terminal size. Please use --quiet option. inappropriate ioctl for device
    mc(s"--quiet cp ${f.getAbsolutePath} ${alias}/abc/aaaaa").!
    mc(s"--quiet cp ${f.getAbsolutePath} ${alias}/abc/bbbbb").!<commit_msg>remove a line from "add bucket" test<commit_after>package akashic.storage

import java.io.File
import java.nio.file.{Paths, Files}

import akashic.storage.server
import akashic.storage.admin.User
import org.apache.commons.codec.binary.Base64
import org.apache.commons.io.{IOUtils, FileUtils}
import org.apache.http.client.methods.{HttpGet, HttpPost}
import org.apache.http.impl.client.HttpClients
import org.apache.http.message.BasicHeader
import scala.xml.XML
import scala.sys.process.Process

class McTest extends ServerTestBase {
  case class FixtureParam()
  override protected def withFixture(test: OneArgTest) = {
    test(FixtureParam())
  }

  val alias = "akashic-storage"
  val confDir = "/tmp/akashic-storage-mc"

  def mc(cmd: String) = {
    // val cmdline = s"mc --config-folder=${confDir} ${cmd}"
    val cmdline = s"mc --debug ${cmd}"
    println(cmdline)
    Process(cmdline)
  }

  override def beforeEach() {
    super.beforeEach()

    val url = s"http://${server.address}/admin/user"
    val authHeader = new BasicHeader("Authorization", s"Basic ${Base64.encodeBase64URLSafeString("admin:passwd".getBytes)}")

    val postReq = new HttpPost(url)
    postReq.addHeader(authHeader)

    val postRes = HttpClients.createDefault.execute(postReq)
    assert(postRes.getStatusLine.getStatusCode === 200)
    val newUser = User.fromXML(XML.load(postRes.getEntity.getContent))
    println(newUser)

    val confDirPath = Paths.get(confDir)
    if (!Files.exists(confDirPath)) {
      Files.createDirectory(confDirPath)
    }
    FileUtils.cleanDirectory(confDirPath.toFile)

    assert(mc(s"config host add ${alias} http://${server.address} ${newUser.accessKey} ${newUser.secretKey} S3v2").! === 0)
    // assert(mc(s"config alias add ${alias} http://${server.address}").! === 0)
  }

  test("add buckets") { _ =>
    // assert(mc(s"ls ${alias}").!!.split("\n").length === 1)
    assert(mc(s"mb ${alias}/abc").! === 0)
    assert(mc(s"ls ${alias}").!!.split("\n").length === 1)
    assert(mc(s"mb ${alias}/def").! === 0)
    assert(mc(s"ls ${alias}").!!.split("\n").length === 2)
  }

  test("ls bucket") { _ =>
    mc(s"mb ${alias}/abc").!
    val f = getTestFile("test.txt")
    // in sbt test we need to add --quiet flag otherwise
    // mc: <ERROR> Unable to get terminal size. Please use --quiet option. inappropriate ioctl for device
    mc(s"--quiet cp ${f.getAbsolutePath} ${alias}/abc/aaaaa").!
    mc(s"--quiet cp ${f.getAbsolutePath} ${alias}/abc/bbbbb").!
{'fraction_non_alphanumeric': 0.1706526145115525, 'fraction_numerical': 0.007296311309282529, 'mean_word_length': 3.886138613861386, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4322351', 'n_tokens_mistral': 1728, 'n_tokens_neox': 1661, 'n_words': 339}