Skip to content

Commit 135418b

Browse files
Fix integration test failure from result formatting and remove a stray instance of results truncation in plugin
cr https://cr.amazon.com/r/6458012/
1 parent 9d1767d commit 135418b

File tree

6 files changed

+70
-40
lines changed

6 files changed

+70
-40
lines changed

agent/contracts/plugin.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,8 @@ func (out *PluginOutput) MarkAsSuccessWithReboot() {
136136
}
137137

138138
// AppendInfo adds info to PluginOutput StandardOut.
139-
func (out *PluginOutput) AppendInfo(log log.T, format string, params ...interface{}) {
140-
if len(format) > 0 {
141-
message := fmt.Sprintf(format, params...)
139+
func (out *PluginOutput) AppendInfo(log log.T, message string) {
140+
if len(message) > 0 {
142141
log.Info(message)
143142
if len(out.Stdout) > 0 {
144143
out.Stdout = fmt.Sprintf("%v\n%v", out.Stdout, message)
@@ -148,10 +147,17 @@ func (out *PluginOutput) AppendInfo(log log.T, format string, params ...interfac
148147
}
149148
}
150149

151-
// AppendError adds errors to PluginOutput StandardErr.
152-
func (out *PluginOutput) AppendError(log log.T, format string, params ...interface{}) {
150+
// AppendInfof adds info to PluginOutput StandardOut with formatting parameters.
151+
func (out *PluginOutput) AppendInfof(log log.T, format string, params ...interface{}) {
153152
if len(format) > 0 {
154153
message := fmt.Sprintf(format, params...)
154+
out.AppendInfo(log, message)
155+
}
156+
}
157+
158+
// AppendError adds errors to PluginOutput StandardErr.
159+
func (out *PluginOutput) AppendError(log log.T, message string) {
160+
if len(message) > 0 {
155161
log.Error(message)
156162
if len(out.Stderr) > 0 {
157163
out.Stderr = fmt.Sprintf("%v\n%v", out.Stderr, message)
@@ -161,6 +167,14 @@ func (out *PluginOutput) AppendError(log log.T, format string, params ...interfa
161167
}
162168
}
163169

170+
// AppendErrorf adds errors to PluginOutput StandardErr with formatting parameters.
171+
func (out *PluginOutput) AppendErrorf(log log.T, format string, params ...interface{}) {
172+
if len(format) > 0 {
173+
message := fmt.Sprintf(format, params...)
174+
out.AppendError(log, message)
175+
}
176+
}
177+
164178
// TruncateOutput truncates the output
165179
func TruncateOutput(stdout string, stderr string, capacity int) (response string) {
166180
outputSize := len(stdout)

agent/contracts/plugin_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,30 @@ func TestAppendInfo(t *testing.T) {
100100
assert.Contains(t, output.Stdout, "Info message")
101101
assert.Contains(t, output.Stdout, "Second entry")
102102
}
103+
104+
func TestAppendSpecialChars(t *testing.T) {
105+
output := PluginOutput{}
106+
107+
var testString = "%v`~!@#$%^&*()-_=+[{]}|\\;:'\",<.>/?"
108+
output.AppendInfo(logger, testString)
109+
output.AppendError(logger, testString)
110+
111+
assert.Contains(t, output.Stdout, testString)
112+
assert.Contains(t, output.Stderr, testString)
113+
}
114+
115+
func TestAppendFormat(t *testing.T) {
116+
output := PluginOutput{}
117+
118+
var testString = "%v`~!@#$%^&*()-_=+[{]}|\\;:'\",<.>/?%%"
119+
120+
// The first % is a %v - a variable to be replaced and we provided a value for it.
121+
// The second % isn't escaped and is treated as a fmt parameter, but no value is provided for it.
122+
// The double %% is an escaped single literal %.
123+
var testStringFormatted = "foo`~!@#$%!^(MISSING)&*()-_=+[{]}|\\;:'\",<.>/?%"
124+
output.AppendInfof(logger, testString, "foo")
125+
output.AppendErrorf(logger, testString, "foo")
126+
127+
assert.Contains(t, output.Stdout, testStringFormatted)
128+
assert.Contains(t, output.Stderr, testStringFormatted)
129+
}

agent/plugins/configurepackage/configurepackage.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func runConfigurePackage(
156156
// if already installed, exit
157157
if version == installedVersion {
158158
// TODO:MF: validate that installed version is basically valid - has manifest and at least one other non-etag file or folder?
159-
output.AppendInfo(log, "%v %v is already installed", input.Name, version)
159+
output.AppendInfof(log, "%v %v is already installed", input.Name, version)
160160
output.MarkAsSucceeded()
161161
return
162162
}
@@ -182,14 +182,14 @@ func runConfigurePackage(
182182
// even though that may or may not be the package that installed it - it is our only decent option
183183
_, ensureErr := manager.ensurePackage(context, configUtil, input.Name, installedVersion, &output)
184184
if ensureErr != nil {
185-
output.AppendError(log, "unable to obtain package: %v", ensureErr)
185+
output.AppendErrorf(log, "unable to obtain package: %v", ensureErr)
186186
} else {
187187
result, err := manager.runUninstallPackagePre(context,
188188
input.Name,
189189
installedVersion,
190190
&output)
191191
if err != nil {
192-
output.AppendError(log, "failed to uninstall currently installed version of package: %v", err)
192+
output.AppendErrorf(log, "failed to uninstall currently installed version of package: %v", err)
193193
} else {
194194
if result == contracts.ResultStatusSuccessAndReboot || result == contracts.ResultStatusPassedAndReboot {
195195
// Reboot before continuing
@@ -211,12 +211,12 @@ func runConfigurePackage(
211211
if err != nil {
212212
output.MarkAsFailed(log, fmt.Errorf("failed to install package: %v", err))
213213
} else if result == contracts.ResultStatusSuccessAndReboot || result == contracts.ResultStatusPassedAndReboot {
214-
output.AppendInfo(log, "Successfully installed %v %v", input.Name, version)
214+
output.AppendInfof(log, "Successfully installed %v %v", input.Name, version)
215215
output.MarkAsSuccessWithReboot()
216216
} else if result != contracts.ResultStatusSuccess {
217217
output.MarkAsFailed(log, fmt.Errorf("install action state was %v and not %v", result, contracts.ResultStatusSuccess))
218218
} else {
219-
output.AppendInfo(log, "Successfully installed %v %v", input.Name, version)
219+
output.AppendInfof(log, "Successfully installed %v %v", input.Name, version)
220220
output.MarkAsSucceeded()
221221
}
222222

@@ -227,7 +227,7 @@ func runConfigurePackage(
227227
installedVersion,
228228
&output)
229229
if err != nil {
230-
output.AppendError(log, "failed to clean up currently installed version of package: %v", err)
230+
output.AppendErrorf(log, "failed to clean up currently installed version of package: %v", err)
231231
}
232232
}
233233

@@ -265,12 +265,12 @@ func runConfigurePackage(
265265

266266
result := contracts.MergeResultStatus(resultPre, resultPost)
267267
if result == contracts.ResultStatusSuccessAndReboot || result == contracts.ResultStatusPassedAndReboot {
268-
output.AppendInfo(log, "Successfully uninstalled %v %v", input.Name, version)
268+
output.AppendInfof(log, "Successfully uninstalled %v %v", input.Name, version)
269269
output.MarkAsSuccessWithReboot()
270270
} else if result != contracts.ResultStatusSuccess {
271271
output.MarkAsFailed(log, fmt.Errorf("uninstall action state was %v and not %v", result, contracts.ResultStatusSuccess))
272272
} else {
273-
output.AppendInfo(log, "Successfully uninstalled %v %v", input.Name, version)
273+
output.AppendInfof(log, "Successfully uninstalled %v %v", input.Name, version)
274274
output.MarkAsSucceeded()
275275
}
276276
default:
@@ -444,7 +444,7 @@ func (m *configurePackage) downloadPackage(context context.T,
444444
return "", errors.New(errMessage)
445445
}
446446

447-
output.AppendInfo(log, "Successfully downloaded %v", downloadInput.SourceURL)
447+
output.AppendInfof(log, "Successfully downloaded %v", downloadInput.SourceURL)
448448

449449
return downloadOutput.LocalFilePath, nil
450450
}
@@ -502,7 +502,7 @@ func (m *configurePackage) executeAction(context context.T,
502502

503503
log := context.Log()
504504
if actionExists {
505-
output.AppendInfo(log, "Initiating %v %v %v", packageName, version, actionName)
505+
output.AppendInfof(log, "Initiating %v %v %v", packageName, version, actionName)
506506
file, err := filesysdep.ReadFile(fileLocation)
507507
if err != nil {
508508
return true, contracts.ResultStatusFailed, err
@@ -521,10 +521,10 @@ func (m *configurePackage) executeAction(context context.T,
521521
for _, pluginOut := range pluginOutputs {
522522
log.Debugf("Plugin %v ResultStatus %v", pluginOut.PluginName, pluginOut.Status)
523523
if pluginOut.StandardOutput != "" {
524-
output.AppendInfo(log, "%v output: %v", actionName, pluginOut.StandardOutput)
524+
output.AppendInfof(log, "%v output: %v", actionName, pluginOut.StandardOutput)
525525
}
526526
if pluginOut.StandardError != "" {
527-
output.AppendInfo(log, "%v errors: %v", actionName, pluginOut.StandardError)
527+
output.AppendInfof(log, "%v errors: %v", actionName, pluginOut.StandardError)
528528
}
529529
if pluginOut.Error != nil {
530530
output.MarkAsFailed(log, pluginOut.Error)
@@ -613,12 +613,12 @@ func (p *Plugin) Execute(context context.T, config contracts.Configuration, canc
613613
} else {
614614
if err := filesysdep.WriteFile(outFile, out[0].Stdout); err != nil {
615615
log.Debugf("Error writing to %v", outFile)
616-
out[0].AppendError(log, "Error saving stdout: %v", err.Error())
616+
out[0].AppendErrorf(log, "Error saving stdout: %v", err.Error())
617617
}
618618
errFile := filepath.Join(config.OrchestrationDirectory, p.StderrFileName)
619619
if err := filesysdep.WriteFile(errFile, out[0].Stderr); err != nil {
620620
log.Debugf("Error writing to %v", errFile)
621-
out[0].AppendError(log, "Error saving stderr: %v", err.Error())
621+
out[0].AppendErrorf(log, "Error saving stderr: %v", err.Error())
622622
}
623623
}
624624
uploadErrs := p.UploadOutputToS3Bucket(log,

agent/plugins/refreshassociation/refreshassociation.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package refreshassociation
1616

1717
import (
18-
"bytes"
1918
"fmt"
2019
"os"
2120
"path/filepath"
@@ -161,16 +160,6 @@ func (p *Plugin) runCommandsRawInput(log log.T, rawPluginInput interface{}, orch
161160
log.Error(err)
162161
}
163162

164-
// read (a prefix of) the standard output/error
165-
out.Stdout, err = pluginutil.ReadPrefix(bytes.NewBufferString(out.Stdout), p.MaxStdoutLength, p.OutputTruncatedSuffix)
166-
if err != nil {
167-
log.Error(err)
168-
}
169-
out.Stderr, err = pluginutil.ReadPrefix(bytes.NewBufferString(out.Stderr), p.MaxStderrLength, p.OutputTruncatedSuffix)
170-
if err != nil {
171-
log.Error(err)
172-
}
173-
174163
// Upload output to S3
175164
uploadOutputToS3BucketErrors := p.ExecuteUploadOutputToS3Bucket(log, pluginInput.ID, orchestrationDirectory, outputS3BucketName, outputS3KeyPrefix, false, "", out.Stdout, out.Stderr)
176165
if len(uploadOutputToS3BucketErrors) > 0 {
@@ -280,7 +269,7 @@ func (p *Plugin) runCommands(log log.T, pluginInput RefreshAssociationPluginInpu
280269
if applyAll {
281270
out.AppendInfo(log, "All associations have been requested to execute immediately")
282271
} else {
283-
out.AppendInfo(log, "Associations %v have been requested to execute immediately", pluginInput.AssociationIds)
272+
out.AppendInfof(log, "Associations %v have been requested to execute immediately", pluginInput.AssociationIds)
284273
}
285274

286275
signal.ExecuteAssociation(log)

agent/plugins/updateec2config/updateec2config.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ func runUpdateAgent(
222222
return
223223
}
224224

225-
out.AppendInfo(log, "Updating %v from %v to %v",
225+
out.AppendInfof(log, "Updating %v from %v to %v",
226226
pluginInput.AgentName,
227227
agentVersion,
228228
targetVersion)
@@ -427,7 +427,7 @@ func (m *updateManager) downloadManifest(log log.T,
427427
downloadOutput.LocalFilePath == "" {
428428
return nil, downloadErr
429429
}
430-
out.AppendInfo(log, "Successfully downloaded %v", downloadInput.SourceURL)
430+
out.AppendInfof(log, "Successfully downloaded %v", downloadInput.SourceURL)
431431
return ParseManifest(log, downloadOutput.LocalFilePath)
432432
}
433433

@@ -470,15 +470,15 @@ func (m *updateManager) downloadUpdater(log log.T,
470470
return version, errors.New(errMessage)
471471
}
472472

473-
out.AppendInfo(log, "Successfully downloaded %v", downloadInput.SourceURL)
473+
out.AppendInfof(log, "Successfully downloaded %v", downloadInput.SourceURL)
474474
if uncompressErr := fileUncompress(
475475
downloadOutput.LocalFilePath,
476476
updateutil.UpdateArtifactFolder(appconfig.EC2UpdateArtifactsRoot, updaterPackageName, version)); uncompressErr != nil {
477477
return version, fmt.Errorf("failed to uncompress updater package, %v, %v",
478478
downloadOutput.LocalFilePath,
479479
uncompressErr.Error())
480480
}
481-
out.AppendInfo(log, "Successfully downloaded %v", downloadInput.SourceURL)
481+
out.AppendInfof(log, "Successfully downloaded %v", downloadInput.SourceURL)
482482

483483
return version, nil
484484
}
@@ -502,7 +502,7 @@ func (m *updateManager) validateUpdate(log log.T,
502502
}
503503

504504
if pluginInput.TargetVersion == currentVersion {
505-
out.AppendInfo(log, "%v %v has already been installed, update skipped",
505+
out.AppendInfof(log, "%v %v has already been installed, update skipped",
506506
pluginInput.AgentName,
507507
currentVersion)
508508
out.MarkAsSucceeded()

agent/plugins/updatessmagent/updateagent.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func runUpdateAgent(
176176
if len(targetVersion) == 0 {
177177
targetVersion = "latest"
178178
}
179-
out.AppendInfo(log, "Updating %v from %v to %v",
179+
out.AppendInfof(log, "Updating %v from %v to %v",
180180
pluginInput.AgentName,
181181
version.Version,
182182
targetVersion)
@@ -331,7 +331,7 @@ func (m *updateManager) downloadManifest(log log.T,
331331
downloadOutput.LocalFilePath == "" {
332332
return nil, downloadErr
333333
}
334-
out.AppendInfo(log, "Successfully downloaded %v", downloadInput.SourceURL)
334+
out.AppendInfof(log, "Successfully downloaded %v", downloadInput.SourceURL)
335335
return ParseManifest(log, downloadOutput.LocalFilePath, context, pluginInput.AgentName)
336336
}
337337

@@ -373,7 +373,7 @@ func (m *updateManager) downloadUpdater(log log.T,
373373
}
374374
return version, errors.New(errMessage)
375375
}
376-
out.AppendInfo(log, "Successfully downloaded %v", downloadInput.SourceURL)
376+
out.AppendInfof(log, "Successfully downloaded %v", downloadInput.SourceURL)
377377
if uncompressErr := fileUncompress(
378378
downloadOutput.LocalFilePath,
379379
updateutil.UpdateArtifactFolder(appconfig.UpdaterArtifactsRoot, updaterPackageName, version)); uncompressErr != nil {
@@ -404,7 +404,7 @@ func (m *updateManager) validateUpdate(log log.T,
404404
}
405405

406406
if pluginInput.TargetVersion == currentVersion {
407-
out.AppendInfo(log, "%v %v has already been installed, update skipped",
407+
out.AppendInfof(log, "%v %v has already been installed, update skipped",
408408
pluginInput.AgentName,
409409
currentVersion)
410410
out.MarkAsSucceeded()

0 commit comments

Comments
 (0)