github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/provider/gce/google/config_credentials_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package google_test 5 6 import ( 7 "bytes" 8 "encoding/json" 9 10 "github.com/juju/errors" 11 jc "github.com/juju/testing/checkers" 12 gc "gopkg.in/check.v1" 13 14 "github.com/juju/juju/provider/gce/google" 15 ) 16 17 type credentialsSuite struct { 18 google.BaseSuite 19 } 20 21 var _ = gc.Suite(&credentialsSuite{}) 22 23 func (s *credentialsSuite) TestNewCredentials(c *gc.C) { 24 values := map[string]string{ 25 google.OSEnvClientID: "abc", 26 google.OSEnvClientEmail: "xyz@g.com", 27 google.OSEnvPrivateKey: "<some-key>", 28 } 29 creds, err := google.NewCredentials(values) 30 c.Assert(err, jc.ErrorIsNil) 31 32 jsonKey := creds.JSONKey 33 creds.JSONKey = nil 34 c.Check(creds, jc.DeepEquals, &google.Credentials{ 35 ClientID: "abc", 36 ClientEmail: "xyz@g.com", 37 PrivateKey: []byte("<some-key>"), 38 }) 39 data := make(map[string]string) 40 err = json.Unmarshal(jsonKey, &data) 41 c.Assert(err, jc.ErrorIsNil) 42 c.Check(data, jc.DeepEquals, map[string]string{ 43 "type": "service_account", 44 "client_id": "abc", 45 "client_email": "xyz@g.com", 46 "private_key": "<some-key>", 47 }) 48 } 49 50 func (s *credentialsSuite) TestNewCredentialsEmpty(c *gc.C) { 51 creds, err := google.NewCredentials(nil) 52 c.Assert(err, jc.ErrorIsNil) 53 54 c.Check(creds, jc.DeepEquals, &google.Credentials{}) 55 } 56 57 func (s *credentialsSuite) TestNewCredentialsUnrecognized(c *gc.C) { 58 values := map[string]string{ 59 "spam": "eggs", 60 } 61 _, err := google.NewCredentials(values) 62 63 c.Check(err, jc.Satisfies, errors.IsNotSupported) 64 } 65 66 func (s *credentialsSuite) TestParseJSONKey(c *gc.C) { 67 original := ` 68 { 69 "private_key_id": "mnopq", 70 "private_key": "<some-key>", 71 "client_email": "xyz@g.com", 72 "client_id": "abc", 73 "type": "service_account" 74 }`[1:] 75 creds, err := google.ParseJSONKey(bytes.NewBufferString(original)) 76 c.Assert(err, jc.ErrorIsNil) 77 78 jsonKey := creds.JSONKey 79 creds.JSONKey = nil 80 c.Check(creds, jc.DeepEquals, &google.Credentials{ 81 ClientID: "abc", 82 ClientEmail: "xyz@g.com", 83 PrivateKey: []byte("<some-key>"), 84 }) 85 c.Check(string(jsonKey), gc.Equals, original) 86 } 87 88 func (s *credentialsSuite) TestCredentialsValues(c *gc.C) { 89 original := map[string]string{ 90 google.OSEnvClientID: "abc", 91 google.OSEnvClientEmail: "xyz@g.com", 92 google.OSEnvPrivateKey: "<some-key>", 93 } 94 creds, err := google.NewCredentials(original) 95 c.Assert(err, jc.ErrorIsNil) 96 values := creds.Values() 97 98 c.Check(values, jc.DeepEquals, original) 99 } 100 101 func (*credentialsSuite) TestValidateValid(c *gc.C) { 102 creds := &google.Credentials{ 103 ClientID: "spam", 104 ClientEmail: "user@mail.com", 105 PrivateKey: []byte("non-empty"), 106 } 107 err := creds.Validate() 108 109 c.Check(err, jc.ErrorIsNil) 110 } 111 112 func (*credentialsSuite) TestValidateMissingID(c *gc.C) { 113 creds := &google.Credentials{ 114 ClientEmail: "user@mail.com", 115 PrivateKey: []byte("non-empty"), 116 } 117 err := creds.Validate() 118 119 c.Assert(err, jc.Satisfies, google.IsInvalidConfigValue) 120 c.Check(err.(*google.InvalidConfigValue).Key, gc.Equals, "GCE_CLIENT_ID") 121 } 122 123 func (*credentialsSuite) TestValidateBadEmail(c *gc.C) { 124 creds := &google.Credentials{ 125 ClientID: "spam", 126 ClientEmail: "bad_email", 127 PrivateKey: []byte("non-empty"), 128 } 129 err := creds.Validate() 130 131 c.Assert(err, jc.Satisfies, google.IsInvalidConfigValue) 132 c.Check(err.(*google.InvalidConfigValue).Key, gc.Equals, "GCE_CLIENT_EMAIL") 133 } 134 135 func (*credentialsSuite) TestValidateMissingKey(c *gc.C) { 136 creds := &google.Credentials{ 137 ClientID: "spam", 138 ClientEmail: "user@mail.com", 139 } 140 err := creds.Validate() 141 142 c.Assert(err, jc.Satisfies, google.IsInvalidConfigValue) 143 c.Check(err.(*google.InvalidConfigValue).Key, gc.Equals, "GCE_PRIVATE_KEY") 144 }