Skip to content

Commit

Permalink
Ensure VPC and Bluemix are non-nil in IBM config (#299)
Browse files Browse the repository at this point in the history
* Ensure VPC and Bluemix are non-nil in IBM config

refactoring tests to use SL_* creds env vars
adding missing optional, but required api field

* Update client.go
  • Loading branch information
tdmanv authored and Ilya Kislenko committed Sep 18, 2019
1 parent 85f08dd commit 615f81d
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 20 deletions.
7 changes: 6 additions & 1 deletion pkg/blockstorage/ibm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,18 @@ func getDefIBMStoreSecret(ctx context.Context, args map[string]string) (*ibmcfg.
if err != nil {
return nil, errors.Wrap(err, "Failed to read Default IBM storage secret.")
}
retConfig := ibmcfg.Config{Softlayer: &softLayerCfg}
retConfig := ibmcfg.Config{
Softlayer: &softLayerCfg,
Bluemix: &ibmcfg.BluemixConfig{},
VPC: &ibmcfg.VPCProviderConfig{},
}
_, err = toml.Decode(string(storeSecret.Data[IBMK8sSecretData]), &retConfig)
if slapi, ok := args[SLAPIKeyArgName]; ok {
retConfig.Softlayer.SoftlayerAPIKey = slapi
}
if slusername, ok := args[SLAPIUsernameArgName]; ok {
retConfig.Softlayer.SoftlayerUsername = slusername
}

return &retConfig, err
}
43 changes: 42 additions & 1 deletion pkg/blockstorage/ibm/client_kube_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ import (
)

const (
testSecretName = "unitetestsecret"
testSecretName = "unitetestsecret"
testOldSecretName = "oldibmsecret"
oldTestTomlPath = "./testdata/correct/libconfig_old.toml"
)

type KubeTestIBMClient struct {
Expand Down Expand Up @@ -89,6 +91,45 @@ func (s KubeTestIBMClient) TestIBMSecret(c *C) {
c.Assert(err, IsNil)
}

func (s KubeTestIBMClient) TestIBMOldSecret(c *C) {
apiKey := os.Getenv(IBMApiKeyEnv)
err := os.Unsetenv(IBMApiKeyEnv)
c.Assert(err, IsNil)
defer os.Setenv(IBMApiKeyEnv, apiKey)
secData, err := ioutil.ReadFile(oldTestTomlPath)
c.Assert(err, IsNil)
secretData := make(map[string][]byte)
secretData[IBMK8sSecretData] = secData

s.k8scli, err = kube.NewClient()
c.Assert(err, IsNil)
k8sSec := v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: testOldSecretName,
},
Type: v1.SecretTypeOpaque,
Data: secretData,
}
s.k8sSec, err = s.k8scli.CoreV1().Secrets(IBMK8sSecretNS).Create(&k8sSec)
defer s.k8scli.CoreV1().Secrets(IBMK8sSecretNS).Delete(testOldSecretName, &metav1.DeleteOptions{})
c.Assert(err, IsNil)
slAPIKey, ok := os.LookupEnv(IBMSLApiKeyEnv)
c.Check(slAPIKey, NotNil)
c.Check(ok, Equals, true)
slAPIUsername, ok := os.LookupEnv(IBMSLApiUsernameEnv)
c.Check(slAPIUsername, NotNil)
c.Check(ok, Equals, true)
ibmCli, err := newClient(context.Background(), map[string]string{CfgSecretNameArgName: testOldSecretName, SLAPIKeyArgName: slAPIKey, SLAPIUsernameArgName: slAPIUsername})
c.Assert(err, IsNil)
c.Assert(ibmCli, NotNil)
c.Assert(ibmCli.Service, NotNil)
defer ibmCli.Service.Close()
defer s.k8scli.CoreV1().Secrets(IBMK8sSecretNS).Delete(testOldSecretName, &metav1.DeleteOptions{})
c.Assert(*ibmCli, FitsTypeOf, client{})
_, err = ibmCli.Service.ListSnapshots()
c.Assert(err, IsNil)
}

func (s *KubeTestIBMClient) TestSecretWSLApiKey(c *C) {
testSlKey := "TestSlKey"
testSlUserName := "TestUserName"
Expand Down
64 changes: 46 additions & 18 deletions pkg/blockstorage/ibm/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,37 @@ import (
)

const (
testTomlPath = "testdata/correct"
testBogusPath = "testdata/incorrect"
workAroundEnv = "IBM_STORE_TOML"
IBMApiKeyEnv = "IBM_API_KEY"
testTomlPath = "testdata/correct"
testBogusPath = "testdata/incorrect"
workAroundEnv = "IBM_STORE_TOML"
IBMApiKeyEnv = "IBM_API_KEY"
IBMSLApiKeyEnv = "IBM_SL_API_KEY"
IBMSLApiUsernameEnv = "IBM_SL_API_USERNAME"
)

//These are not executed as part of Pipeline, but usefull for development
// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) { TestingT(t) }

type ClientSuite struct {
apiKey string
apiKey string
slAPIKey string
slAPIUsername string
}

var _ = Suite(&ClientSuite{})

func (s *ClientSuite) SetUpSuite(c *C) {
if os.Getenv(IBMApiKeyEnv) == "" {
c.Skip(IBMApiKeyEnv + " envionment variable not set")
var ok bool
if s.slAPIKey, ok = os.LookupEnv(IBMSLApiKeyEnv); ok {
if s.slAPIUsername, ok = os.LookupEnv(IBMSLApiUsernameEnv); ok {
return
}
}
s.apiKey = os.Getenv(IBMApiKeyEnv)
if s.apiKey, ok = os.LookupEnv(IBMApiKeyEnv); ok {
return
}
c.Skip(fmt.Sprintf("One of %s, %s and %s environment variable is not set", IBMApiKeyEnv, IBMSLApiKeyEnv, IBMSLApiUsernameEnv))
}

func (s *ClientSuite) TearDownSuite(c *C) {
Expand All @@ -56,13 +66,15 @@ func (s *ClientSuite) TearDownSuite(c *C) {
}

func (s *ClientSuite) TestAPIClient(c *C) {
var apiKey string
if apiK, ok := os.LookupEnv(IBMApiKeyEnv); ok {
apiKey = apiK
if tomlPath, ok := os.LookupEnv(workAroundEnv); ok {
err := os.Setenv(LibDefCfgEnv, filepath.Dir(tomlPath))
c.Assert(err, IsNil)
defer os.Unsetenv(LibDefCfgEnv)
} else {
c.Skip(fmt.Sprintf("Could not find env var %s with API key", IBMApiKeyEnv))
c.Skip(workAroundEnv + " TOML path is not present")
}
ibmCli, err := newClient(context.Background(), map[string]string{APIKeyArgName: apiKey})
args := s.getCredsMap(c)
ibmCli, err := newClient(context.Background(), args)
c.Assert(err, IsNil)
c.Assert(ibmCli, NotNil)
c.Assert(ibmCli.Service, NotNil)
Expand All @@ -73,13 +85,16 @@ func (s *ClientSuite) TestAPIClient(c *C) {
}

func (s *ClientSuite) TestIBMClientSoftlayerFile(c *C) {
var apiKey string
if apiK, ok := os.LookupEnv(IBMApiKeyEnv); ok {
apiKey = apiK
if tomlPath, ok := os.LookupEnv(workAroundEnv); ok {
err := os.Setenv(LibDefCfgEnv, filepath.Dir(tomlPath))
c.Assert(err, IsNil)
defer os.Unsetenv(LibDefCfgEnv)
} else {
c.Skip(fmt.Sprintf("Could not find env var %s with API key", IBMApiKeyEnv))
c.Skip(workAroundEnv + " TOML path is not present")
}
ibmCli, err := newClient(context.Background(), map[string]string{APIKeyArgName: apiKey, SoftlayerFileAttName: "true"})
args := s.getCredsMap(c)
args[SoftlayerFileAttName] = "true"
ibmCli, err := newClient(context.Background(), args)
defer ibmCli.Service.Close()
c.Assert(err, IsNil)
c.Assert(ibmCli.Service, NotNil)
Expand Down Expand Up @@ -121,8 +136,21 @@ func (s *ClientSuite) TestErrorsCases(c *C) {
c.Assert(err, NotNil)
c.Assert(ibmCli, IsNil)
err = os.Setenv(LibDefCfgEnv, testBogusPath)
defer os.Unsetenv(LibDefCfgEnv)
c.Assert(err, IsNil)
ibmCli, err = newClient(context.Background(), make(map[string]string))
c.Assert(err, NotNil)
c.Assert(ibmCli, IsNil)

}

func (s *ClientSuite) getCredsMap(c *C) map[string]string {
if s.slAPIKey != "" {
return map[string]string{SLAPIKeyArgName: s.slAPIKey, SLAPIUsernameArgName: s.slAPIUsername}
}
if s.apiKey != "" {
return map[string]string{APIKeyArgName: s.apiKey}
}
c.Skip(fmt.Sprintf("Neither of %s, %s environment variables set", IBMApiKeyEnv, IBMSLApiKeyEnv))
return map[string]string{}
}
21 changes: 21 additions & 0 deletions pkg/blockstorage/ibm/testdata/correct/libconfig_old.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[bluemix]
iam_url = "https://iam.bluemix.net"
iam_client_id = "bx"
iam_client_secret = "bx"
iam_api_key = "free"
refresh_token = ""
containers_api_route = "https://origin.containers.test.cloud.ibm.com"

[softlayer]
softlayer_block_enabled = true
softlayer_block_provider_name = "SOFTLAYER-BLOCK"
softlayer_file_enabled = false
softlayer_file_provider_name = "SOFTLAYER-FILE"
softlayer_username = ""
softlayer_api_key = ""
softlayer_endpoint_url = "https://api.softlayer.com/rest/v3"
softlayer_iam_endpoint_url = "https://api.softlayer.com/mobile/v3"
softlayer_datacenter = "sjc03"
softlayer_api_timeout = "30s"
softlayer_vol_provision_timeout = "30m"
softlayer_api_retry_interval = "5s"

0 comments on commit 615f81d

Please sign in to comment.