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

Commit

Permalink
tests: added template + metadata functional test
Browse files Browse the repository at this point in the history
Covers #1446
  • Loading branch information
kayrus committed Mar 21, 2016
1 parent d98fc61 commit f7ea93b
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
5 changes: 5 additions & 0 deletions functional/fixtures/units/metadata@.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=hostname=%i
101 changes: 101 additions & 0 deletions functional/metadata_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright 2016 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package functional

import (
"fmt"
"regexp"
"strings"
"testing"

"github.com/coreos/fleet/functional/platform"
"github.com/coreos/fleet/functional/util"
)

// Start three machines and test template units based on machines Metadata
func TestTemplatesWithSpecifiersInMetadata(t *testing.T) {
cluster, err := platform.NewNspawnCluster("smoke")
if err != nil {
t.Fatal(err)
}
defer cluster.Destroy()

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

// Submit one template
if stdout, stderr, err := cluster.Fleetctl(m0, "submit", "fixtures/units/metadata@.service"); err != nil {
t.Fatalf("Unable to submit metadata@.service template: \nstdout: %s\nstderr: %s\nerr: %v", stdout, stderr, err)
}

// Combine all parameters and units in one args slice
args := []string{
"start",
}
for i := len(members)-1; i >= 0; i-- {
args = append(args, fmt.Sprintf("fixtures/units/metadata@smoke%s.service", members[i].ID()))
}

// Launch three template units, each of them on separate machine
if stdout, stderr, err := cluster.Fleetctl(m0, args...); err != nil {
t.Fatalf("Unable to start template based units: \nstdout: %s\nstderr: %s\nerr: %v", stdout, stderr, err)
}

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

if stdout, stderr, err := cluster.Fleetctl(m0, "start", "--block-attempts=20", "fixtures/units/metadata@invalid.service"); err == nil {
t.Fatalf("metadata@invalid unit should not be scheduled: \nstdout: %s\nstderr: %s\nerr: %v", stdout, stderr, err)
}

ndesired := 3
if stdout, stderr, err := cluster.Fleetctl(m0, "list-units", "--no-legend", "--full", "--fields", "unit,active,machine"); err != nil {
t.Fatalf("Unable to get submited units: \nstdout: %s\nstderr: %s\nerr: %v", stdout, stderr, err)
} else {
stdout = strings.TrimSpace(stdout)
if err != nil {
t.Fatalf("Failed to parse stdout: \nstdout: %s\nstderr: %s\nerr: %v", stdout, stderr, err)
}

lines := strings.Split(stdout, "\n")
allStates := util.ParseUnitStates(lines)
active := util.FilterActiveUnits(allStates)
nactive := len(active)
if nactive != ndesired {
t.Fatalf("Failed to get %d active units: \nstdout: %s\nstderr: %s", ndesired, stdout, stderr)
}

for _, state := range active {
re := regexp.MustCompile(`@([^.]*)`)
desiredMachine := re.FindStringSubmatch(state.Name)
if len(desiredMachine) < 2 {
t.Fatalf("Cannot parse state.Name (%v): \nstdout: %s\nstderr: %s", state.Name, stdout, stderr)
}
currentMachine := fmt.Sprintf("smoke%s", state.Machine)
if desiredMachine[1] != currentMachine {
t.Fatalf("Template (%s) has been scheduled on wrong machine (%s): \nstdout: %s\nstderr: %s", state.Name, currentMachine, stdout, stderr)
}
}
}
}
1 change: 1 addition & 0 deletions functional/util/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ coreos:
command: start
content: |
[Service]
Environment=FLEET_METADATA=hostname=%H
ExecStart=/opt/fleet/fleetd -config /opt/fleet/fleet.conf
`
)
Expand Down

0 comments on commit f7ea93b

Please sign in to comment.