Skip to content

Commit

Permalink
Change Job to use EnvVar instead of a seq of String pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
amanjpro committed Aug 27, 2020
1 parent c802bd5 commit afca586
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 38 deletions.
4 changes: 3 additions & 1 deletion src/main/scala/AppConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ object AppConfig {
val startAt = jobConfig.getLongWithDefault(
"start-at", groupStartAt)
val jobInfo = jobConfig.getOptionStringWithDefault("job-info", groupInfo)
val jobEnv = jobConfig.getEnv("env", groupEnv)
val jobEnv = jobConfig.getEnv("env", groupEnv).map {
case (name, value) => EnvVar(name, value)
}

Job(
index,
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/checker/StatusChecker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class StatusChecker(groups: Seq[Group],
val currentClockCounter = clockCounter()
val expireAt = currentClockCounter + 1000 *
refreshValidityInSeconds
self ! BatchRun(job.cmd, periods, job.env,
self ! BatchRun(job.cmd, periods, job.env.map(_.tupled),
group.groupId, job.jobId, job.prometheusId,
currentClockCounter, expireAt)
}
Expand Down
1 change: 0 additions & 1 deletion src/main/scala/models/EnvVar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ object EnvVar {

implicit val envVarDecoer: Decoder[EnvVar] = new Decoder[EnvVar] {
final def apply(obj: HCursor): Decoder.Result[EnvVar] = {
println(obj.downField("type"))
obj.downField("type").as[String].flatMap {
case "secure" => obj.as[SecureEnvVar]
case "plain" => obj.as[PlainEnvVar]
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/models/Job.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ case class Job(
startAt: Long,
alertLevels: AlertLevels,
info: Option[String],
env: Seq[(String, String)]
env: Seq[EnvVar]
) {
val timeFormat = DateTimeFormatter.ofPattern(timePattern)
}
Expand Down
4 changes: 3 additions & 1 deletion src/test/resources/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ check-groups: {
# `env` can be set on both grup and job level
env: {
VAR1: "foo"
VAR2: "bar"
# `secure(...)` pattern tells Greenish that the data should be kept
# secret and not returned in any of the endpoints.
VAR2: "secure(bar)"
}
# Job groups, a group is a set of jobs/data-sets
# that have some sort of logical relation
Expand Down
24 changes: 14 additions & 10 deletions src/test/scala/AppConfigSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,32 @@ class AppConfigSpec() extends Matchers
ZoneId.of("UTC"), 24, 2,
AlertLevels(0, 1, 2, 3),
Some("Job info"),
Seq("VAR1" -> "baz", "VAR2" -> "bazomba",
"VAR3" -> "bada", "VAR4" -> "badam"),
Seq(EnvVar("VAR1", "baz"), EnvVar("VAR2", "bazomba"),
EnvVar("VAR3", "bada"), EnvVar("VAR4", "badam")),
),
Job(1, "Job2", Some("Reporting"), "job_2", "/tmp/second_script job2",
"yyyy-MM-dd-HH", Daily, 2,
ZoneId.of("UTC"), 24, 1,
AlertLevels(0, 1, 2, 3),
Some("Group info"),
Seq("VAR1" -> "baz", "VAR2" -> "bar", "VAR3" -> "bazooka"),
Seq(EnvVar("VAR1", "baz"), EnvVar("VAR2", "secure(bar)"),
EnvVar("VAR3", "bazooka")),
),
Job(2, "Job5", Some("Reporting"), "group1_job5", "/tmp/second_script job5",
"yyyy-MM-dd-HH", Hourly, 2,
ZoneId.of("US/Alaska"), 24, 1,
AlertLevels(0, 1, 2, 3),
Some("Group info"),
Seq("VAR1" -> "baz", "VAR2" -> "bar", "VAR3" -> "bazooka"),
Seq(EnvVar("VAR1", "baz"), EnvVar("VAR2", "secure(bar)"),
EnvVar("VAR3", "bazooka")),
),
Job(3, "Job7", Some("Reporting"), "group1_job7", "/tmp/second_script job7",
"yyyy-MM-dd-HH", Cron("0 * * * *"), 2,
ZoneId.of("US/Alaska"), 24, 1,
AlertLevels(0, 1, 2, 3),
Some("Group info"),
Seq("VAR1" -> "baz", "VAR2" -> "bar", "VAR3" -> "bazooka"),
Seq(EnvVar("VAR1", "baz"), EnvVar("VAR2", "secure(bar)"),
EnvVar("VAR3", "bazooka")),
),
)),
Group(1, "Group2", Seq(
Expand All @@ -53,7 +56,7 @@ class AppConfigSpec() extends Matchers
Some("""|<a href="link">
|Link
|</a>""".stripMargin),
Seq("VAR1" -> "foo", "VAR2" -> "bar"),
Seq(EnvVar("VAR1", "foo"), EnvVar("VAR2", "secure(bar)")),
),
Job(1, "Job4", Some("SRE"), "job_4", "/tmp/fourth_script",
"yyyy-01-01", Annually, 1,
Expand All @@ -62,7 +65,7 @@ class AppConfigSpec() extends Matchers
Some("""|<a href="link">
|Link
|</a>""".stripMargin),
Seq("VAR1" -> "foo", "VAR2" -> "bar"),
Seq(EnvVar("VAR1", "foo"), EnvVar("VAR2", "secure(bar)")),
),
Job(2, "Job6", Some("SRE"), "group2_job6", "/tmp/second_script job6",
"yyyy-MM-dd-HH-mm", Daily, 1,
Expand All @@ -71,7 +74,8 @@ class AppConfigSpec() extends Matchers
Some("""|<a href="link">
|Link
|</a>""".stripMargin),
Seq("VAR1" -> "baz", "VAR2" -> "bar", "VAR3" -> "bazooka"),
Seq(EnvVar("VAR1", "baz"), EnvVar("VAR2", "secure(bar)"),
EnvVar("VAR3", "bazooka")),
),
)),
),
Expand Down Expand Up @@ -192,12 +196,12 @@ class AppConfigSpec() extends Matchers
val appEnv = appConfig.getEnv("env", Seq.empty)

"get value if parent is empty, and key exists" in {
appEnv shouldBe Seq("VAR1" -> "foo", "VAR2" -> "bar")
appEnv shouldBe Seq("VAR1" -> "foo", "VAR2" -> "secure(bar)")
}

"properly dedup parent and child lists, if key exists" in {
val actualGroup = groupConfig.getEnv("env", appEnv)
val expectedGroup = Seq("VAR1" -> "baz", "VAR2" -> "bar",
val expectedGroup = Seq("VAR1" -> "baz", "VAR2" -> "secure(bar)",
"VAR3" -> "bazooka")

actualGroup shouldBe expectedGroup
Expand Down
24 changes: 16 additions & 8 deletions src/test/scala/AppSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,31 @@ class AppSpec() extends Matchers
"yyyy-MM-dd-HH", Hourly, 3,
ZoneId.of("UTC"), 24, 0,
AlertLevels(0, 1, 2, 3),
None, Seq("VAR1" -> "foo", "VAR2" -> "bar"),
None,
Seq(EnvVar("VAR1", "baz"), EnvVar("VAR2", "secure(bar)")),
),
Job(1, "Job2", None, "job_1", "/tmp/second_script job2",
"yyyy-MM-dd-HH", Daily, 2,
ZoneId.of("UTC"), 24, 0,
AlertLevels(0, 1, 2, 3),
None, Seq("VAR1" -> "foo", "VAR2" -> "bar"),
None,
Seq(EnvVar("VAR1", "baz"), EnvVar("VAR2", "secure(bar)")),
),
)),
Group(1, "Group2", Seq(
Job(0, "Job3", None, "job_2", "/tmp/third_script",
"yyyy-MM-dd", Monthly, 1,
ZoneId.of("UTC"), 3, 0,
AlertLevels(0, 1, 2, 3),
None, Seq("VAR1" -> "foo", "VAR2" -> "bar"),
None,
Seq(EnvVar("VAR1", "baz"), EnvVar("VAR2", "secure(bar)")),
),
Job(1, "Job4", None, "job_2", "/tmp/fourth_script",
"yyyy-01-01", Annually, 1,
ZoneId.of("UTC"), 3, 0,
AlertLevels(0, 1, 2, 3),
None, Seq("VAR1" -> "foo", "VAR2" -> "bar"),
None,
Seq(EnvVar("VAR1", "baz"), EnvVar("VAR2", "secure(bar)")),
),
)),
),
Expand All @@ -63,27 +67,31 @@ class AppSpec() extends Matchers
"yyyy-MM-dd-HH", Hourly, 3,
ZoneId.of("UTC"), 24, 0,
AlertLevels(0, 1, 2, 3),
None, Seq("VAR1" -> "foo", "VAR2" -> "bar"),
None,
Seq(EnvVar("VAR1", "baz"), EnvVar("VAR2", "secure(bar)")),
),
Job(1, "Job2", None, "job_2", "/tmp/second_script job2",
"yyyy-MM-dd-HH", Daily, 2,
ZoneId.of("UTC"), 24, 0,
AlertLevels(0, 1, 2, 3),
None, Seq("VAR1" -> "foo", "VAR2" -> "bar"),
None,
Seq(EnvVar("VAR1", "baz"), EnvVar("VAR2", "secure(bar)")),
),
)),
Group(1, "Group2", Seq(
Job(0, "Job3", None, "job_3", "/tmp/third_script",
"yyyy-MM-dd", Monthly, 1,
ZoneId.of("UTC"), 3, 0,
AlertLevels(0, 1, 2, 3),
None, Seq("VAR1" -> "foo", "VAR2" -> "bar"),
None,
Seq(EnvVar("VAR1", "baz"), EnvVar("VAR2", "secure(bar)")),
),
Job(1, "Job4", None, "job_4", "/tmp/fourth_script",
"yyyy-01-01", Annually, 1,
ZoneId.of("UTC"), 3, 0,
AlertLevels(0, 1, 2, 3),
None, Seq("VAR1" -> "foo", "VAR2" -> "bar"),
None,
Seq(EnvVar("VAR1", "baz"), EnvVar("VAR2", "secure(bar)")),
),
)),
),
Expand Down
8 changes: 4 additions & 4 deletions src/test/scala/checker/StatusCheckerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,28 +60,28 @@ class StatusCheckerSpec()
"yyyy-MM-dd-HH", Hourly, 1, ZoneId.of("UTC"),
2, 0, AlertLevels(0, 1, 2, 3),
None,
Seq("GREENISH_VALUE_FOR_TEST" -> "/tmp"),
Seq(EnvVar("GREENISH_VALUE_FOR_TEST", "secure(/tmp)")),
)

val job2 = Job(1, "job2", None, "p2", s"$lsScript /tmp/job2",
"yyyy-MM-dd-HH", Hourly, 2, ZoneId.of("UTC"),
1, 0, AlertLevels(1, 2, 3, 4),
None,
Seq("GREENISH_VALUE_FOR_TEST" -> "/tmp"),
Seq(EnvVar("GREENISH_VALUE_FOR_TEST", "/tmp")),
)

val job3 = Job(0, "job3", None, "p3", s"$lsScript /tmp/job3",
"yyyy-MM-dd-HH", Hourly, 1, ZoneId.of("UTC"),
3, 0, AlertLevels(0, 1, 2, 3),
None,
Seq("GREENISH_VALUE_FOR_TEST" -> "/tmp"),
Seq(EnvVar("GREENISH_VALUE_FOR_TEST", "/tmp")),
)

val job4 = Job(1, "job4", None, "p4", s"$lsEnvScript job4",
"yyyy-MM-dd-HH", Hourly, 1, ZoneId.of("UTC"),
4, 0, AlertLevels(0, 1, 2, 3),
None,
Seq("GREENISH_VALUE_FOR_TEST" -> "/tmp"),
Seq(EnvVar("GREENISH_VALUE_FOR_TEST", "/tmp")),
)

val group1 = Group(0, "group1", Seq(job1, job2))
Expand Down
6 changes: 3 additions & 3 deletions src/test/scala/endpoints/RoutesSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,17 @@ class RoutesSpec()

val job1 = Job(0, "job1", None, "p1", s"$lsScript /tmp",
"yyyy-MM-dd-HH", Hourly, 1, ZoneId.of("UTC"),
2, 0, AlertLevels(0, 1, 2, 3), None, Seq("a" -> "b"),
2, 0, AlertLevels(0, 1, 2, 3), None, Seq(EnvVar("a", "b")),
)

val job2 = Job(1, "job2", None, "p2", s"$lsScript /tmp",
"yyyy-MM-dd-HH", Hourly, 1, ZoneId.of("UTC"),
2, 0, AlertLevels(0, 1, 2, 3), None, Seq("a" -> "b"),
2, 0, AlertLevels(0, 1, 2, 3), None, Seq(EnvVar("a", "b")),
)

val job3 = Job(0, "job3", None, "p3", s"$lsScript /tmp",
"yyyy-MM-dd-HH", Hourly, 1, ZoneId.of("UTC"),
2, 0, AlertLevels(0, 1, 2, 3), None, Seq("a" -> "b"),
2, 0, AlertLevels(0, 1, 2, 3), None, Seq(EnvVar("a", "b")),
)

val group1 = Group(0, "group1", Seq(job1, job2))
Expand Down
4 changes: 2 additions & 2 deletions src/test/scala/models/GroupStatusSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ class GroupStatusSpec() extends Matchers

val job1 = Job(1, "job1", None, "p1", "foo",
"yyyy-MM-dd-HH", Hourly, 1, ZoneId.of("UTC"),
4, 0, AlertLevels(0, 1, 2, 3), None, Seq("a" -> "b")
4, 0, AlertLevels(0, 1, 2, 3), None, Seq(EnvVar("a", "b"))
)

val job2 = Job(2, "job2", None, "p2", "bar",
"yyyy-MM-dd-HH", Hourly, 1, ZoneId.of("UTC"),
4, 0, AlertLevels(0, 1, 2, 3), None, Seq("a" -> "b")
4, 0, AlertLevels(0, 1, 2, 3), None, Seq(EnvVar("a", "secure(b)"))
)

val group1 = Group(0, "group1", Seq(job1))
Expand Down
12 changes: 6 additions & 6 deletions src/test/scala/models/JsonSerdeSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class JsonSerdeSpec() extends Matchers
"Group" must {
val job = Job(1, "j", None, "p", "c", "yyyy-MM-dd",
Hourly, 1, ZoneId.of("UTC"), 2, 0, AlertLevels(3, 4, 5, 6),
None, Seq("a" -> "b"))
None, Seq(EnvVar("a", "b")))
val group = Group(0, "g", Seq(job))

"produce correct JSON" in {
Expand All @@ -173,7 +173,7 @@ class JsonSerdeSpec() extends Matchers
"GroupStatus" must {
val job = Job(1, "j", None, "p", "c", "yyyy-MM-dd",
Hourly, 1, ZoneId.of("UTC"), 2, 0, AlertLevels(3, 4, 5, 6),
None, Seq("a" -> "b"))
None, Seq(EnvVar("a", "b")))
val group = Group(0, "g", Seq(job))
val periods = Seq(PeriodHealth("1", true), PeriodHealth("2", false))
val jobStatus = JobStatus(job, 100, periods)
Expand Down Expand Up @@ -224,7 +224,7 @@ class JsonSerdeSpec() extends Matchers
val alertLevels = AlertLevels(3, 4, 5, 6)
val job = Job(1, "j", None, "p", "c", "yyyy-MM-dd",
Hourly, 1, ZoneId.of("UTC"), 2, 0, alertLevels,
None, Seq("a" -> "b"))
None, Seq(EnvVar("a", "b")))

"produce correct JSON when there is no owner and no info" in {
val alertLevels = AlertLevels(3, 4, 5, 6)
Expand All @@ -244,7 +244,7 @@ class JsonSerdeSpec() extends Matchers
"start_at" -> 0.asJson,
"alert_levels" -> alertLevels.asJson,
"info" -> Json.Null,
"env" -> Seq("a" -> "b").asJson,
"env" -> Seq(EnvVar("a", "b")).asJson,
)

actual shouldBe expected
Expand All @@ -268,7 +268,7 @@ class JsonSerdeSpec() extends Matchers
"start_at" -> 0.asJson,
"alert_levels" -> alertLevels.asJson,
"info" -> "you".asJson,
"env" -> Seq("a" -> "b").asJson,
"env" -> Seq(EnvVar("a", "b")).asJson,
)

actual shouldBe expected
Expand All @@ -285,7 +285,7 @@ class JsonSerdeSpec() extends Matchers
"JobStatus" must {
val job = Job(1, "j", None, "p", "c", "yyyy-MM-dd",
Hourly, 1, ZoneId.of("UTC"), 2, 0, AlertLevels(3, 4, 5, 6),
None, Seq("a" -> "b")
None, Seq(EnvVar("a", "b"))
)
val periods = Seq(PeriodHealth("1", true), PeriodHealth("2", false))
val jobStatus = JobStatus(job, 100, periods)
Expand Down

0 comments on commit afca586

Please sign in to comment.