remotessh

package module
v0.0.0-...-8ea0086 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 30, 2017 License: Apache-2.0 Imports: 10 Imported by: 1

README

remotessh: Test your remote files through SSH with a simple library.

godoc is here

Use this library to do remote testing of vagrant nodes.

For example, this will select the "mynode" node and run "ls" on it.

    vagrant := &Vagrant{}
    vagrant.Setup(false, "", 3) // 3 node cluster, do not run `vagrant up`.
    out, err := vagrant.GetNode("mynode").RunCommandWithOutput("ls")
    if err != nil {
      // exit status != 0
      panic(err)
    }

    fmt.Println(out) // already a string

Similarly for a Baremetal node:

hosts := []HostInfo{
		{
			Name:        "self",
			SSHAddr:     "127.0.0.1",
			SSHPort:     "22",
			User:        "vagrant",
			PrivKeyFile: "/vagrant/testdata/insecure_private_key",
		},
		{
			Name:        "self1",
			SSHAddr:     "127.0.0.1",
			SSHPort:     "22",
			User:        "vagrant",
			PrivKeyFile: "/vagrant/testdata/insecure_private_key",
		},
	}
	bm := &Baremetal{}
	c.Assert(bm.Setup(hosts), IsNil)
    out, err := bm.GetNode("mynode").RunCommandWithOutput("ls")
    if err != nil {
      // exit status != 0
      panic(err)
    }

    fmt.Println(out) // already a string

If you want to walk nodes, you have a few options:

Sequentially:

    vagrant := &remotessh.Vagrant{}
    vagrant.Setup(false, "", 3)
    for _, node := range vagrant.GetNodes() {
      node.RunCommand("something")
    }

In Parallel:

    vagrant := &remotessh.Vagrant{}
    vagrant.Setup(false, "", 3)
    err := vagrant.IterateNodes(func (node remotessh.TestbedNode) error {
      return node.RunCommand("docker ps -aq | xargs docker rm")
    })

    if err != nil {
      // one or more nodes failed
      panic(err)
    }

Copyright 2014 Cisco Systems Inc. All rights reserved.

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.

Documentation

Overview

Package remotessh provides host connectivity in go for system/integration testing in a multi host environment. It supports two testbed environments viz. baremetal and vagrant

Use this library to do remote testing with baremetal or vagrant nodes.

For example, To setup a baremetal setup with a host node with ssh reachability '1.2.3.4' and port '22' for user 'foo', you can initialize the setup as:

	hosts := []HostInfo{
		{
      Name: "mynode",
		  SSHAddr: "1.2.3.4",
		  SSHPort: "22"
		  User: "foo",
		  PrivKey: "path/to/foo's/privkey/file",
      Env: []string{},
    },
	}

  tb := &Baremetal{}
	tb.Setup(hosts)

Or to auto connect to a vagrant based setup you can initialize the setup as:

tb := &Vagrant{}
tb.Setup(false, "", 3) // 3 node cluster, do not run `vagrant up`.

Once you have your favorite setup initialized, this will select the "mynode" node and run "ls" on it.

out, err := tb.GetNode("mynode").RunCommandWithOutput("ls")
if err != nil {
  // exit status != 0
  panic(err)
}

fmt.Println(out) // already a string

If you want to walk nodes, you have a few options:

Sequentially:

for _, node := range tb.GetNodes() {
  node.RunCommand("something")
}

In Parallel:

err := tb.IterateNodes(func (node remotessh.TestbedNode) error {
  return node.RunCommand("docker ps -aq | xargs docker rm")
})

if err != nil {
  // one or more nodes failed
  panic(err)
}

Copyright 2014 Cisco Systems Inc. All rights reserved.

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.

Index

Constants

This section is empty.

Variables

View Source
var MaxSSHRetries = 3

MaxSSHRetries is the number of times we'll retry SSH connection

View Source
var SSHRetryDelay = time.Second

SSHRetryDelay is the delay between SSH connection retries

Functions

This section is empty.

Types

type Baremetal

type Baremetal struct {
	// contains filtered or unexported fields
}

Baremetal implements a host based testbed

func (*Baremetal) GetNode

func (b *Baremetal) GetNode(name string) TestbedNode

GetNode obtains a node by name. The name is the name of the host provided at the time of testbed Setup.

func (*Baremetal) GetNodes

func (b *Baremetal) GetNodes() []TestbedNode

GetNodes returns the nodes in a baremetal setup, returned sequentially.

func (*Baremetal) IterateNodes

func (b *Baremetal) IterateNodes(fn func(TestbedNode) error) error

IterateNodes walks each host and executes the function supplied. On error, it waits for all hosts to complete before returning the error, if any.

func (*Baremetal) SSHExecAllNodes

func (b *Baremetal) SSHExecAllNodes(cmd string) error

SSHExecAllNodes will ssh into each host and run the specified command.

func (*Baremetal) Setup

func (b *Baremetal) Setup(args ...interface{}) error

Setup initializes a baremetal testbed.

func (*Baremetal) Teardown

func (b *Baremetal) Teardown()

Teardown cleans up a baremetal testbed.

type HostInfo

type HostInfo struct {
	Name        string
	SSHAddr     string
	SSHPort     string
	User        string
	PrivKeyFile string
	Env         []string
}

HostInfo contains host specific connectivity info for setting up testbed node

type SSHNode

type SSHNode struct {
	Name string
	// contains filtered or unexported fields
}

SSHNode implements a node with ssh connectivity in a testbed

func NewSSHNode

func NewSSHNode(name, user string, env []string, sshAddr, sshPort, privKeyFile string) (*SSHNode, error)

NewSSHNode intializes a ssh-client based node in a testbed

func (*SSHNode) Cleanup

func (n *SSHNode) Cleanup()

Cleanup does nothing

func (*SSHNode) GetName

func (n *SSHNode) GetName() string

GetName returns vagrant node's name

func (*SSHNode) RunCommand

func (n *SSHNode) RunCommand(cmd string) error

RunCommand runs a shell command in a vagrant node and returns it's exit status

func (*SSHNode) RunCommandBackground

func (n *SSHNode) RunCommandBackground(cmd string) error

RunCommandBackground runs a background command in a vagrant node.

func (*SSHNode) RunCommandWithOutput

func (n *SSHNode) RunCommandWithOutput(cmd string) (string, error)

RunCommandWithOutput runs a shell command in a vagrant node and returns it's exit status and output

type TestCommand

type TestCommand struct {
	ContivNodes int
	ContivEnv   []string
}

TestCommand is a command that is run on a test node

func (*TestCommand) Run

func (c *TestCommand) Run(cmd string, args ...string) error

Run runs a command and return it's exit status

func (*TestCommand) RunWithOutput

func (c *TestCommand) RunWithOutput(cmd string, args ...string) ([]byte, error)

RunWithOutput runs a command and return it's exit status and output

type Testbed

type Testbed interface {
	Setup(args ...interface{}) error
	Teardown()
	GetNodes() []TestbedNode
	GetNode(name string) TestbedNode
	IterateNodes(fn func(TestbedNode) error) error
}

Testbed is a collection of test nodes

type TestbedNode

type TestbedNode interface {
	RunCommand(cmd string) (err error)
	RunCommandWithOutput(cmd string) (output string, err error)
	RunCommandBackground(cmd string) (err error)
	GetName() string
}

TestbedNode is a node under test

type Vagrant

type Vagrant struct {
	// contains filtered or unexported fields
}

Vagrant implements a vagrant based testbed

func (*Vagrant) GetNode

func (v *Vagrant) GetNode(name string) TestbedNode

GetNode obtains a node by name. The name is the name of the VM provided at `config.vm.define` time in Vagrantfiles. It is *not* the hostname of the machine, which is `vagrant` for all VMs by default.

func (*Vagrant) GetNodes

func (v *Vagrant) GetNodes() []TestbedNode

GetNodes returns the nodes in a vagrant setup, returned sequentially.

func (*Vagrant) IterateNodes

func (v *Vagrant) IterateNodes(fn func(TestbedNode) error) error

IterateNodes walks each host and executes the function supplied. On error, it waits for all hosts to complete before returning the error, if any.

func (*Vagrant) SSHExecAllNodes

func (v *Vagrant) SSHExecAllNodes(cmd string) error

SSHExecAllNodes will ssh into each host and run the specified command.

func (*Vagrant) Setup

func (v *Vagrant) Setup(args ...interface{}) error

Setup initializes a vagrant testbed.

func (*Vagrant) Teardown

func (v *Vagrant) Teardown()

Teardown cleans up a vagrant testbed. It performs `vagrant destroy -f` to tear down the environment. While this method can be useful, the notion of VMs that clean up after themselves (with an appropriate Makefile to control vm availability) will be considerably faster than a method that uses this in a suite teardown.

type VagrantCommand

type VagrantCommand struct {
	ContivNodes int
	Env         []string
}

VagrantCommand is a command that is run on a vagrant node

func (*VagrantCommand) Run

func (c *VagrantCommand) Run(cmd string, args ...string) error

Run runs a command and return its exit status

func (*VagrantCommand) RunWithOutput

func (c *VagrantCommand) RunWithOutput(cmd string, args ...string) ([]byte, error)

RunWithOutput runs a command and return its exit status and output

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL