Skip to content

Commit 1b2b029

Browse files
committed
add unit tests for transport credentials funtions
1 parent d68fe7e commit 1b2b029

File tree

5 files changed

+60
-39
lines changed

5 files changed

+60
-39
lines changed

api/grpc/mpi/v1/command.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/grpc/mpi/v1/common.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/grpc/mpi/v1/files.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/grpc/grpc.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -219,22 +219,22 @@ func GetDialOptions(agentConfig *config.Config, resourceID string) []grpc.DialOp
219219
}
220220

221221
func addTransportCredentials(agentConfig *config.Config, opts []grpc.DialOption) ([]grpc.DialOption, bool) {
222-
skipToken := false
223222
transportCredentials, err := getTransportCredentials(agentConfig)
224-
if err == nil {
225-
slog.Debug("Adding transport credentials to gRPC dial options")
226-
opts = append(opts,
227-
grpc.WithTransportCredentials(transportCredentials),
228-
)
229-
} else {
223+
if err != nil {
230224
slog.Error("Unable to add transport credentials to gRPC dial options", "error", err)
231225
slog.Debug("Adding default transport credentials to gRPC dial options")
232226
opts = append(opts,
233227
grpc.WithTransportCredentials(defaultCredentials),
234228
)
235-
skipToken = true
229+
230+
return opts, true
236231
}
237-
return opts, skipToken
232+
slog.Debug("Adding transport credentials to gRPC dial options")
233+
opts = append(opts,
234+
grpc.WithTransportCredentials(transportCredentials),
235+
)
236+
237+
return opts, false
238238
}
239239

240240
func addPerRPCCredentials(agentConfig *config.Config, resourceID string, opts []grpc.DialOption) []grpc.DialOption {

internal/grpc/grpc_test.go

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"os"
1212
"testing"
1313

14+
"google.golang.org/grpc/credentials"
15+
1416
"github.com/cenkalti/backoff/v4"
1517
"github.com/nginx/agent/v3/test/helpers"
1618
"github.com/nginx/agent/v3/test/protos"
@@ -354,70 +356,89 @@ func Test_ValidateGrpcError(t *testing.T) {
354356
assert.IsType(t, &backoff.PermanentError{}, result)
355357
}
356358

359+
// nolint:revive,gocognit
357360
func Test_validateTokenFile(t *testing.T) {
358-
type args struct {
359-
path string
360-
}
361361
tests := []struct {
362362
name string
363-
createToken bool
364-
args args
363+
path string
365364
want string
366365
wantErrMsg string
366+
createToken bool
367367
}{
368368
{
369-
name: "File exists",
369+
name: "Test 1: File exists",
370370
createToken: true,
371-
args: args{
372-
path: "test-tkn",
373-
},
374-
want: "test-tkn",
375-
wantErrMsg: "",
371+
path: "test-tkn",
372+
want: "test-tkn",
373+
wantErrMsg: "",
376374
},
377375
{
378-
name: "File does not exist",
376+
name: "Test 2: File does not exist",
379377
createToken: false,
380-
args: args{
381-
path: "test-tkn",
382-
},
383-
want: "",
384-
wantErrMsg: "open test-tkn: no such file or directory",
378+
path: "test-tkn",
379+
want: "",
380+
wantErrMsg: "open test-tkn: no such file or directory",
385381
},
386382
{
387-
name: "Empty path",
383+
name: "Test 3: Empty path",
388384
createToken: false,
389-
args: args{
390-
path: "",
391-
},
392-
want: "",
393-
wantErrMsg: "token file path is empty",
385+
path: "",
386+
want: "",
387+
wantErrMsg: "token file path is empty",
394388
},
395389
}
396390
for _, tt := range tests {
397391
t.Run(tt.name, func(t *testing.T) {
398392
defer func() {
399393
if tt.createToken {
400-
err := os.Remove(tt.args.path)
394+
err := os.Remove(tt.path)
401395
if err != nil {
402396
t.Log(err)
403397
}
404398
}
405399
}()
406400

407401
if tt.createToken {
408-
err := os.WriteFile(tt.args.path, []byte(tt.args.path), 0666)
402+
err := os.WriteFile(tt.path, []byte(tt.path), 0o600)
409403
if err != nil {
410404
t.Fatal(err)
411405
}
412406
}
413407

414-
got, err := validateTokenFile(tt.args.path)
408+
got, err := validateTokenFile(tt.path)
415409
if err != nil {
416410
if err.Error() != tt.wantErrMsg {
417411
t.Errorf("validateTokenFile() error = %v, wantErr %v", err, tt.wantErrMsg)
418412
}
419413
}
420-
assert.Equalf(t, tt.want, got, "validateTokenFile(%v)", tt.args.path)
414+
assert.Equalf(t, tt.want, got, "validateTokenFile(%v)", tt.path)
415+
})
416+
}
417+
}
418+
419+
func Test_getTransportCredentials(t *testing.T) {
420+
tests := []struct {
421+
want credentials.TransportCredentials
422+
conf *config.Config
423+
wantErr assert.ErrorAssertionFunc
424+
name string
425+
}{
426+
{
427+
name: "No TLS config returns default credentials",
428+
conf: &config.Config{
429+
Command: &config.Command{},
430+
},
431+
want: defaultCredentials,
432+
wantErr: assert.NoError,
433+
},
434+
}
435+
for _, tt := range tests {
436+
t.Run(tt.name, func(t *testing.T) {
437+
got, err := getTransportCredentials(tt.conf)
438+
if !tt.wantErr(t, err, fmt.Sprintf("getTransportCredentials(%v)", tt.conf)) {
439+
return
440+
}
441+
assert.Equalf(t, tt.want, got, "getTransportCredentials(%v)", tt.conf)
421442
})
422443
}
423444
}

0 commit comments

Comments
 (0)