//TestParseManifest testing parses valid manfiest files func TestParseManifest(t *testing.T) { // generate test cases var testCases []testCase for _, manifestFile := range sampleManifests { testCases = append(testCases, testCase{ Input: string(manifestFile), Output: loadManifestFromFile(t, manifestFile), }) } agentName := "amazon-ssm-agent" log := log.NewMockLog() context := mockInstanceContext() // run tests for _, tst := range testCases { // call method parsedMsg, err := ParseManifest(log, tst.Input, context, agentName) // check results assert.Nil(t, err) assert.Equal(t, tst.Output, parsedMsg) assert.Equal(t, parsedMsg.Packages[0].Name, agentName) assert.Equal(t, parsedMsg.Packages[0].Files[0].Name, "amazon-ssm-agent-linux-amd64.tar.gz") assert.Equal( t, parsedMsg.Packages[0].Files[0].AvailableVersions[0].Version, "1.0.178.0") assert.Equal( t, parsedMsg.Packages[0].Files[0].AvailableVersions[0].Checksum, "d2b67b804e0c3d3d83d09992a6a62b9e6a79fa3214b00685b07998b4e548870e") } }
func ExampleSha256HashValue() { path := filepath.Join("testdata", "CheckMyHash.txt") mockLog := log.NewMockLog() content, _ := Sha256HashValue(mockLog, path) fmt.Println(content) // Output: // 090c1965e46155b2b23ba9093ed7c67243957a397e3ad5531a693d57958a760a }
func ExampleMd5HashValue() { path := filepath.Join("testdata", "CheckMyHash.txt") mockLog := log.NewMockLog() content, _ := Md5HashValue(mockLog, path) fmt.Println(content) // Output: // e84913ff3a8eef39238b32170e657ba8 }
// NewMockDefault returns an instance of Mock with default expectations set. func MockContext() *context.Mock { ctx := new(context.Mock) log := log.NewMockLog() config := appconfig.SsmagentConfig{} ctx.On("Log").Return(log) ctx.On("AppConfig").Return(config) ctx.On("With", mock.AnythingOfType("string")).Return(ctx) return ctx }
// NewMockDefault returns an instance of Mock with default expectations set. func NewMockDefault() *Mock { ctx := new(Mock) log := log.NewMockLog() config := appconfig.NewMockAppConfig() ctx.On("Log").Return(log) ctx.On("AppConfig").Return(config) ctx.On("With", mock.AnythingOfType("string")).Return(ctx) return ctx }
func TestGetPlatformName(t *testing.T) { var log = logger.NewMockLog() t.Log("get platform name and version") data, err := PlatformName(log) t.Logf("platform name is %v ", data) assert.NoError(t, err, "get platform name should not result in err") data, err = PlatformVersion(log) t.Logf("platform version is %v ", data) assert.NoError(t, err, "get platform version should not result in err") }
func TestUpdaterFailedSetRegion(t *testing.T) { // setup log = logger.NewMockLog() region = regionFailedStub updater = &stubUpdater{returnUpdateError: true} os.Args = updateCommand // action main() }
func TestUpdater(t *testing.T) { // setup log = logger.NewMockLog() region = regionStub updater = &stubUpdater{} os.Args = updateCommand // action main() }
func TestUpdaterWithDowngrade(t *testing.T) { // setup log = logger.NewMockLog() region = regionStub updater = &stubUpdater{returnUpdateError: true} os.Args = []string{"updater", "-update", "-source.version", "5.0.0.0", "-source.location", "http://source", "-target.version", "1.0.0.0", "-target.location", "http://target"} // action main() // assert assert.Equal(t, *sourceVersion, "5.0.0.0") assert.Equal(t, *targetVersion, "1.0.0.0") }
func TestUpdaterFailedWithoutSourceTargetCmd(t *testing.T) { // setup log = logger.NewMockLog() region = regionStub updater = &stubUpdater{returnUpdateError: true} os.Args = []string{"updater", "-update", "-source.version", "", "-source.location", "http://source", "-target.version", "", "-target.location", "http://target"} // action main() // assert assert.Equal(t, *update, true) assert.Empty(t, *sourceVersion) assert.Empty(t, *targetVersion) }
func TestValidateExecutionTimeout(t *testing.T) { logger := log.NewMockLog() logger.On("Error", mock.Anything).Return(nil) logger.On("Info", mock.Anything).Return(nil) // Run tests var input interface{} var num int // Check with a value less than min value input = 3 num = ValidateExecutionTimeout(logger, input) assert.Equal(t, defaultExecutionTimeoutInSeconds, num) // Check with a value more than max value input = 28900 num = ValidateExecutionTimeout(logger, input) assert.Equal(t, defaultExecutionTimeoutInSeconds, num) // Check with a float64 value input = 5.0 num = ValidateExecutionTimeout(logger, input) assert.Equal(t, 5, num) // Check with int in a string input = "10" num = ValidateExecutionTimeout(logger, input) assert.Equal(t, 10, num) // Check with float64 in a string input = "10.5" num = ValidateExecutionTimeout(logger, input) assert.Equal(t, 10, num) // Check with character string input = "test" num = ValidateExecutionTimeout(logger, input) assert.Equal(t, defaultExecutionTimeoutInSeconds, num) }
//Test ParseManifest with invalid manifest files func TestParseManifestWithError(t *testing.T) { // generate test cases var testCases []testCase for _, manifestFile := range errorManifests { testCases = append(testCases, testCase{ Input: string(manifestFile), Output: loadManifestFromFile(t, manifestFile), }) } agentName := "amazon-ssm-agent" log := log.NewMockLog() context := mockInstanceContext() // run tests for _, tst := range testCases { // call method parsedMsg, err := ParseManifest(log, tst.Input, context, agentName) // check results assert.NotNil(t, err) assert.Equal(t, tst.Output, parsedMsg) } }
func TestHandleAwsErrorCount(t *testing.T) { errorCount := 10 stopPolicy := NewStopPolicy("test", errorCount) log := log.NewMockLog() for i := 0; i < errorCount-1; i++ { HandleAwsError(log, errSample, stopPolicy) if !stopPolicy.IsHealthy() { assert.Fail(t, "stoppolicy should be healthy") } } HandleAwsError(log, errSample, stopPolicy) if stopPolicy.IsHealthy() { assert.Fail(t, "stoppolicy should not be healthy") } HandleAwsError(log, nil, stopPolicy) if !stopPolicy.IsHealthy() { assert.Fail(t, "stoppolicy should have reset and be heallthy") } }
} var ShellCommandExecuterCancelTestCases = []TestCase{ { Commands: []string{ "sh", "-c", echoToStdout(stdoutMsg) + ";" + echoToStderr(stderrMsg) + ";" + "sleep 10" + ";" + echoToStdout("bye stdout") + ";" + echoToStderr("bye stderr"), }, ExpectedStdout: stdoutMsg + "\n", ExpectedStderr: stderrMsg + "\n", ExpectedExitCode: processTerminatedByUserExitCode, }, } var logger = log.NewMockLog() // TestRunCommand tests that RunCommand (in memory call, no local script or output files) works correctly. func TestRunCommand(t *testing.T) { for _, testCase := range RunCommandTestCases { runCommandInvoker, _ := prepareTestRunCommand(t) testCommandInvoker(t, runCommandInvoker, testCase) } } // TestRunCommand_cancel tests that RunCommand (in memory call, no local script or output files) is canceled correctly. func TestRunCommand_cancel(t *testing.T) { for _, testCase := range RunCommandCancelTestCases { runCommandInvoker, cancelFlag := prepareTestRunCommand(t) testCommandInvokerCancel(t, runCommandInvoker, cancelFlag, testCase) }
"fmt" "github.com/aws/amazon-ssm-agent/agent/log" "github.com/stretchr/testify/assert" ) type DownloadTest struct { input DownloadInput expectedOutput DownloadOutput } var ( localPathExist, _ = filepath.Abs(filepath.Join(".", "testdata", "CheckMyHash.txt")) localPathNotExist, _ = filepath.Abs(filepath.Join(".", "testdata", "IDontExist.txt")) downloadFolder, _ = filepath.Abs(filepath.Join(".", "testdata")) mockLog = log.NewMockLog() downloadTests = []DownloadTest{ // {DownloadInput{SourceUrl, DestinationDirectory, SourceHashValue, SourceHashType}, // DownloadOutput{LocalFilePath, IsUpdated, IsHashMatched}}, { // validate sha256 DownloadInput{ localPathExist, downloadFolder, "090c1965e46155b2b23ba9093ed7c67243957a397e3ad5531a693d57958a760a", "sha256"}, DownloadOutput{ localPathExist, false, true},