-
Notifications
You must be signed in to change notification settings - Fork 323
/
test_context.go
125 lines (111 loc) · 4.15 KB
/
test_context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright 2016 Amazon.com, Inc. or its affiliates. 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. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file 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.
package context
import (
"strings"
"github.com/aws/amazon-ssm-agent/agent/appconfig"
"github.com/aws/amazon-ssm-agent/agent/context"
"github.com/aws/amazon-ssm-agent/agent/log"
logmocks "github.com/aws/amazon-ssm-agent/agent/mocks/log"
"github.com/aws/amazon-ssm-agent/common/identity"
identityMocks "github.com/aws/amazon-ssm-agent/common/identity/mocks"
"github.com/stretchr/testify/mock"
)
// Note: This code is used in the test files. However, this code is not in a _test.go file
// because then we would have to copy it in every test package that needs the mock.
// Mock stands for a mocked context.
type Mock struct {
mock.Mock
}
// NewMockDefault returns an instance of Mock with default expectations set.
func NewMockDefault() *Mock {
ctx := new(Mock)
log := logmocks.NewMockLog()
config := appconfig.SsmagentConfig{}
agentIdentity := identityMocks.NewDefaultMockAgentIdentity()
appconst := appconfig.AppConstants{
MinHealthFrequencyMinutes: appconfig.DefaultSsmHealthFrequencyMinutesMin,
MaxHealthFrequencyMinutes: appconfig.DefaultSsmHealthFrequencyMinutesMax,
}
ctx.On("Log").Return(log)
ctx.On("AppConfig").Return(config)
ctx.On("With", mock.AnythingOfType("string")).Return(ctx)
ctx.On("CurrentContext").Return([]string{})
ctx.On("Identity").Return(agentIdentity)
ctx.On("AppConstants").Return(&appconst)
return ctx
}
func NewMockDefaultWithConfig(config appconfig.SsmagentConfig) *Mock {
ctx := new(Mock)
log := logmocks.NewMockLog()
agentIdentity := identityMocks.NewDefaultMockAgentIdentity()
appconst := appconfig.AppConstants{
MinHealthFrequencyMinutes: appconfig.DefaultSsmHealthFrequencyMinutesMin,
MaxHealthFrequencyMinutes: appconfig.DefaultSsmHealthFrequencyMinutesMax,
}
ctx.On("Log").Return(log)
ctx.On("AppConfig").Return(config)
ctx.On("With", mock.AnythingOfType("string")).Return(ctx)
ctx.On("CurrentContext").Return([]string{})
ctx.On("Identity").Return(agentIdentity)
ctx.On("AppConstants").Return(&appconst)
return ctx
}
// NewMockDefaultWithContext returns an instance of Mock with specified context.
func NewMockDefaultWithContext(context []string) *Mock {
ctx := new(Mock)
log := logmocks.NewMockLogWithContext(strings.Join(context, ""))
config := appconfig.SsmagentConfig{}
agentIdentity := identityMocks.NewDefaultMockAgentIdentity()
appconst := appconfig.AppConstants{
MinHealthFrequencyMinutes: appconfig.DefaultSsmHealthFrequencyMinutesMin,
MaxHealthFrequencyMinutes: appconfig.DefaultSsmHealthFrequencyMinutesMax,
}
ctx.On("Log").Return(log)
ctx.On("AppConfig").Return(config)
ctx.On("With", mock.AnythingOfType("string")).Return(ctx)
ctx.On("CurrentContext").Return(context)
ctx.On("Identity").Return(agentIdentity)
ctx.On("AppConstants").Return(&appconst)
return ctx
}
// AppConfig mocks the Config function.
func (m *Mock) AppConfig() appconfig.SsmagentConfig {
args := m.Called()
return args.Get(0).(appconfig.SsmagentConfig)
}
// Log mocks the Log function.
func (m *Mock) Log() log.T {
args := m.Called()
return args.Get(0).(log.T)
}
// With mocks the With function.
func (m *Mock) With(ctx string) context.T {
args := m.Called(ctx)
return args.Get(0).(context.T)
}
// CurrentContext mocks the CurrentContext function.
func (m *Mock) CurrentContext() []string {
args := m.Called()
return args.Get(0).([]string)
}
// AppConstants mocks the AppConstatns function.
func (m *Mock) AppConstants() *appconfig.AppConstants {
args := m.Called()
return args.Get(0).(*appconfig.AppConstants)
}
// Identity mocks the Identity function.
func (m *Mock) Identity() identity.IAgentIdentity {
args := m.Called()
return args.Get(0).(identity.IAgentIdentity)
}