Skip to content

Commit

Permalink
Convert integration tests to use ginkgo
Browse files Browse the repository at this point in the history
Signed-off-by: Maria Shaldibina <[email protected]>
  • Loading branch information
cobyrne-pivot authored and mariash committed Jan 8, 2015
1 parent d274463 commit 78d492c
Show file tree
Hide file tree
Showing 11 changed files with 232 additions and 75 deletions.
8 changes: 7 additions & 1 deletion Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,15 @@ Vagrant.configure('2') do |config|
}
end

config.vm.synced_folder '.', '/home/vagrant/go/src/github.com/cloudfoundry/bosh-agent'
agent_dir = '/home/vagrant/go/src/github.com/cloudfoundry/bosh-agent'

config.vm.synced_folder '.', agent_dir

# config.vm.synced_folder Dir.pwd, '/vagrant', disabled: true
config.vm.provision :shell, inline: "mkdir -p /vagrant && chmod 777 /vagrant"
config.vm.provision :shell, inline: "chmod 777 /var/vcap/sys/log/cpi"

config.vm.provision :shell, inline: "sudo #{agent_dir}/integration/assets/install-go.sh"
config.vm.provision :shell, inline: "sudo #{agent_dir}/integration/assets/install-agent.sh"
config.vm.provision :shell, inline: "sudo #{agent_dir}/integration/assets/install-fake-registry.sh"
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -ex

echo "Running integration/bin/run-test-with-vm-in-container.sh"
echo "Running bin/run-gocd-integration-test"
echo "ENV:"
echo `env`

Expand Down Expand Up @@ -43,7 +43,7 @@ docker run \
-v $SRC_DIR:$BOSH_AGENT_DIR \
-v $PRIVATE_KEY_DIR:$BOSH_MICRO_PRIVATE_KEY_DIR \
$DOCKER_IMAGE \
$BOSH_AGENT_DIR/integration/bin/test --provider=aws \
$BOSH_AGENT_DIR/bin/test-integration --provider=aws \
&

SUBPROC="$!"
Expand Down
20 changes: 9 additions & 11 deletions integration/bin/test → bin/test-integration
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/bin/bash
#!/usr/bin/env bash

set -e -x

base=$( cd $(dirname $0)/../.. && pwd )
bin=$(dirname $0)

base=$( cd ${bin}/.. && pwd )
if [ ! -d $base/tmp ]; then
mkdir -p $base/tmp
fi
Expand Down Expand Up @@ -33,20 +35,16 @@ fi

cd $base
echo -e "\n Running agent integration tests..."
$bin/env go clean -r github.com/cloudfoundry/bosh-agent/
vagrant up $@

vagrant ssh-config > $base/tmp/vagrant-config

srcdir="/home/vagrant/go/src/github.com/cloudfoundry/bosh-agent"
script="$srcdir/integration/assets/configure-environment.sh"
ssh -F $base/tmp/vagrant-config default "chmod +x $script && sh -c $script"
echo -e "\n Installing ginkgo..."
$bin/go install github.com/onsi/ginkgo/ginkgo

# check that agent id has been set as expected
result=`vagrant ssh -c "grep -c the_agent_id /var/vcap/bosh/settings.json" | head -c 1`
if [ "$result" != "1" ]; then
echo "failure: agent_id not set as expected"
exit 1
fi
echo -e "\n Running tests..."
$bin/env ginkgo -race -trace integration

# check that agent unmounted disk
result=`vagrant ssh -c "sudo mount | grep -c /dev/loop2" | head -c 1`
Expand Down
2 changes: 1 addition & 1 deletion bin/test-unit
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ $bin/go install github.com/onsi/ginkgo/ginkgo
let "result+=$?"

echo -e "\n Testing packages..."
$bin/env ginkgo -r $race $bin/..
$bin/env ginkgo -r $race -skipPackage integration $bin/..
let "result+=$?"

echo -e "\n Running build script to confirm everything compiles..."
Expand Down
File renamed without changes.
57 changes: 0 additions & 57 deletions integration/assets/configure-environment.sh

This file was deleted.

30 changes: 30 additions & 0 deletions integration/assets/install-agent.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env bash

set -ex

GOPATH=/home/vagrant/go
export GOROOT=/usr/local/go
export PATH=$GOROOT/bin:$PATH

echo "Installing bosh-agent..."
mkdir -p $(dirname $GOROOT)
chmod -R a+w $GOROOT

if [ ! -d $TMPDIR ]; then
mkdir -p $TMPDIR
fi

agent_dir=$GOPATH/src/github.com/cloudfoundry/bosh-agent

pushd $agent_dir
sudo sv stop agent

# build agent
bin/build

# install new agent
sudo cp out/bosh-agent /var/vcap/bosh/bin/bosh-agent

# start agent
sudo sv start agent
popd
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#!/bin/bash
#!/usr/bin/env bash

set -e
set -ex

GOPATH=/home/vagrant/go
export GOROOT=/usr/local/go
export PATH=$GOROOT/bin:$PATH

base=$( cd $(dirname $0)/../.. && pwd )
bin=$base/bin
Expand All @@ -14,4 +17,4 @@ then
exit 1
fi

$bin/go build -o $base/tmp/fake-registry github.com/cloudfoundry/bosh-agent/integration/fake-registry
$bin/go build -o $base/tmp/fake-registry github.com/cloudfoundry/bosh-agent/integration/fake-registry
55 changes: 55 additions & 0 deletions integration/config_drive_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package integration_test

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

boshlog "github.com/cloudfoundry/bosh-agent/logger"
boshsys "github.com/cloudfoundry/bosh-agent/system"

. "github.com/cloudfoundry/bosh-agent/integration"
)

var _ = Describe("ConfigDrive", func() {
var (
testEnvironment TestEnvironment
)

BeforeEach(func() {
logger := boshlog.NewLogger(boshlog.LevelDebug)
cmdRunner := boshsys.NewExecCmdRunner(logger)
testEnvironment = NewTestEnvironment(cmdRunner)
})

Context("when infrastructure is openstack", func() {
BeforeEach(func() {
err := testEnvironment.SetInfrastructure("openstack")
Expect(err).ToNot(HaveOccurred())
})

Context("when vm is using config drive", func() {
BeforeEach(func() {
err := testEnvironment.SetupConfigDrive()
Expect(err).ToNot(HaveOccurred())

err = testEnvironment.RemoveAgentSettings()
Expect(err).ToNot(HaveOccurred())

err = testEnvironment.StartRegistry(`"{\"agent_id\":\"fake-agent-id\"}"`)
Expect(err).ToNot(HaveOccurred())

err = testEnvironment.UpdateAgentConfig("config-drive-agent.json")
Expect(err).ToNot(HaveOccurred())

err = testEnvironment.RestartAgent()
Expect(err).ToNot(HaveOccurred())
})

It("using config drive to get registry URL", func() {
settingsJSON, err := testEnvironment.GetFileContents("/var/vcap/bosh/settings.json")
Expect(err).ToNot(HaveOccurred())
Expect(settingsJSON).To(ContainSubstring("fake-agent-id"))
})
})
})
})
13 changes: 13 additions & 0 deletions integration/integration_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package integration_test

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"testing"
)

func TestIntegration(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Integration Suite")
}
109 changes: 109 additions & 0 deletions integration/test_environment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package integration

import (
"fmt"

boshsys "github.com/cloudfoundry/bosh-agent/system"
)

type TestEnvironment struct {
cmdRunner boshsys.CmdRunner
}

func NewTestEnvironment(
cmdRunner boshsys.CmdRunner,
) TestEnvironment {
return TestEnvironment{
cmdRunner: cmdRunner,
}
}

func (t TestEnvironment) SetInfrastructure(name string) error {
_, err := t.RunCommand(fmt.Sprintf("echo '%s' | sudo tee /var/vcap/bosh/etc/infrastructure", name))
return err
}

func (t TestEnvironment) SetupConfigDrive() error {
setupConfigDriveTemplate := `
export GOPATH=/home/vagrant/go
export GOROOT=/usr/local/go
export PATH=$GOROOT/bin:$PATH
sudo dd if=/dev/zero of=/virtualfs bs=1024 count=1024
sudo losetup /dev/loop2 /virtualfs
sudo mkfs -t ext3 -m 1 -v /dev/loop2
sudo e2label /dev/loop2 config-2
sudo mkdir /tmp/config-drive
sudo mount /dev/disk/by-label/config-2 /tmp/config-drive
sudo chown vagrant:vagrant /tmp/config-drive
sudo mkdir -p /tmp/config-drive/ec2/latest
sudo cp %s/meta-data.json /tmp/config-drive/ec2/latest/meta-data.json
sudo cp %s/user-data /tmp/config-drive/ec2/latest
sudo umount /tmp/config-drive
`
setupConfigDriveScript := fmt.Sprintf(setupConfigDriveTemplate, t.assetsDir(), t.assetsDir())

_, err := t.RunCommand(setupConfigDriveScript)
return err
}

func (t TestEnvironment) RemoveAgentSettings() error {
_, err := t.RunCommand("sudo rm -f /var/vcap/bosh/settings.json")
return err
}

func (t TestEnvironment) UpdateAgentConfig(configFile string) error {
_, err := t.RunCommand(
fmt.Sprintf(
"sudo cp %s/%s /var/vcap/bosh/agent.json",
t.assetsDir(),
configFile,
),
)
return err
}

func (t TestEnvironment) RestartAgent() error {
_, err := t.RunCommand("sudo sv stop agent && sudo sv start agent")
return err
}

func (t TestEnvironment) StartRegistry(settings string) error {

_, err := t.RunCommand(
fmt.Sprintf(
`nohup %s/tmp/fake-registry -user user -password pass -host localhost -port 9090 -instance instance-id -settings "%s" &> /dev/null &`,
t.agentDir(),
settings,
),
)
return err
}

func (t TestEnvironment) GetFileContents(filePath string) (string, error) {
return t.RunCommand(
fmt.Sprintf(
`cat %s`,
filePath,
),
)
}

func (t TestEnvironment) RunCommand(command string) (string, error) {
stdout, _, _, err := t.cmdRunner.RunCommand(
"vagrant",
"ssh",
"-c",
command,
)

return stdout, err
}

func (t TestEnvironment) agentDir() string {
return "/home/vagrant/go/src/github.com/cloudfoundry/bosh-agent"
}

func (t TestEnvironment) assetsDir() string {
return fmt.Sprintf("%s/integration/assets", t.agentDir())
}

0 comments on commit 78d492c

Please sign in to comment.