Skip to content

Commit ce05e0e

Browse files
committed
test: add unit test for cleanupRemoteAuthorizedKeysSelective to handle selective deploy failure
1 parent f71899e commit ce05e0e

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package core
2+
3+
import (
4+
"errors"
5+
"strings"
6+
"testing"
7+
8+
"github.com/toeirei/keymaster/internal/model"
9+
"github.com/toeirei/keymaster/internal/security"
10+
)
11+
12+
type fakeRemoteSelectiveFail struct {
13+
content []byte
14+
deployErr error
15+
}
16+
17+
func (f *fakeRemoteSelectiveFail) DeployAuthorizedKeys(content string) error { return f.deployErr }
18+
func (f *fakeRemoteSelectiveFail) GetAuthorizedKeys() ([]byte, error) { return f.content, nil }
19+
func (f *fakeRemoteSelectiveFail) Close() {}
20+
21+
func TestCleanupRemoteAuthorizedKeysSelective_SelectiveDeployFail_ReturnsError(t *testing.T) {
22+
acct := model.Account{ID: 55, Username: "u", Hostname: "h"}
23+
orig := NewDeployerFactory
24+
defer func() { NewDeployerFactory = orig }()
25+
NewDeployerFactory = func(host, user string, privateKey security.Secret, passphrase []byte) (RemoteDeployer, error) {
26+
return &fakeRemoteSelectiveFail{content: []byte("# Keymaster Managed Keys\nssh-ed25519 AAA\n"), deployErr: errors.New("deploy fail")}, nil
27+
}
28+
29+
res := &DecommissionResult{}
30+
opts := DecommissionOptions{SelectiveKeys: []int{1}}
31+
err := cleanupRemoteAuthorizedKeysSelective(acct, nil, opts, res)
32+
if err == nil {
33+
t.Fatalf("expected error when deploy fails during selective cleanup")
34+
}
35+
if !strings.Contains(err.Error(), "failed to update authorized_keys") && !strings.Contains(err.Error(), "failed to remove empty authorized_keys file") {
36+
t.Fatalf("unexpected error: %v", err)
37+
}
38+
}

0 commit comments

Comments
 (0)