Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4e68bf9
commit 0c7f87c
Showing
5 changed files
with
96 additions
and
4 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
src/main/scala/org/apache/mesos/chronos/scheduler/jobs/constraints/UnlikeConstraint.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.apache.mesos.chronos.scheduler.jobs.constraints | ||
|
||
import java.util.logging.Logger | ||
|
||
import org.apache.mesos.Protos | ||
import scala.collection.JavaConversions._ | ||
|
||
case class UnlikeConstraint(attribute: String, value: String) extends Constraint { | ||
|
||
val regex = value.r | ||
|
||
private[this] val log = Logger.getLogger(getClass.getName) | ||
|
||
def matches(attributes: Seq[Protos.Attribute]): Boolean = { | ||
attributes.find(a => a.getName == attribute).exists { a => | ||
a.getType match { | ||
case Protos.Value.Type.SCALAR => | ||
!regex.pattern.matcher(a.getScalar.getValue.toString).matches() | ||
case Protos.Value.Type.SET => | ||
!a.getSet.getItemList.exists(regex.pattern.matcher(_).matches()) | ||
case Protos.Value.Type.TEXT => | ||
!regex.pattern.matcher(a.getText.getValue).matches() | ||
case Protos.Value.Type.RANGES => | ||
log.warning("Unlike constraint does not support attributes of type RANGES") | ||
false | ||
case _ => | ||
val t = a.getType.getNumber | ||
log.warning(s"Unknown constraint with number $t in Unlike constraint") | ||
false | ||
} | ||
} | ||
} | ||
} | ||
|
||
object UnlikeConstraint { | ||
val OPERATOR = "UNLIKE" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...test/scala/org/apache/mesos/chronos/scheduler/jobs/constraints/UnlikeConstraintSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.apache.mesos.chronos.scheduler.jobs.constraints | ||
|
||
import java.util.regex.PatternSyntaxException | ||
|
||
import org.specs2.mutable.SpecificationWithJUnit | ||
|
||
class UnlikeConstraintSpec extends SpecificationWithJUnit | ||
with ConstraintSpecHelper { | ||
"matches attributes of type text" in { | ||
val attributes = List(createTextAttribute("dc", "north"), createTextAttribute("rack", "rack-4")) | ||
val constraint = UnlikeConstraint("rack", "rack-[1-3]") | ||
|
||
constraint.matches(attributes) must_== true | ||
|
||
val attributes2 = List(createTextAttribute("dc", "north")) | ||
val constraint2 = UnlikeConstraint("dc", "north|south") | ||
|
||
constraint2.matches(attributes2) must_== false | ||
} | ||
|
||
"matches attributes of type scalar" in { | ||
val attributes = List(createScalarAttribute("number", 1)) | ||
val constraint = UnlikeConstraint("number", """\d\.\d""") | ||
|
||
constraint.matches(attributes) must_== false | ||
|
||
val attributes2 = List(createScalarAttribute("number", 1)) | ||
val constraint2 = UnlikeConstraint("number", """100.\d""") | ||
constraint2.matches(attributes) must_== true | ||
|
||
} | ||
|
||
"matches attributes of type set" in { | ||
val attributes = List(createSetAttribute("dc", Array("north"))) | ||
val constraint = UnlikeConstraint("dc", "^n.*") | ||
|
||
constraint.matches(attributes) must_== false | ||
|
||
val attributes2 = List(createSetAttribute("dc", Array("south"))) | ||
|
||
constraint.matches(attributes2) must_== true | ||
} | ||
|
||
"fails in case of an invalid regular expression" in { | ||
UnlikeConstraint("invalid-regex", "[[[") must throwA[PatternSyntaxException] | ||
} | ||
} |