This repository has been archived by the owner on Dec 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from moadqassem/support-user-ssh-keys-in-vms
adding ssh key support
- Loading branch information
Showing
4 changed files
with
100 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package core | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
) | ||
|
||
var ( | ||
testCases = []struct { | ||
name string | ||
userData string | ||
sshKeys []string | ||
expectedUserData string | ||
expectedError bool | ||
}{ | ||
{ | ||
name: "`ssh_authorized_keys` key already exists error", | ||
userData: "#cloud-config\nchpasswd:\nexpire: false\npassword: pass\nuser: test\nssh_authorized_keys:\n- ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDdOIhYmzCK5DSVLu", | ||
sshKeys: []string{"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDdOIhYmzCK5DSVLu3b"}, | ||
expectedUserData: "", | ||
expectedError: true, | ||
}, | ||
{ | ||
name: "add user ssh key to userdata successfully", | ||
userData: "#cloud-config\nchpasswd:\nexpire: false\npassword: pass\nuser: test", | ||
sshKeys: []string{"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDdOIhYmzCK5DSVLu3b"}, | ||
expectedUserData: "#cloud-config\nchpasswd:\nexpire: false\npassword: pass\nuser: test\nssh_authorized_keys:\n- ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDdOIhYmzCK5DSVLu3b", | ||
expectedError: false, | ||
}, | ||
} | ||
) | ||
|
||
func TestAddUserSSHKeysToUserData(t *testing.T) { | ||
for _, testCase := range testCases { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
u, err := addUserSSHKeysToUserData(testCase.userData, testCase.sshKeys) | ||
if testCase.expectedError && err == nil { | ||
t.Fatal("expected an error but got error: nil") | ||
} | ||
|
||
if err != nil && !testCase.expectedError { | ||
t.Fatalf("unexpected error was encoutred: %v", err) | ||
} | ||
|
||
if strings.TrimSpace(testCase.expectedUserData) != strings.TrimSpace(u) { | ||
t.Fatalf("expecting userdata: %v and got: %v", testCase.expectedUserData, u) | ||
} | ||
|
||
}) | ||
} | ||
} |