From 4f92faae96f344c867f88c37b23868ef6b6ce3cb Mon Sep 17 00:00:00 2001 From: Nirdesh Shukla Date: Sun, 22 Jan 2017 23:06:46 -0800 Subject: [PATCH] Refactoring work: defining SSH params to reuse (addressing Ritesh's comment) --- vmdk_plugin/E2E_Tests/createVolume_test.go | 18 +++++++++--------- .../utils/test_util/VolumeCreationTestUtil.go | 19 ++++++++++++++----- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/vmdk_plugin/E2E_Tests/createVolume_test.go b/vmdk_plugin/E2E_Tests/createVolume_test.go index fb28d4bca..0544439e4 100644 --- a/vmdk_plugin/E2E_Tests/createVolume_test.go +++ b/vmdk_plugin/E2E_Tests/createVolume_test.go @@ -23,10 +23,10 @@ package e2e_test import ( - "testing" - "os" - TestUtil "github.com/vmware/docker-volume-vsphere/vmdk_plugin/utils/test_util" volumeNameUtil "github.com/vmware/docker-volume-vsphere/vmdk_plugin/utils" + TestUtil "github.com/vmware/docker-volume-vsphere/vmdk_plugin/utils/test_util" + "os" + "testing" ) // Test objective: create volume on fresh setup very first time and verifies the volume @@ -34,7 +34,7 @@ import ( /* -The issue was observed on photon so steps are mentioned for photon OS only in fact +The issue was observed on photon so steps are mentioned for photon OS only in fact test should be OS agnostic. 1. create docker volume using following command @@ -46,16 +46,16 @@ expectation: volume should be created correctly */ -func TestVolumeCreationFristTime(t *testing.T) { +func TestVolumeCreationFirstTime(t *testing.T) { var err error var out []byte volumeName := volumeNameUtil.GetVolumeNameWithTimeStamp("abc") - + // create volume out, err = TestUtil.CreateDefaultVolume(os.Getenv("VM1"), volumeName) - - if (err != nil) { - t.Fatalf("\nError has occurred [%s] \n\twhile creating volume [%s] very first time: err -> %v", out, volumeName, err) + + if err != nil { + t.Fatalf("\nError has occurred [%s] \n\twhile creating volume [%s] very first time: err -> %v", out, volumeName, err) } else { t.Logf("\nTestcase passed: successfully able to create volume [%s]\n", out) // delete volume diff --git a/vmdk_plugin/utils/test_util/VolumeCreationTestUtil.go b/vmdk_plugin/utils/test_util/VolumeCreationTestUtil.go index 54c4edd3f..7c787453e 100644 --- a/vmdk_plugin/utils/test_util/VolumeCreationTestUtil.go +++ b/vmdk_plugin/utils/test_util/VolumeCreationTestUtil.go @@ -24,19 +24,28 @@ import ( "strings" ) +var SSH_OPTS []string = []string{strings.Split(os.Getenv("SSH_KEY_OPT"), " ")[0], strings.Split(os.Getenv("SSH_KEY_OPT"), " ")[1], "-q", "-kTax", "-o StrictHostKeyChecking=no"} + // This util method is going to create vsphere docker volume with // defaults. func CreateDefaultVolume(ip string, name string) ([]byte, error) { - fmt.Printf("\ncreating volume [%s] on VM[%s]", name, ip) - - return exec.Command("/usr/bin/ssh", strings.Split(os.Getenv("SSH_KEY_OPT"), " ")[0], strings.Split(os.Getenv("SSH_KEY_OPT"), " ")[1], "-q", "-kTax", "-o StrictHostKeyChecking=no", "root@"+ip, "docker volume create --driver=vmdk --name="+name).CombinedOutput() - + return InvokeCommand(ip, "docker volume create --driver=vmdk --name="+name) } // This helper deletes the created volume as per passed volume name. func DeleteVolume(name string, ip string) ([]byte, error) { fmt.Printf("\ndestroying volume [%s]", name) + return InvokeCommand(ip, "docker volume rm "+name) +} - return exec.Command("/usr/bin/ssh", strings.Split(os.Getenv("SSH_KEY_OPT"), " ")[0], strings.Split(os.Getenv("SSH_KEY_OPT"), " ")[1], "-q", "-kTax", "-o StrictHostKeyChecking=no", "root@"+ip, "docker volume rm "+name).CombinedOutput() +// This helper method can be consumed by test directly to invoke +// any command on the remote host. +// remoteHostIP: +// remote machine address to execute on the machine +// cmd: +// A command string to be executed on the remote host as per +// remoteHostIP value +func InvokeCommand(remoteHostIP string, cmd string) ([]byte, error) { + return exec.Command("/usr/bin/ssh", append(SSH_OPTS, "root@"+remoteHostIP, cmd)...).CombinedOutput() }