mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-30 06:54:01 +00:00
Fix fake clients and unit tests
This commit is contained in:
parent
435df2b007
commit
19e45e35bd
@ -48,19 +48,10 @@ func newFakeAzureLBClient() *fakeAzureLBClient {
|
|||||||
return fLBC
|
return fLBC
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fLBC *fakeAzureLBClient) CreateOrUpdate(resourceGroupName string, loadBalancerName string, parameters network.LoadBalancer, cancel <-chan struct{}) (<-chan network.LoadBalancer, <-chan error) {
|
func (fLBC *fakeAzureLBClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, loadBalancerName string, parameters network.LoadBalancer) (resp *http.Response, err error) {
|
||||||
fLBC.mutex.Lock()
|
fLBC.mutex.Lock()
|
||||||
defer fLBC.mutex.Unlock()
|
defer fLBC.mutex.Unlock()
|
||||||
resultChan := make(chan network.LoadBalancer, 1)
|
|
||||||
errChan := make(chan error, 1)
|
|
||||||
var result network.LoadBalancer
|
|
||||||
var err error
|
|
||||||
defer func() {
|
|
||||||
resultChan <- result
|
|
||||||
errChan <- err
|
|
||||||
close(resultChan)
|
|
||||||
close(errChan)
|
|
||||||
}()
|
|
||||||
if _, ok := fLBC.FakeStore[resourceGroupName]; !ok {
|
if _, ok := fLBC.FakeStore[resourceGroupName]; !ok {
|
||||||
fLBC.FakeStore[resourceGroupName] = make(map[string]network.LoadBalancer)
|
fLBC.FakeStore[resourceGroupName] = make(map[string]network.LoadBalancer)
|
||||||
}
|
}
|
||||||
@ -76,48 +67,27 @@ func (fLBC *fakeAzureLBClient) CreateOrUpdate(resourceGroupName string, loadBala
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
fLBC.FakeStore[resourceGroupName][loadBalancerName] = parameters
|
fLBC.FakeStore[resourceGroupName][loadBalancerName] = parameters
|
||||||
result = fLBC.FakeStore[resourceGroupName][loadBalancerName]
|
|
||||||
result.Response.Response = &http.Response{
|
return nil, nil
|
||||||
StatusCode: http.StatusOK,
|
|
||||||
}
|
|
||||||
err = nil
|
|
||||||
return resultChan, errChan
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fLBC *fakeAzureLBClient) Delete(resourceGroupName string, loadBalancerName string, cancel <-chan struct{}) (<-chan autorest.Response, <-chan error) {
|
func (fLBC *fakeAzureLBClient) Delete(ctx context.Context, resourceGroupName string, loadBalancerName string) (resp *http.Response, err error) {
|
||||||
fLBC.mutex.Lock()
|
fLBC.mutex.Lock()
|
||||||
defer fLBC.mutex.Unlock()
|
defer fLBC.mutex.Unlock()
|
||||||
respChan := make(chan autorest.Response, 1)
|
|
||||||
errChan := make(chan error, 1)
|
|
||||||
var resp autorest.Response
|
|
||||||
var err error
|
|
||||||
defer func() {
|
|
||||||
respChan <- resp
|
|
||||||
errChan <- err
|
|
||||||
close(respChan)
|
|
||||||
close(errChan)
|
|
||||||
}()
|
|
||||||
if rgLBs, ok := fLBC.FakeStore[resourceGroupName]; ok {
|
if rgLBs, ok := fLBC.FakeStore[resourceGroupName]; ok {
|
||||||
if _, ok := rgLBs[loadBalancerName]; ok {
|
if _, ok := rgLBs[loadBalancerName]; ok {
|
||||||
delete(rgLBs, loadBalancerName)
|
delete(rgLBs, loadBalancerName)
|
||||||
resp.Response = &http.Response{
|
return nil, nil
|
||||||
StatusCode: http.StatusAccepted,
|
|
||||||
}
|
}
|
||||||
err = nil
|
|
||||||
return respChan, errChan
|
|
||||||
}
|
|
||||||
}
|
|
||||||
resp.Response = &http.Response{
|
|
||||||
StatusCode: http.StatusNotFound,
|
|
||||||
}
|
|
||||||
err = autorest.DetailedError{
|
|
||||||
StatusCode: http.StatusNotFound,
|
|
||||||
Message: "Not such LB",
|
|
||||||
}
|
|
||||||
return respChan, errChan
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fLBC *fakeAzureLBClient) Get(resourceGroupName string, loadBalancerName string, expand string) (result network.LoadBalancer, err error) {
|
return &http.Response{
|
||||||
|
StatusCode: http.StatusNotFound,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fLBC *fakeAzureLBClient) Get(ctx context.Context, resourceGroupName string, loadBalancerName string, expand string) (result network.LoadBalancer, err error) {
|
||||||
fLBC.mutex.Lock()
|
fLBC.mutex.Lock()
|
||||||
defer fLBC.mutex.Unlock()
|
defer fLBC.mutex.Unlock()
|
||||||
if _, ok := fLBC.FakeStore[resourceGroupName]; ok {
|
if _, ok := fLBC.FakeStore[resourceGroupName]; ok {
|
||||||
@ -131,48 +101,17 @@ func (fLBC *fakeAzureLBClient) Get(resourceGroupName string, loadBalancerName st
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type fakeLoadBalancerListResultPage struct {
|
func (fLBC *fakeAzureLBClient) List(ctx context.Context, resourceGroupName string) (result []network.LoadBalancer, err error) {
|
||||||
next LoadBalancerListResultPage
|
|
||||||
value network.LoadBalancerListResult
|
|
||||||
values []network.LoadBalancer
|
|
||||||
err error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (pg *fakeLoadBalancerListResultPage) Next() error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
func (pg *fakeLoadBalancerListResultPage) NotDone() bool {
|
|
||||||
return pg.next != nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (pg *fakeLoadBalancerListResultPage) Response() network.LoadBalancerListResult {
|
|
||||||
return pg.value
|
|
||||||
}
|
|
||||||
func (pg *fakeLoadBalancerListResultPage) Values() []network.LoadBalancer {
|
|
||||||
return pg.values
|
|
||||||
}
|
|
||||||
|
|
||||||
func (fLBC *fakeAzureLBClient) List(resourceGroupName string) (result LoadBalancerListResultPage, err error) {
|
|
||||||
fLBC.mutex.Lock()
|
fLBC.mutex.Lock()
|
||||||
defer fLBC.mutex.Unlock()
|
defer fLBC.mutex.Unlock()
|
||||||
|
|
||||||
var value []network.LoadBalancer
|
var value []network.LoadBalancer
|
||||||
if _, ok := fLBC.FakeStore[resourceGroupName]; ok {
|
if _, ok := fLBC.FakeStore[resourceGroupName]; ok {
|
||||||
for _, v := range fLBC.FakeStore[resourceGroupName] {
|
for _, v := range fLBC.FakeStore[resourceGroupName] {
|
||||||
value = append(value, v)
|
value = append(value, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return &fakeLoadBalancerListResultPage{
|
return value, nil
|
||||||
value: network.LoadBalancerListResult{
|
|
||||||
Value: &value,
|
|
||||||
},
|
|
||||||
values: value,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (fLBC *fakeAzureLBClient) ListNextResults(resourceGroupName string, lastResult LoadBalancerListResultPage) (result LoadBalancerListResultPage, err error) {
|
|
||||||
fLBC.mutex.Lock()
|
|
||||||
defer fLBC.mutex.Unlock()
|
|
||||||
return &fakeLoadBalancerListResultPage{}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type fakeAzurePIPClient struct {
|
type fakeAzurePIPClient struct {
|
||||||
@ -200,19 +139,10 @@ func newFakeAzurePIPClient(subscriptionID string) *fakeAzurePIPClient {
|
|||||||
return fAPC
|
return fAPC
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fAPC *fakeAzurePIPClient) CreateOrUpdate(resourceGroupName string, publicIPAddressName string, parameters network.PublicIPAddress, cancel <-chan struct{}) (<-chan network.PublicIPAddress, <-chan error) {
|
func (fAPC *fakeAzurePIPClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, publicIPAddressName string, parameters network.PublicIPAddress) (resp *http.Response, err error) {
|
||||||
fAPC.mutex.Lock()
|
fAPC.mutex.Lock()
|
||||||
defer fAPC.mutex.Unlock()
|
defer fAPC.mutex.Unlock()
|
||||||
resultChan := make(chan network.PublicIPAddress, 1)
|
|
||||||
errChan := make(chan error, 1)
|
|
||||||
var result network.PublicIPAddress
|
|
||||||
var err error
|
|
||||||
defer func() {
|
|
||||||
resultChan <- result
|
|
||||||
errChan <- err
|
|
||||||
close(resultChan)
|
|
||||||
close(errChan)
|
|
||||||
}()
|
|
||||||
if _, ok := fAPC.FakeStore[resourceGroupName]; !ok {
|
if _, ok := fAPC.FakeStore[resourceGroupName]; !ok {
|
||||||
fAPC.FakeStore[resourceGroupName] = make(map[string]network.PublicIPAddress)
|
fAPC.FakeStore[resourceGroupName] = make(map[string]network.PublicIPAddress)
|
||||||
}
|
}
|
||||||
@ -229,48 +159,27 @@ func (fAPC *fakeAzurePIPClient) CreateOrUpdate(resourceGroupName string, publicI
|
|||||||
}
|
}
|
||||||
|
|
||||||
fAPC.FakeStore[resourceGroupName][publicIPAddressName] = parameters
|
fAPC.FakeStore[resourceGroupName][publicIPAddressName] = parameters
|
||||||
result = fAPC.FakeStore[resourceGroupName][publicIPAddressName]
|
|
||||||
result.Response.Response = &http.Response{
|
return nil, nil
|
||||||
StatusCode: http.StatusOK,
|
|
||||||
}
|
|
||||||
err = nil
|
|
||||||
return resultChan, errChan
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fAPC *fakeAzurePIPClient) Delete(resourceGroupName string, publicIPAddressName string, cancel <-chan struct{}) (<-chan autorest.Response, <-chan error) {
|
func (fAPC *fakeAzurePIPClient) Delete(ctx context.Context, resourceGroupName string, publicIPAddressName string) (resp *http.Response, err error) {
|
||||||
fAPC.mutex.Lock()
|
fAPC.mutex.Lock()
|
||||||
defer fAPC.mutex.Unlock()
|
defer fAPC.mutex.Unlock()
|
||||||
respChan := make(chan autorest.Response, 1)
|
|
||||||
errChan := make(chan error, 1)
|
|
||||||
var resp autorest.Response
|
|
||||||
var err error
|
|
||||||
defer func() {
|
|
||||||
respChan <- resp
|
|
||||||
errChan <- err
|
|
||||||
close(respChan)
|
|
||||||
close(errChan)
|
|
||||||
}()
|
|
||||||
if rgPIPs, ok := fAPC.FakeStore[resourceGroupName]; ok {
|
if rgPIPs, ok := fAPC.FakeStore[resourceGroupName]; ok {
|
||||||
if _, ok := rgPIPs[publicIPAddressName]; ok {
|
if _, ok := rgPIPs[publicIPAddressName]; ok {
|
||||||
delete(rgPIPs, publicIPAddressName)
|
delete(rgPIPs, publicIPAddressName)
|
||||||
resp.Response = &http.Response{
|
return nil, nil
|
||||||
StatusCode: http.StatusAccepted,
|
|
||||||
}
|
}
|
||||||
err = nil
|
|
||||||
return respChan, errChan
|
|
||||||
}
|
|
||||||
}
|
|
||||||
resp.Response = &http.Response{
|
|
||||||
StatusCode: http.StatusNotFound,
|
|
||||||
}
|
|
||||||
err = autorest.DetailedError{
|
|
||||||
StatusCode: http.StatusNotFound,
|
|
||||||
Message: "Not such PIP",
|
|
||||||
}
|
|
||||||
return respChan, errChan
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fAPC *fakeAzurePIPClient) Get(resourceGroupName string, publicIPAddressName string, expand string) (result network.PublicIPAddress, err error) {
|
return &http.Response{
|
||||||
|
StatusCode: http.StatusNotFound,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fAPC *fakeAzurePIPClient) Get(ctx context.Context, resourceGroupName string, publicIPAddressName string, expand string) (result network.PublicIPAddress, err error) {
|
||||||
fAPC.mutex.Lock()
|
fAPC.mutex.Lock()
|
||||||
defer fAPC.mutex.Unlock()
|
defer fAPC.mutex.Unlock()
|
||||||
if _, ok := fAPC.FakeStore[resourceGroupName]; ok {
|
if _, ok := fAPC.FakeStore[resourceGroupName]; ok {
|
||||||
@ -284,49 +193,18 @@ func (fAPC *fakeAzurePIPClient) Get(resourceGroupName string, publicIPAddressNam
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type fakePublicIPAddressListResultPage struct {
|
func (fAPC *fakeAzurePIPClient) List(ctx context.Context, resourceGroupName string) (result []network.PublicIPAddress, err error) {
|
||||||
next PublicIPAddressListResultPage
|
|
||||||
value network.PublicIPAddressListResult
|
|
||||||
values []network.PublicIPAddress
|
|
||||||
err error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (pg *fakePublicIPAddressListResultPage) Next() error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
func (pg *fakePublicIPAddressListResultPage) NotDone() bool {
|
|
||||||
return pg.next != nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (pg *fakePublicIPAddressListResultPage) Response() network.PublicIPAddressListResult {
|
|
||||||
return pg.value
|
|
||||||
}
|
|
||||||
func (pg *fakePublicIPAddressListResultPage) Values() []network.PublicIPAddress {
|
|
||||||
return pg.values
|
|
||||||
}
|
|
||||||
|
|
||||||
func (fAPC *fakeAzurePIPClient) ListNextResults(resourceGroupName string, lastResults PublicIPAddressListResultPage) (result PublicIPAddressListResultPage, err error) {
|
|
||||||
fAPC.mutex.Lock()
|
fAPC.mutex.Lock()
|
||||||
defer fAPC.mutex.Unlock()
|
defer fAPC.mutex.Unlock()
|
||||||
return &fakePublicIPAddressListResultPage{}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (fAPC *fakeAzurePIPClient) List(resourceGroupName string) (result PublicIPAddressListResultPage, err error) {
|
|
||||||
fAPC.mutex.Lock()
|
|
||||||
defer fAPC.mutex.Unlock()
|
|
||||||
var value []network.PublicIPAddress
|
var value []network.PublicIPAddress
|
||||||
if _, ok := fAPC.FakeStore[resourceGroupName]; ok {
|
if _, ok := fAPC.FakeStore[resourceGroupName]; ok {
|
||||||
for _, v := range fAPC.FakeStore[resourceGroupName] {
|
for _, v := range fAPC.FakeStore[resourceGroupName] {
|
||||||
value = append(value, v)
|
value = append(value, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result = &fakePublicIPAddressListResultPage{
|
|
||||||
value: network.PublicIPAddressListResult{
|
return value, nil
|
||||||
Value: &value,
|
|
||||||
},
|
|
||||||
values: value,
|
|
||||||
}
|
|
||||||
return result, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type fakeAzureInterfacesClient struct {
|
type fakeAzureInterfacesClient struct {
|
||||||
|
@ -215,9 +215,11 @@ func testLoadBalancerServiceDefaultModeSelection(t *testing.T, isInternal bool)
|
|||||||
expectedLBName = testClusterName + "-internal"
|
expectedLBName = testClusterName + "-internal"
|
||||||
}
|
}
|
||||||
|
|
||||||
result, _ := az.LoadBalancerClient.List(az.Config.ResourceGroup)
|
ctx, cancel := getContextWithCancel()
|
||||||
lb := result.Values()[0]
|
defer cancel()
|
||||||
lbCount := len(result.Values())
|
result, _ := az.LoadBalancerClient.List(ctx, az.Config.ResourceGroup)
|
||||||
|
lb := result[0]
|
||||||
|
lbCount := len(result)
|
||||||
expectedNumOfLB := 1
|
expectedNumOfLB := 1
|
||||||
if lbCount != expectedNumOfLB {
|
if lbCount != expectedNumOfLB {
|
||||||
t.Errorf("Unexpected number of LB's: Expected (%d) Found (%d)", expectedNumOfLB, lbCount)
|
t.Errorf("Unexpected number of LB's: Expected (%d) Found (%d)", expectedNumOfLB, lbCount)
|
||||||
@ -265,15 +267,17 @@ func testLoadBalancerServiceAutoModeSelection(t *testing.T, isInternal bool) {
|
|||||||
|
|
||||||
// expected is MIN(index, availabilitySetCount)
|
// expected is MIN(index, availabilitySetCount)
|
||||||
expectedNumOfLB := int(math.Min(float64(index), float64(availabilitySetCount)))
|
expectedNumOfLB := int(math.Min(float64(index), float64(availabilitySetCount)))
|
||||||
result, _ := az.LoadBalancerClient.List(az.Config.ResourceGroup)
|
ctx, cancel := getContextWithCancel()
|
||||||
lbCount := len(result.Values())
|
defer cancel()
|
||||||
|
result, _ := az.LoadBalancerClient.List(ctx, az.Config.ResourceGroup)
|
||||||
|
lbCount := len(result)
|
||||||
if lbCount != expectedNumOfLB {
|
if lbCount != expectedNumOfLB {
|
||||||
t.Errorf("Unexpected number of LB's: Expected (%d) Found (%d)", expectedNumOfLB, lbCount)
|
t.Errorf("Unexpected number of LB's: Expected (%d) Found (%d)", expectedNumOfLB, lbCount)
|
||||||
}
|
}
|
||||||
|
|
||||||
maxRules := 0
|
maxRules := 0
|
||||||
minRules := serviceCount
|
minRules := serviceCount
|
||||||
for _, lb := range result.Values() {
|
for _, lb := range result {
|
||||||
ruleCount := len(*lb.LoadBalancingRules)
|
ruleCount := len(*lb.LoadBalancingRules)
|
||||||
if ruleCount < minRules {
|
if ruleCount < minRules {
|
||||||
minRules = ruleCount
|
minRules = ruleCount
|
||||||
@ -328,8 +332,10 @@ func testLoadBalancerServicesSpecifiedSelection(t *testing.T, isInternal bool) {
|
|||||||
|
|
||||||
// expected is MIN(index, 2)
|
// expected is MIN(index, 2)
|
||||||
expectedNumOfLB := int(math.Min(float64(index), float64(2)))
|
expectedNumOfLB := int(math.Min(float64(index), float64(2)))
|
||||||
result, _ := az.LoadBalancerClient.List(az.Config.ResourceGroup)
|
ctx, cancel := getContextWithCancel()
|
||||||
lbCount := len(result.Values())
|
defer cancel()
|
||||||
|
result, _ := az.LoadBalancerClient.List(ctx, az.Config.ResourceGroup)
|
||||||
|
lbCount := len(result)
|
||||||
if lbCount != expectedNumOfLB {
|
if lbCount != expectedNumOfLB {
|
||||||
t.Errorf("Unexpected number of LB's: Expected (%d) Found (%d)", expectedNumOfLB, lbCount)
|
t.Errorf("Unexpected number of LB's: Expected (%d) Found (%d)", expectedNumOfLB, lbCount)
|
||||||
}
|
}
|
||||||
@ -366,8 +372,10 @@ func testLoadBalancerMaxRulesServices(t *testing.T, isInternal bool) {
|
|||||||
|
|
||||||
// expected is MIN(index, az.Config.MaximumLoadBalancerRuleCount)
|
// expected is MIN(index, az.Config.MaximumLoadBalancerRuleCount)
|
||||||
expectedNumOfLBRules := int(math.Min(float64(index), float64(az.Config.MaximumLoadBalancerRuleCount)))
|
expectedNumOfLBRules := int(math.Min(float64(index), float64(az.Config.MaximumLoadBalancerRuleCount)))
|
||||||
result, _ := az.LoadBalancerClient.List(az.Config.ResourceGroup)
|
ctx, cancel := getContextWithCancel()
|
||||||
lbCount := len(result.Values())
|
defer cancel()
|
||||||
|
result, _ := az.LoadBalancerClient.List(ctx, az.Config.ResourceGroup)
|
||||||
|
lbCount := len(result)
|
||||||
if lbCount != expectedNumOfLBRules {
|
if lbCount != expectedNumOfLBRules {
|
||||||
t.Errorf("Unexpected number of LB's: Expected (%d) Found (%d)", expectedNumOfLBRules, lbCount)
|
t.Errorf("Unexpected number of LB's: Expected (%d) Found (%d)", expectedNumOfLBRules, lbCount)
|
||||||
}
|
}
|
||||||
@ -436,8 +444,10 @@ func testLoadBalancerServiceAutoModeDeleteSelection(t *testing.T, isInternal boo
|
|||||||
|
|
||||||
// expected is MIN(index, availabilitySetCount)
|
// expected is MIN(index, availabilitySetCount)
|
||||||
expectedNumOfLB := int(math.Min(float64(index), float64(availabilitySetCount)))
|
expectedNumOfLB := int(math.Min(float64(index), float64(availabilitySetCount)))
|
||||||
result, _ := az.LoadBalancerClient.List(az.Config.ResourceGroup)
|
ctx, cancel := getContextWithCancel()
|
||||||
lbCount := len(result.Values())
|
defer cancel()
|
||||||
|
result, _ := az.LoadBalancerClient.List(ctx, az.Config.ResourceGroup)
|
||||||
|
lbCount := len(result)
|
||||||
if lbCount != expectedNumOfLB {
|
if lbCount != expectedNumOfLB {
|
||||||
t.Errorf("Unexpected number of LB's: Expected (%d) Found (%d)", expectedNumOfLB, lbCount)
|
t.Errorf("Unexpected number of LB's: Expected (%d) Found (%d)", expectedNumOfLB, lbCount)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user