github.com/1and1/oneandone-cloudserver-sdk-go@v1.4.1/sshkeys_test.go (about) 1 package oneandone 2 3 import ( 4 "fmt" 5 "math/rand" 6 "strings" 7 "sync" 8 "testing" 9 "time" 10 ) 11 12 var ( 13 set_ssh_key sync.Once 14 test_ssh_key *SSHKey 15 test_ssh_name string 16 test_ssh_desc string 17 ) 18 19 func setup_ssh_key() { 20 rand.Seed(time.Now().UnixNano()) 21 rint := rand.Intn(999) 22 fmt.Printf("Creating test ssh_key '%s'...\n", fmt.Sprintf("SSHKEY_%d", rint)) 23 24 pk := `ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCezYMOgAan+JmatgFJ+Q1FUNjrqNVgAvcTkjYJwHVcQaolq9f9qB7tEeUPDNj2oNN49joAmTcllDcPIxryT5PnQUaaUhu4ZJ9+bRtXCyhnf2LJQdVfzBFEBJX9fW4RiV1XtSAtLRBrbrCb4JjHmhIYpvhBHC29Ve+g64nhdvBhqyLZ3SLI2U/opEmt5u2xftWGl0TBSQYveqc4ntz3fe+f9XlBHvK3Nw12bCLmLle7jQuZ4lXyAYqNAfdOMTs2zMTk422Dl/h4+zRh1h4rM9zaCk4+g3kdugJm7Vul03wm43cHmHsJv51R3XKSHzgb7q/eNj+YdMi5Ndt0Bm+bLjw7` 25 26 req := SSHKeyRequest{ 27 Name: fmt.Sprintf("SSHKEY_%d", rint), 28 Description: fmt.Sprintf("SSHKEY_%d description", rint), 29 PublicKey: pk, 30 } 31 32 sshkey_id, sshkey, err := api.CreateSSHKey(&req) 33 34 if err != nil { 35 fmt.Printf("Unable to create the ssh key. Error: %s", err.Error()) 36 return 37 } 38 if sshkey_id == "" || sshkey.Id == "" { 39 fmt.Printf("Unable to create the server.") 40 return 41 } else { 42 sshkey_id = sshkey.Id 43 } 44 45 if err != nil { 46 fmt.Printf("Error: %s", err.Error()) 47 } 48 49 test_ssh_key = sshkey 50 } 51 52 func TestListSSHKeys(t *testing.T) { 53 set_ssh_key.Do(setup_ssh_key) 54 keys, err := api.ListSSHKeys() 55 56 if err != nil { 57 t.Errorf(err.Error()) 58 t.Fail() 59 } 60 61 if len(keys) < 1 { 62 t.Errorf("Assertion failed.") 63 } 64 } 65 66 func TestCreateSSHKey(t *testing.T) { 67 rand.Seed(time.Now().UnixNano()) 68 rint := rand.Intn(999) 69 pk := `ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCezYMOgAan+JmatgFJ+Q1FUNjrqNVgAvcTkjYJwHVcQaolq9f9qB7tEeUPDNj2oNN49joAmTcllDcPIxryT5PnQUaaUhu4ZJ9+bRtXCyhnf2LJQdVfzBFEBJX9fW4RiV1XtSAtLRBrbrCb4JjHmhIYpvhBHC29Ve+g64nhdvBhqyLZ3SLI2U/opEmt5u2xftWGl0TBSQYveqc4ntz3fe+f9XlBHvK3Nw12bCLmLle7jQuZ4lXyAYqNAfdOMTs2zMTk422Dl/h4+zRh1h4rM9zaCk4+g3kdugJm7Vul03wm43cHmHsJv51R3XKSHzgb7q/eNj+YdMi5Ndt0Bm+bLjw7` 70 71 req := SSHKeyRequest{ 72 Name: fmt.Sprintf("SSHKEY_%d", rint), 73 Description: fmt.Sprintf("SSHKEY_%d description", rint), 74 PublicKey: pk, 75 } 76 77 _, sshkey, err := api.CreateSSHKey(&req) 78 79 if err != nil { 80 t.Errorf(err.Error()) 81 t.Fail() 82 } 83 84 if sshkey.PublicKey != strings.Replace(pk, "ssh-rsa ", "", -1) { 85 t.Errorf("Keys are not the same") 86 t.Fail() 87 } 88 89 _, err = api.DeleteSSHKey(sshkey.Id) 90 if err != nil { 91 t.Errorf(err.Error()) 92 t.Fail() 93 } 94 } 95 96 func TestRenameSSHKey(t *testing.T) { 97 set_ssh_key.Do(setup_ssh_key) 98 99 sshKey, err := api.RenameSSHKey(test_ssh_key.Id, test_ssh_key.Name+"1", test_ssh_key.Description+"1") 100 101 if err != nil { 102 t.Errorf(err.Error()) 103 t.Fail() 104 } 105 106 if sshKey.Name == sshKey.Name+"1" { 107 t.Errorf("Names are not the same") 108 t.Fail() 109 } 110 111 if sshKey.Description == sshKey.Description+"1" { 112 t.Errorf("Descriptions are not the same") 113 t.Fail() 114 } 115 } 116 117 func TestDeleteSSHKey(t *testing.T) { 118 set_ssh_key.Do(setup_ssh_key) 119 _, err := api.DeleteSSHKey(test_ssh_key.Id) 120 if err != nil { 121 t.Errorf(err.Error()) 122 t.Fail() 123 } 124 125 sshKey, err := api.GetSSHKey(test_ssh_key.Id) 126 127 if err == nil { 128 t.Errorf(err.Error()) 129 t.Fail() 130 } 131 132 if sshKey != nil { 133 t.Errorf("SSH Key was not deleted") 134 t.Fail() 135 } 136 137 }