Skip to content

Commit 3c9e03f

Browse files
committed
Added test for utils
1 parent 9fddf4d commit 3c9e03f

File tree

1 file changed

+71
-7
lines changed

1 file changed

+71
-7
lines changed

internal/pkg/services/logs/utils/utils_test.go

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ import (
1212
)
1313

1414
var (
15-
testProjectId = uuid.NewString()
16-
testInstanceId = uuid.NewString()
15+
testProjectId = uuid.NewString()
16+
testInstanceId = uuid.NewString()
17+
testAccessTokenId = uuid.NewString()
1718
)
1819

1920
const (
@@ -22,9 +23,10 @@ const (
2223
)
2324

2425
type logsClientMocked struct {
25-
getInstanceFails bool
26-
getInstanceResp *logs.LogsInstance
27-
getAccessTokenResp *logs.AccessToken
26+
getInstanceFails bool
27+
getInstanceResp *logs.LogsInstance
28+
getAccessTokenFails bool
29+
getAccessTokenResp *logs.AccessToken
2830
}
2931

3032
func (m *logsClientMocked) GetLogsInstanceExecute(_ context.Context, _, _, _ string) (*logs.LogsInstance, error) {
@@ -35,8 +37,8 @@ func (m *logsClientMocked) GetLogsInstanceExecute(_ context.Context, _, _, _ str
3537
}
3638

3739
func (m *logsClientMocked) GetAccessTokenExecute(_ context.Context, _, _, _, _ string) (*logs.AccessToken, error) {
38-
if m.getInstanceFails {
39-
return nil, fmt.Errorf("could not get instance")
40+
if m.getAccessTokenFails {
41+
return nil, fmt.Errorf("could not get access token")
4042
}
4143
return m.getAccessTokenResp, nil
4244
}
@@ -102,3 +104,65 @@ func TestGetInstanceName(t *testing.T) {
102104
})
103105
}
104106
}
107+
108+
func TestGetAccessTokenName(t *testing.T) {
109+
tests := []struct {
110+
description string
111+
getAccessTokenFails bool
112+
getAccessTokenResp *logs.AccessToken
113+
isValid bool
114+
expectedOutput string
115+
}{
116+
{
117+
description: "base",
118+
getAccessTokenResp: &logs.AccessToken{
119+
DisplayName: utils.Ptr(testInstanceName),
120+
},
121+
isValid: true,
122+
expectedOutput: testInstanceName,
123+
},
124+
{
125+
description: "get instance fails",
126+
getAccessTokenFails: true,
127+
isValid: false,
128+
},
129+
{
130+
description: "response is nil",
131+
getAccessTokenFails: false,
132+
getAccessTokenResp: nil,
133+
isValid: false,
134+
},
135+
{
136+
description: "name in response is nil",
137+
getAccessTokenFails: false,
138+
getAccessTokenResp: &logs.AccessToken{
139+
DisplayName: nil,
140+
},
141+
isValid: false,
142+
},
143+
}
144+
145+
for _, tt := range tests {
146+
t.Run(tt.description, func(t *testing.T) {
147+
client := &logsClientMocked{
148+
getAccessTokenFails: tt.getAccessTokenFails,
149+
getAccessTokenResp: tt.getAccessTokenResp,
150+
}
151+
152+
output, err := GetAccessTokenName(context.Background(), client, testProjectId, testRegion, testInstanceId, testAccessTokenId)
153+
154+
if tt.isValid && err != nil {
155+
t.Errorf("failed on valid input")
156+
}
157+
if !tt.isValid && err == nil {
158+
t.Errorf("did not fail on invalid input")
159+
}
160+
if !tt.isValid {
161+
return
162+
}
163+
if output != tt.expectedOutput {
164+
t.Errorf("expected output to be %s, got %s", tt.expectedOutput, output)
165+
}
166+
})
167+
}
168+
}

0 commit comments

Comments
 (0)