Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
functional: introduce a test TestMetadataOperator
Browse files Browse the repository at this point in the history
TestMetadataOperator ensures that metadata operators work also for
extended operators such as ">=", "<=", "<", ">", "!=", or "==".
First make the test machine have "ram=1024" in its machine metadata.
Then in TestMetadataOperator, check each possible operator one after
another, to make sure that each works without error.
  • Loading branch information
Dongsu Park committed Jul 13, 2016
1 parent 05eba5b commit cde04ef
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
5 changes: 5 additions & 0 deletions functional/fixtures/units/metadata-op.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[Service]
ExecStart=/bin/bash -c "while true; do echo Hello, World %i!; sleep 1; done"

[X-Fleet]
MachineMetadata="ram>=1024"
66 changes: 66 additions & 0 deletions functional/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package functional

import (
"fmt"
"path"
"regexp"
"strings"
"testing"
Expand Down Expand Up @@ -90,3 +91,68 @@ func TestTemplatesWithSpecifiersInMetadata(t *testing.T) {
t.Fatalf("metadata@invalid unit should not be scheduled: \nstdout: %s\nstderr: %s", stdout, stderr)
}
}

// TestMetadataOperator ensures that metadata operators work also for
// extended operators such as ">=", "<=", "<", ">", "!=", or "==".
func TestMetadataOperator(t *testing.T) {
cluster, err := platform.NewNspawnCluster("smoke")
if err != nil {
t.Fatal(err)
}
defer cluster.Destroy(t)

members, err := platform.CreateNClusterMembers(cluster, 1)
if err != nil {
t.Fatal(err)
}
m0 := members[0]
_, err = cluster.WaitForNMachines(m0, 1)
if err != nil {
t.Fatal(err)
}

stdout, stderr, err := cluster.Fleetctl(m0, "list-machines", "--fields", "machine,metadata")
if err != nil {
t.Fatalf("Unable to get machine metadata\nstdout: %s\nstderr: %s\nerr: %v", stdout, stderr, err)
}

runMetaOp := func(ramEq string) {
tmpMdOpService := "/tmp/metadata-op.service"
MdOpService := "fixtures/units/metadata-op.service"
MdOpBaseName := path.Base(MdOpService)

stdout, stderr, err = cluster.Fleetctl(m0, "start", MdOpService)
if err != nil {
t.Fatalf("unit %s with metadata operators cannot be started: \nstdout: %s\nstderr: %s\nerr: %v",
MdOpService, stdout, stderr, err)
}

_, err = cluster.WaitForNActiveUnits(m0, 1)
if err != nil {
t.Fatal(err)
}

stdout, stderr, err = cluster.Fleetctl(m0, "stop", MdOpBaseName)
if err != nil {
t.Fatalf("unit %s cannot be stopped: \nstdout: %s\nstderr: %s\nerr: %v",
MdOpBaseName, stdout, stderr, err)
}

_, err = cluster.WaitForNActiveUnits(m0, 0)
if err != nil {
t.Fatal(err)
}

err = util.GenNewFleetService(tmpMdOpService, MdOpService, ramEq, "ram>=1024")
if err != nil {
t.Fatalf("Failed to generate a temp fleet service: %v", err)
}
}

runMetaOp("ram>=1024")
runMetaOp("ram<=1024")
runMetaOp("ram>1023")
runMetaOp("ram<1025")
runMetaOp("ram!=1025")
runMetaOp("ram==1024")
}
5 changes: 4 additions & 1 deletion functional/util/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

const (
fleetAPIPort = 54728
fleetRAM = 1024 // in MiB
FleetTTL = "3s"
cloudConfig = `#cloud-config
Expand Down Expand Up @@ -62,7 +63,7 @@ coreos:
command: start
content: |
[Service]
Environment=FLEET_METADATA=hostname=%H
Environment=FLEET_METADATA=hostname=%H,ram={{printf "%d" .FleetRAM}}
ExecStart=/opt/fleet/fleetd -config /opt/fleet/fleet.conf
`
)
Expand All @@ -80,6 +81,7 @@ type configValues struct {
EtcdEndpoint string
EtcdKeyPrefix string
FleetAPIPort int
FleetRAM int
FleetAgentTTL string
FleetExtra string
}
Expand Down Expand Up @@ -118,6 +120,7 @@ func BuildCloudConfig(dst io.Writer, ip, etcdEndpoint, etcdKeyPrefix string) err
EtcdEndpoint: etcdEndpoint,
EtcdKeyPrefix: etcdKeyPrefix,
FleetAPIPort: fleetAPIPort,
FleetRAM: fleetRAM,
FleetAgentTTL: FleetTTL,
FleetExtra: os.Getenv("FLEETD_TEST_ENV"),
}
Expand Down

0 comments on commit cde04ef

Please sign in to comment.