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

Commit

Permalink
Automate vsan policy test case (#1361)
Browse files Browse the repository at this point in the history
* Automate vsanpolicy test.

* Cleanup the code.

* Address comments from Shahzeb and Anup.

* Move the test code to vsan_test.go and delete unused files.

Move the test code to vsan_test.go and delete unused files.

Minor fix.

* Address comments from Sam.

Address comments from Sam.

Minor fix.

* Address comments from Nirdesh and Sam.

* Small fix to change the test name.

* Address more comments from Nirdesh.

* Add code to removing policys in test tearDown.
  • Loading branch information
lipingxue committed Jun 10, 2017
1 parent 7299e75 commit 8ed1887
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 28 deletions.
3 changes: 0 additions & 3 deletions tests/constants/admincli/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ const (
// ListVolumes referring to vmdkops_admin volume ls
ListVolumes = vmdkopsAdminVolume + "ls "

// CreatePolicy Create a policy
CreatePolicy = vmdkopsAdmin + " policy create "

// SetVolumeAccess set volume access
SetVolumeAccess = vmdkopsAdminVolume + " set "

Expand Down
37 changes: 37 additions & 0 deletions tests/constants/admincli/vsanpolicy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2017 VMware, 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.

// A home to hold test constants which will be used to vsan related tests.

package admincli

const (
// PolicyName is the name of vsan policy which will be used in test
PolicyName = "some-policy"

// PolicyContent is the content of vsan policy which will be used in test
PolicyContent = "'((\"proportionalCapacity\" i50)''(\"hostFailuresToTolerate\" i0))'"

// CreatePolicy Create a policy
CreatePolicy = vmdkopsAdmin + " policy create --name="

// ListPolicy referring to list all existing policies
ListPolicy = vmdkopsAdmin + "policy ls "

// RemovePolicy referring to remove a policy
RemovePolicy = vmdkopsAdmin + "policy rm --name="

// VsanPolicyFlag is the flag that will be passed in "vmdkops_admin policy XXX" command
VsanPolicyFlag = "vsan-policy-name"
)
109 changes: 85 additions & 24 deletions tests/e2e/vsan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
package e2e

import (
"log"
"strings"

adminclicon "github.com/vmware/docker-volume-vsphere/tests/constants/admincli"
"github.com/vmware/docker-volume-vsphere/tests/utils/admincli"
"github.com/vmware/docker-volume-vsphere/tests/utils/dockercli"
"github.com/vmware/docker-volume-vsphere/tests/utils/govc"
Expand All @@ -35,16 +37,12 @@ import (
. "gopkg.in/check.v1"
)

const (
vsanVolumeTest = "vsan_vol"
vsanPolicyFlag = "vsan-policy-name"
)

type VsanTestSuite struct {
config *inputparams.TestConfig

vsanDSName string
volumeName string
policyList []string
}

func (s *VsanTestSuite) SetUpSuite(c *C) {
Expand All @@ -60,44 +58,107 @@ func (s *VsanTestSuite) SetUpSuite(c *C) {
}

func (s *VsanTestSuite) TearDownTest(c *C) {
out, err := dockercli.DeleteVolume(s.config.DockerHosts[0], s.volumeName)
c.Assert(err, IsNil, Commentf(out))
if s.volumeName != "" {
out, err := dockercli.DeleteVolume(s.config.DockerHosts[0], s.volumeName)
c.Assert(err, IsNil, Commentf(out))
}
if len(s.policyList) > 0 {
for _, policyName := range s.policyList {
out, err := admincli.RemovePolicy(s.config.EsxHost, policyName)
c.Assert(err, IsNil, Commentf(out))
}
}
}

var _ = Suite(&VsanTestSuite{})

// Vsan related test
// Steps:
// 1. Create a valid vsan policy
// 2. Create an invalid vsan policy (wrong content)
// 3. Volume creation with valid policy should pass
// 4. Valid volume should be accessible
// 5. Volume creation with non existing policy should fail
// 6. Volume creation with invalid policy should fail
func (s *VsanTestSuite) TestVSANPolicy(c *C) {
misc.LogTestStart(volCreateTest, "TestVSANPolicy")

// 2. Volume creation with valid policy should pass
// 3. Valid volume should be accessible
func (s *VsanTestSuite) TestValidPolicy(c *C) {
misc.LogTestStart("", c.TestName())
s.volumeName = ""
policyName := "validPolicy"
out, err := admincli.CreatePolicy(s.config.EsxHost, policyName, "'((\"proportionalCapacity\" i50)''(\"hostFailuresToTolerate\" i0))'")
c.Assert(err, IsNil, Commentf(out))

invalidContentPolicyName := "invalidPolicy"
out, err = admincli.CreatePolicy(s.config.EsxHost, invalidContentPolicyName, "'((\"wrongKey\" i50)'")
out, err := admincli.CreatePolicy(s.config.EsxHost, policyName, adminclicon.PolicyContent)
c.Assert(err, IsNil, Commentf(out))
s.policyList = append(s.policyList, policyName)

s.volumeName = inputparams.GetVolumeNameWithTimeStamp("vsanVol") + "@" + s.vsanDSName
vsanOpts := " -o " + vsanPolicyFlag + "=" + policyName
vsanOpts := " -o " + adminclicon.VsanPolicyFlag + "=" + policyName

out, err = dockercli.CreateVolumeWithOptions(s.config.DockerHosts[0], s.volumeName, vsanOpts)
c.Assert(err, IsNil, Commentf(out))
isAvailable := verification.CheckVolumeAvailability(s.config.DockerHosts[0], s.volumeName)
c.Assert(isAvailable, Equals, true, Commentf("Volume %s is not available after creation", s.volumeName))

invalidVsanOpts := [2]string{"-o " + vsanPolicyFlag + "=IDontExist", "-o " + vsanPolicyFlag + "=" + invalidContentPolicyName}
misc.LogTestEnd("", c.TestName())
}

// Steps:
// 1. Create an invalid vsan policy (wrong content)
// 2. Volume creation with non existing policy should fail
// 3. Volume creation with invalid policy should fail
func (s *VsanTestSuite) TestInvalidPolicy(c *C) {
misc.LogTestStart("", c.TestName())
s.volumeName = ""
invalidContentPolicyName := "invalidPolicy"
out, err := admincli.CreatePolicy(s.config.EsxHost, invalidContentPolicyName, "'((\"wrongKey\" i50)'")
c.Assert(err, IsNil, Commentf(out))
s.policyList = append(s.policyList, invalidContentPolicyName)

invalidVsanOpts := [2]string{"-o " + adminclicon.VsanPolicyFlag + "=IDontExist", "-o " +
adminclicon.VsanPolicyFlag + "=" + invalidContentPolicyName}
for _, option := range invalidVsanOpts {
invalidVolName := inputparams.GetVolumeNameWithTimeStamp("vsanVol") + "@" + s.vsanDSName
out, _ = dockercli.CreateVolumeWithOptions(s.config.DockerHosts[0], invalidVolName, option)
c.Assert(strings.HasPrefix(out, ErrorVolumeCreate), Equals, true)
}

misc.LogTestStart(volCreateTest, "TestVSANPolicy")
misc.LogTestEnd("", c.TestName())
}

// The purpose of this test is to verify:
// 1) a volume can be created with vsan policy specified
// 2) A vsan policy cannot be removed if volumes still use it

// Test step:
// 1. create a vsan policy
// 2. run "vmdkops_admin policy ls", check the "Active" column of the output to make sure it
// is shown as "Unused"
// 3. create a volume on vsanDatastore with the vsan policy we created
// 4. run "docker volume inspect" on the volume to verify the output "vsan-policy-name" field
// 5. run "vmdkops_admin policy ls", check the "Active" column of the output to make sure it
// is shown as "In use by 1 volumes"
// 6. run "vmdkops_admin policy rm" to remove the policy, which should fail since the volume is still
// use the vsan policy
func (s *VsanTestSuite) TestDeleteVsanPolicyAlreadyInUse(c *C) {
misc.LogTestStart("", c.TestName())
s.volumeName = ""
out, err := admincli.CreatePolicy(s.config.EsxHost, adminclicon.PolicyName, adminclicon.PolicyContent)
c.Assert(err, IsNil, Commentf(out))

s.policyList = append(s.policyList, adminclicon.PolicyName)

res := admincli.VerifyActiveFromVsanPolicyListOutput(s.config.EsxHost, adminclicon.PolicyName, "Unused")
c.Assert(res, Equals, true, Commentf("vsanPolicy should be \"Unused\""))

s.volumeName = inputparams.GetVolumeNameWithTimeStamp("vsanVol") + "@" + s.vsanDSName
vsanOpts := " -o " + adminclicon.VsanPolicyFlag + "=" + adminclicon.PolicyName
out, err = dockercli.CreateVolumeWithOptions(s.config.DockerHosts[0], s.volumeName, vsanOpts)
c.Assert(err, IsNil, Commentf(out))

policyName, err := verification.GetAssociatedPolicyName(s.config.DockerHosts[0], s.volumeName)
c.Assert(err, IsNil, Commentf("Get associated policy for volume %s failed", s.volumeName))
c.Assert(policyName, Equals, adminclicon.PolicyName, Commentf("The name of vsan policy used by volume "+s.vsanDSName+" returns incorrect value "+policyName))

res = admincli.VerifyActiveFromVsanPolicyListOutput(s.config.EsxHost, adminclicon.PolicyName, "In use by 1 volumes")
c.Assert(res, Equals, true, Commentf("vsanPolicy should be \"In use by 1 volumes\""))

out, err = admincli.RemovePolicy(s.config.EsxHost, adminclicon.PolicyName)
log.Printf("Remove vsanPolicy \"%s\" returns with %s", adminclicon.PolicyName, out)
c.Assert(out, Matches, "Error: Cannot remove.*", Commentf("vsanPolicy is still used by volumes and cannot be removed"))

misc.LogTestEnd("", c.TestName())

}
21 changes: 20 additions & 1 deletion tests/utils/admincli/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,26 @@ import (
// CreatePolicy creates a policy
func CreatePolicy(ip, name, content string) (string, error) {
log.Printf("Creating policy [%s] with content [%s] on ESX [%s]\n", name, content, ip)
return ssh.InvokeCommand(ip, admincli.CreatePolicy+" --name "+name+" --content "+content)
return ssh.InvokeCommand(ip, admincli.CreatePolicy+name+" --content "+content)
}

// RemovePolicy removes a policy.
func RemovePolicy(ip, policyName string) (string, error) {
log.Printf("Removing policy [%s] on esx [%s]\n", policyName, ip)
return ssh.InvokeCommand(ip, admincli.RemovePolicy+policyName)
}

// VerifyActiveFromVsanPolicyListOutput is going to check, for the given vsan policy, whether the active
// column returned by "vmdkops policy ls" command matches the value specified by input param "active"
func VerifyActiveFromVsanPolicyListOutput(ip, policyName, active string) bool {
log.Printf("Verify vsanPolicy [%s] on esx [%s] has active as %s\n", policyName, ip, active)
cmd := admincli.ListPolicy + " 2>/dev/null | grep " + policyName
out, err := ssh.InvokeCommand(ip, cmd)
if err != nil {
return false
}
log.Printf("policy ls output for vsanPolicy [%s] is %s:", policyName, out)
return strings.Contains(out, active)
}

// UpdateVolumeAccess update the volume access as per params
Expand Down
10 changes: 10 additions & 0 deletions tests/utils/verification/volumeproperties.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,13 @@ func VerifyDetachedStatus(name, hostName, esxName string) bool {
log.Printf("Timed out to poll status\n")
return false
}

// GetAssociatedPolicyName returns the vsan policy name used by the volume using docker cli
func GetAssociatedPolicyName(hostname string, volName string) (string, error) {
cmd := dockercli.InspectVolume + " --format '{{index .Status \"vsan-policy-name\"}}' " + volName
op, err := ssh.InvokeCommand(hostname, cmd)
if op == "" {
log.Printf("Null value is returned by docker cli when looking for the name of vsan policy used by volume. Output: ", op)
}
return op, err
}

0 comments on commit 8ed1887

Please sign in to comment.