-
Notifications
You must be signed in to change notification settings - Fork 97
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
Showing
34 changed files
with
1,678 additions
and
144 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package skuber | ||
|
||
import akka.Done | ||
import akka.stream.scaladsl.{Sink, Source} | ||
import org.scalatest.{BeforeAndAfterAll, Matchers} | ||
import org.scalatest.concurrent.Eventually | ||
import skuber.json.format._ | ||
|
||
import scala.concurrent.duration._ | ||
import scala.concurrent.duration.Duration | ||
import scala.concurrent.{Await, Future, Promise} | ||
|
||
|
||
class ExecSpec extends K8SFixture with Eventually with Matchers with BeforeAndAfterAll { | ||
val nginxPodName: String = java.util.UUID.randomUUID().toString | ||
|
||
behavior of "Exec" | ||
|
||
override def beforeAll(): Unit = { | ||
super.beforeAll() | ||
|
||
val k8s = k8sInit | ||
Await.result(k8s.create(getNginxPod(nginxPodName, "1.7.9")), 3 second) | ||
// Let the pod running | ||
Thread.sleep(3000) | ||
k8s.close | ||
} | ||
|
||
override def afterAll(): Unit = { | ||
val k8s = k8sInit | ||
Await.result(k8s.delete[Pod](nginxPodName), 3 second) | ||
Thread.sleep(3000) | ||
k8s.close | ||
|
||
super.afterAll() | ||
} | ||
|
||
it should "execute a command in the running pod" in { k8s => | ||
var output = "" | ||
val stdout: Sink[String, Future[Done]] = Sink.foreach(output += _) | ||
var errorOutput = "" | ||
val stderr: Sink[String, Future[Done]] = Sink.foreach(errorOutput += _) | ||
k8s.exec(nginxPodName, Seq("whoami"), maybeStdout = Some(stdout), maybeStderr = Some(stderr), maybeClose = Some(closeAfter(1 second))).map { _ => | ||
assert(output == "root\n") | ||
assert(errorOutput == "") | ||
} | ||
} | ||
|
||
it should "execute a command in the specified container of the running pod" in { k8s => | ||
var output = "" | ||
val stdout: Sink[String, Future[Done]] = Sink.foreach(output += _) | ||
var errorOutput = "" | ||
val stderr: Sink[String, Future[Done]] = Sink.foreach(errorOutput += _) | ||
k8s.exec(nginxPodName, Seq("whoami"), maybeContainerName = Some("nginx"), | ||
maybeStdout = Some(stdout), maybeStderr = Some(stderr), maybeClose = Some(closeAfter(1 second))).map { _ => | ||
assert(output == "root\n") | ||
assert(errorOutput == "") | ||
} | ||
} | ||
|
||
it should "execute a command that outputs to stderr in the running pod" in { k8s => | ||
var output = "" | ||
val stdout: Sink[String, Future[Done]] = Sink.foreach(output += _) | ||
var errorOutput = "" | ||
val stderr: Sink[String, Future[Done]] = Sink.foreach(errorOutput += _) | ||
k8s.exec(nginxPodName, Seq("sh", "-c", "whoami >&2"), | ||
maybeStdout = Some(stdout), maybeStderr = Some(stderr), maybeClose = Some(closeAfter(1 second))).map { _ => | ||
assert(output == "") | ||
assert(errorOutput == "root\n") | ||
} | ||
} | ||
|
||
it should "execute a command in an interactive shell of the running pod" in { k8s => | ||
val stdin = Source.single("whoami\n") | ||
var output = "" | ||
val stdout: Sink[String, Future[Done]] = Sink.foreach(output += _) | ||
var errorOutput = "" | ||
val stderr: Sink[String, Future[Done]] = Sink.foreach(errorOutput += _) | ||
k8s.exec(nginxPodName, Seq("sh"), maybeStdin = Some(stdin), | ||
maybeStdout = Some(stdout), maybeStderr = Some(stderr), tty = true, maybeClose = Some(closeAfter(1 second))).map { _ => | ||
assert(output == "# whoami\r\nroot\r\n# ") | ||
assert(errorOutput == "") | ||
} | ||
} | ||
|
||
it should "throw an exception without stdin, stdout nor stderr in the running pod" in { k8s => | ||
k8s.exec(nginxPodName, Seq("whoami")).failed.map { | ||
case e: K8SException => | ||
assert(e.status.code == Some(400)) | ||
} | ||
} | ||
|
||
it should "throw an exception against an unexisting pod" in { k8s => | ||
k8s.exec(nginxPodName + "x", Seq("whoami")).failed.map { | ||
case e: K8SException => | ||
assert(e.status.code == Some(404)) | ||
} | ||
} | ||
|
||
def closeAfter(duration: Duration) = { | ||
val promise = Promise[Unit]() | ||
Future { | ||
Thread.sleep(duration.toMillis) | ||
promise.success(()) | ||
} | ||
promise | ||
} | ||
|
||
def getNginxContainer(version: String): Container = Container(name = "nginx", image = "nginx:" + version).exposePort(80) | ||
|
||
def getNginxPod(name: String, version: String): Pod = { | ||
val nginxContainer = getNginxContainer(version) | ||
val nginxPodSpec = Pod.Spec(containers = List((nginxContainer))) | ||
Pod.named(nginxPodName).copy(spec = Some(nginxPodSpec)) | ||
} | ||
} |
101 changes: 101 additions & 0 deletions
101
client/src/it/scala/skuber/HorizontalPodAutoscalerV2Beta1Spec.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,101 @@ | ||
package skuber | ||
|
||
import org.scalatest.Matchers | ||
import org.scalatest.concurrent.Eventually | ||
import skuber.apps.v1.Deployment | ||
import skuber.autoscaling.v2beta1.HorizontalPodAutoscaler | ||
import skuber.autoscaling.v2beta1.HorizontalPodAutoscaler.ResourceMetricSource | ||
|
||
class HorizontalPodAutoscalerV2Beta1Spec extends K8SFixture with Eventually with Matchers { | ||
behavior of "HorizontalPodAutoscalerV2Beta1" | ||
|
||
it should "create a HorizontalPodAutoscaler" in { k8s => | ||
val name: String = java.util.UUID.randomUUID().toString | ||
println(name) | ||
k8s.create(getNginxDeployment(name, "1.7.9")) flatMap { d => | ||
k8s.create( | ||
HorizontalPodAutoscaler(name).withSpec( | ||
HorizontalPodAutoscaler.Spec("v1", "Deployment", "nginx") | ||
.withMinReplicas(1) | ||
.withMaxReplicas(2) | ||
.addResourceMetric(ResourceMetricSource(Resource.cpu, Some(80), None)) | ||
) | ||
).map { result => | ||
assert(result.name == name) | ||
assert(result.spec.contains( | ||
HorizontalPodAutoscaler.Spec("v1", "Deployment", "nginx") | ||
.withMinReplicas(1) | ||
.withMaxReplicas(2) | ||
.addResourceMetric(ResourceMetricSource(Resource.cpu, Some(80), None))) | ||
) | ||
} | ||
} | ||
} | ||
|
||
it should "update a HorizontalPodAutoscaler" in { k8s => | ||
val name: String = java.util.UUID.randomUUID().toString | ||
k8s.create(getNginxDeployment(name, "1.7.9")) flatMap { d => | ||
k8s.create( | ||
HorizontalPodAutoscaler(name).withSpec( | ||
HorizontalPodAutoscaler.Spec("v1", "Deployment", "nginx") | ||
.withMinReplicas(1) | ||
.withMaxReplicas(2) | ||
.addResourceMetric(ResourceMetricSource(Resource.cpu, Some(80), None)) | ||
) | ||
).flatMap(created => | ||
eventually( | ||
k8s.get[HorizontalPodAutoscaler](created.name).flatMap { existing => | ||
val udpated = existing.withSpec(HorizontalPodAutoscaler.Spec("v1", "Deployment", "nginx") | ||
.withMinReplicas(1) | ||
.withMaxReplicas(3) | ||
.addResourceMetric(ResourceMetricSource(Resource.cpu, Some(80), None))) | ||
|
||
k8s.update(udpated).map { result => | ||
assert(result.name == name) | ||
assert(result.spec.contains( | ||
HorizontalPodAutoscaler.Spec("v1", "Deployment", "nginx") | ||
.withMinReplicas(1) | ||
.withMaxReplicas(3) | ||
.addResourceMetric(ResourceMetricSource(Resource.cpu, Some(80), None)) | ||
)) | ||
} | ||
} | ||
) | ||
) | ||
} | ||
} | ||
|
||
it should "delete a HorizontalPodAutoscaler" in { k8s => | ||
val name: String = java.util.UUID.randomUUID().toString | ||
k8s.create(getNginxDeployment(name, "1.7.9")) flatMap { d => | ||
k8s.create( | ||
HorizontalPodAutoscaler(name).withSpec( | ||
HorizontalPodAutoscaler.Spec("v1", "Deployment", "nginx") | ||
.withMinReplicas(1) | ||
.withMaxReplicas(2) | ||
.addResourceMetric(ResourceMetricSource(Resource.cpu, Some(80), None)) | ||
) | ||
).flatMap { created => | ||
k8s.delete[HorizontalPodAutoscaler](created.name).flatMap { deleteResult => | ||
k8s.get[HorizontalPodAutoscaler](created.name).map { x => | ||
assert(false) | ||
} recoverWith { | ||
case ex: K8SException if ex.status.code.contains(404) => assert(true) | ||
case _ => assert(false) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
def getNginxDeployment(name: String, version: String): Deployment = { | ||
import LabelSelector.dsl._ | ||
val nginxContainer = getNginxContainer(version) | ||
val nginxTemplate = Pod.Template.Spec.named("nginx").addContainer(nginxContainer).addLabel("app" -> "nginx") | ||
Deployment(name).withTemplate(nginxTemplate).withLabelSelector("app" is "nginx") | ||
} | ||
|
||
def getNginxContainer(version: String): Container = { | ||
Container(name = "nginx", image = "nginx:" + version).exposePort(80) | ||
} | ||
} |
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
Oops, something went wrong.