From f4494b93cd58e1deeee9e0a246f177803feabde8 Mon Sep 17 00:00:00 2001 From: chengwang86 Date: Fri, 14 Apr 2017 16:19:06 -0700 Subject: [PATCH 01/11] detect VCH update/upgrade status by setting UpdateInProgress in ExtraConfig --- cmd/vic-machine/inspect/inspect.go | 6 ++-- cmd/vic-machine/upgrade/upgrade.go | 39 ++++++++++++++++++++++++ lib/install/data/data.go | 5 ++-- pkg/vsphere/vm/vm.go | 48 ++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 5 deletions(-) diff --git a/cmd/vic-machine/inspect/inspect.go b/cmd/vic-machine/inspect/inspect.go index 0c33ef35e1..03234ffc4c 100644 --- a/cmd/vic-machine/inspect/inspect.go +++ b/cmd/vic-machine/inspect/inspect.go @@ -168,13 +168,13 @@ func (i *Inspect) upgradeStatusMessage(ctx context.Context, vch *vm.VirtualMachi return } - upgrading, _, err := vch.UpgradeInProgress(ctx, management.UpgradePrefix) + upgrading, err := vch.GetVCHUpdateStatus(ctx) if err != nil { - log.Errorf("Unable to determine if upgrade is in progress: %s", err) + log.Errorf("Unable to determine if upgrade/date is in progress: %s", err) return } if upgrading { - log.Info("Upgrade in progress") + log.Info("Upgrade/update in progress") return } diff --git a/cmd/vic-machine/upgrade/upgrade.go b/cmd/vic-machine/upgrade/upgrade.go index be2dc15877..3652aaae15 100644 --- a/cmd/vic-machine/upgrade/upgrade.go +++ b/cmd/vic-machine/upgrade/upgrade.go @@ -64,6 +64,11 @@ func (u *Upgrade) Flags() []cli.Flag { Usage: "Roll back VCH version to before the previous upgrade", Destination: &u.Rollback, }, + cli.BoolFlag{ + Name: "resetInProgressFlag", + Usage: "Reset the UpdateInProgress flag", + Destination: &u.ResetInProgressFlag, + }, } target := u.TargetFlags() @@ -148,6 +153,40 @@ func (u *Upgrade) Run(clic *cli.Context) (err error) { log.Infof("") log.Infof("VCH ID: %s", vch.Reference().String()) + if u.ResetInProgressFlag { + if err = vch.SetVCHUpdateStatus(ctx, false); err != nil { + log.Error("Failed to reset UpdateInprogress flag") + log.Error(err) + return errors.New("upgrade failed") + } + log.Infof("Reset UpdateInProgress flag successfully") + return nil + } + + upgrading, err := vch.GetVCHUpdateStatus(ctx) + if err != nil { + log.Error("Unable to determine if upgrade/update is in progress") + log.Error(err) + return errors.New("upgrade failed") + } + if upgrading { + log.Error("Upgrade failed: another upgrade/update operation is in progress") + return errors.New("upgrade failed") + } + + if err = vch.SetVCHUpdateStatus(ctx, true); err != nil { + log.Error("Failed to set UpdateInProgress flag to true") + log.Error(err) + return errors.New("upgrade failed") + } + + defer func() { + if err = vch.SetVCHUpdateStatus(ctx, false); err != nil { + log.Error("Failed to reset UpdateInProgress") + log.Error(err) + } + } () + vchConfig, err := executor.FetchAndMigrateVCHConfig(vch) if err != nil { log.Error("Failed to get Virtual Container Host configuration") diff --git a/lib/install/data/data.go b/lib/install/data/data.go index 8cd1d47073..889e6c7886 100644 --- a/lib/install/data/data.go +++ b/lib/install/data/data.go @@ -76,8 +76,9 @@ type Data struct { Timeout time.Duration - Force bool - UseRP bool + Force bool + UseRP bool + ResetInProgressFlag bool AsymmetricRouting bool diff --git a/pkg/vsphere/vm/vm.go b/pkg/vsphere/vm/vm.go index 2b9546c9d7..f1568ae55e 100644 --- a/pkg/vsphere/vm/vm.go +++ b/pkg/vsphere/vm/vm.go @@ -21,6 +21,7 @@ import ( "fmt" "net/url" "path" + "strconv" "strings" "sync/atomic" @@ -32,10 +33,13 @@ import ( "github.com/vmware/govmomi/vim25/mo" "github.com/vmware/govmomi/vim25/types" + "github.com/vmware/vic/pkg/vsphere/extraconfig/vmomi" "github.com/vmware/vic/pkg/vsphere/session" "github.com/vmware/vic/pkg/vsphere/tasks" ) +const UpdateInProgress = "UpdateInProgress" + type InvalidState struct { r types.ManagedObjectReference } @@ -560,3 +564,47 @@ func (vm *VirtualMachine) DatastoreReference(ctx context.Context) ([]types.Manag } return mvm.Datastore, nil } + +// GetVCHUpdateStatus tells if an upgrade/update has already been started based on the UpdateInProgress flag in ExtraConfig +// If error != nil, VCH upgrade/update will exit; so the value of the VCH upgrade/update status is not important +func (vm *VirtualMachine) GetVCHUpdateStatus(ctx context.Context) (bool, error) { + var mvm mo.VirtualMachine + + if err := vm.Properties(ctx, vm.Reference(), []string{"config.extraConfig"}, &mvm); err != nil { + log.Errorf("Unable to get vm config: %s", err) + return false, err + } + + for _, bov := range mvm.Config.ExtraConfig { + ov := bov.GetOptionValue() + if ov.Key == UpdateInProgress { + if updateStatus, err := strconv.ParseBool(ov.Value.(string)); err != nil { + return false, err + } else { + return updateStatus, nil + } + } + } + // If "UpdateInProgress" is not found, it might be the case that no update/upgrade has been done to this VCH before + return false, nil +} + +// SetVCHUpdateStatus sets the "UpdateInProgress" flag in ExtraConfig +func (vm *VirtualMachine) SetVCHUpdateStatus(ctx context.Context, updateStatus bool) error { + + info, err := vm.FetchExtraConfig(ctx) + if err != nil { + return err + } + info[UpdateInProgress] = strconv.FormatBool(updateStatus) + + s := &types.VirtualMachineConfigSpec{ + ExtraConfig: vmomi.OptionValueFromMap(info), + } + + _, err = vm.WaitForResult(ctx, func(ctx context.Context) (tasks.Task, error) { + return vm.Reconfigure(ctx, *s) + }) + + return err +} From 900101b7c16d595b38c1911c03a666c47f829415 Mon Sep 17 00:00:00 2001 From: chengwang86 Date: Fri, 21 Apr 2017 15:03:01 -0700 Subject: [PATCH 02/11] add test cases --- cmd/vic-machine/upgrade/upgrade.go | 6 +-- pkg/vsphere/vm/vm.go | 34 ++++++-------- .../11-04-Upgrade-UpdateInProgress.md | 25 ++++++++++ .../11-04-Upgrade-UpdateInProgress.robot | 47 +++++++++++++++++++ 4 files changed, 89 insertions(+), 23 deletions(-) create mode 100644 tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpdateInProgress.md create mode 100644 tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpdateInProgress.robot diff --git a/cmd/vic-machine/upgrade/upgrade.go b/cmd/vic-machine/upgrade/upgrade.go index 3652aaae15..1af2508ebf 100644 --- a/cmd/vic-machine/upgrade/upgrade.go +++ b/cmd/vic-machine/upgrade/upgrade.go @@ -154,7 +154,7 @@ func (u *Upgrade) Run(clic *cli.Context) (err error) { log.Infof("VCH ID: %s", vch.Reference().String()) if u.ResetInProgressFlag { - if err = vch.SetVCHUpdateStatus(ctx, false); err != nil { + if err = vch.SetVCHUpdateStatus(ctx, "false"); err != nil { log.Error("Failed to reset UpdateInprogress flag") log.Error(err) return errors.New("upgrade failed") @@ -174,14 +174,14 @@ func (u *Upgrade) Run(clic *cli.Context) (err error) { return errors.New("upgrade failed") } - if err = vch.SetVCHUpdateStatus(ctx, true); err != nil { + if err = vch.SetVCHUpdateStatus(ctx, "true"); err != nil { log.Error("Failed to set UpdateInProgress flag to true") log.Error(err) return errors.New("upgrade failed") } defer func() { - if err = vch.SetVCHUpdateStatus(ctx, false); err != nil { + if err = vch.SetVCHUpdateStatus(ctx, "false"); err != nil { log.Error("Failed to reset UpdateInProgress") log.Error(err) } diff --git a/pkg/vsphere/vm/vm.go b/pkg/vsphere/vm/vm.go index f1568ae55e..fa0cb1e5b5 100644 --- a/pkg/vsphere/vm/vm.go +++ b/pkg/vsphere/vm/vm.go @@ -568,41 +568,35 @@ func (vm *VirtualMachine) DatastoreReference(ctx context.Context) ([]types.Manag // GetVCHUpdateStatus tells if an upgrade/update has already been started based on the UpdateInProgress flag in ExtraConfig // If error != nil, VCH upgrade/update will exit; so the value of the VCH upgrade/update status is not important func (vm *VirtualMachine) GetVCHUpdateStatus(ctx context.Context) (bool, error) { - var mvm mo.VirtualMachine - - if err := vm.Properties(ctx, vm.Reference(), []string{"config.extraConfig"}, &mvm); err != nil { - log.Errorf("Unable to get vm config: %s", err) + info, err := vm.FetchExtraConfig(ctx) + if err != nil { + log.Errorf("Unable to get vm ExtraConfig: %s", err) return false, err } - for _, bov := range mvm.Config.ExtraConfig { - ov := bov.GetOptionValue() - if ov.Key == UpdateInProgress { - if updateStatus, err := strconv.ParseBool(ov.Value.(string)); err != nil { - return false, err - } else { - return updateStatus, nil - } + if v, ok := info[UpdateInProgress]; ok { + updateStatus, err := strconv.ParseBool(v) + if err != nil { + // If error occurs, return true to cancel VCH upgrade. The user can reset the UpdateInProgress flag later. + return true, fmt.Errorf("failed to parse %s to bool: %s", v, err) } + return updateStatus, nil } + // If "UpdateInProgress" is not found, it might be the case that no update/upgrade has been done to this VCH before return false, nil } // SetVCHUpdateStatus sets the "UpdateInProgress" flag in ExtraConfig -func (vm *VirtualMachine) SetVCHUpdateStatus(ctx context.Context, updateStatus bool) error { - - info, err := vm.FetchExtraConfig(ctx) - if err != nil { - return err - } - info[UpdateInProgress] = strconv.FormatBool(updateStatus) +func (vm *VirtualMachine) SetVCHUpdateStatus(ctx context.Context, updateStatus string) error { + info := make(map[string]string) + info[UpdateInProgress] = updateStatus s := &types.VirtualMachineConfigSpec{ ExtraConfig: vmomi.OptionValueFromMap(info), } - _, err = vm.WaitForResult(ctx, func(ctx context.Context) (tasks.Task, error) { + _, err := vm.WaitForResult(ctx, func(ctx context.Context) (tasks.Task, error) { return vm.Reconfigure(ctx, *s) }) diff --git a/tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpdateInProgress.md b/tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpdateInProgress.md new file mode 100644 index 0000000000..b27a158db2 --- /dev/null +++ b/tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpdateInProgress.md @@ -0,0 +1,25 @@ +Test 11-02 - Upgrade Exec +======= + +#Purpose: +To verify that vic-machine inspect could detect the upgrade status of a VCH + +#Environment: +This test requires that a vSphere server is running and available + +#Test Steps: +1. Download vic_7315.tar.gz from bintray +2. Deploy VIC 7315 to vsphere server +3. Set UpdateInProgress to true using govc +4. Upgrade VCH +5. Run vic-machine upgrade --resetInProgressFlag to reset UpdateInprogress to false +6. Upgrade VCH +7. Run vic-machine inspect to check the upgrade status of the VCH (this should run in parallel with step 6) +8. After step 3 finishes, run step 4 again. + +#Expected Outcome: +* In step 4, output should contain "Upgrade failed: another upgrade/update operation is in progress" +* In step 5, output should contain "Reset UpdateInProgress flag successfully" +* In step 6, output should contain "Completed successfully" +* In step 7, output should contain "Upgrade/update in progress" +* In step 8, output should not contain "Upgrade/update in progress" diff --git a/tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpdateInProgress.robot b/tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpdateInProgress.robot new file mode 100644 index 0000000000..bbb93f7811 --- /dev/null +++ b/tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpdateInProgress.robot @@ -0,0 +1,47 @@ +# 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 + +*** Settings *** +Documentation Test 11-04 - Upgrade-Inspect +Suite Setup Install VIC with version to Test Server 7315 +Suite Teardown Clean up VIC Appliance And Local Binary +Resource ../../resources/Util.robot + +*** Keywords *** +Inspect VCH + [Arguments] ${expected} + ${rc} ${output}= Run And Return Rc And Output bin/vic-machine-linux inspect --name=%{VCH-NAME} --target=%{TEST_URL} --thumbprint=%{TEST_THUMBPRINT} --user=%{TEST_USERNAME} --password=%{TEST_PASSWORD} --compute-resource=%{TEST_RESOURCE} + Should Be Equal As Integers ${rc} 0 + Should Contain ${output} ${expected} + +Check UpdateInProgress + [Arguments] ${expected} + ${rc} ${output}= Run And Return Rc And Output govc vm.info -e %{VCH-NAME} | grep UpdateInProgress + Should Be Equal As Integers ${rc} 0 + Should Contain ${output} ${expected} + +Inspect VCH Upgrade Completed + ${rc} ${output}= Run And Return Rc And Output bin/vic-machine-linux inspect --name=%{VCH-NAME} --target=%{TEST_URL} --thumbprint=%{TEST_THUMBPRINT} --user=%{TEST_USERNAME} --password=%{TEST_PASSWORD} --compute-resource=%{TEST_RESOURCE} + Should Be Equal As Integers ${rc} 0 + Should Contain ${output} Completed successfully + +*** Test Cases *** +Upgrade and inspect VCH + #Start Process bin/vic-machine-linux create --help --name\=aaa alias=create + #Wait For Process create + Start Process bin/vic-machine-linux upgrade --debug 1 --name %{VCH-NAME} --target %{TEST_URL} --user %{TEST_USERNAME} --password %{TEST_PASSWORD} --force --compute-resource %{TEST_RESOURCE} --timeout %{TEST_TIMEOUT} alias=UpgradeVCH + Wait Until Keyword Succeeds 20x 5s Inspect VCH Upgrade/update in progress + Wait For Process UpgradeVCH + Inspect VCH Completed successfully + Check UpdateInProgress false From 4040a129d09c5906fb922915ef87d2fb6d7acfef Mon Sep 17 00:00:00 2001 From: chengwang86 Date: Fri, 21 Apr 2017 16:25:01 -0700 Subject: [PATCH 03/11] update tests --- cmd/vic-machine/inspect/inspect.go | 2 +- .../11-04-Upgrade-UpdateInProgress.md | 2 +- .../11-04-Upgrade-UpdateInProgress.robot | 13 ++++++++++--- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/cmd/vic-machine/inspect/inspect.go b/cmd/vic-machine/inspect/inspect.go index 03234ffc4c..4df6c1b9d4 100644 --- a/cmd/vic-machine/inspect/inspect.go +++ b/cmd/vic-machine/inspect/inspect.go @@ -170,7 +170,7 @@ func (i *Inspect) upgradeStatusMessage(ctx context.Context, vch *vm.VirtualMachi upgrading, err := vch.GetVCHUpdateStatus(ctx) if err != nil { - log.Errorf("Unable to determine if upgrade/date is in progress: %s", err) + log.Errorf("Unable to determine if upgrade/update is in progress: %s", err) return } if upgrading { diff --git a/tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpdateInProgress.md b/tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpdateInProgress.md index b27a158db2..c91a445367 100644 --- a/tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpdateInProgress.md +++ b/tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpdateInProgress.md @@ -1,4 +1,4 @@ -Test 11-02 - Upgrade Exec +Test 11-04 - Upgrade UpdateInProgress ======= #Purpose: diff --git a/tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpdateInProgress.robot b/tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpdateInProgress.robot index bbb93f7811..da29bea5d3 100644 --- a/tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpdateInProgress.robot +++ b/tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpdateInProgress.robot @@ -37,10 +37,17 @@ Inspect VCH Upgrade Completed Should Contain ${output} Completed successfully *** Test Cases *** +Upgrade VCH with UpdateInProgress + Run govc vm.change -vm=%{VCH-NAME} -e=UpdateInProgress=true + Check UpdateInProgress true + ${rc} ${output}= Run And Return Rc And Output bin/vic-machine-linux upgrade --debug 1 --name=%{VCH-NAME} --target=%{TEST_URL} --user=%{TEST_USERNAME} --password=%{TEST_PASSWORD} --force=true --compute-resource=%{TEST_RESOURCE} --timeout %{TEST_TIMEOUT} + Should Contain ${output} Upgrade failed: another upgrade/update operation is in progress + ${rc} ${output}= Run And Return Rc And Output bin/vic-machine-linux upgrade --resetInProgressFlag --name=%{VCH-NAME} --target=%{TEST_URL} --user=%{TEST_USERNAME} --password=%{TEST_PASSWORD} --force=true --compute-resource=%{TEST_RESOURCE} --timeout %{TEST_TIMEOUT} + Should Contain ${output} Reset UpdateInProgress flag successfully + Check UpdateInProgress false + Upgrade and inspect VCH - #Start Process bin/vic-machine-linux create --help --name\=aaa alias=create - #Wait For Process create - Start Process bin/vic-machine-linux upgrade --debug 1 --name %{VCH-NAME} --target %{TEST_URL} --user %{TEST_USERNAME} --password %{TEST_PASSWORD} --force --compute-resource %{TEST_RESOURCE} --timeout %{TEST_TIMEOUT} alias=UpgradeVCH + Start Process bin/vic-machine-linux upgrade --debug\=1 --name\=%{VCH-NAME} --target\=%{TEST_URL} --user\=%{TEST_USERNAME} --password\=%{TEST_PASSWORD} --force --compute-resource\=%{TEST_RESOURCE} --timeout\=%{TEST_TIMEOUT} alias=UpgradeVCH Wait Until Keyword Succeeds 20x 5s Inspect VCH Upgrade/update in progress Wait For Process UpgradeVCH Inspect VCH Completed successfully From 32fae613155bbdb59b792636ef13ef1d3569cae1 Mon Sep 17 00:00:00 2001 From: chengwang86 Date: Fri, 21 Apr 2017 16:34:58 -0700 Subject: [PATCH 04/11] fix gofmt error --- cmd/vic-machine/upgrade/upgrade.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/vic-machine/upgrade/upgrade.go b/cmd/vic-machine/upgrade/upgrade.go index 1af2508ebf..4baba5288b 100644 --- a/cmd/vic-machine/upgrade/upgrade.go +++ b/cmd/vic-machine/upgrade/upgrade.go @@ -185,7 +185,7 @@ func (u *Upgrade) Run(clic *cli.Context) (err error) { log.Error("Failed to reset UpdateInProgress") log.Error(err) } - } () + }() vchConfig, err := executor.FetchAndMigrateVCHConfig(vch) if err != nil { From cc3b27b12687e5a9b2faa00e068c02f0bd4f9ccb Mon Sep 17 00:00:00 2001 From: chengwang86 Date: Sun, 23 Apr 2017 22:04:21 -0700 Subject: [PATCH 05/11] update test case 11-04 --- .../Group11-Upgrade/11-04-Upgrade-UpdateInProgress.robot | 7 +------ tests/test-cases/Group11-Upgrade/ttt.robot | 8 ++++++++ 2 files changed, 9 insertions(+), 6 deletions(-) create mode 100644 tests/test-cases/Group11-Upgrade/ttt.robot diff --git a/tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpdateInProgress.robot b/tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpdateInProgress.robot index da29bea5d3..07e93e250a 100644 --- a/tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpdateInProgress.robot +++ b/tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpdateInProgress.robot @@ -31,11 +31,6 @@ Check UpdateInProgress Should Be Equal As Integers ${rc} 0 Should Contain ${output} ${expected} -Inspect VCH Upgrade Completed - ${rc} ${output}= Run And Return Rc And Output bin/vic-machine-linux inspect --name=%{VCH-NAME} --target=%{TEST_URL} --thumbprint=%{TEST_THUMBPRINT} --user=%{TEST_USERNAME} --password=%{TEST_PASSWORD} --compute-resource=%{TEST_RESOURCE} - Should Be Equal As Integers ${rc} 0 - Should Contain ${output} Completed successfully - *** Test Cases *** Upgrade VCH with UpdateInProgress Run govc vm.change -vm=%{VCH-NAME} -e=UpdateInProgress=true @@ -47,7 +42,7 @@ Upgrade VCH with UpdateInProgress Check UpdateInProgress false Upgrade and inspect VCH - Start Process bin/vic-machine-linux upgrade --debug\=1 --name\=%{VCH-NAME} --target\=%{TEST_URL} --user\=%{TEST_USERNAME} --password\=%{TEST_PASSWORD} --force --compute-resource\=%{TEST_RESOURCE} --timeout\=%{TEST_TIMEOUT} alias=UpgradeVCH + Start Process bin/vic-machine-linux upgrade --debug 1 --name %{VCH-NAME} --target %{TEST_URL} --user %{TEST_USERNAME} --password %{TEST_PASSWORD} --force --compute-resource %{TEST_RESOURCE} --timeout %{TEST_TIMEOUT} shell=True alias=UpgradeVCH Wait Until Keyword Succeeds 20x 5s Inspect VCH Upgrade/update in progress Wait For Process UpgradeVCH Inspect VCH Completed successfully diff --git a/tests/test-cases/Group11-Upgrade/ttt.robot b/tests/test-cases/Group11-Upgrade/ttt.robot new file mode 100644 index 0000000000..bb27195ff7 --- /dev/null +++ b/tests/test-cases/Group11-Upgrade/ttt.robot @@ -0,0 +1,8 @@ +*** Settings *** +Resource ../../resources/Util.robot + +*** Test Cases *** +Upgrade VCH with UpdateInProgress + Start Process bin/vic-machine-linux create --debug 1 --name vch-2 --target 10.192.190.169 --user Administrator@vsphere.local --password Admin!23 --force true --compute-resource --compute-resource /ha-datacenter/host/10.160.130.21 --timeout 20m shell=True alias=UpgradeVCH + Wait For Process UpgradeVCH + From 99b4905da7ec6dfa1c67fca508b85404984a0339 Mon Sep 17 00:00:00 2001 From: chengwang86 Date: Sun, 23 Apr 2017 22:07:06 -0700 Subject: [PATCH 06/11] update tests --- tests/test-cases/Group11-Upgrade/ttt.robot | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 tests/test-cases/Group11-Upgrade/ttt.robot diff --git a/tests/test-cases/Group11-Upgrade/ttt.robot b/tests/test-cases/Group11-Upgrade/ttt.robot deleted file mode 100644 index bb27195ff7..0000000000 --- a/tests/test-cases/Group11-Upgrade/ttt.robot +++ /dev/null @@ -1,8 +0,0 @@ -*** Settings *** -Resource ../../resources/Util.robot - -*** Test Cases *** -Upgrade VCH with UpdateInProgress - Start Process bin/vic-machine-linux create --debug 1 --name vch-2 --target 10.192.190.169 --user Administrator@vsphere.local --password Admin!23 --force true --compute-resource --compute-resource /ha-datacenter/host/10.160.130.21 --timeout 20m shell=True alias=UpgradeVCH - Wait For Process UpgradeVCH - From 2aa03a5a98edc0617c03283fa83b4899108163ab Mon Sep 17 00:00:00 2001 From: chengwang86 Date: Sun, 23 Apr 2017 22:09:12 -0700 Subject: [PATCH 07/11] update test --- .../Group11-Upgrade/11-04-Upgrade-UpdateInProgress.robot | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpdateInProgress.robot b/tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpdateInProgress.robot index 07e93e250a..7e4b941b37 100644 --- a/tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpdateInProgress.robot +++ b/tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpdateInProgress.robot @@ -13,7 +13,7 @@ # limitations under the License *** Settings *** -Documentation Test 11-04 - Upgrade-Inspect +Documentation Test 11-04 - Upgrade-UpdateInProgress Suite Setup Install VIC with version to Test Server 7315 Suite Teardown Clean up VIC Appliance And Local Binary Resource ../../resources/Util.robot From a64d5b66fe81553f205348c5ae4f369f1579958a Mon Sep 17 00:00:00 2001 From: chengwang86 Date: Mon, 24 Apr 2017 08:45:15 -0700 Subject: [PATCH 08/11] update tests and log messages --- cmd/vic-machine/inspect/inspect.go | 6 ++-- cmd/vic-machine/upgrade/upgrade.go | 23 +++++++------- pkg/vsphere/vm/vm.go | 24 +++++++------- tests/resources/VCH-Util.robot | 12 +++++++ ....md => 11-04-Upgrade-UpgradeInProgress.md} | 12 +++---- ... => 11-04-Upgrade-UpgradeInProgress.robot} | 31 ++++++------------- 6 files changed, 54 insertions(+), 54 deletions(-) rename tests/test-cases/Group11-Upgrade/{11-04-Upgrade-UpdateInProgress.md => 11-04-Upgrade-UpgradeInProgress.md} (62%) rename tests/test-cases/Group11-Upgrade/{11-04-Upgrade-UpdateInProgress.robot => 11-04-Upgrade-UpgradeInProgress.robot} (61%) diff --git a/cmd/vic-machine/inspect/inspect.go b/cmd/vic-machine/inspect/inspect.go index 4df6c1b9d4..0443b53863 100644 --- a/cmd/vic-machine/inspect/inspect.go +++ b/cmd/vic-machine/inspect/inspect.go @@ -168,13 +168,13 @@ func (i *Inspect) upgradeStatusMessage(ctx context.Context, vch *vm.VirtualMachi return } - upgrading, err := vch.GetVCHUpdateStatus(ctx) + upgrading, err := vch.VCHUpgradeStatus(ctx) if err != nil { - log.Errorf("Unable to determine if upgrade/update is in progress: %s", err) + log.Errorf("Unable to determine if upgrade/configure is in progress: %s", err) return } if upgrading { - log.Info("Upgrade/update in progress") + log.Info("Upgrade/configure in progress") return } diff --git a/cmd/vic-machine/upgrade/upgrade.go b/cmd/vic-machine/upgrade/upgrade.go index 4baba5288b..5c28b67941 100644 --- a/cmd/vic-machine/upgrade/upgrade.go +++ b/cmd/vic-machine/upgrade/upgrade.go @@ -66,7 +66,7 @@ func (u *Upgrade) Flags() []cli.Flag { }, cli.BoolFlag{ Name: "resetInProgressFlag", - Usage: "Reset the UpdateInProgress flag", + Usage: "Reset the UpgradeInProgress flag", Destination: &u.ResetInProgressFlag, }, } @@ -154,35 +154,36 @@ func (u *Upgrade) Run(clic *cli.Context) (err error) { log.Infof("VCH ID: %s", vch.Reference().String()) if u.ResetInProgressFlag { - if err = vch.SetVCHUpdateStatus(ctx, "false"); err != nil { - log.Error("Failed to reset UpdateInprogress flag") + if err = vch.SetVCHUpgradeStatus(ctx, false); err != nil { + log.Error("Failed to reset UpgradeInprogress flag") log.Error(err) return errors.New("upgrade failed") } - log.Infof("Reset UpdateInProgress flag successfully") + log.Infof("Reset UpgradeInProgress flag successfully") return nil } - upgrading, err := vch.GetVCHUpdateStatus(ctx) + upgrading, err := vch.VCHUpgradeStatus(ctx) if err != nil { - log.Error("Unable to determine if upgrade/update is in progress") + log.Error("Unable to determine if upgrade/configure is in progress") log.Error(err) return errors.New("upgrade failed") } if upgrading { - log.Error("Upgrade failed: another upgrade/update operation is in progress") + log.Error("Upgrade failed: another upgrade/configure operation is in progress") + log.Error("Use --resetInProgressFlag to reset the VCH upgrade/configure status") return errors.New("upgrade failed") } - if err = vch.SetVCHUpdateStatus(ctx, "true"); err != nil { - log.Error("Failed to set UpdateInProgress flag to true") + if err = vch.SetVCHUpgradeStatus(ctx, true); err != nil { + log.Error("Failed to set UpgradeInProgress flag to true") log.Error(err) return errors.New("upgrade failed") } defer func() { - if err = vch.SetVCHUpdateStatus(ctx, "false"); err != nil { - log.Error("Failed to reset UpdateInProgress") + if err = vch.SetVCHUpgradeStatus(ctx, false); err != nil { + log.Error("Failed to reset UpgradeInProgress") log.Error(err) } }() diff --git a/pkg/vsphere/vm/vm.go b/pkg/vsphere/vm/vm.go index fa0cb1e5b5..d9b2dd5d81 100644 --- a/pkg/vsphere/vm/vm.go +++ b/pkg/vsphere/vm/vm.go @@ -38,7 +38,7 @@ import ( "github.com/vmware/vic/pkg/vsphere/tasks" ) -const UpdateInProgress = "UpdateInProgress" +const UpgradeInProgress = "UpgradeInProgress" type InvalidState struct { r types.ManagedObjectReference @@ -565,32 +565,32 @@ func (vm *VirtualMachine) DatastoreReference(ctx context.Context) ([]types.Manag return mvm.Datastore, nil } -// GetVCHUpdateStatus tells if an upgrade/update has already been started based on the UpdateInProgress flag in ExtraConfig -// If error != nil, VCH upgrade/update will exit; so the value of the VCH upgrade/update status is not important -func (vm *VirtualMachine) GetVCHUpdateStatus(ctx context.Context) (bool, error) { +// VCHUpgradeStatus tells if an upgrade/configure has already been started based on the UpgradeInProgress flag in ExtraConfig +// It returns the error if the vm operation does not succeed +func (vm *VirtualMachine) VCHUpgradeStatus(ctx context.Context) (bool, error) { info, err := vm.FetchExtraConfig(ctx) if err != nil { log.Errorf("Unable to get vm ExtraConfig: %s", err) return false, err } - if v, ok := info[UpdateInProgress]; ok { - updateStatus, err := strconv.ParseBool(v) + if v, ok := info[UpgradeInProgress]; ok { + upgradeStatus, err := strconv.ParseBool(v) if err != nil { - // If error occurs, return true to cancel VCH upgrade. The user can reset the UpdateInProgress flag later. + // If error occurs, the bool return value does not matter for the caller. return true, fmt.Errorf("failed to parse %s to bool: %s", v, err) } - return updateStatus, nil + return upgradeStatus, nil } - // If "UpdateInProgress" is not found, it might be the case that no update/upgrade has been done to this VCH before + // If "UpgradeInProgress" is not found, it might be the case that no upgrade/configure has been done to this VCH before return false, nil } -// SetVCHUpdateStatus sets the "UpdateInProgress" flag in ExtraConfig -func (vm *VirtualMachine) SetVCHUpdateStatus(ctx context.Context, updateStatus string) error { +// SetVCHUpgradeStatus sets the "UpgradeInProgress" flag in ExtraConfig +func (vm *VirtualMachine) SetVCHUpgradeStatus(ctx context.Context, upgradeStatus bool) error { info := make(map[string]string) - info[UpdateInProgress] = updateStatus + info[UpgradeInProgress] = strconv.FormatBool(upgradeStatus) s := &types.VirtualMachineConfigSpec{ ExtraConfig: vmomi.OptionValueFromMap(info), diff --git a/tests/resources/VCH-Util.robot b/tests/resources/VCH-Util.robot index 7f1487c473..76ff2a82bc 100644 --- a/tests/resources/VCH-Util.robot +++ b/tests/resources/VCH-Util.robot @@ -206,6 +206,18 @@ Run VIC Machine Inspect Command ${rc} ${output}= Run Secret VIC Machine Inspect Command %{VCH-NAME} Get Docker Params ${output} ${true} +Inspect VCH + [Arguments] ${expected} + ${rc} ${output}= Run And Return Rc And Output bin/vic-machine-linux inspect --name=%{VCH-NAME} --target=%{TEST_URL} --thumbprint=%{TEST_THUMBPRINT} --user=%{TEST_USERNAME} --password=%{TEST_PASSWORD} --compute-resource=%{TEST_RESOURCE} + Should Be Equal As Integers ${rc} 0 + Should Contain ${output} ${expected} + +Check UpgradeInProgress + [Arguments] ${expected} + ${rc} ${output}= Run And Return Rc And Output govc vm.info -e %{VCH-NAME} | grep UpgradeInProgress + Should Be Equal As Integers ${rc} 0 + Should Contain ${output} ${expected} + Gather Logs From Test Server [Tags] secret Run Keyword And Continue On Failure Run zip %{VCH-NAME}-certs -r %{VCH-NAME} diff --git a/tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpdateInProgress.md b/tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpgradeInProgress.md similarity index 62% rename from tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpdateInProgress.md rename to tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpgradeInProgress.md index c91a445367..8d348346c5 100644 --- a/tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpdateInProgress.md +++ b/tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpgradeInProgress.md @@ -1,4 +1,4 @@ -Test 11-04 - Upgrade UpdateInProgress +Test 11-04 - Upgrade UpgradeInProgress ======= #Purpose: @@ -12,14 +12,14 @@ This test requires that a vSphere server is running and available 2. Deploy VIC 7315 to vsphere server 3. Set UpdateInProgress to true using govc 4. Upgrade VCH -5. Run vic-machine upgrade --resetInProgressFlag to reset UpdateInprogress to false +5. Run vic-machine upgrade --resetInProgressFlag to reset UpgradeInProgress to false 6. Upgrade VCH 7. Run vic-machine inspect to check the upgrade status of the VCH (this should run in parallel with step 6) 8. After step 3 finishes, run step 4 again. #Expected Outcome: -* In step 4, output should contain "Upgrade failed: another upgrade/update operation is in progress" -* In step 5, output should contain "Reset UpdateInProgress flag successfully" +* In step 4, output should contain "Upgrade failed: another upgrade/configure operation is in progress" +* In step 5, output should contain "Reset UpgradeInProgress flag successfully" * In step 6, output should contain "Completed successfully" -* In step 7, output should contain "Upgrade/update in progress" -* In step 8, output should not contain "Upgrade/update in progress" +* In step 7, output should contain "Upgrade/configure in progress" +* In step 8, output should not contain "Upgrade/configure in progress" diff --git a/tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpdateInProgress.robot b/tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpgradeInProgress.robot similarity index 61% rename from tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpdateInProgress.robot rename to tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpgradeInProgress.robot index 7e4b941b37..59a62fb3e3 100644 --- a/tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpdateInProgress.robot +++ b/tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpgradeInProgress.robot @@ -13,37 +13,24 @@ # limitations under the License *** Settings *** -Documentation Test 11-04 - Upgrade-UpdateInProgress +Documentation Test 11-04 - Upgrade-UpgradeInProgress Suite Setup Install VIC with version to Test Server 7315 Suite Teardown Clean up VIC Appliance And Local Binary Resource ../../resources/Util.robot -*** Keywords *** -Inspect VCH - [Arguments] ${expected} - ${rc} ${output}= Run And Return Rc And Output bin/vic-machine-linux inspect --name=%{VCH-NAME} --target=%{TEST_URL} --thumbprint=%{TEST_THUMBPRINT} --user=%{TEST_USERNAME} --password=%{TEST_PASSWORD} --compute-resource=%{TEST_RESOURCE} - Should Be Equal As Integers ${rc} 0 - Should Contain ${output} ${expected} - -Check UpdateInProgress - [Arguments] ${expected} - ${rc} ${output}= Run And Return Rc And Output govc vm.info -e %{VCH-NAME} | grep UpdateInProgress - Should Be Equal As Integers ${rc} 0 - Should Contain ${output} ${expected} - *** Test Cases *** -Upgrade VCH with UpdateInProgress - Run govc vm.change -vm=%{VCH-NAME} -e=UpdateInProgress=true - Check UpdateInProgress true +Upgrade VCH with UpgradeInProgress + Run govc vm.change -vm=%{VCH-NAME} -e=UpgradeInProgress=true + Check UpgradeInProgress true ${rc} ${output}= Run And Return Rc And Output bin/vic-machine-linux upgrade --debug 1 --name=%{VCH-NAME} --target=%{TEST_URL} --user=%{TEST_USERNAME} --password=%{TEST_PASSWORD} --force=true --compute-resource=%{TEST_RESOURCE} --timeout %{TEST_TIMEOUT} - Should Contain ${output} Upgrade failed: another upgrade/update operation is in progress + Should Contain ${output} Upgrade failed: another upgrade/configure operation is in progress ${rc} ${output}= Run And Return Rc And Output bin/vic-machine-linux upgrade --resetInProgressFlag --name=%{VCH-NAME} --target=%{TEST_URL} --user=%{TEST_USERNAME} --password=%{TEST_PASSWORD} --force=true --compute-resource=%{TEST_RESOURCE} --timeout %{TEST_TIMEOUT} - Should Contain ${output} Reset UpdateInProgress flag successfully - Check UpdateInProgress false + Should Contain ${output} Reset UpgradeInProgress flag successfully + Check UpgradeInProgress false Upgrade and inspect VCH Start Process bin/vic-machine-linux upgrade --debug 1 --name %{VCH-NAME} --target %{TEST_URL} --user %{TEST_USERNAME} --password %{TEST_PASSWORD} --force --compute-resource %{TEST_RESOURCE} --timeout %{TEST_TIMEOUT} shell=True alias=UpgradeVCH - Wait Until Keyword Succeeds 20x 5s Inspect VCH Upgrade/update in progress + Wait Until Keyword Succeeds 20x 5s Inspect VCH Upgrade/configure in progress Wait For Process UpgradeVCH Inspect VCH Completed successfully - Check UpdateInProgress false + Check UpgradeInProgress false From bc5305510466516f03fe3fa6e0e1dd7769539fdd Mon Sep 17 00:00:00 2001 From: chengwang86 Date: Mon, 24 Apr 2017 11:20:08 -0700 Subject: [PATCH 09/11] added unit tests --- cmd/vic-machine/inspect/inspect.go | 2 +- cmd/vic-machine/upgrade/upgrade.go | 12 +- pkg/vsphere/vm/vm.go | 14 +-- pkg/vsphere/vm/vm_test.go | 176 +++++++++++++++++++++++++++++ 4 files changed, 190 insertions(+), 14 deletions(-) diff --git a/cmd/vic-machine/inspect/inspect.go b/cmd/vic-machine/inspect/inspect.go index 0443b53863..571ef7fcec 100644 --- a/cmd/vic-machine/inspect/inspect.go +++ b/cmd/vic-machine/inspect/inspect.go @@ -168,7 +168,7 @@ func (i *Inspect) upgradeStatusMessage(ctx context.Context, vch *vm.VirtualMachi return } - upgrading, err := vch.VCHUpgradeStatus(ctx) + upgrading, err := vch.VCHUpdateStatus(ctx) if err != nil { log.Errorf("Unable to determine if upgrade/configure is in progress: %s", err) return diff --git a/cmd/vic-machine/upgrade/upgrade.go b/cmd/vic-machine/upgrade/upgrade.go index 5c28b67941..59401f0138 100644 --- a/cmd/vic-machine/upgrade/upgrade.go +++ b/cmd/vic-machine/upgrade/upgrade.go @@ -66,7 +66,7 @@ func (u *Upgrade) Flags() []cli.Flag { }, cli.BoolFlag{ Name: "resetInProgressFlag", - Usage: "Reset the UpgradeInProgress flag", + Usage: "Reset the UpgradeInProgress flag. Warning: Do not reset this flag if another upgrade/configure process is running", Destination: &u.ResetInProgressFlag, }, } @@ -154,7 +154,7 @@ func (u *Upgrade) Run(clic *cli.Context) (err error) { log.Infof("VCH ID: %s", vch.Reference().String()) if u.ResetInProgressFlag { - if err = vch.SetVCHUpgradeStatus(ctx, false); err != nil { + if err = vch.SetVCHUpdateStatus(ctx, false); err != nil { log.Error("Failed to reset UpgradeInprogress flag") log.Error(err) return errors.New("upgrade failed") @@ -163,7 +163,7 @@ func (u *Upgrade) Run(clic *cli.Context) (err error) { return nil } - upgrading, err := vch.VCHUpgradeStatus(ctx) + upgrading, err := vch.VCHUpdateStatus(ctx) if err != nil { log.Error("Unable to determine if upgrade/configure is in progress") log.Error(err) @@ -171,18 +171,18 @@ func (u *Upgrade) Run(clic *cli.Context) (err error) { } if upgrading { log.Error("Upgrade failed: another upgrade/configure operation is in progress") - log.Error("Use --resetInProgressFlag to reset the VCH upgrade/configure status") + log.Error("If no other upgrade/configure process is running, use --resetInProgressFlag to reset the VCH upgrade/configure status") return errors.New("upgrade failed") } - if err = vch.SetVCHUpgradeStatus(ctx, true); err != nil { + if err = vch.SetVCHUpdateStatus(ctx, true); err != nil { log.Error("Failed to set UpgradeInProgress flag to true") log.Error(err) return errors.New("upgrade failed") } defer func() { - if err = vch.SetVCHUpgradeStatus(ctx, false); err != nil { + if err = vch.SetVCHUpdateStatus(ctx, false); err != nil { log.Error("Failed to reset UpgradeInProgress") log.Error(err) } diff --git a/pkg/vsphere/vm/vm.go b/pkg/vsphere/vm/vm.go index d9b2dd5d81..7ceca3185a 100644 --- a/pkg/vsphere/vm/vm.go +++ b/pkg/vsphere/vm/vm.go @@ -565,9 +565,9 @@ func (vm *VirtualMachine) DatastoreReference(ctx context.Context) ([]types.Manag return mvm.Datastore, nil } -// VCHUpgradeStatus tells if an upgrade/configure has already been started based on the UpgradeInProgress flag in ExtraConfig +// VCHUpdateStatus tells if an upgrade/configure has already been started based on the UpgradeInProgress flag in ExtraConfig // It returns the error if the vm operation does not succeed -func (vm *VirtualMachine) VCHUpgradeStatus(ctx context.Context) (bool, error) { +func (vm *VirtualMachine) VCHUpdateStatus(ctx context.Context) (bool, error) { info, err := vm.FetchExtraConfig(ctx) if err != nil { log.Errorf("Unable to get vm ExtraConfig: %s", err) @@ -575,22 +575,22 @@ func (vm *VirtualMachine) VCHUpgradeStatus(ctx context.Context) (bool, error) { } if v, ok := info[UpgradeInProgress]; ok { - upgradeStatus, err := strconv.ParseBool(v) + updateStatus, err := strconv.ParseBool(v) if err != nil { // If error occurs, the bool return value does not matter for the caller. return true, fmt.Errorf("failed to parse %s to bool: %s", v, err) } - return upgradeStatus, nil + return updateStatus, nil } // If "UpgradeInProgress" is not found, it might be the case that no upgrade/configure has been done to this VCH before return false, nil } -// SetVCHUpgradeStatus sets the "UpgradeInProgress" flag in ExtraConfig -func (vm *VirtualMachine) SetVCHUpgradeStatus(ctx context.Context, upgradeStatus bool) error { +// SetVCHUpdateStatus sets the "UpgradeInProgress" flag in ExtraConfig +func (vm *VirtualMachine) SetVCHUpdateStatus(ctx context.Context, updateStatus bool) error { info := make(map[string]string) - info[UpgradeInProgress] = strconv.FormatBool(upgradeStatus) + info[UpgradeInProgress] = strconv.FormatBool(updateStatus) s := &types.VirtualMachineConfigSpec{ ExtraConfig: vmomi.OptionValueFromMap(info), diff --git a/pkg/vsphere/vm/vm_test.go b/pkg/vsphere/vm/vm_test.go index 37dd7c700d..bedd51f125 100644 --- a/pkg/vsphere/vm/vm_test.go +++ b/pkg/vsphere/vm/vm_test.go @@ -32,6 +32,7 @@ import ( "github.com/vmware/govmomi/vim25/mo" "github.com/vmware/govmomi/vim25/types" "github.com/vmware/vic/lib/guest" + "github.com/vmware/vic/pkg/vsphere/extraconfig/vmomi" "github.com/vmware/vic/pkg/vsphere/session" "github.com/vmware/vic/pkg/vsphere/simulator" "github.com/vmware/vic/pkg/vsphere/sys" @@ -603,3 +604,178 @@ func TestWaitForResult(t *testing.T) { assert.True(t, called == 2, "task should be retried once") assert.True(t, !vmm.IsInvalidState(ctx), "vm state should be fixed") } + +// SetUpdateStatus sets the VCH upgrade/configure status. +func SetUpdateStatus(ctx context.Context, updateStatus string, vm *VirtualMachine) error { + info := make(map[string]string) + info[UpgradeInProgress] = updateStatus + + s := &types.VirtualMachineConfigSpec{ + ExtraConfig: vmomi.OptionValueFromMap(info), + } + + _, err := vm.WaitForResult(ctx, func(ctx context.Context) (tasks.Task, error) { + return vm.Reconfigure(ctx, *s) + }) + if err != nil { + return err + } + + return nil +} + +// TestVCHUpdateStatus tests if VCHUpdateStatus() could obtain the correct VCH upgrade/configure status +func TestVCHUpdateStatus(t *testing.T) { + ctx := context.Background() + + // Nothing VC specific in this test, so we use the simpler ESX model + model := simulator.ESX() + defer model.Remove() + err := model.Create() + if err != nil { + t.Fatal(err) + } + + server := model.Service.NewServer() + defer server.Close() + client, err := govmomi.NewClient(ctx, server.URL, true) + if err != nil { + t.Fatal(err) + } + + // Any VM will do + finder := find.NewFinder(client.Client, false) + vmo, err := finder.VirtualMachine(ctx, "/ha-datacenter/vm/*_VM0") + if err != nil { + t.Fatal(err) + } + + config := &session.Config{ + Service: server.URL.String(), + Insecure: true, + Keepalive: time.Duration(5) * time.Minute, + DatacenterPath: "", + DatastorePath: "/ha-datacenter/datastore/*", + HostPath: "/ha-datacenter/host/*/*", + PoolPath: "/ha-datacenter/host/*/Resources", + } + + s, err := session.NewSession(config).Connect(ctx) + if err != nil { + t.Fatal(err) + } + s.Populate(ctx) + vmm := NewVirtualMachine(ctx, s, vmo.Reference()) + + updateStatus, err := vmm.VCHUpdateStatus(ctx) + if err != nil { + t.Fatalf("ERROR: %s", err) + } + assert.False(t, updateStatus, "updateStatus should be false if UpgradeInProgress is not set in the VCH's ExtraConfig") + + // Set UpgradeInProgress to false + SetUpdateStatus(ctx, "false", vmm) + + updateStatus, err = vmm.VCHUpdateStatus(ctx) + if err != nil { + t.Fatalf("ERROR: %s", err) + } + assert.False(t, updateStatus, "updateStatus should be false since UpgradeInProgress is set to false") + + // Set UpgradeInProgress to true + SetUpdateStatus(ctx, "true", vmm) + + updateStatus, err = vmm.VCHUpdateStatus(ctx) + if err != nil { + t.Fatalf("ERROR: %s", err) + } + assert.True(t, updateStatus, "updateStatus should be true since UpgradeInProgress is set to true") + + // Set UpgradeInProgress to NonBool + SetUpdateStatus(ctx, "NonBool", vmm) + + updateStatus, err = vmm.VCHUpdateStatus(ctx) + if assert.Error(t, err, "An error was expected") { + assert.Contains(t, err.Error(), "failed to parse", "Error msg should contain 'failed to parse' since UpgradeInProgress is set to NonBool") + } +} + +// TestSetVCHUpdateStatus tests if SetVCHUpdateStatus() could set the VCH upgrade/configure status correctly +func TestSetVCHUpdateStatus(t *testing.T) { + ctx := context.Background() + + // Nothing VC specific in this test, so we use the simpler ESX model + model := simulator.ESX() + defer model.Remove() + err := model.Create() + if err != nil { + t.Fatal(err) + } + + server := model.Service.NewServer() + defer server.Close() + client, err := govmomi.NewClient(ctx, server.URL, true) + if err != nil { + t.Fatal(err) + } + + // Any VM will do + finder := find.NewFinder(client.Client, false) + vmo, err := finder.VirtualMachine(ctx, "/ha-datacenter/vm/*_VM0") + if err != nil { + t.Fatal(err) + } + + config := &session.Config{ + Service: server.URL.String(), + Insecure: true, + Keepalive: time.Duration(5) * time.Minute, + DatacenterPath: "", + DatastorePath: "/ha-datacenter/datastore/*", + HostPath: "/ha-datacenter/host/*/*", + PoolPath: "/ha-datacenter/host/*/Resources", + } + + s, err := session.NewSession(config).Connect(ctx) + if err != nil { + t.Fatal(err) + } + s.Populate(ctx) + vmm := NewVirtualMachine(ctx, s, vmo.Reference()) + + // Set UpgradeInProgress to true and then check status + err = vmm.SetVCHUpdateStatus(ctx, true) + if err != nil { + t.Fatalf("ERROR: %s", err) + } + + info, err := vmm.FetchExtraConfig(ctx) + if err != nil { + t.Fatalf("ERROR: %s", err) + } + + v, ok := info[UpgradeInProgress] + if ok { + assert.Equal(t, "true", v, "UpgradeInProgress should be true") + } else { + t.Fatal("ERROR: UpgradeInProgress does not exist in ExtraConfig") + } + + // Set UpgradeInProgress to false and then check status + err = vmm.SetVCHUpdateStatus(ctx, false) + if err != nil { + t.Fatalf("ERROR: %s", err) + } + + info, err = vmm.FetchExtraConfig(ctx) + if err != nil { + t.Fatalf("ERROR: %s", err) + } + + v, ok = info[UpgradeInProgress] + if ok { + assert.Equal(t, "false", v, "UpgradeInProgress should be false") + } else { + t.Fatal("ERROR: UpgradeInProgress does not exist in ExtraConfig") + } +} From ddb52789d90eb1641956e0e21f936258e10eee91 Mon Sep 17 00:00:00 2001 From: chengwang86 Date: Mon, 24 Apr 2017 12:31:36 -0700 Subject: [PATCH 10/11] cosmetic change --- pkg/vsphere/vm/vm.go | 18 +++++++++--------- pkg/vsphere/vm/vm_test.go | 6 +++--- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/pkg/vsphere/vm/vm.go b/pkg/vsphere/vm/vm.go index 7ceca3185a..f0e3d3ee5e 100644 --- a/pkg/vsphere/vm/vm.go +++ b/pkg/vsphere/vm/vm.go @@ -38,7 +38,7 @@ import ( "github.com/vmware/vic/pkg/vsphere/tasks" ) -const UpgradeInProgress = "UpgradeInProgress" +const UpdateStatus = "UpgradeInProgress" type InvalidState struct { r types.ManagedObjectReference @@ -574,23 +574,23 @@ func (vm *VirtualMachine) VCHUpdateStatus(ctx context.Context) (bool, error) { return false, err } - if v, ok := info[UpgradeInProgress]; ok { - updateStatus, err := strconv.ParseBool(v) + if v, ok := info[UpdateStatus]; ok { + status, err := strconv.ParseBool(v) if err != nil { // If error occurs, the bool return value does not matter for the caller. - return true, fmt.Errorf("failed to parse %s to bool: %s", v, err) + return false, fmt.Errorf("failed to parse %s to bool: %s", v, err) } - return updateStatus, nil + return status, nil } - // If "UpgradeInProgress" is not found, it might be the case that no upgrade/configure has been done to this VCH before + // If UpdateStatus is not found, it might be the case that no upgrade/configure has been done to this VCH before return false, nil } -// SetVCHUpdateStatus sets the "UpgradeInProgress" flag in ExtraConfig -func (vm *VirtualMachine) SetVCHUpdateStatus(ctx context.Context, updateStatus bool) error { +// SetVCHUpdateStatus sets the VCH update status in ExtraConfig +func (vm *VirtualMachine) SetVCHUpdateStatus(ctx context.Context, status bool) error { info := make(map[string]string) - info[UpgradeInProgress] = strconv.FormatBool(updateStatus) + info[UpdateStatus] = strconv.FormatBool(status) s := &types.VirtualMachineConfigSpec{ ExtraConfig: vmomi.OptionValueFromMap(info), diff --git a/pkg/vsphere/vm/vm_test.go b/pkg/vsphere/vm/vm_test.go index bedd51f125..5dfd6b51bb 100644 --- a/pkg/vsphere/vm/vm_test.go +++ b/pkg/vsphere/vm/vm_test.go @@ -608,7 +608,7 @@ func TestWaitForResult(t *testing.T) { // SetUpdateStatus sets the VCH upgrade/configure status. func SetUpdateStatus(ctx context.Context, updateStatus string, vm *VirtualMachine) error { info := make(map[string]string) - info[UpgradeInProgress] = updateStatus + info[UpdateStatus] = updateStatus s := &types.VirtualMachineConfigSpec{ ExtraConfig: vmomi.OptionValueFromMap(info), @@ -754,7 +754,7 @@ func TestSetVCHUpdateStatus(t *testing.T) { t.Fatalf("ERROR: %s", err) } - v, ok := info[UpgradeInProgress] + v, ok := info[UpdateStatus] if ok { assert.Equal(t, "true", v, "UpgradeInProgress should be true") } else { @@ -772,7 +772,7 @@ func TestSetVCHUpdateStatus(t *testing.T) { t.Fatalf("ERROR: %s", err) } - v, ok = info[UpgradeInProgress] + v, ok = info[UpdateStatus] if ok { assert.Equal(t, "false", v, "UpgradeInProgress should be false") } else { From 43925adbc5fb79305f20ab2753c8e531c0656cb5 Mon Sep 17 00:00:00 2001 From: chengwang86 Date: Mon, 24 Apr 2017 13:03:17 -0700 Subject: [PATCH 11/11] cosmetic changes --- cmd/vic-machine/list/list.go | 2 +- cmd/vic-machine/upgrade/upgrade.go | 10 +++---- pkg/vsphere/vm/vm.go | 18 ++----------- pkg/vsphere/vm/vm_test.go | 26 +++++++++---------- tests/resources/VCH-Util.robot | 4 +-- ...s.md => 11-04-Upgrade-UpdateInProgress.md} | 6 ++--- ...t => 11-04-Upgrade-UpdateInProgress.robot} | 14 +++++----- 7 files changed, 33 insertions(+), 47 deletions(-) rename tests/test-cases/Group11-Upgrade/{11-04-Upgrade-UpgradeInProgress.md => 11-04-Upgrade-UpdateInProgress.md} (80%) rename tests/test-cases/Group11-Upgrade/{11-04-Upgrade-UpgradeInProgress.robot => 11-04-Upgrade-UpdateInProgress.robot} (84%) diff --git a/cmd/vic-machine/list/list.go b/cmd/vic-machine/list/list.go index 27f6690837..d045572087 100644 --- a/cmd/vic-machine/list/list.go +++ b/cmd/vic-machine/list/list.go @@ -192,7 +192,7 @@ func (l *List) upgradeStatusMessage(ctx context.Context, vch *vm.VirtualMachine, return "Up to date" } - upgrading, _, err := vch.UpgradeInProgress(ctx, management.UpgradePrefix) + upgrading, err := vch.VCHUpdateStatus(ctx) if err != nil { return fmt.Sprintf("Unknown: %s", err) } diff --git a/cmd/vic-machine/upgrade/upgrade.go b/cmd/vic-machine/upgrade/upgrade.go index 59401f0138..d3db8bb993 100644 --- a/cmd/vic-machine/upgrade/upgrade.go +++ b/cmd/vic-machine/upgrade/upgrade.go @@ -66,7 +66,7 @@ func (u *Upgrade) Flags() []cli.Flag { }, cli.BoolFlag{ Name: "resetInProgressFlag", - Usage: "Reset the UpgradeInProgress flag. Warning: Do not reset this flag if another upgrade/configure process is running", + Usage: "Reset the UpdateInProgress flag. Warning: Do not reset this flag if another upgrade/configure process is running", Destination: &u.ResetInProgressFlag, }, } @@ -155,11 +155,11 @@ func (u *Upgrade) Run(clic *cli.Context) (err error) { if u.ResetInProgressFlag { if err = vch.SetVCHUpdateStatus(ctx, false); err != nil { - log.Error("Failed to reset UpgradeInprogress flag") + log.Error("Failed to reset UpdateInProgress flag") log.Error(err) return errors.New("upgrade failed") } - log.Infof("Reset UpgradeInProgress flag successfully") + log.Infof("Reset UpdateInProgress flag successfully") return nil } @@ -176,14 +176,14 @@ func (u *Upgrade) Run(clic *cli.Context) (err error) { } if err = vch.SetVCHUpdateStatus(ctx, true); err != nil { - log.Error("Failed to set UpgradeInProgress flag to true") + log.Error("Failed to set UpdateInProgress flag to true") log.Error(err) return errors.New("upgrade failed") } defer func() { if err = vch.SetVCHUpdateStatus(ctx, false); err != nil { - log.Error("Failed to reset UpgradeInProgress") + log.Error("Failed to reset UpdateInProgress") log.Error(err) } }() diff --git a/pkg/vsphere/vm/vm.go b/pkg/vsphere/vm/vm.go index f0e3d3ee5e..707064f488 100644 --- a/pkg/vsphere/vm/vm.go +++ b/pkg/vsphere/vm/vm.go @@ -38,7 +38,7 @@ import ( "github.com/vmware/vic/pkg/vsphere/tasks" ) -const UpdateStatus = "UpgradeInProgress" +const UpdateStatus = "UpdateInProgress" type InvalidState struct { r types.ManagedObjectReference @@ -363,20 +363,6 @@ func IsUpgradeSnapshot(node *types.VirtualMachineSnapshotTree, upgradePrefix str return node != nil && strings.HasPrefix(node.Name, upgradePrefix) } -// UpgradeInProgress tells if an upgrade has already been started based on snapshot name beginning with upgradePrefix -func (vm *VirtualMachine) UpgradeInProgress(ctx context.Context, upgradePrefix string) (bool, string, error) { - node, err := vm.GetCurrentSnapshotTree(ctx) - if err != nil { - return false, "", fmt.Errorf("Failed to check upgrade snapshot status: %s", err) - } - - if IsUpgradeSnapshot(node, upgradePrefix) { - return true, node.Name, nil - } - - return false, "", nil -} - func (vm *VirtualMachine) registerVM(ctx context.Context, path, name string, vapp, pool, host *types.ManagedObjectReference, vmfolder *object.Folder) (*object.Task, error) { log.Debugf("Register VM %s", name) @@ -565,7 +551,7 @@ func (vm *VirtualMachine) DatastoreReference(ctx context.Context) ([]types.Manag return mvm.Datastore, nil } -// VCHUpdateStatus tells if an upgrade/configure has already been started based on the UpgradeInProgress flag in ExtraConfig +// VCHUpdateStatus tells if an upgrade/configure has already been started based on the UpdateInProgress flag in ExtraConfig // It returns the error if the vm operation does not succeed func (vm *VirtualMachine) VCHUpdateStatus(ctx context.Context) (bool, error) { info, err := vm.FetchExtraConfig(ctx) diff --git a/pkg/vsphere/vm/vm_test.go b/pkg/vsphere/vm/vm_test.go index 5dfd6b51bb..f1dfcfe014 100644 --- a/pkg/vsphere/vm/vm_test.go +++ b/pkg/vsphere/vm/vm_test.go @@ -671,32 +671,32 @@ func TestVCHUpdateStatus(t *testing.T) { if err != nil { t.Fatalf("ERROR: %s", err) } - assert.False(t, updateStatus, "updateStatus should be false if UpgradeInProgress is not set in the VCH's ExtraConfig") + assert.False(t, updateStatus, "updateStatus should be false if UpdateInProgress is not set in the VCH's ExtraConfig") - // Set UpgradeInProgress to false + // Set UpdateInProgress to false SetUpdateStatus(ctx, "false", vmm) updateStatus, err = vmm.VCHUpdateStatus(ctx) if err != nil { t.Fatalf("ERROR: %s", err) } - assert.False(t, updateStatus, "updateStatus should be false since UpgradeInProgress is set to false") + assert.False(t, updateStatus, "updateStatus should be false since UpdateInProgress is set to false") - // Set UpgradeInProgress to true + // Set UpdateInProgress to true SetUpdateStatus(ctx, "true", vmm) updateStatus, err = vmm.VCHUpdateStatus(ctx) if err != nil { t.Fatalf("ERROR: %s", err) } - assert.True(t, updateStatus, "updateStatus should be true since UpgradeInProgress is set to true") + assert.True(t, updateStatus, "updateStatus should be true since UpdateInProgress is set to true") - // Set UpgradeInProgress to NonBool + // Set UpdateInProgress to NonBool SetUpdateStatus(ctx, "NonBool", vmm) updateStatus, err = vmm.VCHUpdateStatus(ctx) if assert.Error(t, err, "An error was expected") { - assert.Contains(t, err.Error(), "failed to parse", "Error msg should contain 'failed to parse' since UpgradeInProgress is set to NonBool") + assert.Contains(t, err.Error(), "failed to parse", "Error msg should contain 'failed to parse' since UpdateInProgress is set to NonBool") } } @@ -743,7 +743,7 @@ func TestSetVCHUpdateStatus(t *testing.T) { s.Populate(ctx) vmm := NewVirtualMachine(ctx, s, vmo.Reference()) - // Set UpgradeInProgress to true and then check status + // Set UpdateInProgress to true and then check status err = vmm.SetVCHUpdateStatus(ctx, true) if err != nil { t.Fatalf("ERROR: %s", err) @@ -756,12 +756,12 @@ func TestSetVCHUpdateStatus(t *testing.T) { v, ok := info[UpdateStatus] if ok { - assert.Equal(t, "true", v, "UpgradeInProgress should be true") + assert.Equal(t, "true", v, "UpdateInProgress should be true") } else { - t.Fatal("ERROR: UpgradeInProgress does not exist in ExtraConfig") + t.Fatal("ERROR: UpdateInProgress does not exist in ExtraConfig") } - // Set UpgradeInProgress to false and then check status + // Set UpdateInProgress to false and then check status err = vmm.SetVCHUpdateStatus(ctx, false) if err != nil { t.Fatalf("ERROR: %s", err) @@ -774,8 +774,8 @@ func TestSetVCHUpdateStatus(t *testing.T) { v, ok = info[UpdateStatus] if ok { - assert.Equal(t, "false", v, "UpgradeInProgress should be false") + assert.Equal(t, "false", v, "UpdateInProgress should be false") } else { - t.Fatal("ERROR: UpgradeInProgress does not exist in ExtraConfig") + t.Fatal("ERROR: UpdateInProgress does not exist in ExtraConfig") } } diff --git a/tests/resources/VCH-Util.robot b/tests/resources/VCH-Util.robot index 76ff2a82bc..eb2bc9625b 100644 --- a/tests/resources/VCH-Util.robot +++ b/tests/resources/VCH-Util.robot @@ -212,9 +212,9 @@ Inspect VCH Should Be Equal As Integers ${rc} 0 Should Contain ${output} ${expected} -Check UpgradeInProgress +Check UpdateInProgress [Arguments] ${expected} - ${rc} ${output}= Run And Return Rc And Output govc vm.info -e %{VCH-NAME} | grep UpgradeInProgress + ${rc} ${output}= Run And Return Rc And Output govc vm.info -e %{VCH-NAME} | grep UpdateInProgress Should Be Equal As Integers ${rc} 0 Should Contain ${output} ${expected} diff --git a/tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpgradeInProgress.md b/tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpdateInProgress.md similarity index 80% rename from tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpgradeInProgress.md rename to tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpdateInProgress.md index 8d348346c5..62e591e243 100644 --- a/tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpgradeInProgress.md +++ b/tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpdateInProgress.md @@ -1,4 +1,4 @@ -Test 11-04 - Upgrade UpgradeInProgress +Test 11-04 - Upgrade UpdateInProgress ======= #Purpose: @@ -12,14 +12,14 @@ This test requires that a vSphere server is running and available 2. Deploy VIC 7315 to vsphere server 3. Set UpdateInProgress to true using govc 4. Upgrade VCH -5. Run vic-machine upgrade --resetInProgressFlag to reset UpgradeInProgress to false +5. Run vic-machine upgrade --resetInProgressFlag to reset UpdateInProgress to false 6. Upgrade VCH 7. Run vic-machine inspect to check the upgrade status of the VCH (this should run in parallel with step 6) 8. After step 3 finishes, run step 4 again. #Expected Outcome: * In step 4, output should contain "Upgrade failed: another upgrade/configure operation is in progress" -* In step 5, output should contain "Reset UpgradeInProgress flag successfully" +* In step 5, output should contain "Reset UpdateInProgress flag successfully" * In step 6, output should contain "Completed successfully" * In step 7, output should contain "Upgrade/configure in progress" * In step 8, output should not contain "Upgrade/configure in progress" diff --git a/tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpgradeInProgress.robot b/tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpdateInProgress.robot similarity index 84% rename from tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpgradeInProgress.robot rename to tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpdateInProgress.robot index 59a62fb3e3..367328a2bf 100644 --- a/tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpgradeInProgress.robot +++ b/tests/test-cases/Group11-Upgrade/11-04-Upgrade-UpdateInProgress.robot @@ -13,24 +13,24 @@ # limitations under the License *** Settings *** -Documentation Test 11-04 - Upgrade-UpgradeInProgress +Documentation Test 11-04 - Upgrade-UpdateInProgress Suite Setup Install VIC with version to Test Server 7315 Suite Teardown Clean up VIC Appliance And Local Binary Resource ../../resources/Util.robot *** Test Cases *** -Upgrade VCH with UpgradeInProgress - Run govc vm.change -vm=%{VCH-NAME} -e=UpgradeInProgress=true - Check UpgradeInProgress true +Upgrade VCH with UpdateInProgress + Run govc vm.change -vm=%{VCH-NAME} -e=UpdateInProgress=true + Check UpdateInProgress true ${rc} ${output}= Run And Return Rc And Output bin/vic-machine-linux upgrade --debug 1 --name=%{VCH-NAME} --target=%{TEST_URL} --user=%{TEST_USERNAME} --password=%{TEST_PASSWORD} --force=true --compute-resource=%{TEST_RESOURCE} --timeout %{TEST_TIMEOUT} Should Contain ${output} Upgrade failed: another upgrade/configure operation is in progress ${rc} ${output}= Run And Return Rc And Output bin/vic-machine-linux upgrade --resetInProgressFlag --name=%{VCH-NAME} --target=%{TEST_URL} --user=%{TEST_USERNAME} --password=%{TEST_PASSWORD} --force=true --compute-resource=%{TEST_RESOURCE} --timeout %{TEST_TIMEOUT} - Should Contain ${output} Reset UpgradeInProgress flag successfully - Check UpgradeInProgress false + Should Contain ${output} Reset UpdateInProgress flag successfully + Check UpdateInProgress false Upgrade and inspect VCH Start Process bin/vic-machine-linux upgrade --debug 1 --name %{VCH-NAME} --target %{TEST_URL} --user %{TEST_USERNAME} --password %{TEST_PASSWORD} --force --compute-resource %{TEST_RESOURCE} --timeout %{TEST_TIMEOUT} shell=True alias=UpgradeVCH Wait Until Keyword Succeeds 20x 5s Inspect VCH Upgrade/configure in progress Wait For Process UpgradeVCH Inspect VCH Completed successfully - Check UpgradeInProgress false + Check UpdateInProgress false