MCPcopy Create free account
hub / github.com/cli/cli / TestGenerateAutomaticSSHKeys

Function TestGenerateAutomaticSSHKeys

pkg/cmd/codespace/ssh_test.go:31–119  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

29}
30
31func TestGenerateAutomaticSSHKeys(t *testing.T) {
32 tests := []struct {
33 // These files exist when calling generateAutomaticSSHKeys
34 existingFiles []string
35 // These files should exist after generateAutomaticSSHKeys finishes
36 wantFinalFiles []string
37 }{
38 // Basic case: no existing keys, they should be created
39 {
40 nil,
41 []string{automaticPrivateKeyName, automaticPrivateKeyName + ".pub"},
42 },
43 // Basic case: keys already exist
44 {
45 []string{automaticPrivateKeyName, automaticPrivateKeyName + ".pub"},
46 []string{automaticPrivateKeyName, automaticPrivateKeyName + ".pub"},
47 },
48 // Backward compatibility: both old keys exist, they should be renamed
49 {
50 []string{automaticPrivateKeyNameOld, automaticPrivateKeyNameOld + ".pub"},
51 []string{automaticPrivateKeyName, automaticPrivateKeyName + ".pub"},
52 },
53 // Backward compatibility: old private key exists but not the public key, the new keys should be created
54 {
55 []string{automaticPrivateKeyNameOld},
56 []string{automaticPrivateKeyNameOld, automaticPrivateKeyName, automaticPrivateKeyName + ".pub"},
57 },
58 // Backward compatibility: old public key exists but not the private key, the new keys should be created
59 {
60 []string{automaticPrivateKeyNameOld + ".pub"},
61 []string{automaticPrivateKeyNameOld + ".pub", automaticPrivateKeyName, automaticPrivateKeyName + ".pub"},
62 },
63 // Backward compatibility (edge case): files exist which contains old key name as a substring, the new keys should be created
64 {
65 []string{"foo" + automaticPrivateKeyNameOld + ".pub", "foo" + automaticPrivateKeyNameOld},
66 []string{"foo" + automaticPrivateKeyNameOld + ".pub", "foo" + automaticPrivateKeyNameOld, automaticPrivateKeyName, automaticPrivateKeyName + ".pub"},
67 },
68 }
69
70 for _, tt := range tests {
71 dir := t.TempDir()
72
73 sshContext := ssh.NewContextForTests(dir, "")
74
75 for _, file := range tt.existingFiles {
76 f, err := os.Create(filepath.Join(dir, file))
77 if err != nil {
78 t.Errorf("Failed to setup test files: %v", err)
79 }
80 // If the file isn't closed here windows will have errors about file already in use
81 f.Close()
82 }
83
84 keyPair, err := generateAutomaticSSHKeys(sshContext)
85 if err != nil {
86 t.Errorf("Unexpected error from generateAutomaticSSHKeys: %v", err)
87 }
88 if keyPair == nil {

Callers

nothing calls this directly

Calls 8

NewContextForTestsFunction · 0.92
generateAutomaticSSHKeysFunction · 0.85
JoinMethod · 0.80
ContainsMethod · 0.80
CreateMethod · 0.65
ErrorfMethod · 0.65
CloseMethod · 0.65
NameMethod · 0.65

Tested by

no test coverage detected