forked from IBM/vpc-node-label-updater
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from dobsonj/rebase-v4.1.0
Rebase ibm-vpc-node-label-updater on v4.1.0
- Loading branch information
Showing
39 changed files
with
17,036 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
/** | ||
* Copyright 2020 IBM Corp. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
//Package nodeupdater ... | ||
package nodeupdater | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
errors "errors" | ||
"fmt" | ||
"net/url" | ||
"strings" | ||
"testing" | ||
|
||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
) | ||
|
||
// MockVPCNodeLabelUpdater ... | ||
type MockVPCNodeLabelUpdater struct { | ||
Node *v1.Node | ||
K8sClient *kubernetes.Clientset | ||
Logger *zap.Logger | ||
StorageSecretConfig *StorageSecretConfig | ||
} | ||
|
||
// GetTestLogger ... | ||
func GetTestLogger(t *testing.T) (logger *zap.Logger, teardown func()) { | ||
atom := zap.NewAtomicLevel() | ||
atom.SetLevel(zap.DebugLevel) | ||
|
||
encoderCfg := zap.NewProductionEncoderConfig() | ||
encoderCfg.TimeKey = "timestamp" | ||
encoderCfg.EncodeTime = zapcore.ISO8601TimeEncoder | ||
|
||
buf := &bytes.Buffer{} | ||
|
||
logger = zap.New( | ||
zapcore.NewCore( | ||
zapcore.NewJSONEncoder(encoderCfg), | ||
zapcore.AddSync(buf), | ||
atom, | ||
), | ||
zap.AddCaller(), | ||
) | ||
|
||
teardown = func() { | ||
_ = logger.Sync() // #nosec G104: flush any buffered log entries only on best-effort basis.Error cannot be usefully handled. | ||
if t.Failed() { | ||
t.Log(buf) | ||
} | ||
} | ||
return | ||
} | ||
|
||
func initMockNodeLabelUpdater(t *testing.T) *MockVPCNodeLabelUpdater { | ||
logger, teardown := GetTestLogger(t) | ||
defer teardown() | ||
mockVPCNodeLabelUpdater := &MockVPCNodeLabelUpdater{ | ||
Node: &v1.Node{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "fake-node", | ||
Namespace: "fake", | ||
Labels: map[string]string{"test": "test"}, | ||
}, | ||
}, | ||
Logger: logger, | ||
StorageSecretConfig: &StorageSecretConfig{}, | ||
K8sClient: nil, | ||
} | ||
return mockVPCNodeLabelUpdater | ||
} | ||
|
||
// GetWorkerDetails ... | ||
func (m *MockVPCNodeLabelUpdater) GetWorkerDetails(workerNodeName string) (*NodeInfo, error) { | ||
if strings.Contains(workerNodeName, "ip") { | ||
return m.GetInstanceByIP(workerNodeName) | ||
} | ||
return m.GetInstanceByName(workerNodeName) | ||
} | ||
|
||
// GetInstancesFromVPC ... | ||
func (m *MockVPCNodeLabelUpdater) GetInstancesFromVPC(riaasInstanceURL *url.URL) ([]*Instance, error) { | ||
fmt.Println(m.StorageSecretConfig.IAMAccessToken) | ||
if m.StorageSecretConfig.IAMAccessToken == "" { | ||
return nil, errors.New("failed to get worker details as instance list is empty") | ||
} | ||
if riaasInstanceURL.Scheme == "" { | ||
return nil, errors.New("get \"\": unsupported protocol scheme \"\"") | ||
} | ||
if strings.Contains(riaasInstanceURL.Host, "invalid") { | ||
return nil, errors.New("get \"https://invalid\": dial tcp: lookup invalid") | ||
} | ||
ins := &Instance{ | ||
Name: "valid-worker", | ||
ID: "valid-instance-id", | ||
PrimaryNetworkInterface: &NetworkInterface{ | ||
PrimaryIpv4Address: "valid-worker-ip", | ||
}, | ||
} | ||
insL := []*Instance{ins} | ||
return insL, nil | ||
} | ||
|
||
// GetInstanceByName ... | ||
func (m *MockVPCNodeLabelUpdater) GetInstanceByName(workerNodeName string) (*NodeInfo, error) { | ||
if strings.Contains(workerNodeName, "invalid") { | ||
return nil, errors.New("failed to get worker details as instance list is empty") | ||
} | ||
insL, err := m.GetInstancesFromVPC(m.StorageSecretConfig.RiaasEndpointURL) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return m.getNodeInfo(insL[0]) | ||
} | ||
|
||
// GetInstanceByIP ... | ||
func (m *MockVPCNodeLabelUpdater) GetInstanceByIP(workerNodeName string) (*NodeInfo, error) { | ||
if strings.Contains(workerNodeName, "invalid-ip") { | ||
return nil, errors.New("failed to get worker details as instance list is empty") | ||
} | ||
if m.StorageSecretConfig.IAMAccessToken == "" { | ||
return nil, errors.New("failed to get worker details as instance list is empty") | ||
} | ||
if m.StorageSecretConfig.RiaasEndpointURL.Scheme == "" { | ||
return nil, errors.New("get \"\": unsupported protocol scheme \"\"") | ||
} | ||
if strings.Contains(m.StorageSecretConfig.RiaasEndpointURL.Host, "invalid") { | ||
return nil, errors.New("get \"https://invalid\": dial tcp: lookup invalid") | ||
} | ||
if strings.Contains(workerNodeName, "valid-worker-ip") { | ||
return &NodeInfo{}, nil | ||
} | ||
return nil, errors.New("") | ||
} | ||
|
||
func (m *MockVPCNodeLabelUpdater) getNodeInfo(instance *Instance) (*NodeInfo, error) { | ||
if instance == nil { | ||
return nil, errors.New("instance is nil") | ||
} | ||
return &NodeInfo{ | ||
InstanceID: instance.ID, | ||
Zone: "valid-zone", | ||
}, nil | ||
} | ||
|
||
// UpdateNodeLabel ... | ||
func (m *MockVPCNodeLabelUpdater) UpdateNodeLabel(ctx context.Context, workerNodeName string) (done bool, err error) { | ||
if strings.Contains(workerNodeName, "valid") { | ||
return true, nil | ||
} | ||
return false, errors.New("") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/** | ||
* Copyright 2020 IBM Corp. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
//Package nodeupdater ... | ||
package nodeupdater | ||
|
||
import ( | ||
"context" | ||
errors "errors" | ||
"net/url" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestUpdateNodeLabel(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
workerNodeName string | ||
riaasInstanceURL string | ||
accessToken string | ||
expErr error | ||
}{ | ||
{ | ||
name: "valid Request", | ||
workerNodeName: "valid-worker", | ||
accessToken: "", | ||
riaasInstanceURL: "https://us-south.iaas.cloud.ibm.com", | ||
expErr: nil, | ||
}, | ||
{ | ||
name: "empty accessToken", | ||
workerNodeName: "valid-worker", | ||
accessToken: "", | ||
riaasInstanceURL: "https://us-south.iaas.cloud.ibm.com", | ||
expErr: errors.New("failed to unmarshal json response of instances"), | ||
}, | ||
|
||
{ | ||
name: "Empty riaasInstanceURL", | ||
riaasInstanceURL: "", | ||
workerNodeName: "", | ||
accessToken: "valid-token", | ||
expErr: errors.New("Get \"?name=\": unsupported protocol scheme \"\""), //nolint | ||
}, | ||
{ | ||
name: "invalid riaasInstanceURL", | ||
workerNodeName: "", | ||
riaasInstanceURL: "https://invalid", | ||
accessToken: "valid-token", | ||
expErr: errors.New("Get \"https://invalid?name=\": dial tcp: lookup invalid"), //nolint | ||
}, | ||
} | ||
mockupdater := initNodeLabelUpdater(t) | ||
|
||
for _, tc := range testCases { | ||
if tc.name == "valid Request" { | ||
mockupdater := initMockNodeLabelUpdater(t) | ||
_, err := mockupdater.UpdateNodeLabel(context.TODO(), tc.workerNodeName) | ||
assert.Nil(t, err) | ||
} else { | ||
t.Logf("Test case: %s", tc.name) | ||
riaasInsURL, _ := url.Parse(tc.riaasInstanceURL) | ||
mockupdater.StorageSecretConfig.IAMAccessToken = tc.accessToken | ||
mockupdater.StorageSecretConfig.RiaasEndpointURL = riaasInsURL | ||
_, err := mockupdater.UpdateNodeLabel(context.TODO(), tc.workerNodeName) | ||
if err != nil { | ||
if err.Error() != tc.expErr.Error() && !strings.Contains(err.Error(), tc.expErr.Error()) { | ||
t.Fatalf("Expected error : %v, got: %v. err : %v", tc.expErr, err, err) | ||
} | ||
} | ||
continue | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[header] | ||
id = 1 | ||
name = "test" | ||
yesOrNo = true | ||
pi = 3.14 | ||
list = "1, 2" | ||
[vpc] | ||
vpc_enabled = true | ||
vpc_block_provider_name = "vpc-classic" | ||
gc_token_exchange_endpoint_url = "https://iam.bluemix.net" | ||
gc_riaas_endpoint_url = "https://us-south.iaas.cloud.ibm.com:443" | ||
gc_resource_group_id = "RESOURCEGROUP" | ||
gc_api_key = "IAM_API_KEY" | ||
encryption = false | ||
vpc_timeout = "120s" | ||
max_retry_attempt = 10 | ||
max_retry_gap = 60 | ||
api_version = "2019-07-02" |
Oops, something went wrong.