github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/provider/vsphere/fixture_test.go (about) 1 // Copyright 2017 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package vsphere_test 5 6 import ( 7 "fmt" 8 "net/http" 9 "net/http/httptest" 10 11 "github.com/juju/testing" 12 jc "github.com/juju/testing/checkers" 13 "github.com/vmware/govmomi/vim25/soap" 14 "github.com/vmware/govmomi/vim25/types" 15 gc "gopkg.in/check.v1" 16 17 "github.com/juju/juju/environs" 18 "github.com/juju/juju/environs/context" 19 "github.com/juju/juju/environs/imagemetadata" 20 "github.com/juju/juju/provider/vsphere" 21 "github.com/juju/juju/provider/vsphere/internal/ovatest" 22 coretesting "github.com/juju/juju/testing" 23 ) 24 25 type ProviderFixture struct { 26 testing.IsolationSuite 27 dialStub testing.Stub 28 client *mockClient 29 provider environs.CloudEnvironProvider 30 callCtx context.ProviderCallContext 31 } 32 33 func (s *ProviderFixture) SetUpTest(c *gc.C) { 34 s.IsolationSuite.SetUpTest(c) 35 s.dialStub.ResetCalls() 36 s.client = &mockClient{} 37 s.provider = vsphere.NewEnvironProvider(vsphere.EnvironProviderConfig{ 38 Dial: newMockDialFunc(&s.dialStub, s.client), 39 }) 40 s.callCtx = context.NewCloudCallContext() 41 } 42 43 type EnvironFixture struct { 44 ProviderFixture 45 imageServer *httptest.Server 46 imageServerRequests []*http.Request 47 env environs.Environ 48 callCtx context.ProviderCallContext 49 } 50 51 func (s *EnvironFixture) SetUpTest(c *gc.C) { 52 s.ProviderFixture.SetUpTest(c) 53 54 s.imageServerRequests = nil 55 s.imageServer = serveImageMetadata(&s.imageServerRequests) 56 s.AddCleanup(func(*gc.C) { 57 s.imageServer.Close() 58 }) 59 60 env, err := s.provider.Open(environs.OpenParams{ 61 Cloud: fakeCloudSpec(), 62 Config: fakeConfig(c, coretesting.Attrs{ 63 "image-metadata-url": s.imageServer.URL, 64 }), 65 }) 66 c.Assert(err, jc.ErrorIsNil) 67 s.env = env 68 69 // Make sure we don't fall back to the public image sources. 70 s.PatchValue(&imagemetadata.DefaultUbuntuBaseURL, "") 71 s.PatchValue(&imagemetadata.DefaultJujuBaseURL, "") 72 s.callCtx = context.NewCloudCallContext() 73 } 74 75 func serveImageMetadata(requests *[]*http.Request) *httptest.Server { 76 index := `{ 77 "index": { 78 "com.ubuntu.cloud:released:download": { 79 "datatype": "image-downloads", 80 "path": "streams/v1/com.ubuntu.cloud:released:download.json", 81 "updated": "Tue, 24 Feb 2015 10:16:54 +0000", 82 "products": ["com.ubuntu.cloud:server:14.04:amd64"], 83 "format": "products:1.0" 84 } 85 }, 86 "updated": "Tue, 24 Feb 2015 14:14:24 +0000", 87 "format": "index:1.0" 88 }` 89 90 download := fmt.Sprintf(`{ 91 "updated": "Thu, 05 Mar 2015 12:14:40 +0000", 92 "license": "http://www.canonical.com/intellectual-property-policy", 93 "format": "products:1.0", 94 "datatype": "image-downloads", 95 "products": { 96 "com.ubuntu.cloud:server:14.04:amd64": { 97 "release": "trusty", 98 "version": "14.04", 99 "arch": "amd64", 100 "versions": { 101 "20150305": { 102 "items": { 103 "ova": { 104 "size": 7196, 105 "path": "server/releases/trusty/release-20150305/ubuntu-14.04-server-cloudimg-amd64.ova", 106 "ftype": "ova", 107 "sha256": "%s", 108 "md5": "00662c59ca52558e7a3bb9a67d194730" 109 } 110 } 111 } 112 } 113 } 114 } 115 }`, ovatest.FakeOVASHA256()) 116 117 files := map[string][]byte{ 118 "/streams/v1/index.json": []byte(index), 119 "/streams/v1/com.ubuntu.cloud:released:download.json": []byte(download), 120 "/server/releases/trusty/release-20150305/ubuntu-14.04-server-cloudimg-amd64.ova": ovatest.FakeOVAContents(), 121 } 122 mux := http.NewServeMux() 123 for path := range files { 124 mux.HandleFunc(path, func(w http.ResponseWriter, req *http.Request) { 125 *requests = append(*requests, req) 126 w.Write(files[req.URL.Path]) 127 }) 128 } 129 return httptest.NewServer(mux) 130 } 131 132 func AssertInvalidatesCredential(c *gc.C, client *mockClient, f func(context.ProviderCallContext) error) { 133 client.SetErrors(soap.WrapSoapFault(&soap.Fault{ 134 Code: "ServerFaultCode", 135 String: "No way José", 136 Detail: struct { 137 Fault types.AnyType `xml:",any,typeattr"` 138 }{Fault: types.NoPermission{}}, 139 })) 140 var called bool 141 ctx := &context.CloudCallContext{ 142 InvalidateCredentialFunc: func(string) error { 143 called = true 144 return nil 145 }, 146 } 147 err := f(ctx) 148 c.Assert(err, gc.ErrorMatches, ".*ServerFaultCode: No way José$") 149 c.Assert(called, gc.Equals, true) 150 }