forked from adamlaska/machine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_test.go
More file actions
60 lines (48 loc) · 1.61 KB
/
check_test.go
File metadata and controls
60 lines (48 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package check
import (
"errors"
"testing"
"crypto/tls"
"github.com/docker/machine/libmachine/auth"
"github.com/docker/machine/libmachine/cert"
"github.com/stretchr/testify/assert"
)
type FakeValidateCertificate struct {
IsValid bool
Err error
}
type FakeCertGenerator struct {
fakeValidateCertificate *FakeValidateCertificate
}
func (fcg FakeCertGenerator) GenerateCACertificate(certFile, keyFile, org string, bits int) error {
return nil
}
func (fcg FakeCertGenerator) GenerateCert(opts *cert.Options) error {
return nil
}
func (fcg FakeCertGenerator) ValidateCertificate(addr string, authOptions *auth.Options) (bool, error) {
return fcg.fakeValidateCertificate.IsValid, fcg.fakeValidateCertificate.Err
}
func (fcg FakeCertGenerator) ReadTLSConfig(addr string, authOptions *auth.Options) (*tls.Config, error) {
return nil, nil
}
func TestCheckCert(t *testing.T) {
errCertsExpired := errors.New("Certs have expired")
cases := []struct {
hostURL string
authOptions *auth.Options
valid bool
checkErr error
expectedErr error
}{
{"192.168.99.100:2376", &auth.Options{}, true, nil, nil},
{"192.168.99.100:2376", &auth.Options{}, false, nil, ErrCertInvalid{wrappedErr: nil, hostURL: "192.168.99.100:2376"}},
{"192.168.99.100:2376", &auth.Options{}, false, errCertsExpired, ErrCertInvalid{wrappedErr: errCertsExpired, hostURL: "192.168.99.100:2376"}},
}
for _, c := range cases {
fcg := FakeCertGenerator{fakeValidateCertificate: &FakeValidateCertificate{c.valid, c.checkErr}}
cert.SetCertGenerator(fcg)
err := checkCert(c.hostURL, c.authOptions)
assert.Equal(t, c.expectedErr, err)
}
}