Skip to content

Commit 0367406

Browse files
authored
Integration tests (#4)
* AppTest - app starts * DbUpraderTest * AppTest returns js bundle * AppTest returns status
1 parent 6635423 commit 0367406

17 files changed

+234
-110
lines changed

.travis.sh

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#!/usr/bin/env bash
22

3-
function wait_for_service() {
3+
function wait_for_server() {
44
n=0
55
until [ $n -ge 30 ]
66
do
7-
echo -e "Waiting for rbackup"
7+
echo -e "Waiting for RBackup"
88

99
if [ "$(curl "http://$RBACKUP_IP:3369/status" 2>/dev/null | jq -r .status)" == "RBackup running" ]; then
1010
break
@@ -18,15 +18,16 @@ function wait_for_service() {
1818
exit 1
1919
fi
2020

21-
echo -e "rbackup ready"
21+
echo -e "RBackup ready"
2222
}
2323

2424
function client_test() {
2525
cd ci-tests && \
2626
docker-compose up -d --build --force-recreate && \
2727
export RBACKUP_IP=$(docker inspect citests_tests_1 | jq -r '.[0] | .NetworkSettings.Ports."3369/tcp" | .[0].HostIp') && \
28-
wait_for_service && \
28+
wait_for_server && \
2929
cd .. && \
30+
mkdir public && mkdir public/bundle && touch public/bundle/js.bundle.txt && touch public/bundle/style.bundle.txt && \
3031
sbt ";clean;test" && \
3132
cd ci-tests && \
3233
docker-compose down

app/lib/Constants.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package lib
22

33
trait Constants {
4-
final val versionStr: String = "0.1.0"
4+
final val versionStr: String = "0.1.4"
55

66
final val SentryDsn: Option[String] = None
77
}

app/lib/db/DbScheme.scala

+20-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package lib.db
22

3+
import com.typesafe.scalalogging.StrictLogging
4+
import lib.{App, AppVersion}
35
import scalikejdbc._
46

57
//noinspection SqlNoDataSourceInspection
6-
object DbScheme {
8+
object DbScheme extends StrictLogging {
79
def create(implicit session: DBSession): Unit = {
10+
val currentVersionStr = App.versionStr
11+
812
sql"""
913
|CREATE TABLE IF NOT EXISTS FILES
1014
|(
@@ -40,16 +44,26 @@ object DbScheme {
4044
|);
4145
|
4246
""".stripMargin.executeUpdate().apply()
47+
48+
if (App.version > AppVersion(0, 1, 3)) {
49+
if (sql"""select value from settings where key='db_version'""".map(_.string("value")).single().apply().isEmpty) {
50+
logger.info(s"Didn't found db_version in DB, setting to $currentVersionStr")
51+
52+
sql"""
53+
|insert ignore into settings values ('db_version', ${currentVersionStr});
54+
""".stripMargin.executeUpdate().apply()
55+
}
56+
}
4357
}
4458

45-
def truncateAll(implicit session: DBSession): Unit = {
59+
def dropAll(implicit session: DBSession): Unit = {
4660
sql"""
4761
|SET REFERENTIAL_INTEGRITY FALSE;
4862
|
49-
|truncate table files;
50-
|truncate table settings;
51-
|truncate table backup_sets;
52-
|truncate table backup_sets_files;
63+
|drop table if exists files;
64+
|drop table if exists settings;
65+
|drop table if exists backup_sets;
66+
|drop table if exists backup_sets_files;
5367
|
5468
|SET REFERENTIAL_INTEGRITY TRUE;
5569
""".stripMargin.executeUpdate().apply()

app/lib/db/DbUpgrader.scala

+1-5
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,7 @@ class DbUpgrader(dao: Dao)(implicit sch: Scheduler) extends StrictLogging {
5151
}
5252
.toList
5353
.sequentially
54-
.flatMap { _ =>
55-
dao
56-
.setSetting("db_version", App.versionStr)
57-
.map(_ => logger.debug(s"DB upgraded to version ${App.versionStr}"))
58-
}
54+
.map(_ => ())
5955
}
6056
}
6157
}

build.sbt

+7-4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ libraryDependencies ++= Seq(
4444
"io.sentry" % "sentry-logback" % "1.7.14",
4545
"com.typesafe.scala-logging" %% "scala-logging" % "3.9.0",
4646
"org.scalatest" %% "scalatest" % "3.0.5",
47+
"org.scalaj" %% "scalaj-http" % "2.4.1" % "test",
48+
"org.scalatestplus.play" %% "scalatestplus-play" % "3.1.2" % "test",
4749
"org.mockito" % "mockito-core" % "2.23.0" % "test"
4850
)
4951

@@ -94,11 +96,12 @@ lazy val setVersionInSources = taskKey[Unit]("Sets build version into sources")
9496

9597
setVersionInSources := {
9698
import java.io.PrintWriter
99+
97100
import scala.io.Source
98-
101+
99102
val version = sys.env.getOrElse("VERSION", throw new IllegalArgumentException("Missing VERSION env property"))
100103
println(s"Setting app version to $version")
101-
104+
102105
val src = Source.fromFile(ConstantsPath).mkString
103106
val updated = src.replaceAll(
104107
"""final val versionStr: String = "\d+.\d+.\d+"""",
@@ -132,8 +135,8 @@ setSentryDsnInSources := {
132135
}
133136
}
134137

135-
sources in (Compile, doc) := Seq.empty
136-
publishArtifact in (Compile, packageDoc) := false
138+
sources in(Compile, doc) := Seq.empty
139+
publishArtifact in(Compile, packageDoc) := false
137140

138141
PlayKeys.playDefaultPort := 3370
139142
PlayKeys.devSettings := Seq("play.server.http.port" -> "3370")

ci-tests/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ FROM jendakol/rbackup:0.1
33
WORKDIR /
44
COPY . .
55

6-
ENTRYPOINT /wait-for-it.sh mysql:3306 -s -t 30 && /start-rbackup.sh
6+
ENTRYPOINT /wait-for-it.sh mysql:3306 -s -t 30 && /start-server.sh
File renamed without changes.

ci-tests/tests.sh

-69
This file was deleted.

conf/reference.conf

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ cloudConnectorDefaults {
1111
chunkSize = 2048
1212
}
1313

14+
cloudConnector {}
15+
1416
fileHandler {
1517
parallelism = 2 // change the maxConnections accordingly!
1618
retries = 2

test/IntegrationTest.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import utils.TestOps._
1212

1313
class IntegrationTest extends FunSuite with StrictLogging {
1414
private val rbackupIp: String = {
15-
val ip = System.getenv("RBACKUP_IP")
15+
val ip = Option(System.getenv("RBACKUP_IP")).getOrElse("localhost")
1616

1717
if (ip == "0.0.0.0") "localhost" else ip
1818
}

test/lib/AppTest.scala

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package lib
2+
3+
import io.circe.Json
4+
import io.circe.parser._
5+
import lib.db.DbScheme
6+
import org.scalatest.FunSuite
7+
import org.scalatest.concurrent.Eventually
8+
import org.scalatest.time.{Seconds, Span}
9+
import org.scalatestplus.play.guice.GuiceOneServerPerSuite
10+
import play.api.Application
11+
import play.api.inject.guice.GuiceApplicationBuilder
12+
import scalaj.http.Http
13+
import scalikejdbc.{ConnectionPool, DB}
14+
15+
import scala.io.Source
16+
17+
class AppTest extends FunSuite with Eventually with GuiceOneServerPerSuite {
18+
19+
private val rbackupIp: String = {
20+
val ip = Option(System.getenv("RBACKUP_IP")).getOrElse("localhost")
21+
22+
if (ip == "0.0.0.0") "localhost" else ip
23+
}
24+
25+
test("app starts") {
26+
eventually(timeout(Span(10, Seconds))) {
27+
Source.fromURL(s"http://$rbackupIp:$port/").mkString // it should only not fail
28+
}
29+
}
30+
31+
test("returns js bundle") {
32+
eventually(timeout(Span(10, Seconds))) {
33+
Source.fromURL(s"http://$rbackupIp:$port/assets/bundle/js.bundle.txt").mkString // it should only not fail
34+
}
35+
}
36+
37+
test("returns status") {
38+
eventually(timeout(Span(10, Seconds))) {
39+
val json = sendCommand("status", Json.Null)
40+
41+
assertResult(Right("INSTALLED"))(json.hcursor.get[String]("status"))
42+
assertResult(Right(true))(json.hcursor.get[Boolean]("success"))
43+
}
44+
}
45+
46+
override def fakeApplication(): Application = {
47+
ConnectionPool.singleton(s"jdbc:h2:tcp://$rbackupIp:1521/test;MODE=MySQL", "sa", "")
48+
49+
DB.autoCommit { implicit session =>
50+
DbScheme.dropAll
51+
}
52+
53+
new GuiceApplicationBuilder()
54+
.configure("play.filters.hosts.allowed" -> Seq(s"localhost:$port"))
55+
.build()
56+
}
57+
58+
private def sendCommand(name: String, data: Json): Json = {
59+
val resp = Http(s"http://localhost:$port/ajax-api").postData(s"""{"name": "$name", "data": $data}""").asString
60+
61+
if (!resp.is2xx) fail(resp.body)
62+
63+
parse(resp.body).toTry.fold(throw _, identity)
64+
}
65+
66+
}

test/lib/DbScheme1_0_3.scala

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package lib
2+
3+
import scalikejdbc._
4+
5+
object DbScheme1_0_3 {
6+
def create(implicit session: DBSession): Unit = {
7+
sql"""
8+
|CREATE TABLE IF NOT EXISTS FILES
9+
|(
10+
| PATH VARCHAR(65536) PRIMARY KEY NOT NULL,
11+
| LAST_MODIFIED TIMESTAMP NOT NULL,
12+
| SIZE BIGINT NOT NULL,
13+
| REMOTE_FILE TEXT
14+
|);
15+
|
16+
|CREATE UNIQUE INDEX IF NOT EXISTS "files_PATH_uindex" ON FILES (PATH);
17+
|
18+
|create table if not exists settings
19+
|(
20+
| key varchar(200) not null,
21+
| value varchar(65536) not null
22+
|);
23+
|
24+
|create table if not exists backup_sets
25+
|(
26+
| id int primary key auto_increment not null,
27+
| name varchar(200) not null,
28+
| frequency int not null default 360,
29+
| last_execution DATETIME,
30+
| processing bool default false not null
31+
|);
32+
|
33+
|create table if not exists backup_sets_files
34+
|(
35+
| path VARCHAR(65536) NOT NULL,
36+
| set_id int not null,
37+
| foreign key (set_id) references backup_sets(id) on delete cascade on update cascade,
38+
| primary key (path, set_id)
39+
|);
40+
|
41+
""".stripMargin.executeUpdate().apply()
42+
}
43+
44+
}

test/lib/TestWithDB.scala

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package lib
2+
3+
import lib.db.DbScheme
4+
import org.scalatest.{BeforeAndAfterEach, FunSuite}
5+
import scalikejdbc.config.DBs
6+
import scalikejdbc.{ConnectionPool, DB}
7+
8+
trait TestWithDB extends FunSuite with BeforeAndAfterEach {
9+
override protected def beforeEach(): Unit = {
10+
ConnectionPool.singleton("jdbc:h2:tcp://localhost:1521/test;MODE=MySQL", "sa", "")
11+
12+
DB.autoCommit { implicit session =>
13+
DbScheme.dropAll
14+
DbScheme.create
15+
}
16+
}
17+
18+
override protected def afterEach(): Unit = {
19+
DBs.close()
20+
}
21+
22+
}

0 commit comments

Comments
 (0)