add TestMachineController interface to clusterapi
This change adds a new interface to the test controller with simplified access to adding configurations and objects. This change is being proposed to help make the tests easier to understand and expand.
This commit is contained in:
parent
6f0d49c47c
commit
0a460df4a9
|
|
@ -63,8 +63,9 @@ func TestControllerFindMachine(t *testing.T) {
|
|||
}}
|
||||
|
||||
test := func(t *testing.T, tc testCase, testConfig *TestConfig) {
|
||||
controller, stop := mustCreateTestController(t, testConfig)
|
||||
defer stop()
|
||||
controller := NewTestMachineController(t)
|
||||
defer controller.Stop()
|
||||
controller.AddTestConfigs(testConfig)
|
||||
|
||||
machine, err := controller.findMachine(path.Join(tc.namespace, tc.name))
|
||||
if err != nil {
|
||||
|
|
@ -128,8 +129,9 @@ func TestControllerFindMachineOwner(t *testing.T) {
|
|||
}).
|
||||
Build()
|
||||
|
||||
controller, stop := mustCreateTestController(t, testConfig)
|
||||
defer stop()
|
||||
controller := NewTestMachineController(t)
|
||||
defer controller.Stop()
|
||||
controller.AddTestConfigs(testConfig)
|
||||
|
||||
// Test #1: Lookup succeeds
|
||||
testResult1, err := controller.findMachineOwner(testConfig.machines[0].DeepCopy())
|
||||
|
|
@ -159,7 +161,7 @@ func TestControllerFindMachineOwner(t *testing.T) {
|
|||
}
|
||||
|
||||
// Test #3: Delete the MachineSet and lookup should fail
|
||||
if err := deleteResource(controller.managementClient, controller.machineSetInformer, controller.machineSetResource, testConfig.machineSet); err != nil {
|
||||
if err := controller.DeleteResource(controller.machineSetInformer, controller.machineSetResource, testConfig.machineSet); err != nil {
|
||||
t.Fatalf("unexpected error, got %v", err)
|
||||
}
|
||||
testResult3, err := controller.findMachineOwner(testConfig.machines[0].DeepCopy())
|
||||
|
|
@ -181,8 +183,9 @@ func TestControllerFindMachineByProviderID(t *testing.T) {
|
|||
}).
|
||||
Build()
|
||||
|
||||
controller, stop := mustCreateTestController(t, testConfig)
|
||||
defer stop()
|
||||
controller := NewTestMachineController(t)
|
||||
defer controller.Stop()
|
||||
controller.AddTestConfigs(testConfig)
|
||||
|
||||
// Remove all the "machine" annotation values on all the
|
||||
// nodes. We want to force findMachineByProviderID() to only
|
||||
|
|
@ -224,7 +227,7 @@ func TestControllerFindMachineByProviderID(t *testing.T) {
|
|||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if err := updateResource(controller.managementClient, controller.machineInformer, controller.machineResource, machine); err != nil {
|
||||
if err := controller.UpdateResource(controller.machineInformer, controller.machineResource, machine); err != nil {
|
||||
t.Fatalf("unexpected error updating machine, got %v", err)
|
||||
}
|
||||
|
||||
|
|
@ -247,8 +250,9 @@ func TestControllerFindNodeByNodeName(t *testing.T) {
|
|||
}).
|
||||
Build()
|
||||
|
||||
controller, stop := mustCreateTestController(t, testConfig)
|
||||
defer stop()
|
||||
controller := NewTestMachineController(t)
|
||||
defer controller.Stop()
|
||||
controller.AddTestConfigs(testConfig)
|
||||
|
||||
// Test #1: Verify known node can be found
|
||||
node, err := controller.findNodeByNodeName(testConfig.nodes[0].Name)
|
||||
|
|
@ -271,10 +275,11 @@ func TestControllerFindNodeByNodeName(t *testing.T) {
|
|||
|
||||
func TestControllerListMachinesForScalableResource(t *testing.T) {
|
||||
test := func(t *testing.T, testConfig1 *TestConfig, testConfig2 *TestConfig) {
|
||||
controller, stop := mustCreateTestController(t, testConfig1)
|
||||
defer stop()
|
||||
controller := NewTestMachineController(t)
|
||||
defer controller.Stop()
|
||||
controller.AddTestConfigs(testConfig1)
|
||||
|
||||
if err := addTestConfigs(t, controller, testConfig2); err != nil {
|
||||
if err := controller.AddTestConfigs(testConfig2); err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
|
|
@ -406,8 +411,9 @@ func TestControllerListMachinesForScalableResource(t *testing.T) {
|
|||
|
||||
func TestControllerLookupNodeGroupForNonExistentNode(t *testing.T) {
|
||||
test := func(t *testing.T, testConfig *TestConfig) {
|
||||
controller, stop := mustCreateTestController(t, testConfig)
|
||||
defer stop()
|
||||
controller := NewTestMachineController(t)
|
||||
defer controller.Stop()
|
||||
controller.AddTestConfigs(testConfig)
|
||||
|
||||
node := testConfig.nodes[0].DeepCopy()
|
||||
node.Spec.ProviderID = "does-not-exist"
|
||||
|
|
@ -452,13 +458,14 @@ func TestControllerLookupNodeGroupForNonExistentNode(t *testing.T) {
|
|||
|
||||
func TestControllerNodeGroupForNodeWithMissingMachineOwner(t *testing.T) {
|
||||
test := func(t *testing.T, testConfig *TestConfig) {
|
||||
controller, stop := mustCreateTestController(t, testConfig)
|
||||
defer stop()
|
||||
controller := NewTestMachineController(t)
|
||||
defer controller.Stop()
|
||||
controller.AddTestConfigs(testConfig)
|
||||
|
||||
machine := testConfig.machines[0].DeepCopy()
|
||||
machine.SetOwnerReferences([]metav1.OwnerReference{})
|
||||
|
||||
if err := updateResource(controller.managementClient, controller.machineInformer, controller.machineResource, machine); err != nil {
|
||||
if err := controller.UpdateResource(controller.machineInformer, controller.machineResource, machine); err != nil {
|
||||
t.Fatalf("unexpected error updating machine, got %v", err)
|
||||
}
|
||||
|
||||
|
|
@ -507,13 +514,14 @@ func TestControllerNodeGroupForNodeWithMissingSetMachineOwner(t *testing.T) {
|
|||
}).
|
||||
Build()
|
||||
|
||||
controller, stop := mustCreateTestController(t, testConfig)
|
||||
defer stop()
|
||||
controller := NewTestMachineController(t)
|
||||
defer controller.Stop()
|
||||
controller.AddTestConfigs(testConfig)
|
||||
|
||||
machineSet := testConfig.machineSet.DeepCopy()
|
||||
machineSet.SetOwnerReferences([]metav1.OwnerReference{})
|
||||
|
||||
if err := updateResource(controller.managementClient, controller.machineSetInformer, controller.machineSetResource, machineSet); err != nil {
|
||||
if err := controller.UpdateResource(controller.machineSetInformer, controller.machineSetResource, machineSet); err != nil {
|
||||
t.Fatalf("unexpected error updating machine, got %v", err)
|
||||
}
|
||||
|
||||
|
|
@ -529,8 +537,9 @@ func TestControllerNodeGroupForNodeWithMissingSetMachineOwner(t *testing.T) {
|
|||
|
||||
func TestControllerNodeGroupForNodeWithPositiveScalingBounds(t *testing.T) {
|
||||
test := func(t *testing.T, testConfig *TestConfig) {
|
||||
controller, stop := mustCreateTestController(t, testConfig)
|
||||
defer stop()
|
||||
controller := NewTestMachineController(t)
|
||||
defer controller.Stop()
|
||||
controller.AddTestConfigs(testConfig)
|
||||
|
||||
ng, err := controller.nodeGroupForNode(testConfig.nodes[0])
|
||||
if err != nil {
|
||||
|
|
@ -568,7 +577,7 @@ func TestControllerNodeGroupForNodeWithPositiveScalingBounds(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestControllerNodeGroups(t *testing.T) {
|
||||
assertNodegroupLen := func(t *testing.T, controller *machineController, expected int) {
|
||||
assertNodegroupLen := func(t *testing.T, controller *testMachineController, expected int) {
|
||||
t.Helper()
|
||||
nodegroups, err := controller.nodeGroups()
|
||||
if err != nil {
|
||||
|
|
@ -584,8 +593,8 @@ func TestControllerNodeGroups(t *testing.T) {
|
|||
nodeGroupMaxSizeAnnotationKey: "2",
|
||||
}
|
||||
|
||||
controller, stop := mustCreateTestController(t)
|
||||
defer stop()
|
||||
controller := NewTestMachineController(t)
|
||||
defer controller.Stop()
|
||||
|
||||
namespace := RandomString(6)
|
||||
clusterName := RandomString(6)
|
||||
|
|
@ -601,7 +610,7 @@ func TestControllerNodeGroups(t *testing.T) {
|
|||
WithNodeCount(1).
|
||||
WithAnnotations(annotations).
|
||||
BuildMultiple(5)
|
||||
if err := addTestConfigs(t, controller, machineSetConfigs...); err != nil {
|
||||
if err := controller.AddTestConfigs(machineSetConfigs...); err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
assertNodegroupLen(t, controller, 5)
|
||||
|
|
@ -614,19 +623,19 @@ func TestControllerNodeGroups(t *testing.T) {
|
|||
WithNodeCount(1).
|
||||
WithAnnotations(annotations).
|
||||
BuildMultiple(2)
|
||||
if err := addTestConfigs(t, controller, machineDeploymentConfigs...); err != nil {
|
||||
if err := controller.AddTestConfigs(machineDeploymentConfigs...); err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
assertNodegroupLen(t, controller, 7)
|
||||
|
||||
// Test #3: delete 5 machineset-backed objects
|
||||
if err := deleteTestConfigs(t, controller, machineSetConfigs...); err != nil {
|
||||
if err := controller.DeleteTestConfigs(machineSetConfigs...); err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
assertNodegroupLen(t, controller, 2)
|
||||
|
||||
// Test #4: delete 2 machinedeployment-backed objects
|
||||
if err := deleteTestConfigs(t, controller, machineDeploymentConfigs...); err != nil {
|
||||
if err := controller.DeleteTestConfigs(machineDeploymentConfigs...); err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
assertNodegroupLen(t, controller, 0)
|
||||
|
|
@ -644,7 +653,7 @@ func TestControllerNodeGroups(t *testing.T) {
|
|||
WithNodeCount(1).
|
||||
WithAnnotations(annotations).
|
||||
BuildMultiple(5)
|
||||
if err := addTestConfigs(t, controller, machineSetConfigs...); err != nil {
|
||||
if err := controller.AddTestConfigs(machineSetConfigs...); err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
assertNodegroupLen(t, controller, 5)
|
||||
|
|
@ -657,7 +666,7 @@ func TestControllerNodeGroups(t *testing.T) {
|
|||
WithNodeCount(1).
|
||||
WithAnnotations(annotations).
|
||||
BuildMultiple(2)
|
||||
if err := addTestConfigs(t, controller, machineDeploymentConfigs...); err != nil {
|
||||
if err := controller.AddTestConfigs(machineDeploymentConfigs...); err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
assertNodegroupLen(t, controller, 7)
|
||||
|
|
@ -675,7 +684,7 @@ func TestControllerNodeGroups(t *testing.T) {
|
|||
WithNodeCount(5).
|
||||
WithAnnotations(annotations).
|
||||
BuildMultiple(1)
|
||||
if err := addTestConfigs(t, controller, machineSetConfigs...); err != nil {
|
||||
if err := controller.AddTestConfigs(machineSetConfigs...); err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if _, err := controller.nodeGroups(); err == nil {
|
||||
|
|
@ -690,7 +699,7 @@ func TestControllerNodeGroups(t *testing.T) {
|
|||
WithNodeCount(2).
|
||||
WithAnnotations(annotations).
|
||||
BuildMultiple(1)
|
||||
if err := addTestConfigs(t, controller, machineDeploymentConfigs...); err != nil {
|
||||
if err := controller.AddTestConfigs(machineDeploymentConfigs...); err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if _, err := controller.nodeGroups(); err == nil {
|
||||
|
|
@ -724,8 +733,9 @@ func TestControllerNodeGroupsNodeCount(t *testing.T) {
|
|||
}}
|
||||
|
||||
test := func(t *testing.T, tc testCase, testConfigs []*TestConfig) {
|
||||
controller, stop := mustCreateTestController(t, testConfigs...)
|
||||
defer stop()
|
||||
controller := NewTestMachineController(t)
|
||||
defer controller.Stop()
|
||||
controller.AddTestConfigs(testConfigs...)
|
||||
|
||||
nodegroups, err := controller.nodeGroups()
|
||||
if err != nil {
|
||||
|
|
@ -784,8 +794,9 @@ func TestControllerFindMachineFromNodeAnnotation(t *testing.T) {
|
|||
}).
|
||||
Build()
|
||||
|
||||
controller, stop := mustCreateTestController(t, testConfig)
|
||||
defer stop()
|
||||
controller := NewTestMachineController(t)
|
||||
defer controller.Stop()
|
||||
controller.AddTestConfigs(testConfig)
|
||||
|
||||
// Remove all the provider ID values on all the machines. We
|
||||
// want to force findMachineByProviderID() to fallback to
|
||||
|
|
@ -793,7 +804,7 @@ func TestControllerFindMachineFromNodeAnnotation(t *testing.T) {
|
|||
for _, machine := range testConfig.machines {
|
||||
unstructured.RemoveNestedField(machine.Object, "spec", "providerID")
|
||||
|
||||
if err := updateResource(controller.managementClient, controller.machineInformer, controller.machineResource, machine); err != nil {
|
||||
if err := controller.UpdateResource(controller.machineInformer, controller.machineResource, machine); err != nil {
|
||||
t.Fatalf("unexpected error updating machine, got %v", err)
|
||||
}
|
||||
}
|
||||
|
|
@ -836,8 +847,9 @@ func TestControllerMachineSetNodeNamesWithoutLinkage(t *testing.T) {
|
|||
}).
|
||||
Build()
|
||||
|
||||
controller, stop := mustCreateTestController(t, testConfig)
|
||||
defer stop()
|
||||
controller := NewTestMachineController(t)
|
||||
defer controller.Stop()
|
||||
controller.AddTestConfigs(testConfig)
|
||||
|
||||
// Remove all linkage between node and machine.
|
||||
for i := range testConfig.machines {
|
||||
|
|
@ -846,7 +858,7 @@ func TestControllerMachineSetNodeNamesWithoutLinkage(t *testing.T) {
|
|||
unstructured.RemoveNestedField(machine.Object, "spec", "providerID")
|
||||
unstructured.RemoveNestedField(machine.Object, "status", "nodeRef")
|
||||
|
||||
if err := updateResource(controller.managementClient, controller.machineInformer, controller.machineResource, machine); err != nil {
|
||||
if err := controller.UpdateResource(controller.machineInformer, controller.machineResource, machine); err != nil {
|
||||
t.Fatalf("unexpected error updating machine, got %v", err)
|
||||
}
|
||||
}
|
||||
|
|
@ -889,8 +901,9 @@ func TestControllerMachineSetNodeNamesUsingProviderID(t *testing.T) {
|
|||
}).
|
||||
Build()
|
||||
|
||||
controller, stop := mustCreateTestController(t, testConfig)
|
||||
defer stop()
|
||||
controller := NewTestMachineController(t)
|
||||
defer controller.Stop()
|
||||
controller.AddTestConfigs(testConfig)
|
||||
|
||||
nodegroups, err := controller.nodeGroups()
|
||||
if err != nil {
|
||||
|
|
@ -932,8 +945,9 @@ func TestControllerMachineSetNodeNamesUsingStatusNodeRefName(t *testing.T) {
|
|||
}).
|
||||
Build()
|
||||
|
||||
controller, stop := mustCreateTestController(t, testConfig)
|
||||
defer stop()
|
||||
controller := NewTestMachineController(t)
|
||||
defer controller.Stop()
|
||||
controller.AddTestConfigs(testConfig)
|
||||
|
||||
// Remove all the provider ID values on all the machines. We
|
||||
// want to force machineSetNodeNames() to fallback to
|
||||
|
|
@ -943,7 +957,7 @@ func TestControllerMachineSetNodeNamesUsingStatusNodeRefName(t *testing.T) {
|
|||
|
||||
unstructured.RemoveNestedField(machine.Object, "spec", "providerID")
|
||||
|
||||
if err := updateResource(controller.managementClient, controller.machineInformer, controller.machineResource, machine); err != nil {
|
||||
if err := controller.UpdateResource(controller.machineInformer, controller.machineResource, machine); err != nil {
|
||||
t.Fatalf("unexpected error updating machine, got %v", err)
|
||||
}
|
||||
}
|
||||
|
|
@ -1034,8 +1048,9 @@ func TestControllerGetAPIVersionGroupWithMachineDeployments(t *testing.T) {
|
|||
machine.SetAPIVersion(fmt.Sprintf("%s/v1beta1", customCAPIGroup))
|
||||
}
|
||||
|
||||
controller, stop := mustCreateTestController(t, testConfig)
|
||||
defer stop()
|
||||
controller := NewTestMachineController(t)
|
||||
defer controller.Stop()
|
||||
controller.AddTestConfigs(testConfig)
|
||||
|
||||
machineDeployments, err := controller.managementClient.Resource(controller.machineDeploymentResource).Namespace(testConfig.spec.namespace).
|
||||
List(context.TODO(), metav1.ListOptions{})
|
||||
|
|
@ -1440,8 +1455,9 @@ func Test_machineController_listScalableResources(t *testing.T) {
|
|||
wantErr: false,
|
||||
}} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
c, stop := mustCreateTestController(t, allTestConfigs...)
|
||||
defer stop()
|
||||
c := NewTestMachineController(t)
|
||||
defer c.Stop()
|
||||
c.AddTestConfigs(allTestConfigs...)
|
||||
c.autoDiscoverySpecs = tc.autoDiscoverySpecs
|
||||
|
||||
got, err := c.listScalableResources()
|
||||
|
|
@ -1554,8 +1570,9 @@ func Test_machineController_nodeGroupForNode(t *testing.T) {
|
|||
wantErr: false,
|
||||
}} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
c, stop := mustCreateTestController(t, allTestConfigs...)
|
||||
defer stop()
|
||||
c := NewTestMachineController(t)
|
||||
defer c.Stop()
|
||||
c.AddTestConfigs(allTestConfigs...)
|
||||
c.autoDiscoverySpecs = tc.autoDiscoverySpecs
|
||||
|
||||
got, err := c.nodeGroupForNode(tc.node)
|
||||
|
|
@ -1692,8 +1709,9 @@ func Test_machineController_nodeGroups(t *testing.T) {
|
|||
wantErr: false,
|
||||
}} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
c, stop := mustCreateTestController(t, allTestConfigs...)
|
||||
defer stop()
|
||||
c := NewTestMachineController(t)
|
||||
defer c.Stop()
|
||||
c.AddTestConfigs(allTestConfigs...)
|
||||
c.autoDiscoverySpecs = tc.autoDiscoverySpecs
|
||||
|
||||
got, err := c.nodeGroups()
|
||||
|
|
|
|||
|
|
@ -123,16 +123,17 @@ func TestNodeGroupNewNodeGroupConstructor(t *testing.T) {
|
|||
expectNil: false,
|
||||
}}
|
||||
|
||||
newNodeGroup := func(controller *machineController, testConfig *TestConfig) (*nodegroup, error) {
|
||||
newNodeGroup := func(controller *testMachineController, testConfig *TestConfig) (*nodegroup, error) {
|
||||
if testConfig.machineDeployment != nil {
|
||||
return newNodeGroupFromScalableResource(controller, testConfig.machineDeployment)
|
||||
return newNodeGroupFromScalableResource(controller.machineController, testConfig.machineDeployment)
|
||||
}
|
||||
return newNodeGroupFromScalableResource(controller, testConfig.machineSet)
|
||||
return newNodeGroupFromScalableResource(controller.machineController, testConfig.machineSet)
|
||||
}
|
||||
|
||||
test := func(t *testing.T, tc testCase, testConfig *TestConfig) {
|
||||
controller, stop := mustCreateTestController(t, testConfig)
|
||||
defer stop()
|
||||
controller := NewTestMachineController(t)
|
||||
defer controller.Stop()
|
||||
controller.AddTestConfigs(testConfig)
|
||||
|
||||
ng, err := newNodeGroup(controller, testConfig)
|
||||
if tc.errors && err == nil {
|
||||
|
|
@ -259,8 +260,9 @@ func TestNodeGroupIncreaseSizeErrors(t *testing.T) {
|
|||
}}
|
||||
|
||||
test := func(t *testing.T, tc *testCase, testConfig *TestConfig) {
|
||||
controller, stop := mustCreateTestController(t, testConfig)
|
||||
defer stop()
|
||||
controller := NewTestMachineController(t)
|
||||
defer controller.Stop()
|
||||
controller.AddTestConfigs(testConfig)
|
||||
|
||||
nodegroups, err := controller.nodeGroups()
|
||||
if err != nil {
|
||||
|
|
@ -356,8 +358,9 @@ func TestNodeGroupIncreaseSize(t *testing.T) {
|
|||
}
|
||||
|
||||
test := func(t *testing.T, tc *testCase, testConfig *TestConfig) {
|
||||
controller, stop := mustCreateTestController(t, testConfig)
|
||||
defer stop()
|
||||
controller := NewTestMachineController(t)
|
||||
defer controller.Stop()
|
||||
controller.AddTestConfigs(testConfig)
|
||||
|
||||
nodegroups, err := controller.nodeGroups()
|
||||
if err != nil {
|
||||
|
|
@ -451,8 +454,9 @@ func TestNodeGroupDecreaseTargetSize(t *testing.T) {
|
|||
}
|
||||
|
||||
test := func(t *testing.T, tc *testCase, testConfig *TestConfig) {
|
||||
controller, stop := mustCreateTestController(t, testConfig)
|
||||
defer stop()
|
||||
controller := NewTestMachineController(t)
|
||||
defer controller.Stop()
|
||||
controller.AddTestConfigs(testConfig)
|
||||
|
||||
// machines in deletion should not be counted towards the active nodes when calculating a decrease in size.
|
||||
if tc.includeDeletingMachine {
|
||||
|
|
@ -465,7 +469,7 @@ func TestNodeGroupDecreaseTargetSize(t *testing.T) {
|
|||
timestamp := metav1.Now()
|
||||
machine.SetDeletionTimestamp(×tamp)
|
||||
|
||||
if err := updateResource(controller.managementClient, controller.machineInformer, controller.machineResource, machine); err != nil {
|
||||
if err := controller.UpdateResource(controller.machineInformer, controller.machineResource, machine); err != nil {
|
||||
t.Fatalf("unexpected error updating machine, got %v", err)
|
||||
}
|
||||
}
|
||||
|
|
@ -485,7 +489,7 @@ func TestNodeGroupDecreaseTargetSize(t *testing.T) {
|
|||
}
|
||||
unstructured.SetNestedField(machine.Object, "FailureMessage", "status", "failureMessage")
|
||||
|
||||
if err := updateResource(controller.managementClient, controller.machineInformer, controller.machineResource, machine); err != nil {
|
||||
if err := controller.UpdateResource(controller.machineInformer, controller.machineResource, machine); err != nil {
|
||||
t.Fatalf("unexpected error updating machine, got %v", err)
|
||||
}
|
||||
}
|
||||
|
|
@ -505,7 +509,7 @@ func TestNodeGroupDecreaseTargetSize(t *testing.T) {
|
|||
}
|
||||
unstructured.RemoveNestedField(machine.Object, "status", "nodeRef")
|
||||
|
||||
if err := updateResource(controller.managementClient, controller.machineInformer, controller.machineResource, machine); err != nil {
|
||||
if err := controller.UpdateResource(controller.machineInformer, controller.machineResource, machine); err != nil {
|
||||
t.Fatalf("unexpected error updating machine, got %v", err)
|
||||
}
|
||||
}
|
||||
|
|
@ -515,7 +519,7 @@ func TestNodeGroupDecreaseTargetSize(t *testing.T) {
|
|||
for _, machine := range testConfig.machines {
|
||||
updated := machine.DeepCopy()
|
||||
unstructured.RemoveNestedField(updated.Object, "spec", "providerID")
|
||||
if err := updateResource(controller.managementClient, controller.machineInformer, controller.machineResource, updated); err != nil {
|
||||
if err := controller.UpdateResource(controller.machineInformer, controller.machineResource, updated); err != nil {
|
||||
t.Fatalf("unexpected error updating machine, got %v", err)
|
||||
}
|
||||
}
|
||||
|
|
@ -554,7 +558,7 @@ func TestNodeGroupDecreaseTargetSize(t *testing.T) {
|
|||
if u.GetResourceVersion() != scalableResource.GetResourceVersion() {
|
||||
return false, nil
|
||||
}
|
||||
ng, err := newNodeGroupFromScalableResource(controller, u)
|
||||
ng, err := newNodeGroupFromScalableResource(controller.machineController, u)
|
||||
if err != nil {
|
||||
return true, fmt.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
|
|
@ -761,8 +765,9 @@ func TestNodeGroupDecreaseSizeErrors(t *testing.T) {
|
|||
}}
|
||||
|
||||
test := func(t *testing.T, tc *testCase, testConfig *TestConfig) {
|
||||
controller, stop := mustCreateTestController(t, testConfig)
|
||||
defer stop()
|
||||
controller := NewTestMachineController(t)
|
||||
defer controller.Stop()
|
||||
controller.AddTestConfigs(testConfig)
|
||||
|
||||
nodegroups, err := controller.nodeGroups()
|
||||
if err != nil {
|
||||
|
|
@ -848,8 +853,9 @@ func TestNodeGroupDecreaseSizeErrors(t *testing.T) {
|
|||
|
||||
func TestNodeGroupDeleteNodes(t *testing.T) {
|
||||
test := func(t *testing.T, testConfig *TestConfig) {
|
||||
controller, stop := mustCreateTestController(t, testConfig)
|
||||
defer stop()
|
||||
controller := NewTestMachineController(t)
|
||||
defer controller.Stop()
|
||||
controller.AddTestConfigs(testConfig)
|
||||
|
||||
nodegroups, err := controller.nodeGroups()
|
||||
if err != nil {
|
||||
|
|
@ -944,8 +950,9 @@ func TestNodeGroupDeleteNodes(t *testing.T) {
|
|||
func TestNodeGroupMachineSetDeleteNodesWithMismatchedNodes(t *testing.T) {
|
||||
test := func(t *testing.T, expected int, testConfigs []*TestConfig) {
|
||||
testConfig0, testConfig1 := testConfigs[0], testConfigs[1]
|
||||
controller, stop := mustCreateTestController(t, testConfigs...)
|
||||
defer stop()
|
||||
controller := NewTestMachineController(t)
|
||||
defer controller.Stop()
|
||||
controller.AddTestConfigs(testConfigs...)
|
||||
|
||||
nodegroups, err := controller.nodeGroups()
|
||||
if err != nil {
|
||||
|
|
@ -1074,8 +1081,9 @@ func TestNodeGroupDeleteNodesTwice(t *testing.T) {
|
|||
expectedSize := 7
|
||||
|
||||
test := func(t *testing.T, testConfig *TestConfig) {
|
||||
controller, stop := mustCreateTestController(t, testConfig)
|
||||
defer stop()
|
||||
controller := NewTestMachineController(t)
|
||||
defer controller.Stop()
|
||||
controller.AddTestConfigs(testConfig)
|
||||
|
||||
nodegroups, err := controller.nodeGroups()
|
||||
if err != nil {
|
||||
|
|
@ -1133,13 +1141,15 @@ func TestNodeGroupDeleteNodesTwice(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, node := range nodesToBeDeleted {
|
||||
if err := addDeletionTimestampToMachine(controller, node); err != nil {
|
||||
if err := addDeletionTimestampToMachine(controller.machineController, node); err != nil {
|
||||
t.Fatalf("unexpected err: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Wait for the machineset to have been updated
|
||||
if err := wait.PollImmediate(100*time.Millisecond, 5*time.Second, func() (bool, error) {
|
||||
deadlineCtx, deadlineFn := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer deadlineFn()
|
||||
if err := wait.PollUntilContextTimeout(deadlineCtx, 100*time.Millisecond, 5*time.Second, true, func(_ context.Context) (bool, error) {
|
||||
nodegroups, err = controller.nodeGroups()
|
||||
if err != nil {
|
||||
return false, err
|
||||
|
|
@ -1174,7 +1184,9 @@ func TestNodeGroupDeleteNodesTwice(t *testing.T) {
|
|||
// when fetched from the API
|
||||
for _, node := range nodesToBeDeleted {
|
||||
// Ensure the update has propogated
|
||||
if err := wait.PollImmediate(100*time.Millisecond, 5*time.Minute, func() (bool, error) {
|
||||
deadlineCtx, deadlineFn := context.WithTimeout(context.Background(), 5*time.Minute)
|
||||
defer deadlineFn()
|
||||
if err := wait.PollUntilContextTimeout(deadlineCtx, 100*time.Millisecond, 5*time.Minute, true, func(_ context.Context) (bool, error) {
|
||||
m, err := controller.findMachineByProviderID(normalizedProviderString(node.Spec.ProviderID))
|
||||
if err != nil {
|
||||
return false, err
|
||||
|
|
@ -1240,8 +1252,9 @@ func TestNodeGroupDeleteNodesSequential(t *testing.T) {
|
|||
expectedSize := 7
|
||||
|
||||
test := func(t *testing.T, testConfig *TestConfig) {
|
||||
controller, stop := mustCreateTestController(t, testConfig)
|
||||
defer stop()
|
||||
controller := NewTestMachineController(t)
|
||||
defer controller.Stop()
|
||||
controller.AddTestConfigs(testConfig)
|
||||
|
||||
nodegroups, err := controller.nodeGroups()
|
||||
if err != nil {
|
||||
|
|
@ -1377,8 +1390,9 @@ func TestNodeGroupDeleteNodesSequential(t *testing.T) {
|
|||
|
||||
func TestNodeGroupWithFailedMachine(t *testing.T) {
|
||||
test := func(t *testing.T, testConfig *TestConfig) {
|
||||
controller, stop := mustCreateTestController(t, testConfig)
|
||||
defer stop()
|
||||
controller := NewTestMachineController(t)
|
||||
defer controller.Stop()
|
||||
controller.AddTestConfigs(testConfig)
|
||||
|
||||
// Simulate a failed machine
|
||||
machine := testConfig.machines[3].DeepCopy()
|
||||
|
|
@ -1388,7 +1402,7 @@ func TestNodeGroupWithFailedMachine(t *testing.T) {
|
|||
t.Fatalf("unexpected error setting nested field: %v", err)
|
||||
}
|
||||
|
||||
if err := updateResource(controller.managementClient, controller.machineInformer, controller.machineResource, machine); err != nil {
|
||||
if err := controller.UpdateResource(controller.machineInformer, controller.machineResource, machine); err != nil {
|
||||
t.Fatalf("unexpected error updating machine, got %v", err)
|
||||
}
|
||||
|
||||
|
|
@ -1615,8 +1629,9 @@ func TestNodeGroupTemplateNodeInfo(t *testing.T) {
|
|||
testConfig.nodes = []*corev1.Node{}
|
||||
}
|
||||
|
||||
controller, stop := mustCreateTestController(t, testConfig)
|
||||
defer stop()
|
||||
controller := NewTestMachineController(t)
|
||||
defer controller.Stop()
|
||||
controller.AddTestConfigs(testConfig)
|
||||
|
||||
nodegroups, err := controller.nodeGroups()
|
||||
if err != nil {
|
||||
|
|
@ -1773,8 +1788,9 @@ func TestNodeGroupGetOptions(t *testing.T) {
|
|||
}
|
||||
|
||||
test := func(t *testing.T, testConfig *TestConfig, expectedOptions *config.NodeGroupAutoscalingOptions) {
|
||||
controller, stop := mustCreateTestController(t, testConfig)
|
||||
defer stop()
|
||||
controller := NewTestMachineController(t)
|
||||
defer controller.Stop()
|
||||
controller.AddTestConfigs(testConfig)
|
||||
|
||||
nodegroups, err := controller.nodeGroups()
|
||||
if err != nil {
|
||||
|
|
@ -1860,8 +1876,9 @@ func TestNodeGroupNodesInstancesStatus(t *testing.T) {
|
|||
}
|
||||
|
||||
test := func(t *testing.T, tc *testCase, testConfig *TestConfig) {
|
||||
controller, stop := mustCreateTestController(t, testConfig)
|
||||
defer stop()
|
||||
controller := NewTestMachineController(t)
|
||||
defer controller.Stop()
|
||||
controller.AddTestConfigs(testConfig)
|
||||
|
||||
if tc.includePendingMachine {
|
||||
if tc.nodeCount < 1 {
|
||||
|
|
@ -1872,7 +1889,7 @@ func TestNodeGroupNodesInstancesStatus(t *testing.T) {
|
|||
unstructured.RemoveNestedField(machine.Object, "spec", "providerID")
|
||||
unstructured.RemoveNestedField(machine.Object, "status", "nodeRef")
|
||||
|
||||
if err := updateResource(controller.managementClient, controller.machineInformer, controller.machineResource, machine); err != nil {
|
||||
if err := controller.UpdateResource(controller.machineInformer, controller.machineResource, machine); err != nil {
|
||||
t.Fatalf("unexpected error updating machine, got %v", err)
|
||||
}
|
||||
}
|
||||
|
|
@ -1886,7 +1903,7 @@ func TestNodeGroupNodesInstancesStatus(t *testing.T) {
|
|||
timestamp := metav1.Now()
|
||||
machine.SetDeletionTimestamp(×tamp)
|
||||
|
||||
if err := updateResource(controller.managementClient, controller.machineInformer, controller.machineResource, machine); err != nil {
|
||||
if err := controller.UpdateResource(controller.machineInformer, controller.machineResource, machine); err != nil {
|
||||
t.Fatalf("unexpected error updating machine, got %v", err)
|
||||
}
|
||||
}
|
||||
|
|
@ -1900,7 +1917,7 @@ func TestNodeGroupNodesInstancesStatus(t *testing.T) {
|
|||
unstructured.SetNestedField(machine.Object, "node-1", "status", "nodeRef", "name")
|
||||
unstructured.SetNestedField(machine.Object, "ErrorMessage", "status", "errorMessage")
|
||||
|
||||
if err := updateResource(controller.managementClient, controller.machineInformer, controller.machineResource, machine); err != nil {
|
||||
if err := controller.UpdateResource(controller.machineInformer, controller.machineResource, machine); err != nil {
|
||||
t.Fatalf("unexpected error updating machine, got %v", err)
|
||||
}
|
||||
}
|
||||
|
|
@ -1914,7 +1931,7 @@ func TestNodeGroupNodesInstancesStatus(t *testing.T) {
|
|||
unstructured.RemoveNestedField(machine.Object, "status", "nodeRef")
|
||||
unstructured.SetNestedField(machine.Object, "ErrorMessage", "status", "errorMessage")
|
||||
|
||||
if err := updateResource(controller.managementClient, controller.machineInformer, controller.machineResource, machine); err != nil {
|
||||
if err := controller.UpdateResource(controller.machineInformer, controller.machineResource, machine); err != nil {
|
||||
t.Fatalf("unexpected error updating machine, got %v", err)
|
||||
}
|
||||
}
|
||||
|
|
@ -1929,7 +1946,7 @@ func TestNodeGroupNodesInstancesStatus(t *testing.T) {
|
|||
machine.SetDeletionTimestamp(×tamp)
|
||||
unstructured.SetNestedField(machine.Object, "ErrorMessage", "status", "errorMessage")
|
||||
|
||||
if err := updateResource(controller.managementClient, controller.machineInformer, controller.machineResource, machine); err != nil {
|
||||
if err := controller.UpdateResource(controller.machineInformer, controller.machineResource, machine); err != nil {
|
||||
t.Fatalf("unexpected error updating machine, got %v", err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,10 +29,10 @@ import (
|
|||
func TestProviderConstructorProperties(t *testing.T) {
|
||||
resourceLimits := cloudprovider.ResourceLimiter{}
|
||||
|
||||
controller, stop := mustCreateTestController(t)
|
||||
defer stop()
|
||||
controller := NewTestMachineController(t)
|
||||
defer controller.Stop()
|
||||
|
||||
provider := newProvider(cloudprovider.ClusterAPIProviderName, &resourceLimits, controller)
|
||||
provider := newProvider(cloudprovider.ClusterAPIProviderName, &resourceLimits, controller.machineController)
|
||||
if actual := provider.Name(); actual != cloudprovider.ClusterAPIProviderName {
|
||||
t.Errorf("expected %q, got %q", cloudprovider.ClusterAPIProviderName, actual)
|
||||
}
|
||||
|
|
@ -108,8 +108,8 @@ func BenchmarkNodeGroups(b *testing.B) {
|
|||
nodeGroupMaxSizeAnnotationKey: "2",
|
||||
}
|
||||
|
||||
controller, stop := mustCreateTestController(b)
|
||||
defer stop()
|
||||
controller := NewTestMachineController(b)
|
||||
defer controller.Stop()
|
||||
machineSetConfigs := NewTestConfigBuilder().
|
||||
ForMachineSet().
|
||||
WithNamespace("namespace").
|
||||
|
|
@ -117,11 +117,11 @@ func BenchmarkNodeGroups(b *testing.B) {
|
|||
WithNodeCount(1).
|
||||
WithAnnotations(annotations).
|
||||
BuildMultiple(100)
|
||||
if err := addTestConfigs(b, controller, machineSetConfigs...); err != nil {
|
||||
if err := controller.AddTestConfigs(machineSetConfigs...); err != nil {
|
||||
b.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
provider := newProvider(cloudprovider.ClusterAPIProviderName, &resourceLimits, controller)
|
||||
provider := newProvider(cloudprovider.ClusterAPIProviderName, &resourceLimits, controller.machineController)
|
||||
if actual := provider.Name(); actual != cloudprovider.ClusterAPIProviderName {
|
||||
b.Errorf("expected %q, got %q", cloudprovider.ClusterAPIProviderName, actual)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,7 +37,6 @@ import (
|
|||
"k8s.io/apimachinery/pkg/util/wait"
|
||||
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider"
|
||||
fakediscovery "k8s.io/client-go/discovery/fake"
|
||||
"k8s.io/client-go/dynamic"
|
||||
fakedynamic "k8s.io/client-go/dynamic/fake"
|
||||
"k8s.io/client-go/informers"
|
||||
fakekube "k8s.io/client-go/kubernetes/fake"
|
||||
|
|
@ -418,144 +417,24 @@ func makeLinkedNodeAndMachine(i int, namespace, clusterName string, owner metav1
|
|||
return node, machine
|
||||
}
|
||||
|
||||
func addTestConfigs(t testing.TB, controller *machineController, testConfigs ...*TestConfig) error {
|
||||
t.Helper()
|
||||
|
||||
for _, config := range testConfigs {
|
||||
if config.machineDeployment != nil {
|
||||
if err := createResource(controller.managementClient, controller.machineDeploymentInformer, controller.machineDeploymentResource, config.machineDeployment); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := createResource(controller.managementClient, controller.machineSetInformer, controller.machineSetResource, config.machineSet); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if config.machinePool != nil {
|
||||
if err := createResource(controller.managementClient, controller.machinePoolInformer, controller.machinePoolResource, config.machinePool); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
for i := range config.machines {
|
||||
if err := createResource(controller.managementClient, controller.machineInformer, controller.machineResource, config.machines[i]); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
for i := range config.nodes {
|
||||
if err := controller.nodeInformer.GetStore().Add(config.nodes[i]); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func createResource(client dynamic.Interface, informer informers.GenericInformer, gvr schema.GroupVersionResource, resource *unstructured.Unstructured) error {
|
||||
if _, err := client.Resource(gvr).Namespace(resource.GetNamespace()).Create(context.TODO(), resource, metav1.CreateOptions{}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return wait.PollImmediate(time.Microsecond, fifteenSecondDuration, func() (bool, error) {
|
||||
_, err := informer.Lister().ByNamespace(resource.GetNamespace()).Get(resource.GetName())
|
||||
if err != nil {
|
||||
if apierrors.IsNotFound(err) {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
|
||||
return true, nil
|
||||
})
|
||||
}
|
||||
|
||||
func updateResource(client dynamic.Interface, informer informers.GenericInformer, gvr schema.GroupVersionResource, resource *unstructured.Unstructured) error {
|
||||
updateResult, err := client.Resource(gvr).Namespace(resource.GetNamespace()).Update(context.TODO(), resource, metav1.UpdateOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return wait.PollImmediate(time.Microsecond, fifteenSecondDuration, func() (bool, error) {
|
||||
result, err := informer.Lister().ByNamespace(resource.GetNamespace()).Get(resource.GetName())
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return reflect.DeepEqual(updateResult, result), nil
|
||||
})
|
||||
}
|
||||
|
||||
func deleteResource(client dynamic.Interface, informer informers.GenericInformer, gvr schema.GroupVersionResource, resource *unstructured.Unstructured) error {
|
||||
if err := client.Resource(gvr).Namespace(resource.GetNamespace()).Delete(context.TODO(), resource.GetName(), metav1.DeleteOptions{}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return wait.PollImmediate(time.Microsecond, fifteenSecondDuration, func() (bool, error) {
|
||||
_, err := informer.Lister().ByNamespace(resource.GetNamespace()).Get(resource.GetName())
|
||||
if err != nil && apierrors.IsNotFound(err) {
|
||||
return true, nil
|
||||
}
|
||||
return false, err
|
||||
})
|
||||
}
|
||||
|
||||
func deleteTestConfigs(t *testing.T, controller *machineController, testConfigs ...*TestConfig) error {
|
||||
t.Helper()
|
||||
|
||||
for _, config := range testConfigs {
|
||||
for i := range config.nodes {
|
||||
if err := controller.nodeInformer.GetStore().Delete(config.nodes[i]); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for i := range config.machines {
|
||||
if err := deleteResource(controller.managementClient, controller.machineInformer, controller.machineResource, config.machines[i]); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := deleteResource(controller.managementClient, controller.machineSetInformer, controller.machineSetResource, config.machineSet); err != nil {
|
||||
return err
|
||||
}
|
||||
if config.machineDeployment != nil {
|
||||
if err := deleteResource(controller.managementClient, controller.machineDeploymentInformer, controller.machineDeploymentResource, config.machineDeployment); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type testControllerShutdownFunc func()
|
||||
|
||||
const customCAPIGroup = "custom.x-k8s.io"
|
||||
const fifteenSecondDuration = time.Second * 15
|
||||
|
||||
func mustCreateTestController(t testing.TB, testConfigs ...*TestConfig) (*machineController, testControllerShutdownFunc) {
|
||||
type testMachineController struct {
|
||||
*machineController
|
||||
|
||||
stopCh chan struct{}
|
||||
testingTb testing.TB
|
||||
dynamicClientset *fakedynamic.FakeDynamicClient
|
||||
}
|
||||
|
||||
// NewTestMachineController returns a new machineController wrapped by a test harness and associated with a testing interface.
|
||||
func NewTestMachineController(t testing.TB) *testMachineController {
|
||||
t.Helper()
|
||||
|
||||
nodeObjects := make([]runtime.Object, 0)
|
||||
machineObjects := make([]runtime.Object, 0)
|
||||
|
||||
for _, config := range testConfigs {
|
||||
for i := range config.nodes {
|
||||
nodeObjects = append(nodeObjects, config.nodes[i])
|
||||
}
|
||||
|
||||
for i := range config.machines {
|
||||
machineObjects = append(machineObjects, config.machines[i])
|
||||
}
|
||||
|
||||
machineObjects = append(machineObjects, config.machineSet)
|
||||
if config.machineDeployment != nil {
|
||||
machineObjects = append(machineObjects, config.machineDeployment)
|
||||
}
|
||||
|
||||
if config.machineTemplate != nil {
|
||||
machineObjects = append(machineObjects, config.machineTemplate)
|
||||
}
|
||||
}
|
||||
|
||||
kubeclientSet := fakekube.NewSimpleClientset(nodeObjects...)
|
||||
kubeclientSet := fakekube.NewSimpleClientset()
|
||||
dynamicClientset := fakedynamic.NewSimpleDynamicClientWithCustomListKinds(
|
||||
runtime.NewScheme(),
|
||||
map[schema.GroupVersionResource]string{
|
||||
|
|
@ -572,7 +451,6 @@ func mustCreateTestController(t testing.TB, testConfigs ...*TestConfig) (*machin
|
|||
{Group: "custom.x-k8s.io", Version: "v1beta1", Resource: "machinesets"}: "kindList",
|
||||
{Group: "infrastructure.cluster.x-k8s.io", Version: "v1beta1", Resource: "machinetemplates"}: "kindList",
|
||||
},
|
||||
machineObjects...,
|
||||
)
|
||||
discoveryClient := &fakediscovery.FakeDiscovery{
|
||||
Fake: &clientgotesting.Fake{
|
||||
|
|
@ -754,7 +632,131 @@ func mustCreateTestController(t testing.TB, testConfigs ...*TestConfig) (*machin
|
|||
t.Fatalf("failed to run controller: %v", err)
|
||||
}
|
||||
|
||||
return controller, func() {
|
||||
close(stopCh)
|
||||
return &testMachineController{
|
||||
machineController: controller,
|
||||
stopCh: stopCh,
|
||||
testingTb: t,
|
||||
dynamicClientset: dynamicClientset,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *testMachineController) Stop() {
|
||||
close(c.stopCh)
|
||||
}
|
||||
|
||||
func (c *testMachineController) AddTestConfigs(testConfigs ...*TestConfig) error {
|
||||
c.testingTb.Helper()
|
||||
|
||||
for _, config := range testConfigs {
|
||||
if config.machineDeployment != nil {
|
||||
if err := c.CreateResource(c.machineDeploymentInformer, c.machineDeploymentResource, config.machineDeployment); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := c.CreateResource(c.machineSetInformer, c.machineSetResource, config.machineSet); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if config.machinePool != nil {
|
||||
if err := c.CreateResource(c.machinePoolInformer, c.machinePoolResource, config.machinePool); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if config.machineTemplate != nil {
|
||||
c.dynamicClientset.Tracker().Add(config.machineTemplate)
|
||||
}
|
||||
|
||||
for i := range config.machines {
|
||||
if err := c.CreateResource(c.machineInformer, c.machineResource, config.machines[i]); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
for i := range config.nodes {
|
||||
if err := c.nodeInformer.GetStore().Add(config.nodes[i]); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *testMachineController) DeleteTestConfigs(testConfigs ...*TestConfig) error {
|
||||
c.testingTb.Helper()
|
||||
|
||||
for _, config := range testConfigs {
|
||||
for i := range config.nodes {
|
||||
if err := c.nodeInformer.GetStore().Delete(config.nodes[i]); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for i := range config.machines {
|
||||
if err := c.DeleteResource(c.machineInformer, c.machineResource, config.machines[i]); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := c.DeleteResource(c.machineSetInformer, c.machineSetResource, config.machineSet); err != nil {
|
||||
return err
|
||||
}
|
||||
if config.machineDeployment != nil {
|
||||
if err := c.DeleteResource(c.machineDeploymentInformer, c.machineDeploymentResource, config.machineDeployment); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *testMachineController) CreateResource(informer informers.GenericInformer, gvr schema.GroupVersionResource, resource *unstructured.Unstructured) error {
|
||||
if _, err := c.managementClient.Resource(gvr).Namespace(resource.GetNamespace()).Create(context.TODO(), resource, metav1.CreateOptions{}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
deadlineCtx, deadlineFn := context.WithTimeout(context.Background(), fifteenSecondDuration)
|
||||
defer deadlineFn()
|
||||
return wait.PollUntilContextTimeout(deadlineCtx, time.Microsecond, fifteenSecondDuration, true, func(_ context.Context) (bool, error) {
|
||||
_, err := informer.Lister().ByNamespace(resource.GetNamespace()).Get(resource.GetName())
|
||||
if err != nil {
|
||||
if apierrors.IsNotFound(err) {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
|
||||
return true, nil
|
||||
})
|
||||
}
|
||||
|
||||
func (c *testMachineController) UpdateResource(informer informers.GenericInformer, gvr schema.GroupVersionResource, resource *unstructured.Unstructured) error {
|
||||
updateResult, err := c.managementClient.Resource(gvr).Namespace(resource.GetNamespace()).Update(context.TODO(), resource, metav1.UpdateOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
deadlineCtx, deadlineFn := context.WithTimeout(context.Background(), fifteenSecondDuration)
|
||||
defer deadlineFn()
|
||||
return wait.PollUntilContextTimeout(deadlineCtx, time.Microsecond, fifteenSecondDuration, true, func(_ context.Context) (bool, error) {
|
||||
result, err := informer.Lister().ByNamespace(resource.GetNamespace()).Get(resource.GetName())
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return reflect.DeepEqual(updateResult, result), nil
|
||||
})
|
||||
}
|
||||
|
||||
func (c *testMachineController) DeleteResource(informer informers.GenericInformer, gvr schema.GroupVersionResource, resource *unstructured.Unstructured) error {
|
||||
if err := c.managementClient.Resource(gvr).Namespace(resource.GetNamespace()).Delete(context.TODO(), resource.GetName(), metav1.DeleteOptions{}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
deadlineCtx, deadlineFn := context.WithTimeout(context.Background(), fifteenSecondDuration)
|
||||
defer deadlineFn()
|
||||
return wait.PollUntilContextTimeout(deadlineCtx, time.Microsecond, fifteenSecondDuration, true, func(_ context.Context) (bool, error) {
|
||||
_, err := informer.Lister().ByNamespace(resource.GetNamespace()).Get(resource.GetName())
|
||||
if err != nil && apierrors.IsNotFound(err) {
|
||||
return true, nil
|
||||
}
|
||||
return false, err
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -44,15 +44,16 @@ func TestSetSize(t *testing.T) {
|
|||
finalReplicas := 0
|
||||
|
||||
test := func(t *testing.T, testConfig *TestConfig) {
|
||||
controller, stop := mustCreateTestController(t, testConfig)
|
||||
defer stop()
|
||||
controller := NewTestMachineController(t)
|
||||
defer controller.Stop()
|
||||
controller.AddTestConfigs(testConfig)
|
||||
|
||||
testResource := testConfig.machineSet
|
||||
if testConfig.machineDeployment != nil {
|
||||
testResource = testConfig.machineDeployment
|
||||
}
|
||||
|
||||
sr, err := newUnstructuredScalableResource(controller, testResource)
|
||||
sr, err := newUnstructuredScalableResource(controller.machineController, testResource)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
|
@ -145,15 +146,16 @@ func TestReplicas(t *testing.T) {
|
|||
updatedReplicas := 5
|
||||
|
||||
test := func(t *testing.T, testConfig *TestConfig) {
|
||||
controller, stop := mustCreateTestController(t, testConfig)
|
||||
defer stop()
|
||||
controller := NewTestMachineController(t)
|
||||
defer controller.Stop()
|
||||
controller.AddTestConfigs(testConfig)
|
||||
|
||||
testResource := testConfig.machineSet
|
||||
if testConfig.machineDeployment != nil {
|
||||
testResource = testConfig.machineDeployment
|
||||
}
|
||||
|
||||
sr, err := newUnstructuredScalableResource(controller, testResource)
|
||||
sr, err := newUnstructuredScalableResource(controller.machineController, testResource)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
|
@ -187,7 +189,7 @@ func TestReplicas(t *testing.T) {
|
|||
if !ok {
|
||||
return false, nil
|
||||
}
|
||||
sr, err := newUnstructuredScalableResource(controller, u)
|
||||
sr, err := newUnstructuredScalableResource(controller.machineController, u)
|
||||
if err != nil {
|
||||
return true, err
|
||||
}
|
||||
|
|
@ -261,15 +263,16 @@ func TestSetSizeAndReplicas(t *testing.T) {
|
|||
updatedReplicas := 5
|
||||
|
||||
test := func(t *testing.T, testConfig *TestConfig) {
|
||||
controller, stop := mustCreateTestController(t, testConfig)
|
||||
defer stop()
|
||||
controller := NewTestMachineController(t)
|
||||
defer controller.Stop()
|
||||
controller.AddTestConfigs(testConfig)
|
||||
|
||||
testResource := testConfig.machineSet
|
||||
if testConfig.machineDeployment != nil {
|
||||
testResource = testConfig.machineDeployment
|
||||
}
|
||||
|
||||
sr, err := newUnstructuredScalableResource(controller, testResource)
|
||||
sr, err := newUnstructuredScalableResource(controller.machineController, testResource)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
|
@ -367,10 +370,11 @@ func TestAnnotations(t *testing.T) {
|
|||
}
|
||||
|
||||
test := func(t *testing.T, testConfig *TestConfig, testResource *unstructured.Unstructured) {
|
||||
controller, stop := mustCreateTestController(t, testConfig)
|
||||
defer stop()
|
||||
controller := NewTestMachineController(t)
|
||||
defer controller.Stop()
|
||||
controller.AddTestConfigs(testConfig)
|
||||
|
||||
sr, err := newUnstructuredScalableResource(controller, testResource)
|
||||
sr, err := newUnstructuredScalableResource(controller.machineController, testResource)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
|
@ -548,12 +552,13 @@ func TestCanScaleFromZero(t *testing.T) {
|
|||
WithAnnotations(tc.annotations).
|
||||
WithCapacity(tc.capacity).
|
||||
Build()
|
||||
controller, stop := mustCreateTestController(t, msTestConfig)
|
||||
defer stop()
|
||||
controller := NewTestMachineController(t)
|
||||
defer controller.Stop()
|
||||
controller.AddTestConfigs(msTestConfig)
|
||||
|
||||
testResource := msTestConfig.machineSet
|
||||
|
||||
sr, err := newUnstructuredScalableResource(controller, testResource)
|
||||
sr, err := newUnstructuredScalableResource(controller.machineController, testResource)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
|
@ -574,12 +579,13 @@ func TestCanScaleFromZero(t *testing.T) {
|
|||
WithAnnotations(tc.annotations).
|
||||
WithCapacity(tc.capacity).
|
||||
Build()
|
||||
controller, stop := mustCreateTestController(t, mdTestConfig)
|
||||
defer stop()
|
||||
controller := NewTestMachineController(t)
|
||||
defer controller.Stop()
|
||||
controller.AddTestConfigs(mdTestConfig)
|
||||
|
||||
testResource := mdTestConfig.machineDeployment
|
||||
|
||||
sr, err := newUnstructuredScalableResource(controller, testResource)
|
||||
sr, err := newUnstructuredScalableResource(controller.machineController, testResource)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue