Skip to content

Commit

Permalink
Fail windows update when installed version does not match
Browse files Browse the repository at this point in the history
  • Loading branch information
VishnuKarthikRavindran authored and Aperocky committed Oct 21, 2024
1 parent 8b5d4e3 commit e3d30d0
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 65 deletions.
9 changes: 6 additions & 3 deletions agent/update/processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,11 +578,14 @@ func verifyInstallation(mgr *updateManager, log log.T, updateDetail *UpdateDetai
}

if !isRollback {
// reset the sub status as it is used in the succeeded function now
mgr.subStatus = ""
// initiate rollback when agent installation is not complete
if versionInstalledErrCode := mgr.util.VerifyInstalledVersion(log, version); versionInstalledErrCode != "" {
mgr.subStatus = string(versionInstalledErrCode)
message := updateutil.BuildMessage(nil,
"failed to identify installed target agent version: %v",
updateDetail.TargetVersion)
updateDetail.AppendError(log, message)
// we will receive only 1 error code - ErrorInstTargetVersionNotFoundViaReg
return mgr.failed(updateDetail, log, versionInstalledErrCode, message, false)
}
log.Infof("%v is running", updateDetail.PackageName)
return mgr.succeeded(updateDetail, log)
Expand Down
37 changes: 11 additions & 26 deletions agent/update/processor/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1684,30 +1684,11 @@ func TestVerifyInstallation_VersionCheck_Success(t *testing.T) {
return nil
}

// action
err := verifyInstallation(updater.mgr, logger, updateDetail, false)

// assert
assert.NoError(t, err)
assert.False(t, isRollbackCalled)
assert.Equal(t, updateDetail.State, Completed)
assert.Equal(t, updateDetail.Result, contracts.ResultStatusSuccess)
assert.Equal(t, updater.mgr.subStatus, "")
}

func TestVerifyInstallation_VersionCheck_Failed_WMI(t *testing.T) {
// setup
var logger = logmocks.NewMockLog()
control := &stubControl{serviceIsRunning: true, versionErrorCode: updateconstants.ErrorInstTargetVersionNotFoundViaWMIC}
updater := createUpdaterStubs(control)
updateDetail := createUpdateDetail(Installed)
isRollbackCalled := false

updater.mgr.rollback = func(mgr *updateManager, log log.T, updateDetail *UpdateDetail) (err error) {
isRollbackCalled = true
finalize_error_code := ""
updater.mgr.finalize = func(mgr *updateManager, updateDetail *UpdateDetail, errorCode string) (err error) {
finalize_error_code = errorCode
return nil
}

// action
err := verifyInstallation(updater.mgr, logger, updateDetail, false)

Expand All @@ -1716,7 +1697,7 @@ func TestVerifyInstallation_VersionCheck_Failed_WMI(t *testing.T) {
assert.False(t, isRollbackCalled)
assert.Equal(t, updateDetail.State, Completed)
assert.Equal(t, updateDetail.Result, contracts.ResultStatusSuccess)
assert.Equal(t, updater.mgr.subStatus, string(updateconstants.ErrorInstTargetVersionNotFoundViaWMIC))
assert.Equal(t, string(""), finalize_error_code)
}

func TestVerifyInstallation_VersionCheck_Failed_Reg(t *testing.T) {
Expand All @@ -1731,16 +1712,20 @@ func TestVerifyInstallation_VersionCheck_Failed_Reg(t *testing.T) {
isRollbackCalled = true
return nil
}

finalize_error_code := ""
updater.mgr.finalize = func(mgr *updateManager, updateDetail *UpdateDetail, errorCode string) (err error) {
finalize_error_code = errorCode
return nil
}
// action
err := verifyInstallation(updater.mgr, logger, updateDetail, false)

// assert
assert.NoError(t, err)
assert.False(t, isRollbackCalled)
assert.Equal(t, updateDetail.State, Completed)
assert.Equal(t, updateDetail.Result, contracts.ResultStatusSuccess)
assert.Equal(t, updater.mgr.subStatus, string(updateconstants.ErrorInstTargetVersionNotFoundViaReg))
assert.Equal(t, updateDetail.Result, contracts.ResultStatusFailed)
assert.Equal(t, string(updateconstants.ErrorInstTargetVersionNotFoundViaReg), finalize_error_code)
}

func TestVerifyInstallationCannotStartAgent(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions agent/update/processor/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ func (u *updateManager) succeeded(updateDetail *UpdateDetail, log logPkg.T) (err
log.WriteEvent(
logger.AgentUpdateResultMessage,
updateDetail.SourceVersion,
PrepareHealthStatus(updateDetail, u.subStatus, updateDetail.TargetVersion))
return u.finalize(u, updateDetail, u.subStatus)
PrepareHealthStatus(updateDetail, "", updateDetail.TargetVersion))
return u.finalize(u, updateDetail, "")
}

// failed sets update to failed with error messages
Expand Down
1 change: 0 additions & 1 deletion agent/update/processor/progress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

// Package processor contains the methods for update ssm agent.
// It also provides methods for sendReply and updateInstanceInfo

//go:build e2e
// +build e2e

Expand Down
5 changes: 1 addition & 4 deletions agent/updateutil/updateconstants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,7 @@ const (
ErrorCannotStartService ErrorCode = "ErrorCannotStartService"

// ErrorInstTargetVersionNotFoundViaReg represents that the target agent version could not be found using Registry
ErrorInstTargetVersionNotFoundViaReg ErrorCode = "InstTgtVerNotFoundViaReg"

// ErrorInstTargetVersionNotFoundViaWMIC represents that the target agent version could not be found using WMIC
ErrorInstTargetVersionNotFoundViaWMIC ErrorCode = "InstTgtVerNotFoundViaWMIC"
ErrorInstTargetVersionNotFoundViaReg ErrorCode = "ErrorInstTargetVersionNotFoundViaReg"

// ErrorCannotStopService represents Cannot stop Ec2Config service
ErrorCannotStopService ErrorCode = "ErrorCannotStopService"
Expand Down
8 changes: 1 addition & 7 deletions agent/updateutil/updateutil_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ var (
deleteFile = fileutil.DeleteFile
fileWrite = fileutil.WriteIntoFileWithPermissions
getVersionThroughRegistryKeyRef = getVersionThroughRegistryKey
getVersionThroughWMIRef = getVersionThroughWMI
)

type UpdatePluginRunState struct {
Expand Down Expand Up @@ -339,12 +338,7 @@ func verifyVersion(log log.T, targetVersion string) updateconstants.ErrorCode {
log.Infof("Verifying Agent version using Registry")
registryCurrentAgentVersion := getVersionThroughRegistryKeyRef(log)
if targetVersion == registryCurrentAgentVersion {
log.Infof("Verifying Agent version using WMI query")
wmiCurrentAgentVersion := getVersionThroughWMIRef(log)
if targetVersion == wmiCurrentAgentVersion {
return "" // return blank when success
}
return updateconstants.ErrorInstTargetVersionNotFoundViaWMIC
return "" // success code
}
return updateconstants.ErrorInstTargetVersionNotFoundViaReg
}
Expand Down
22 changes: 0 additions & 22 deletions agent/updateutil/updateutil_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,35 +127,16 @@ func TestVerifyVersion_Success(t *testing.T) {
getVersionThroughRegistryKeyRef = func(log logPkg.T) string {
return expectedVersion
}
getVersionThroughWMIRef = func(log logPkg.T) string {
return expectedVersion
}
logMock := log.NewMockLog()
errCode := verifyVersion(logMock, expectedVersion)
assert.Equal(t, updateconstants.ErrorCode(""), errCode)
}

func TestVerifyVersion_Failed_WMI(t *testing.T) {
expectedVersion := "3.2.0.0"
getVersionThroughRegistryKeyRef = func(log logPkg.T) string {
return expectedVersion
}
getVersionThroughWMIRef = func(log logPkg.T) string {
return ""
}
logMock := log.NewMockLog()
errCode := verifyVersion(logMock, expectedVersion)
assert.Equal(t, updateconstants.ErrorInstTargetVersionNotFoundViaWMIC, errCode)
}

func TestVerifyVersion_Failed_Registry(t *testing.T) {
expectedVersion := "3.2.0.0"
getVersionThroughRegistryKeyRef = func(log logPkg.T) string {
return ""
}
getVersionThroughWMIRef = func(log logPkg.T) string {
return expectedVersion
}
logMock := log.NewMockLog()
errCode := verifyVersion(logMock, expectedVersion)
assert.Equal(t, updateconstants.ErrorInstTargetVersionNotFoundViaReg, errCode)
Expand All @@ -165,9 +146,6 @@ func TestVerifyVersion_Failed_Both(t *testing.T) {
getVersionThroughRegistryKeyRef = func(log logPkg.T) string {
return ""
}
getVersionThroughWMIRef = func(log logPkg.T) string {
return ""
}
logMock := log.NewMockLog()
expectedVersion := "3.2.0.0"
errCode := verifyVersion(logMock, expectedVersion)
Expand Down

0 comments on commit e3d30d0

Please sign in to comment.