From 78d492c443853a3215e7d496defb5137d671563d Mon Sep 17 00:00:00 2001 From: Colin OByrne Date: Wed, 7 Jan 2015 17:45:22 -0800 Subject: [PATCH] Convert integration tests to use ginkgo Signed-off-by: Maria Shaldibina --- Vagrantfile | 8 +- .../run-gocd-integration-test | 4 +- integration/bin/test => bin/test-integration | 20 ++-- bin/test-unit | 2 +- .../{agent.json => config-drive-agent.json} | 0 integration/assets/configure-environment.sh | 57 --------- integration/assets/install-agent.sh | 30 +++++ .../build => assets/install-fake-registry.sh} | 9 +- integration/config_drive_test.go | 55 +++++++++ integration/integration_suite_test.go | 13 +++ integration/test_environment.go | 109 ++++++++++++++++++ 11 files changed, 232 insertions(+), 75 deletions(-) rename integration/bin/run-test-with-vm-in-container.sh => bin/run-gocd-integration-test (92%) rename integration/bin/test => bin/test-integration (70%) rename integration/assets/{agent.json => config-drive-agent.json} (100%) delete mode 100755 integration/assets/configure-environment.sh create mode 100755 integration/assets/install-agent.sh rename integration/{bin/build => assets/install-fake-registry.sh} (66%) create mode 100644 integration/config_drive_test.go create mode 100644 integration/integration_suite_test.go create mode 100644 integration/test_environment.go diff --git a/Vagrantfile b/Vagrantfile index 98759d2a1..3eb325aa8 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -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 diff --git a/integration/bin/run-test-with-vm-in-container.sh b/bin/run-gocd-integration-test similarity index 92% rename from integration/bin/run-test-with-vm-in-container.sh rename to bin/run-gocd-integration-test index 2d4431691..91df0c18e 100755 --- a/integration/bin/run-test-with-vm-in-container.sh +++ b/bin/run-gocd-integration-test @@ -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` @@ -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="$!" diff --git a/integration/bin/test b/bin/test-integration similarity index 70% rename from integration/bin/test rename to bin/test-integration index 8b2995c82..ebd22e7e6 100755 --- a/integration/bin/test +++ b/bin/test-integration @@ -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 @@ -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` diff --git a/bin/test-unit b/bin/test-unit index aa0e88621..25ad48ab0 100755 --- a/bin/test-unit +++ b/bin/test-unit @@ -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..." diff --git a/integration/assets/agent.json b/integration/assets/config-drive-agent.json similarity index 100% rename from integration/assets/agent.json rename to integration/assets/config-drive-agent.json diff --git a/integration/assets/configure-environment.sh b/integration/assets/configure-environment.sh deleted file mode 100755 index 298b39f6d..000000000 --- a/integration/assets/configure-environment.sh +++ /dev/null @@ -1,57 +0,0 @@ -#!/bin/bash - -# Assume to be running on bosh lite -set -x -e - -export GOPATH=/home/vagrant/go -export GOROOT=/usr/local/go -export PATH=$GOROOT/bin:$PATH - -agent_dir=$GOPATH/src/github.com/cloudfoundry/bosh-agent -assets_dir=$agent_dir/integration/assets - -# stop the currently running agent -sudo sv stop agent - -# remove settings.json -sudo mv /var/vcap/bosh/settings.json{,.bk} - -# prepare config-drive -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 $assets_dir/meta-data.json /tmp/config-drive/ec2/latest/meta-data.json -sudo cp $assets_dir/user-data /tmp/config-drive/ec2/latest -sudo umount /tmp/config-drive - -# change the agent's infrastructure -sudo mv /var/vcap/bosh/etc/infrastructure{,.bk} -echo 'openstack' | sudo tee /var/vcap/bosh/etc/infrastructure - -# get agent settings from test assets -sudo cp $assets_dir/agent.json /var/vcap/bosh/agent.json - -pushd $agent_dir - # install golang - sudo $agent_dir/integration/assets/install-go.sh - - # build fake registry - ./integration/bin/build - - # start registry and seed it with a new agent_id - nohup tmp/fake-registry -user user -password pass -host localhost -port 9090 -instance instance-id -settings "{\"agent_id\":\"the_agent_id\"}" &> /dev/null & - - # 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 diff --git a/integration/assets/install-agent.sh b/integration/assets/install-agent.sh new file mode 100755 index 000000000..f434ad5b0 --- /dev/null +++ b/integration/assets/install-agent.sh @@ -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 \ No newline at end of file diff --git a/integration/bin/build b/integration/assets/install-fake-registry.sh similarity index 66% rename from integration/bin/build rename to integration/assets/install-fake-registry.sh index ba1bf0363..85331d0a9 100755 --- a/integration/bin/build +++ b/integration/assets/install-fake-registry.sh @@ -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 @@ -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 \ No newline at end of file diff --git a/integration/config_drive_test.go b/integration/config_drive_test.go new file mode 100644 index 000000000..e3d8c4f8e --- /dev/null +++ b/integration/config_drive_test.go @@ -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")) + }) + }) + }) +}) diff --git a/integration/integration_suite_test.go b/integration/integration_suite_test.go new file mode 100644 index 000000000..50ed1e010 --- /dev/null +++ b/integration/integration_suite_test.go @@ -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") +} diff --git a/integration/test_environment.go b/integration/test_environment.go new file mode 100644 index 000000000..113cf196b --- /dev/null +++ b/integration/test_environment.go @@ -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()) +}