Skip to content

Commit

Permalink
Correct tests after rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
fanf committed Feb 16, 2021
1 parent b377ebf commit ad00f1b
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,14 @@ object Doobie {
)

implicit val ReportRead: Read[Reports] = {
type R = (DateTime, RuleId, DirectiveRId, NodeId, Int, String, String, DateTime, String, String)
type R = (DateTime, RuleId, String, NodeId, Int, String, String, DateTime, String, String)
Read[R].map(
(t: R ) => Reports.factory(t._1,t._2,t._3,t._4,t._5,t._6,t._7,t._8,t._9,t._10))
(t: R ) => {
DirectiveRId.parse(t._3) match {
case Right(drid) => Reports.factory(t._1,t._2,drid,t._4,t._5,t._6,t._7,t._8,t._9,t._10)
case Left(err) => throw new IllegalArgumentException(s"Error when unserializing a report from ruddersysevents base: can not parse directive ID: ${t._3}: ${err}")
}
})
}
implicit val ReportWrite: Write[Reports] = {
type R = (DateTime, RuleId, DirectiveRId, NodeId, Int, String, String, DateTime, String, String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
package com.normation.rudder.repository.jdbc

import java.sql.Timestamp

import com.normation.inventory.domain.NodeId
import com.normation.rudder.domain.policies.RuleId
import com.normation.rudder.domain.reports._
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"common-root","audit","merged","false","common","1.0","true","Common"
"root-distributePolicy","audit","merged","false","distributePolicy","1.0","true","Distribute Policy"
"common-root","audit","merged","false","common","1.0","true","Common"
"inventory-all","audit","merged","false","inventory","1.0","true","Inventory"
"server-roles-directive","audit","merged","false","server-roles","1.0","true","Server Roles"
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"DENYBADCLOCKS":"true",
"MODIFIED_FILES_TTL":"30",
"NODEROLE":" classes: \n \"rudder-ldap\" expression => \"any\";\n \"policy_server\" expression => \"any\";\n \"rudder-inventory-ldap\" expression => \"any\";\n \"rudder-db\" expression => \"any\";\n \"rudder-jetty\" expression => \"any\";\n \"rudder-webapp\" expression => \"any\";\n \"rudder-inventory-endpoint\" expression => \"any\";\n \"root_server\" expression => \"any\";\n \"rudder-reports\" expression => \"any\";\n \"rudder-server-root\" expression => \"any\";",
"POLICY_SERVER_KEY":"MD5=081cf3aac62624ebbc83be7e23cb104d",
"RELAY_SYNC_METHOD":"classic",
"RELAY_SYNC_PROMISES":"false",
"RELAY_SYNC_SHAREDFILES":"false",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import doobie.implicits._
import cats.implicits._
import com.normation.rudder.db.DB
import com.normation.rudder.domain.policies.DirectiveRId
import net.liftweb.common._
import zio.interop.catz._


Expand All @@ -63,6 +64,14 @@ import zio.interop.catz._
@RunWith(classOf[JUnitRunner])
class ReportsTest extends DBCommon {

implicit class ForceOpen[A](box: Box[A]) {
def open = box match {
case Full(x) => x
case eb: EmptyBox => throw new IllegalArgumentException(s"Test failed, open an empty box: ${eb}")
}
}


//clean data base
def cleanTables() = {
transacRun(xa => sql"DELETE FROM ReportsExecution; DELETE FROM RudderSysEvents;".update.run.transact(xa))
Expand Down Expand Up @@ -120,24 +129,25 @@ class ReportsTest extends DBCommon {
}

"find the last reports for node0" in {
val result = repostsRepo.getExecutionReports(Set(AgentRunId(NodeId("n0"), run1)), Set()).openOrThrowException("Test failed with exception")
val result = repostsRepo.getExecutionReports(Set(AgentRunId(NodeId("n0"), run1)), Set()).open
result.values.flatten.toSeq must contain(exactly(reports("n0")(0)))
}

"find reports for node 0,1,2" in {
val runs = Set(("n0", run1), ("n1", run1), ("n2", run1) )
val result = repostsRepo.getExecutionReports(runs, Set() ).openOrThrowException("Test failed with exception")
val result = repostsRepo.getExecutionReports(runs, Set() ).open
result.values.flatten.toSeq must contain(exactly(reports("n0")++reports("n1").reverse.tail++reports("n2"):_*))
}

"not find report for none existing agent run id" in {
val runs = Set( ("n2", run2), ("n3", run1))
val result = repostsRepo.getExecutionReports(runs, Set() ).openOrThrowException("Test failed with exception")
val result = repostsRepo.getExecutionReports(runs, Set() ).open
result must beEmpty
}
}

"Finding execution" should {
// since #18093, we assume that all run are complete, so we ignore runs without an "end" control element
"Finding execution, ignoring non-ending runs" should {
val reports = (
Map[String, Seq[Reports]]() +
node("n0")(
Expand All @@ -155,10 +165,12 @@ class ReportsTest extends DBCommon {
//run2
, ("rudder", "run", 0, "start", "n1_run2", run2, "control", "Start execution")
, ("r1", "d1", 0, "c2", "cv2", run2, "result_success", "msg1")
//run3
, ("rudder", "run", 0, "end", "n1_run2", run2, "control", "End execution")
//run3 will be ignore: no end element
, ("rudder", "run", 0, "start", "n1_run3", run3, "control", "Start execution")
) +
node("n2")(
// run will be taken even without a start: only 'end' counts.
("rudder", "run", 0, "end", "n2_run1", run1, "control", "End execution")
, ("r1", "d1", 0, "c1", "cv1", run1, "result_success", "msg1")
, ("r1", "d1", 0, "c2", "cv2", run1, "result_success", "msg1")
Expand All @@ -176,18 +188,17 @@ class ReportsTest extends DBCommon {
* - test case where there is no StartRun/EndRun
*/
"get reports" in {
val res = repostsRepo.getReportsfromId(0, DateTime.now().plusDays(1)).openOrThrowException("Test failed")
val res = repostsRepo.getReportsfromId(0, DateTime.now().plusDays(1)).open
val expected = Seq(
AgentRun(AgentRunId(NodeId("n1"),run2),None,false, 116)
, AgentRun(AgentRunId(NodeId("n2"),run1),Some(NodeConfigId("n2_run1")),true, 119)
, AgentRun(AgentRunId(NodeId("n1"),run1),Some(NodeConfigId("n1_run1")),true, 110)
, AgentRun(AgentRunId(NodeId("n1"),run3),None,false, 118)
, AgentRun(AgentRunId(NodeId("n0"),run1),None,true, 109)
AgentRun(AgentRunId(NodeId("n0"),run1),None,true, 109)
, AgentRun(AgentRunId(NodeId("n1"),run1),Some(NodeConfigId("n1_run1")),true, 115)
, AgentRun(AgentRunId(NodeId("n1"),run2),Some(NodeConfigId("n1_run2")),true, 118)
, AgentRun(AgentRunId(NodeId("n2"),run1),Some(NodeConfigId("n2_run1")),true, 120)
)

val checkInsert = transacRun(xa => sql"""select id from ruddersysevents""".query[Long].to[Vector].transact(xa)).size

(checkInsert must beEqualTo(13)) and
(checkInsert must beEqualTo(14)) and
(res._1 must contain(exactly(expected:_*)))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ object JsonResponseObjects {
, rule.categoryId.value
, rule.shortDescription
, rule.longDescription
, rule.directiveIds.map(_.value).toList.sorted
, rule.directiveIds.map(_.id.value).toList.sorted
, rule.targets.toList.sortBy(_.target).map(t => JRRuleTarget(t))
, rule.isEnabledStatus
, rule.isSystem
Expand Down Expand Up @@ -432,7 +432,7 @@ object JsonQueryObjects {
val updateCategory = category.map(RuleCategoryId).getOrElse(rule.categoryId)
val updateShort = shortDescription.getOrElse(rule.shortDescription)
val updateLong = longDescription.getOrElse(rule.longDescription)
val updateDirectives = directives.getOrElse(rule.directiveIds)
val updateDirectives = directives.map(_.map(DirectiveRId(_))).getOrElse(rule.directiveIds)
val updateTargets = targets.map(t => Set[RuleTarget](RuleTarget.merge(t.map(_.toRuleTarget)))).getOrElse(rule.targets)
val updateEnabled = enabled.getOrElse(rule.isEnabledStatus)
val updateTags = tags.getOrElse(rule.tags)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import com.normation.rudder.rest.RestUtils._
import com.normation.rudder.services.user.PersonIdentService
import com.normation.rudder.web.services._
import com.normation.utils.DateFormaterService
import com.normation.utils.Utils.DateToIsoString
import net.liftweb.common._
import net.liftweb.http.rest.RestHelper
import net.liftweb.http.{JsonResponse, LiftResponse, Req, S}
Expand All @@ -54,6 +55,7 @@ import net.liftweb.util.Helpers.tryo
import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormat


class EventLogAPI (
repos: EventLogRepository
, restExtractor : RestExtractorService
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ class RuleApiService14 (
case None =>
// create from scratch - base rule is the same with default values
val category = restRule.category.getOrElse("rootRuleCategory")
val baseRule = Rule(ruleId, name, RuleCategoryId(category))
val baseRule = Rule(ruleId, None, name, RuleCategoryId(category))
// If enable is missing in parameter consider it to true
val defaultEnabled = restRule.enabled.getOrElse(true)

Expand Down
Loading

0 comments on commit ad00f1b

Please sign in to comment.