func TestLoop_Once(t *testing.T) { // Test loop with valid response contextMock := MockContext() log := contextMock.Log() // create mocked service and set expectations mdsMock := new(MockedMDS) mdsMock.On("GetMessages", log, sampleInstanceID).Return(&ssmmds.GetMessagesOutput{}, nil) newMdsService = func(appconfig.SsmagentConfig) service.Service { return mdsMock } called := 0 job := func() { called++ } messagePollJob, _ := scheduler.Every(10).Seconds().NotImmediately().Run(job) proc := Processor{ context: contextMock, service: mdsMock, messagePollJob: messagePollJob, processorStopPolicy: sdkutil.NewStopPolicy(name, stopPolicyThreshold), } proc.loop() time.Sleep(1 * time.Second) assert.Equal(t, 1, called) }
func TestLoop_Multiple_Parallel_Error(t *testing.T) { // Test loop multiple times in parallel with simple error contextMock := MockContext() log := contextMock.Log() // create mocked service and set expectations mdsMock := new(MockedMDS) mdsMock.On("GetMessages", log, sampleInstanceID).Return(&ssmmds.GetMessagesOutput{}, errSample) newMdsService = func(appconfig.SsmagentConfig) service.Service { return mdsMock } called := 0 job := func() { called++ } messagePollJob, _ := scheduler.Every(10).Seconds().NotImmediately().Run(job) proc := Processor{ context: contextMock, service: mdsMock, messagePollJob: messagePollJob, processorStopPolicy: sdkutil.NewStopPolicy(name, stopPolicyThreshold), } for i := 0; i < multipleRetryCount; i++ { go proc.loop() } time.Sleep(4 * time.Second) assert.Equal(t, 1, called) }
// NewService creates a new SSM service instance. func NewService() Service { if ssmStopPolicy == nil { // create a stop policy where we will stop after 10 consecutive errors and if time period expires. ssmStopPolicy = sdkutil.NewStopPolicy("ssmService", 10) } awsConfig := sdkutil.AwsConfig() // parse appConfig overrides appConfig, err := appconfig.Config(false) if err == nil { if appConfig.Ssm.Endpoint != "" { awsConfig.Endpoint = &appConfig.Ssm.Endpoint } // TODO: test hook, can be removed before release // this is to skip ssl verification for the beta self signed certs if appConfig.Ssm.InsecureSkipVerify { tr := &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, } awsConfig.HTTPClient = &http.Client{Transport: tr} } } ssmService := ssm.New(session.New(awsConfig)) return &sdkService{sdk: ssmService} }
// NewHealthCheck creates a new health check core plugin. func NewHealthCheck(context context.T) *HealthCheck { healthContext := context.With("[" + name + "]") healthCheckStopPolicy := sdkutil.NewStopPolicy(name, 10) svc := ssm.NewService() return &HealthCheck{ context: healthContext, healthCheckStopPolicy: healthCheckStopPolicy, service: svc, } }
func TestLoop_Multiple_Serial_Error(t *testing.T) { // Test loop multiple times with simple error contextMock := MockContext() log := contextMock.Log() // create mocked service and set expectations mdsMock := new(MockedMDS) mdsMock.On("GetMessages", log, sampleInstanceID).Return(&ssmmds.GetMessagesOutput{}, errSample) newMdsService = func(appconfig.SsmagentConfig) service.Service { return mdsMock } called := 0 job := func() { called++ } messagePollJob, _ := scheduler.Every(10).Seconds().NotImmediately().Run(job) proc := Processor{ context: contextMock, service: mdsMock, messagePollJob: messagePollJob, processorStopPolicy: sdkutil.NewStopPolicy(name, stopPolicyThreshold), } start := time.Now() for i := 0; i < multipleRetryCount; i++ { proc.loop() } // elapsed should be greater than number of polls in seconds as we force a 1 second delay elapsed := time.Since(start) time.Sleep(1 * time.Second) // number of tries should be the same as stop threshold +1 assert.Equal(t, stopPolicyThreshold+1, called) assert.True(t, stopPolicyThreshold+1 < elapsed.Seconds()) }
} } func processSendReply(log log.T, messageID string, mdsService service.Service, payloadDoc messageContracts.SendReplyPayload, processorStopPolicy *sdkutil.StopPolicy) { payloadB, err := json.Marshal(payloadDoc) if err != nil { log.Error("could not marshal reply payload!", err) } payload := string(payloadB) log.Info("Sending reply ", jsonutil.Indent(payload)) err = mdsService.SendReply(log, messageID, payload) if err != nil { sdkutil.HandleAwsError(log, err, processorStopPolicy) } } var newMdsService = func(config appconfig.SsmagentConfig) service.Service { connectionTimeout := time.Duration(config.Mds.StopTimeoutMillis) * time.Millisecond return service.NewService( config.Agent.Region, config.Mds.Endpoint, nil, connectionTimeout, ) } var newStopPolicy = func() *sdkutil.StopPolicy { return sdkutil.NewStopPolicy(name, stopPolicyErrorThreshold) }