mirror of
https://github.com/kemko/nomad.git
synced 2026-01-06 18:35:44 +03:00
Added guards to tests so we can more gracefully degrade the test suite when thing are not installed
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package driver
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -8,7 +9,12 @@ import (
|
||||
"github.com/hashicorp/nomad/nomad/structs"
|
||||
)
|
||||
|
||||
var dockerLocated bool = true
|
||||
// dockerLocated looks to see whether docker is available on this system before
|
||||
// we try to run tests. We'll keep it simple and just check for the CLI.
|
||||
func dockerLocated() bool {
|
||||
_, err := exec.Command("docker", "-v").CombinedOutput()
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func TestDockerDriver_Handle(t *testing.T) {
|
||||
h := &dockerHandle{
|
||||
@@ -25,6 +31,7 @@ func TestDockerDriver_Handle(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// The fingerprinter test should always pass, even if Docker is not installed.
|
||||
func TestDockerDriver_Fingerprint(t *testing.T) {
|
||||
d := NewDockerDriver(testDriverContext())
|
||||
node := &structs.Node{
|
||||
@@ -38,14 +45,13 @@ func TestDockerDriver_Fingerprint(t *testing.T) {
|
||||
t.Fatalf("should apply")
|
||||
}
|
||||
if node.Attributes["driver.docker"] == "" {
|
||||
dockerLocated = false
|
||||
t.Fatalf("Docker not found. The remainder of the docker tests will be skipped.")
|
||||
}
|
||||
t.Logf("Found docker version %s", node.Attributes["driver.docker.version"])
|
||||
}
|
||||
|
||||
func TestDockerDriver_StartOpen_Wait(t *testing.T) {
|
||||
if !dockerLocated {
|
||||
if !dockerLocated() {
|
||||
t.SkipNow()
|
||||
}
|
||||
ctx := NewExecContext()
|
||||
@@ -53,10 +59,10 @@ func TestDockerDriver_StartOpen_Wait(t *testing.T) {
|
||||
|
||||
task := &structs.Task{
|
||||
Config: map[string]string{
|
||||
"image": "cbednarski/python-demo",
|
||||
"image": "redis",
|
||||
},
|
||||
Resources: &structs.Resources{
|
||||
MemoryMB: 1024,
|
||||
MemoryMB: 256,
|
||||
CPU: 512,
|
||||
},
|
||||
}
|
||||
@@ -80,7 +86,7 @@ func TestDockerDriver_StartOpen_Wait(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDockerDriver_Start_Wait(t *testing.T) {
|
||||
if !dockerLocated {
|
||||
if !dockerLocated() {
|
||||
t.SkipNow()
|
||||
}
|
||||
ctx := NewExecContext()
|
||||
@@ -88,10 +94,10 @@ func TestDockerDriver_Start_Wait(t *testing.T) {
|
||||
|
||||
task := &structs.Task{
|
||||
Config: map[string]string{
|
||||
"image": "cbednarski/python-demo",
|
||||
"image": "redis",
|
||||
},
|
||||
Resources: &structs.Resources{
|
||||
MemoryMB: 1024,
|
||||
MemoryMB: 256,
|
||||
CPU: 512,
|
||||
},
|
||||
}
|
||||
@@ -121,7 +127,7 @@ func TestDockerDriver_Start_Wait(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDockerDriver_Start_Kill_Wait(t *testing.T) {
|
||||
if !dockerLocated {
|
||||
if !dockerLocated() {
|
||||
t.SkipNow()
|
||||
}
|
||||
ctx := NewExecContext()
|
||||
@@ -129,10 +135,10 @@ func TestDockerDriver_Start_Kill_Wait(t *testing.T) {
|
||||
|
||||
task := &structs.Task{
|
||||
Config: map[string]string{
|
||||
"image": "cbednarski/python-demo",
|
||||
"image": "redis",
|
||||
},
|
||||
Resources: &structs.Resources{
|
||||
MemoryMB: 1024,
|
||||
MemoryMB: 256,
|
||||
CPU: 512,
|
||||
},
|
||||
}
|
||||
@@ -162,3 +168,45 @@ func TestDockerDriver_Start_Kill_Wait(t *testing.T) {
|
||||
t.Fatalf("timeout")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDocker_StartTwo(t *testing.T) {
|
||||
task1 := &structs.Task{
|
||||
Config: map[string]string{
|
||||
"image": "redis",
|
||||
},
|
||||
Resources: &structs.Resources{
|
||||
MemoryMB: 256,
|
||||
CPU: 512,
|
||||
Networks: []*structs.NetworkResource{
|
||||
&structs.NetworkResource{
|
||||
IP: "127.0.0.1",
|
||||
ReservedPorts: []int{11114},
|
||||
DynamicPorts: []string{"REDIS"},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
task2 := &structs.Task{
|
||||
Config: map[string]string{
|
||||
"image": "redis",
|
||||
},
|
||||
Resources: &structs.Resources{
|
||||
MemoryMB: 256,
|
||||
CPU: 512,
|
||||
Networks: []*structs.NetworkResource{
|
||||
&structs.NetworkResource{
|
||||
IP: "127.0.0.1",
|
||||
ReservedPorts: []int{11115},
|
||||
DynamicPorts: []string{"REDIS"},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
ctx := NewExecContext()
|
||||
d := NewDockerDriver(testDriverContext())
|
||||
|
||||
d.Start(ctx, task1)
|
||||
d.Start(ctx, task2)
|
||||
}
|
||||
|
||||
@@ -58,33 +58,44 @@ func TestPopulateEnvironment(t *testing.T) {
|
||||
cpu := "NOMAD_CPU_LIMIT=1000"
|
||||
if !contains(env, cpu) {
|
||||
t.Errorf("%s is missing from env", cpu)
|
||||
t.Fail()
|
||||
}
|
||||
memory := "NOMAD_MEMORY_LIMIT=500"
|
||||
if !contains(env, memory) {
|
||||
t.Errorf("%s is missing from env", memory)
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
// Networking
|
||||
ip := "NOMAD_IP=1.2.3.4"
|
||||
if !contains(env, ip) {
|
||||
t.Errorf("%s is missing from env", ip)
|
||||
t.Fail()
|
||||
}
|
||||
labelport := "NOMAD_PORT_ADMIN=8080"
|
||||
if !contains(env, labelport) {
|
||||
t.Errorf("%s is missing from env", labelport)
|
||||
t.Fail()
|
||||
}
|
||||
numberport := "NOMAD_PORT_5000=12345"
|
||||
if !contains(env, numberport) {
|
||||
t.Errorf("%s is missing from env", numberport)
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
// Metas
|
||||
chocolate := "NOMAD_META_CHOCOLATE=cake"
|
||||
if !contains(env, chocolate) {
|
||||
t.Errorf("%s is missing from env", chocolate)
|
||||
t.Fail()
|
||||
}
|
||||
strawberry := "NOMAD_META_STRAWBERRY=icecream"
|
||||
if !contains(env, strawberry) {
|
||||
t.Errorf("%s is missing from env", strawberry)
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
if t.Failed() {
|
||||
t.Logf("env: %#v", env)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package driver
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -9,6 +10,13 @@ import (
|
||||
"github.com/hashicorp/nomad/nomad/structs"
|
||||
)
|
||||
|
||||
// javaLocated checks whether java is installed so we can run java stuff.
|
||||
func javaLocated() bool {
|
||||
_, err := exec.Command("java", "-version").CombinedOutput()
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// The fingerprinter test should always pass, even if Java is not installed.
|
||||
func TestJavaDriver_Fingerprint(t *testing.T) {
|
||||
d := NewJavaDriver(testDriverContext())
|
||||
node := &structs.Node{
|
||||
@@ -32,6 +40,10 @@ func TestJavaDriver_Fingerprint(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestJavaDriver_StartOpen_Wait(t *testing.T) {
|
||||
if !javaLocated() {
|
||||
t.Skip("Java not found; skipping")
|
||||
}
|
||||
|
||||
ctx := NewExecContext()
|
||||
ctx.AllocDir = os.TempDir()
|
||||
d := NewJavaDriver(testDriverContext())
|
||||
@@ -69,6 +81,10 @@ func TestJavaDriver_StartOpen_Wait(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestJavaDriver_Start_Wait(t *testing.T) {
|
||||
if !javaLocated() {
|
||||
t.Skip("Java not found; skipping")
|
||||
}
|
||||
|
||||
ctx := NewExecContext()
|
||||
ctx.AllocDir = os.TempDir()
|
||||
d := NewJavaDriver(testDriverContext())
|
||||
@@ -107,6 +123,10 @@ func TestJavaDriver_Start_Wait(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestJavaDriver_Start_Kill_Wait(t *testing.T) {
|
||||
if !javaLocated() {
|
||||
t.Skip("Java not found; skipping")
|
||||
}
|
||||
|
||||
ctx := NewExecContext()
|
||||
ctx.AllocDir = os.TempDir()
|
||||
d := NewJavaDriver(testDriverContext())
|
||||
|
||||
@@ -3,12 +3,21 @@ package driver
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/nomad/client/config"
|
||||
"github.com/hashicorp/nomad/nomad/structs"
|
||||
)
|
||||
|
||||
// qemuLocated looks to see whether qemu binaries are available on this system
|
||||
// before we try to run tests. We may need to tweak this for cross-OS support
|
||||
// but I think this should work on *nix at least.
|
||||
func qemuLocated() bool {
|
||||
_, err := exec.Command("qemu-x86_64", "-version").CombinedOutput()
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func TestQemuDriver_Handle(t *testing.T) {
|
||||
h := &qemuHandle{
|
||||
proc: &os.Process{Pid: 123},
|
||||
@@ -24,6 +33,7 @@ func TestQemuDriver_Handle(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// The fingerprinter test should always pass, even if QEMU is not installed.
|
||||
func TestQemuDriver_Fingerprint(t *testing.T) {
|
||||
d := NewQemuDriver(testDriverContext())
|
||||
node := &structs.Node{
|
||||
@@ -45,6 +55,10 @@ func TestQemuDriver_Fingerprint(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestQemuDriver_Start(t *testing.T) {
|
||||
if !qemuLocated() {
|
||||
t.Skip("QEMU not found; skipping")
|
||||
}
|
||||
|
||||
ctx := NewExecContext()
|
||||
ctx.AllocDir = os.TempDir()
|
||||
d := NewQemuDriver(testDriverContext())
|
||||
@@ -89,6 +103,10 @@ func TestQemuDriver_Start(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestQemuDriver_RequiresMemory(t *testing.T) {
|
||||
if !qemuLocated() {
|
||||
t.Skip("QEMU not found; skipping")
|
||||
}
|
||||
|
||||
ctx := NewExecContext()
|
||||
ctx.AllocDir = os.TempDir()
|
||||
d := NewQemuDriver(testDriverContext())
|
||||
|
||||
@@ -32,6 +32,10 @@ Nomad provides automatic and manual mapping schemes for Docker. You can use
|
||||
either or both schemes for a task. Nomad binds both tcp and udp protocols to
|
||||
ports used for Docker containers. This is not configurable.
|
||||
|
||||
Note: You are not required to map any ports, for example if your task is running
|
||||
a crawler or aggregator and does not provide a network service. Tasks without a
|
||||
port mapping will still be able to make outbound network connections.
|
||||
|
||||
#### Automatic Port Mapping
|
||||
|
||||
Typically when you create a Docker container you configure the service to start
|
||||
|
||||
Reference in New Issue
Block a user