|
1 | 1 | package ssh |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
| 5 | + "io/ioutil" |
| 6 | + "os" |
4 | 7 | "runtime" |
5 | 8 | "testing" |
6 | 9 |
|
@@ -46,39 +49,58 @@ func TestGetSSHCmdArgs(t *testing.T) { |
46 | 49 | } |
47 | 50 |
|
48 | 51 | func TestNewExternalClient(t *testing.T) { |
| 52 | + keyFile, err := ioutil.TempFile("", "docker-machine-tests-dummy-private-key") |
| 53 | + if err != nil { |
| 54 | + t.Fatal(err) |
| 55 | + } |
| 56 | + defer keyFile.Close() |
| 57 | + |
| 58 | + keyFilename := keyFile.Name() |
| 59 | + defer os.Remove(keyFilename) |
| 60 | + |
49 | 61 | cases := []struct { |
50 | 62 | sshBinaryPath string |
51 | 63 | user string |
52 | 64 | host string |
53 | 65 | port int |
54 | 66 | auth *Auth |
| 67 | + perm os.FileMode |
55 | 68 | expectedError string |
56 | 69 | skipOS string |
57 | 70 | }{ |
58 | 71 | { |
59 | | - sshBinaryPath: "/usr/local/bin/ssh", |
60 | | - user: "docker", |
61 | | - host: "localhost", |
62 | | - port: 22, |
63 | 72 | auth: &Auth{Keys: []string{"/tmp/private-key-not-exist"}}, |
64 | 73 | expectedError: "stat /tmp/private-key-not-exist: no such file or directory", |
65 | 74 | skipOS: "none", |
66 | 75 | }, |
67 | 76 | { |
68 | | - sshBinaryPath: "/usr/local/bin/ssh", |
69 | | - user: "docker", |
70 | | - host: "localhost", |
71 | | - port: 22, |
72 | | - auth: &Auth{Keys: []string{"/dev/null"}}, |
73 | | - expectedError: "Permissions 0410000666 for '/dev/null' are too open.", |
| 77 | + auth: &Auth{Keys: []string{keyFilename}}, |
| 78 | + perm: 0400, |
| 79 | + skipOS: "windows", |
| 80 | + }, |
| 81 | + { |
| 82 | + auth: &Auth{Keys: []string{keyFilename}}, |
| 83 | + perm: 0100, |
| 84 | + expectedError: fmt.Sprintf("'%s' is not readable", keyFilename), |
| 85 | + skipOS: "windows", |
| 86 | + }, |
| 87 | + { |
| 88 | + auth: &Auth{Keys: []string{keyFilename}}, |
| 89 | + perm: 0644, |
| 90 | + expectedError: fmt.Sprintf("permissions 0644 for '%s' are too open", keyFilename), |
74 | 91 | skipOS: "windows", |
75 | 92 | }, |
76 | 93 | } |
77 | 94 |
|
78 | 95 | for _, c := range cases { |
79 | 96 | if runtime.GOOS != c.skipOS { |
| 97 | + keyFile.Chmod(c.perm) |
80 | 98 | _, err := NewExternalClient(c.sshBinaryPath, c.user, c.host, c.port, c.auth) |
81 | | - assert.EqualError(t, err, c.expectedError) |
| 99 | + if c.expectedError != "" { |
| 100 | + assert.EqualError(t, err, c.expectedError) |
| 101 | + } else { |
| 102 | + assert.Equal(t, err, nil) |
| 103 | + } |
82 | 104 | } |
83 | 105 | } |
84 | 106 | } |
0 commit comments