Skip to content

Commit 4b3127b

Browse files
committed
test(config): Add tests for private key config
1 parent c39fcd4 commit 4b3127b

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

config/endpoint/endpoint_test.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -511,26 +511,40 @@ func TestEndpoint_ValidateAndSetDefaultsWithSSH(t *testing.T) {
511511
name string
512512
username string
513513
password string
514+
privateKey string
514515
expectedErr error
515516
}{
516517
{
517-
name: "fail when has no user",
518+
name: "fail when has no user but has password",
518519
username: "",
519520
password: "password",
520521
expectedErr: ssh.ErrEndpointWithoutSSHUsername,
521522
},
522523
{
523-
name: "fail when has no password",
524+
name: "fail when has no user but has private key",
525+
username: "",
526+
privateKey: "-----BEGIN",
527+
expectedErr: ssh.ErrEndpointWithoutSSHUsername,
528+
},
529+
{
530+
name: "fail when has no password or private key",
524531
username: "username",
525532
password: "",
533+
privateKey: "",
526534
expectedErr: ssh.ErrEndpointWithoutSSHAuth,
527535
},
528536
{
529-
name: "success when all fields are set",
537+
name: "success when username and password are set",
530538
username: "username",
531539
password: "password",
532540
expectedErr: nil,
533541
},
542+
{
543+
name: "success when username and private key are set",
544+
username: "username",
545+
privateKey: "-----BEGIN",
546+
expectedErr: nil,
547+
},
534548
}
535549

536550
for _, scenario := range scenarios {
@@ -539,8 +553,9 @@ func TestEndpoint_ValidateAndSetDefaultsWithSSH(t *testing.T) {
539553
Name: "ssh-test",
540554
URL: "https://example.com",
541555
SSHConfig: &ssh.Config{
542-
Username: scenario.username,
543-
Password: scenario.password,
556+
Username: scenario.username,
557+
Password: scenario.password,
558+
PrivateKey: scenario.privateKey,
544559
},
545560
Conditions: []Condition{Condition("[STATUS] == 0")},
546561
}

config/endpoint/ssh/ssh_test.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"testing"
66
)
77

8-
func TestSSH_validate(t *testing.T) {
8+
func TestSSH_validatePasswordCfg(t *testing.T) {
99
cfg := &Config{}
1010
if err := cfg.Validate(); err != nil {
1111
t.Error("didn't expect an error")
@@ -21,3 +21,18 @@ func TestSSH_validate(t *testing.T) {
2121
t.Errorf("expected no error, got '%v'", err)
2222
}
2323
}
24+
25+
func TestSSH_validatePrivateKeyCfg(t *testing.T) {
26+
t.Run("fail when username missing but private key provided", func(t *testing.T) {
27+
cfg := &Config{PrivateKey: "-----BEGIN"}
28+
if err := cfg.Validate(); !errors.Is(err, ErrEndpointWithoutSSHUsername) {
29+
t.Fatalf("expected ErrEndpointWithoutSSHUsername, got %v", err)
30+
}
31+
})
32+
t.Run("success when username with private key", func(t *testing.T) {
33+
cfg := &Config{Username: "user", PrivateKey: "-----BEGIN"}
34+
if err := cfg.Validate(); err != nil {
35+
t.Fatalf("expected no error, got %v", err)
36+
}
37+
})
38+
}

0 commit comments

Comments
 (0)