github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/provider/vsphere/provider_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package vsphere_test 5 6 import ( 7 "errors" 8 "net/url" 9 10 jc "github.com/juju/testing/checkers" 11 "github.com/juju/utils" 12 "golang.org/x/net/context" 13 gc "gopkg.in/check.v1" 14 "gopkg.in/yaml.v2" 15 16 "github.com/juju/juju/cloud" 17 "github.com/juju/juju/environs" 18 ) 19 20 type providerSuite struct { 21 ProviderFixture 22 } 23 24 var _ = gc.Suite(&providerSuite{}) 25 26 func (s *providerSuite) TestRegistered(c *gc.C) { 27 provider, err := environs.Provider("vsphere") 28 c.Assert(err, jc.ErrorIsNil) 29 c.Assert(provider, gc.NotNil) 30 } 31 32 func (s *providerSuite) TestOpen(c *gc.C) { 33 config := fakeConfig(c) 34 env, err := s.provider.Open(environs.OpenParams{ 35 Cloud: fakeCloudSpec(), 36 Config: config, 37 }) 38 c.Check(err, jc.ErrorIsNil) 39 40 envConfig := env.Config() 41 c.Assert(envConfig.Name(), gc.Equals, "testmodel") 42 } 43 44 func (s *providerSuite) TestOpenInvalidCloudSpec(c *gc.C) { 45 spec := fakeCloudSpec() 46 spec.Name = "" 47 s.testOpenError(c, spec, `validating cloud spec: cloud name "" not valid`) 48 } 49 50 func (s *providerSuite) TestOpenMissingCredential(c *gc.C) { 51 spec := fakeCloudSpec() 52 spec.Credential = nil 53 s.testOpenError(c, spec, `validating cloud spec: missing credential not valid`) 54 } 55 56 func (s *providerSuite) TestOpenUnsupportedCredential(c *gc.C) { 57 credential := cloud.NewCredential(cloud.OAuth1AuthType, map[string]string{}) 58 spec := fakeCloudSpec() 59 spec.Credential = &credential 60 s.testOpenError(c, spec, `validating cloud spec: "oauth1" auth-type not supported`) 61 } 62 63 func (s *providerSuite) testOpenError(c *gc.C, spec environs.CloudSpec, expect string) { 64 _, err := s.provider.Open(environs.OpenParams{ 65 Cloud: spec, 66 Config: fakeConfig(c), 67 }) 68 c.Assert(err, gc.ErrorMatches, expect) 69 } 70 71 func (s *providerSuite) TestPrepareConfig(c *gc.C) { 72 cfg, err := s.provider.PrepareConfig(environs.PrepareConfigParams{ 73 Config: fakeConfig(c), 74 Cloud: fakeCloudSpec(), 75 }) 76 c.Check(err, jc.ErrorIsNil) 77 c.Check(cfg, gc.NotNil) 78 } 79 80 func (s *providerSuite) TestValidate(c *gc.C) { 81 config := fakeConfig(c) 82 validCfg, err := s.provider.Validate(config, nil) 83 c.Check(err, jc.ErrorIsNil) 84 85 validAttrs := validCfg.AllAttrs() 86 c.Assert(config.AllAttrs(), gc.DeepEquals, validAttrs) 87 } 88 89 func (s *providerSuite) TestSchema(c *gc.C) { 90 y := []byte(` 91 auth-types: [userpass] 92 endpoint: http://foo.com/vsphere 93 regions: 94 foo: {} 95 bar: {} 96 `[1:]) 97 var v interface{} 98 err := yaml.Unmarshal(y, &v) 99 c.Assert(err, jc.ErrorIsNil) 100 v, err = utils.ConformYAML(v) 101 c.Assert(err, jc.ErrorIsNil) 102 103 err = s.provider.CloudSchema().Validate(v) 104 c.Assert(err, jc.ErrorIsNil) 105 } 106 107 type pingSuite struct { 108 ProviderFixture 109 } 110 111 var _ = gc.Suite(&pingSuite{}) 112 113 func (s *pingSuite) TestPingInvalidHost(c *gc.C) { 114 s.dialStub.SetErrors( 115 errors.New("foo"), 116 errors.New("bar"), 117 errors.New("baz"), 118 ) 119 tests := []string{ 120 "foo.com", 121 "http://foo.test", 122 "http://foo.test:77", 123 } 124 for _, t := range tests { 125 err := s.provider.Ping(s.callCtx, t) 126 if err == nil { 127 c.Errorf("ping %q: expected error, but got nil.", t) 128 continue 129 } 130 expected := "No vCenter/ESXi available at " + t 131 if err.Error() != expected { 132 c.Errorf("ping %q: expected %q got %v", t, expected, err) 133 } 134 } 135 } 136 137 func (s *pingSuite) TestPingInvalidURL(c *gc.C) { 138 err := s.provider.Ping(s.callCtx, "abc%sdef") 139 c.Assert(err, gc.ErrorMatches, "Invalid endpoint format, please give a full url or IP/hostname.") 140 } 141 142 func (s *pingSuite) TestPingInvalidScheme(c *gc.C) { 143 err := s.provider.Ping(s.callCtx, "gopher://abcdef.com") 144 c.Assert(err, gc.ErrorMatches, "Invalid endpoint format, please use an http or https URL.") 145 } 146 147 func (s *pingSuite) TestPingLoginSucceeded(c *gc.C) { 148 // This test shows that when - against all odds - the 149 // login succeeds, Ping returns nil. 150 151 err := s.provider.Ping(s.callCtx, "testing.invalid") 152 c.Assert(err, jc.ErrorIsNil) 153 154 s.dialStub.CheckCallNames(c, "Dial") 155 call := s.dialStub.Calls()[0] 156 c.Assert(call.Args, gc.HasLen, 3) 157 c.Assert(call.Args[0], gc.Implements, new(context.Context)) 158 c.Assert(call.Args[1], jc.DeepEquals, &url.URL{ 159 Scheme: "https", 160 Host: "testing.invalid", 161 Path: "/sdk", 162 User: url.User("juju"), 163 }) 164 c.Assert(call.Args[2], gc.Equals, "") 165 166 s.client.CheckCallNames(c, "Close") 167 }