github.com/mwhudson/juju@v0.0.0-20160512215208-90ff01f3497f/environs/testing/credentials.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package testing 5 6 import ( 7 "fmt" 8 9 "github.com/juju/errors" 10 jc "github.com/juju/testing/checkers" 11 gc "gopkg.in/check.v1" 12 13 "github.com/juju/juju/cloud" 14 "github.com/juju/juju/environs" 15 ) 16 17 // AssertProviderAuthTypes asserts that the given provider has credential 18 // schemas for exactly the specified set of authentication types. 19 func AssertProviderAuthTypes(c *gc.C, p environs.EnvironProvider, expectedAuthTypes ...cloud.AuthType) { 20 var authTypes []cloud.AuthType 21 for authType := range p.CredentialSchemas() { 22 authTypes = append(authTypes, authType) 23 } 24 c.Assert(authTypes, jc.SameContents, expectedAuthTypes) 25 } 26 27 // AssertProviderCredentialsValid asserts that the given provider is 28 // able to validate the given authentication type and credential 29 // attributes; and that removing any one of the attributes will cause 30 // the validation to fail. 31 func AssertProviderCredentialsValid(c *gc.C, p environs.EnvironProvider, authType cloud.AuthType, attrs map[string]string) { 32 schema, ok := p.CredentialSchemas()[authType] 33 c.Assert(ok, jc.IsTrue, gc.Commentf("missing schema for %q auth-type", authType)) 34 validate := func(attrs map[string]string) error { 35 _, err := schema.Finalize(attrs, func(string) ([]byte, error) { 36 return nil, errors.NotSupportedf("reading files") 37 }) 38 return err 39 } 40 41 err := validate(attrs) 42 c.Assert(err, jc.ErrorIsNil) 43 44 for excludedKey := range attrs { 45 field, _ := schema.Attribute(excludedKey) 46 if field.Optional { 47 continue 48 } 49 reducedAttrs := make(map[string]string) 50 for key, value := range attrs { 51 if key != excludedKey { 52 reducedAttrs[key] = value 53 } 54 } 55 err := validate(reducedAttrs) 56 if field.FileAttr != "" { 57 c.Assert(err, gc.ErrorMatches, fmt.Sprintf( 58 `either %q or %q must be specified`, excludedKey, field.FileAttr), 59 ) 60 } else { 61 c.Assert(err, gc.ErrorMatches, excludedKey+": expected string, got nothing") 62 } 63 } 64 } 65 66 // AssertProviderCredentialsAttributesHidden asserts that the provider 67 // credentials schema for the given provider and authentication type 68 // marks the specified attributes (and only those attributes) as being 69 // hidden. 70 func AssertProviderCredentialsAttributesHidden(c *gc.C, p environs.EnvironProvider, authType cloud.AuthType, expectedHidden ...string) { 71 var hidden []string 72 schema, ok := p.CredentialSchemas()[authType] 73 c.Assert(ok, jc.IsTrue, gc.Commentf("missing schema for %q auth-type", authType)) 74 for _, field := range schema { 75 if field.Hidden { 76 hidden = append(hidden, field.Name) 77 } 78 } 79 c.Assert(hidden, jc.SameContents, expectedHidden) 80 }