github.com/naphatkrit/deis@v1.12.3/client/cmd/keys_test.go (about) 1 package cmd 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path" 7 "reflect" 8 "testing" 9 10 "github.com/deis/deis/client/controller/api" 11 ) 12 13 func TestGetKey(t *testing.T) { 14 t.Parallel() 15 16 file, err := ioutil.TempFile("", "deis-key") 17 18 if err != nil { 19 t.Fatal(err) 20 } 21 22 toWrite := []byte("ssh-rsa abc test@example.com") 23 24 expected := api.KeyCreateRequest{ 25 ID: "test@example.com", 26 Public: string(toWrite), 27 Name: file.Name(), 28 } 29 30 if _, err = file.Write(toWrite); err != nil { 31 t.Fatal(err) 32 } 33 34 key, err := getKey(file.Name()) 35 36 if err != nil { 37 t.Fatal(err) 38 } 39 40 if !reflect.DeepEqual(expected, key) { 41 t.Errorf("Expected %v, Got %v", expected, key) 42 } 43 } 44 45 func TestGetKeyNoComment(t *testing.T) { 46 t.Parallel() 47 48 file, err := ioutil.TempFile("", "deis-key") 49 50 if err != nil { 51 t.Fatal(err) 52 } 53 54 toWrite := []byte("ssh-rsa abc") 55 56 expected := api.KeyCreateRequest{ 57 ID: path.Base(file.Name()), 58 Public: string(toWrite), 59 Name: file.Name(), 60 } 61 62 if _, err = file.Write(toWrite); err != nil { 63 t.Fatal(err) 64 } 65 66 key, err := getKey(file.Name()) 67 68 if err != nil { 69 t.Fatal(err) 70 } 71 72 if !reflect.DeepEqual(expected, key) { 73 t.Errorf("Expected %v, Got %v", expected, key) 74 } 75 } 76 77 func TestListKeys(t *testing.T) { 78 name, err := ioutil.TempDir("", "deis-key") 79 80 if err != nil { 81 t.Fatal(err) 82 } 83 84 os.Setenv("HOME", name) 85 86 folder := path.Join(name, ".ssh") 87 88 if err = os.Mkdir(folder, 0755); err != nil { 89 t.Fatal(err) 90 } 91 92 toWrite := []byte("ssh-rsa abc test@example.com") 93 fileNames := []string{"test1.pub", "test2.pub"} 94 95 expected := []api.KeyCreateRequest{ 96 api.KeyCreateRequest{ 97 ID: "test@example.com", 98 Public: string(toWrite), 99 Name: path.Join(folder, fileNames[0]), 100 }, 101 api.KeyCreateRequest{ 102 ID: "test@example.com", 103 Public: string(toWrite), 104 Name: path.Join(folder, fileNames[1]), 105 }, 106 } 107 108 for _, file := range fileNames { 109 if err = ioutil.WriteFile(path.Join(folder, file), toWrite, 0775); err != nil { 110 t.Fatal(err) 111 } 112 } 113 114 keys, err := listKeys() 115 116 if err != nil { 117 t.Fatal(err) 118 } 119 120 if !reflect.DeepEqual(expected, keys) { 121 t.Errorf("Expected %v, Got %v", expected, keys) 122 } 123 }