github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/provider/oci/common/client_test.go (about) 1 // Copyright 2018 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package common_test 5 6 import ( 7 "fmt" 8 9 ocitesting "github.com/juju/juju/provider/oci/testing" 10 "github.com/juju/juju/testing" 11 gc "gopkg.in/check.v1" 12 13 "github.com/juju/juju/provider/oci/common" 14 ) 15 16 type clientSuite struct { 17 testing.BaseSuite 18 19 config *common.JujuConfigProvider 20 } 21 22 var _ = gc.Suite(&clientSuite{}) 23 24 func (s *clientSuite) SetUpSuite(c *gc.C) { 25 s.BaseSuite.SetUpSuite(c) 26 } 27 28 func (s *clientSuite) SetUpTest(c *gc.C) { 29 s.BaseSuite.SetUpTest(c) 30 31 s.config = &common.JujuConfigProvider{ 32 Key: []byte(ocitesting.PrivateKeyUnencrypted), 33 Fingerprint: ocitesting.PrivateKeyUnencryptedFingerprint, 34 Tenancy: "fake", 35 User: "fake", 36 OCIRegion: "us-phoenix-1", 37 } 38 } 39 40 func (s *clientSuite) TestValidateKeyUnencrypted(c *gc.C) { 41 err := common.ValidateKey([]byte(ocitesting.PrivateKeyUnencrypted), "") 42 c.Assert(err, gc.IsNil) 43 44 err = common.ValidateKey([]byte(ocitesting.PrivateKeyUnencrypted), "so secret") 45 c.Assert(err, gc.IsNil) 46 47 err = common.ValidateKey([]byte("bogus"), "") 48 c.Check(err, gc.ErrorMatches, "invalid private key") 49 } 50 51 func (s *clientSuite) TestValidateKeyEncrypted(c *gc.C) { 52 err := common.ValidateKey([]byte(ocitesting.PrivateKeyEncrypted), ocitesting.PrivateKeyPassphrase) 53 c.Assert(err, gc.IsNil) 54 55 err = common.ValidateKey([]byte(ocitesting.PrivateKeyEncrypted), "wrong passphrase") 56 c.Assert(err, gc.ErrorMatches, "decrypting private key: x509: decryption password incorrect") 57 58 // empty passphrase 59 err = common.ValidateKey([]byte(ocitesting.PrivateKeyEncrypted), "") 60 c.Assert(err, gc.ErrorMatches, "decrypting private key: x509: decryption password incorrect") 61 } 62 63 func (s *clientSuite) TestTenancyOCID(c *gc.C) { 64 ocid, err := s.config.TenancyOCID() 65 c.Assert(err, gc.IsNil) 66 c.Check(ocid, gc.Equals, "fake") 67 68 s.config.Tenancy = "" 69 ocid, err = s.config.TenancyOCID() 70 c.Assert(err, gc.ErrorMatches, "tenancyOCID is not set") 71 c.Check(ocid, gc.Equals, "") 72 } 73 74 func (s *clientSuite) TestUserOCID(c *gc.C) { 75 ocid, err := s.config.UserOCID() 76 c.Assert(err, gc.IsNil) 77 c.Check(ocid, gc.Equals, "fake") 78 79 s.config.User = "" 80 ocid, err = s.config.UserOCID() 81 c.Assert(err, gc.ErrorMatches, "userOCID is not set") 82 c.Check(ocid, gc.Equals, "") 83 } 84 85 func (s *clientSuite) TestKeyFingerprint(c *gc.C) { 86 fp, err := s.config.KeyFingerprint() 87 c.Assert(err, gc.IsNil) 88 c.Check(fp, gc.Equals, ocitesting.PrivateKeyUnencryptedFingerprint) 89 90 s.config.Fingerprint = "" 91 fp, err = s.config.KeyFingerprint() 92 c.Assert(err, gc.ErrorMatches, "Fingerprint is not set") 93 c.Check(fp, gc.Equals, "") 94 } 95 96 func (s *clientSuite) TestRegion(c *gc.C) { 97 region, err := s.config.Region() 98 c.Assert(err, gc.IsNil) 99 c.Check(region, gc.Equals, "us-phoenix-1") 100 101 s.config.OCIRegion = "" 102 region, err = s.config.Region() 103 c.Assert(err, gc.ErrorMatches, "Region is not set") 104 c.Check(region, gc.Equals, "") 105 } 106 107 func (s *clientSuite) TestPrivateRSAKey(c *gc.C) { 108 pkey, err := s.config.PrivateRSAKey() 109 c.Assert(err, gc.IsNil) 110 c.Assert(pkey, gc.NotNil) 111 112 s.config.Key = []byte(ocitesting.PrivateKeyEncrypted) 113 pkey, err = s.config.PrivateRSAKey() 114 c.Assert(err, gc.ErrorMatches, "x509: decryption password incorrect") 115 c.Assert(pkey, gc.IsNil) 116 117 s.config.Passphrase = ocitesting.PrivateKeyPassphrase 118 pkey, err = s.config.PrivateRSAKey() 119 c.Assert(err, gc.IsNil) 120 } 121 122 func (s *clientSuite) TestKeyID(c *gc.C) { 123 id := fmt.Sprintf("%s/%s/%s", s.config.Tenancy, s.config.User, s.config.Fingerprint) 124 keyID, err := s.config.KeyID() 125 c.Assert(err, gc.IsNil) 126 c.Check(keyID, gc.Equals, id) 127 128 s.config.Tenancy = "" 129 keyID, err = s.config.KeyID() 130 c.Assert(err, gc.ErrorMatches, "config provider is not properly initialized") 131 }