github.com/scaleway/scaleway-cli@v1.11.1/pkg/sshcommand/sshcommand_test.go (about) 1 package sshcommand 2 3 import ( 4 "fmt" 5 "testing" 6 7 . "github.com/smartystreets/goconvey/convey" 8 ) 9 10 func ExampleCommand() { 11 cmd := Command{ 12 Host: "1.2.3.4", 13 } 14 15 // Do stuff 16 fmt.Println(cmd) 17 } 18 19 func ExampleCommand_options() { 20 cmd := Command{ 21 SkipHostKeyChecking: true, 22 Host: "1.2.3.4", 23 Quiet: true, 24 AllocateTTY: true, 25 Command: []string{"echo", "hello world"}, 26 Debug: true, 27 } 28 29 // Do stuff 30 fmt.Println(cmd) 31 } 32 33 func ExampleCommand_complex() { 34 cmd := Command{ 35 SkipHostKeyChecking: true, 36 Host: "1.2.3.4", 37 Quiet: true, 38 AllocateTTY: true, 39 Command: []string{"echo", "hello world"}, 40 Gateway: &Command{ 41 Host: "5.6.7.8", 42 User: "toor", 43 Quiet: true, 44 AllocateTTY: true, 45 }, 46 } 47 48 // Do stuff 49 fmt.Println(cmd) 50 } 51 52 func ExampleCommand_gateway() { 53 cmd := Command{ 54 Host: "1.2.3.4", 55 Gateway: New("5.6.7.8"), 56 } 57 58 // Do stuff 59 fmt.Println(cmd) 60 } 61 62 func ExampleNew() { 63 cmd := New("1.2.3.4") 64 65 // Do stuff 66 fmt.Println(cmd) 67 } 68 69 func ExampleCommand_String() { 70 fmt.Println(New("1.2.3.4").String()) 71 // Output: ssh 1.2.3.4 -p 22 72 } 73 74 func ExampleCommand_String_options() { 75 command := Command{ 76 SkipHostKeyChecking: true, 77 Host: "1.2.3.4", 78 Quiet: true, 79 AllocateTTY: true, 80 Command: []string{"echo", "hello world"}, 81 Debug: true, 82 } 83 fmt.Println(command.String()) 84 85 // Output: 86 // ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no 1.2.3.4 -t -t -p 22 -- /bin/sh -e -x -c "\"\\\"echo\\\" \\\"hello world\\\"\"" 87 } 88 89 func ExampleCommand_String_complex() { 90 command := Command{ 91 SkipHostKeyChecking: true, 92 Host: "1.2.3.4", 93 Quiet: true, 94 AllocateTTY: true, 95 Command: []string{"echo", "hello world"}, 96 Gateway: &Command{ 97 SkipHostKeyChecking: true, 98 Host: "5.6.7.8", 99 User: "toor", 100 Quiet: true, 101 AllocateTTY: true, 102 }, 103 } 104 fmt.Println(command.String()) 105 106 // Output: 107 // ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o "ProxyCommand=ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -W %h:%p -l toor 5.6.7.8 -t -t -p 22" 1.2.3.4 -t -t -p 22 -- /bin/sh -e -c "\"\\\"echo\\\" \\\"hello world\\\"\"" 108 } 109 110 func ExampleCommand_Slice() { 111 fmt.Println(New("1.2.3.4").Slice()) 112 // Output: [ssh 1.2.3.4 -p 22] 113 } 114 115 func ExampleCommand_Slice_user() { 116 fmt.Println(New("root@1.2.3.4").Slice()) 117 // Output: [ssh -l root 1.2.3.4 -p 22] 118 } 119 120 func ExampleCommand_Slice_options() { 121 command := Command{ 122 SkipHostKeyChecking: true, 123 Host: "1.2.3.4", 124 Quiet: true, 125 AllocateTTY: true, 126 Command: []string{"echo", "hello world"}, 127 Debug: true, 128 User: "root", 129 } 130 fmt.Printf("%q\n", command.Slice()) 131 132 // Output: 133 // ["ssh" "-q" "-o" "UserKnownHostsFile=/dev/null" "-o" "StrictHostKeyChecking=no" "-l" "root" "1.2.3.4" "-t" "-t" "-p" "22" "--" "/bin/sh" "-e" "-x" "-c" "\"\\\"echo\\\" \\\"hello world\\\"\""] 134 } 135 136 func ExampleCommand_Slice_gateway() { 137 command := Command{ 138 Host: "1.2.3.4", 139 Gateway: New("5.6.7.8"), 140 } 141 fmt.Printf("%q\n", command.Slice()) 142 143 // Output: 144 // ["ssh" "-o" "ProxyCommand=ssh -W %h:%p 5.6.7.8 -p 22" "1.2.3.4" "-p" "22"] 145 } 146 147 func ExampleCommand_Slice_complex() { 148 command := Command{ 149 SkipHostKeyChecking: true, 150 Host: "1.2.3.4", 151 Quiet: true, 152 AllocateTTY: true, 153 Command: []string{"echo", "hello world"}, 154 NoEscapeCommand: true, 155 Gateway: &Command{ 156 SkipHostKeyChecking: true, 157 Host: "5.6.7.8", 158 User: "toor", 159 Quiet: true, 160 AllocateTTY: true, 161 }, 162 } 163 fmt.Printf("%q\n", command.Slice()) 164 165 // Output: 166 // ["ssh" "-q" "-o" "UserKnownHostsFile=/dev/null" "-o" "StrictHostKeyChecking=no" "-o" "ProxyCommand=ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -W %h:%p -l toor 5.6.7.8 -t -t -p 22" "1.2.3.4" "-t" "-t" "-p" "22" "--" "/bin/sh" "-e" "-c" "\"echo hello world\""] 167 } 168 169 func TestCommand_defaults(t *testing.T) { 170 Convey("Testing Command{} default values", t, func() { 171 command := Command{} 172 So(command.Host, ShouldEqual, "") 173 So(command.Port, ShouldEqual, 0) 174 So(command.User, ShouldEqual, "") 175 So(command.SkipHostKeyChecking, ShouldEqual, false) 176 So(command.Quiet, ShouldEqual, false) 177 So(len(command.SSHOptions), ShouldEqual, 0) 178 So(command.Gateway, ShouldEqual, nil) 179 So(command.AllocateTTY, ShouldEqual, false) 180 So(len(command.Command), ShouldEqual, 0) 181 So(command.Debug, ShouldEqual, false) 182 So(command.NoEscapeCommand, ShouldEqual, false) 183 So(command.isGateway, ShouldEqual, false) 184 }) 185 } 186 187 func TestCommand_applyDefaults(t *testing.T) { 188 Convey("Testing Command.applyDefaults()", t, func() { 189 Convey("On a Command{}", func() { 190 command := Command{} 191 command.applyDefaults() 192 So(command.Host, ShouldEqual, "") 193 So(command.Port, ShouldEqual, 22) 194 So(command.User, ShouldEqual, "") 195 So(command.SkipHostKeyChecking, ShouldEqual, false) 196 So(command.Quiet, ShouldEqual, false) 197 So(len(command.SSHOptions), ShouldEqual, 0) 198 So(command.Gateway, ShouldEqual, nil) 199 So(command.AllocateTTY, ShouldEqual, false) 200 So(len(command.Command), ShouldEqual, 0) 201 So(command.Debug, ShouldEqual, false) 202 So(command.NoEscapeCommand, ShouldEqual, false) 203 So(command.isGateway, ShouldEqual, false) 204 }) 205 Convey("On a New(\"example.com\")", func() { 206 command := New("example.com") 207 command.applyDefaults() 208 So(command.Host, ShouldEqual, "example.com") 209 So(command.Port, ShouldEqual, 22) 210 So(command.User, ShouldEqual, "") 211 So(command.SkipHostKeyChecking, ShouldEqual, false) 212 So(command.Quiet, ShouldEqual, false) 213 So(len(command.SSHOptions), ShouldEqual, 0) 214 So(command.Gateway, ShouldEqual, nil) 215 So(command.AllocateTTY, ShouldEqual, false) 216 So(len(command.Command), ShouldEqual, 0) 217 So(command.Debug, ShouldEqual, false) 218 So(command.NoEscapeCommand, ShouldEqual, false) 219 So(command.isGateway, ShouldEqual, false) 220 }) 221 Convey("On a New(\"toto@example.com\")", func() { 222 command := New("toto@example.com") 223 command.applyDefaults() 224 So(command.Host, ShouldEqual, "example.com") 225 So(command.Port, ShouldEqual, 22) 226 So(command.User, ShouldEqual, "toto") 227 So(command.SkipHostKeyChecking, ShouldEqual, false) 228 So(command.Quiet, ShouldEqual, false) 229 So(len(command.SSHOptions), ShouldEqual, 0) 230 So(command.Gateway, ShouldEqual, nil) 231 So(command.AllocateTTY, ShouldEqual, false) 232 So(len(command.Command), ShouldEqual, 0) 233 So(command.Debug, ShouldEqual, false) 234 So(command.NoEscapeCommand, ShouldEqual, false) 235 So(command.isGateway, ShouldEqual, false) 236 }) 237 }) 238 }