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

Commit

Permalink
tests: added "fleetctl --tunnel" tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kayrus committed Mar 17, 2016
1 parent 2e03edd commit ece468e
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 2 deletions.
2 changes: 2 additions & 0 deletions functional/fixtures/units/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[Service]
ExecStart=/bin/bash -c "while true; do echo Hello, World %i!; sleep 1; done"
2 changes: 2 additions & 0 deletions functional/platform/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ type Cluster interface {
// client operations
Fleetctl(m Member, args ...string) (string, string, error)
FleetctlWithInput(m Member, input string, args ...string) (string, string, error)
FleetctlTunnel(m Member, args ...string) (string, string, error)
FleetctlTunnelWithInput(m Member, input string, args ...string) (string, string, error)
WaitForNActiveUnits(Member, int) (map[string][]util.UnitState, error)
WaitForNMachines(Member, int) ([]string, error)
}
Expand Down
51 changes: 49 additions & 2 deletions functional/platform/nspawn.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,25 @@ const (
)

var fleetdBinPath string
var fleetctlBinPath string

func init() {
fleetdBinPath = os.Getenv("FLEETD_BIN")
fleetctlBinPath = os.Getenv("FLEETCTL_BIN")
if fleetdBinPath == "" {
fmt.Println("FLEETD_BIN environment variable must be set")
os.Exit(1)
} else if _, err := os.Stat(fleetdBinPath); err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}
if fleetctlBinPath == "" {
fmt.Println("FLEETCTL_BIN environment variable must be set")
os.Exit(1)
} else if _, err := os.Stat(fleetctlBinPath); err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}
// sanity check etcd availability
cmd := exec.Command("etcdctl", "ls")
out, err := cmd.CombinedOutput()
Expand Down Expand Up @@ -104,6 +113,16 @@ func (nc *nspawnCluster) FleetctlWithInput(m Member, input string, args ...strin
return util.RunFleetctlWithInput(input, args...)
}

func (nc *nspawnCluster) FleetctlTunnel(m Member, args ...string) (string, string, error) {
args = append([]string{"--tunnel=" + m.IP()}, args...)
return util.RunFleetctl(args...)
}

func (nc *nspawnCluster) FleetctlTunnelWithInput(m Member, input string, args ...string) (string, string, error) {
args = append([]string{"--tunnel=" + m.IP()}, args...)
return util.RunFleetctlWithInput(input, args...)
}

func (nc *nspawnCluster) WaitForNActiveUnits(m Member, count int) (map[string][]util.UnitState, error) {
var nactive int
states := make(map[string][]util.UnitState)
Expand Down Expand Up @@ -238,6 +257,16 @@ func (nc *nspawnCluster) insertFleetd(dir string) error {
return copyFile(fleetdBinPath, fleetdBinDst, 0755)
}

func (nc *nspawnCluster) insertFleetctl(dir string) error {
cmd := fmt.Sprintf("mkdir -p %s/opt/fleet", dir)
if _, _, err := run(cmd); err != nil {
return err
}

fleetctlBinDst := path.Join(dir, "opt", "fleet", "fleetctl")
return copyFile(fleetctlBinPath, fleetctlBinDst, 0755)
}

func (nc *nspawnCluster) buildConfigDrive(dir, ip string) error {
latest := path.Join(dir, "media/configdrive/openstack/latest")
userPath := path.Join(latest, "user_data")
Expand Down Expand Up @@ -303,8 +332,6 @@ func (nc *nspawnCluster) createMember(id string) (m Member, err error) {
// minimum requirements for running systemd/coreos in a container
fmt.Sprintf("mkdir -p %s/usr", fsdir),
fmt.Sprintf("cp /etc/os-release %s/etc", fsdir),
fmt.Sprintf("echo 'core:x:500:500:CoreOS Admin:/home/core:/bin/bash' > %s/etc/passwd", fsdir),
fmt.Sprintf("echo 'core:x:500:' > %s/etc/group", fsdir),
fmt.Sprintf("ln -s /proc/self/mounts %s/etc/mtab", fsdir),
fmt.Sprintf("ln -s usr/lib64 %s/lib64", fsdir),
fmt.Sprintf("ln -s lib64 %s/lib", fsdir),
Expand Down Expand Up @@ -354,11 +381,31 @@ UseDNS no
return
}

if err = ioutil.WriteFile(path.Join(fsdir, "/etc/passwd"), []byte("core:x:500:500:CoreOS Admin:/home/core:/bin/bash"), 0644); err != nil {
log.Printf("Failed writing /etc/passwd: %v", err)
return
}

if err = ioutil.WriteFile(path.Join(fsdir, "/etc/group"), []byte("core:x:500:"), 0644); err != nil {
log.Printf("Failed writing /etc/group: %v", err)
return
}

if err = ioutil.WriteFile(path.Join(fsdir, "/home/core/.bash_profile"), []byte("export PATH=/opt/fleet:$PATH"), 0644); err != nil {
log.Printf("Failed writing /home/core/.bash_profile: %v", err)
return
}

if err = nc.insertFleetd(fsdir); err != nil {
log.Printf("Failed preparing fleetd in filesystem: %v", err)
return
}

if err = nc.insertFleetctl(fsdir); err != nil {
log.Printf("Failed preparing fleetctl in filesystem: %v", err)
return
}

if err = nc.buildConfigDrive(fsdir, nm.IP()); err != nil {
log.Printf("Failed building config drive: %v", err)
return
Expand Down
68 changes: 68 additions & 0 deletions functional/tunnel_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2014 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"
"io/ioutil"
"os"
"syscall"
"strings"
"testing"

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

// Start three units using ssh tunnel
func TestTunnelScheduleBatchUnits(t *testing.T) {
cluster, err := platform.NewNspawnCluster("smoke")
if err != nil {
t.Fatal(err)
}
defer cluster.Destroy()

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)
}

tmp, err := ioutil.TempFile(os.TempDir(), "known-hosts")
if err != nil {
t.Fatal(err)
}
tmp.Close()
defer syscall.Unlink(tmp.Name())

khFile := tmp.Name()

// Launch one unit
if stdout, stderr, err := cluster.FleetctlTunnelWithInput(m0, "yes", "--strict-host-key-checking=true", fmt.Sprintf("--known-hosts-file=%s", khFile), "start", "fixtures/units/hello.service"); err != nil {
t.Fatalf("Unable to submit one unit using ssh tunnel: \nstdout: %s\nstderr: %s\nerr: %v", stdout, stderr, err)
} else if strings.Contains(stderr, "Error") {
t.Fatalf("Failed to correctly submit unit using ssh tunnel: \nstdout: %s\nstderr: %s\nerr: %v", stdout, stderr, err)
}

// Launch a batch of units
if stdout, stderr, err := cluster.FleetctlTunnel(m0, "--strict-host-key-checking=true", fmt.Sprintf("--known-hosts-file=%s", khFile), "start", "fixtures/units/[email protected]", "fixtures/units/[email protected]", "fixtures/units/[email protected]"); err != nil {
t.Fatalf("Unable to submit batch of units using ssh tunnel: \nstdout: %s\nstderr: %s\nerr: %v", stdout, stderr, err)
} else if strings.Contains(stderr, "Error") {
t.Fatalf("Failed to correctly submit batch of units using ssh tunnel: \nstdout: %s\nstderr: %s\nerr: %v", stdout, stderr, err)
}
}
3 changes: 3 additions & 0 deletions functional/util/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,12 @@ coreos:
Address={{.IP}}/16
- name: fleet.socket
command: start
- name: fleet-tcp.socket
command: start
content: |
[Socket]
ListenStream={{printf "%d" .FleetAPIPort}}
Service=fleet.service
- name: fleet.service
command: start
content: |
Expand Down

0 comments on commit ece468e

Please sign in to comment.