Skip to content

Commit

Permalink
Get unit tests green after kube 1.18 deps update
Browse files Browse the repository at this point in the history
This commit mostly ensures we set the API version in the expected objects otherwise equality.Semantic.DeepEqual fails to compare with what is coming from the fake client.
  • Loading branch information
enxebre committed Mar 25, 2020
1 parent fbb880d commit 015378d
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 19 deletions.
3 changes: 1 addition & 2 deletions hack/fetch_ext_bins.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ if [ -n "$TRACE" ]; then
set -x
fi

# k8s_version=1.10.1
k8s_version=1.14.1
k8s_version=1.16.4
goarch=amd64
goos="unknown"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,8 @@ func TestGetTargetsFromMHC(t *testing.T) {
Labels: map[string]string{},
},
TypeMeta: metav1.TypeMeta{
Kind: "Node",
Kind: "Node",
APIVersion: "v1",
},
Status: corev1.NodeStatus{
Conditions: []corev1.NodeCondition{},
Expand All @@ -963,7 +964,8 @@ func TestGetTargetsFromMHC(t *testing.T) {
Labels: map[string]string{},
},
TypeMeta: metav1.TypeMeta{
Kind: "Node",
Kind: "Node",
APIVersion: "v1",
},
Status: corev1.NodeStatus{
Conditions: []corev1.NodeCondition{},
Expand All @@ -979,7 +981,8 @@ func TestGetTargetsFromMHC(t *testing.T) {
Labels: map[string]string{},
},
TypeMeta: metav1.TypeMeta{
Kind: "Node",
Kind: "Node",
APIVersion: "v1",
},
Status: corev1.NodeStatus{
Conditions: []corev1.NodeCondition{},
Expand All @@ -1000,7 +1003,8 @@ func TestGetTargetsFromMHC(t *testing.T) {
Labels: map[string]string{},
},
TypeMeta: metav1.TypeMeta{
Kind: "Node",
Kind: "Node",
APIVersion: "v1",
},
Status: corev1.NodeStatus{
Conditions: []corev1.NodeCondition{},
Expand All @@ -1020,7 +1024,8 @@ func TestGetTargetsFromMHC(t *testing.T) {
Labels: map[string]string{},
},
TypeMeta: metav1.TypeMeta{
Kind: "Node",
Kind: "Node",
APIVersion: "v1",
},
Status: corev1.NodeStatus{
Conditions: []corev1.NodeCondition{},
Expand Down Expand Up @@ -1100,7 +1105,7 @@ func TestGetTargetsFromMHC(t *testing.T) {
t.Run(tc.testCase, func(t *testing.T) {
got, err := newFakeReconciler(objects...).getTargetsFromMHC(*tc.mhc)
if !equality.Semantic.DeepEqual(got, tc.expectedTargets) {
t.Errorf("Case: %v. Got: %v, expected: %v", tc.testCase, got, tc.expectedTargets)
t.Errorf("Case: %v. Got: %+v, expected: %+v", tc.testCase, got, tc.expectedTargets)
}
if tc.expectedError != (err != nil) {
t.Errorf("Case: %v. Got: %v, expected error: %v", tc.testCase, err, tc.expectedError)
Expand Down Expand Up @@ -1162,7 +1167,8 @@ func TestGetNodeFromMachine(t *testing.T) {
Labels: map[string]string{},
},
TypeMeta: metav1.TypeMeta{
Kind: "Node",
Kind: "Node",
APIVersion: "v1",
},
Status: corev1.NodeStatus{
Conditions: []corev1.NodeCondition{},
Expand Down Expand Up @@ -1883,7 +1889,10 @@ func TestRemediate(t *testing.T) {
testCase: "no master",
target: &target{
Machine: mapiv1beta1.Machine{
TypeMeta: metav1.TypeMeta{Kind: "Machine"},
TypeMeta: metav1.TypeMeta{
Kind: "Machine",
APIVersion: "machine.openshift.io/v1beta1",
},
ObjectMeta: metav1.ObjectMeta{
Annotations: make(map[string]string),
Name: "test",
Expand Down Expand Up @@ -1918,7 +1927,10 @@ func TestRemediate(t *testing.T) {
testCase: "node master",
target: &target{
Machine: mapiv1beta1.Machine{
TypeMeta: metav1.TypeMeta{Kind: "Machine"},
TypeMeta: metav1.TypeMeta{
Kind: "Machine",
APIVersion: "machine.openshift.io/v1beta1",
},
ObjectMeta: metav1.ObjectMeta{
Annotations: make(map[string]string),
Name: "test",
Expand Down Expand Up @@ -1956,7 +1968,10 @@ func TestRemediate(t *testing.T) {
testCase: "machine master",
target: &target{
Machine: mapiv1beta1.Machine{
TypeMeta: metav1.TypeMeta{Kind: "Machine"},
TypeMeta: metav1.TypeMeta{
Kind: "Machine",
APIVersion: "machine.openshift.io/v1beta1",
},
ObjectMeta: metav1.ObjectMeta{
Annotations: make(map[string]string),
Name: "test",
Expand Down Expand Up @@ -1989,7 +2004,7 @@ func TestRemediate(t *testing.T) {
}
assertEvents(t, tc.testCase, tc.expectedEvents, recorder.Events)
machine := &mapiv1beta1.Machine{}
err := r.client.Get(context.TODO(), namespacedName(machine), machine)
err := r.client.Get(context.TODO(), namespacedName(&tc.target.Machine), machine)
if tc.deletion {
if err != nil {
if !errors.IsNotFound(err) {
Expand All @@ -2001,7 +2016,7 @@ func TestRemediate(t *testing.T) {
}
if !tc.deletion {
if !equality.Semantic.DeepEqual(*machine, tc.target.Machine) {
t.Errorf("Case: %v. Got: %v, expected: %v", tc.testCase, *machine, tc.target.Machine)
t.Errorf("Case: %v. Got: %+v, expected: %+v", tc.testCase, *machine, tc.target.Machine)
}
}
})
Expand Down
12 changes: 10 additions & 2 deletions pkg/controller/machineset/machineset_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ var expectedRequest = reconcile.Request{NamespacedName: types.NamespacedName{Nam

const timeout = time.Second * 5

// TODO: drop this in favour of ginkgo like validations
// similar to https://github.com/kubernetes-sigs/cluster-api/blob/master/controllers/machineset_controller_test.go
func TestReconcile(t *testing.T) {
// Setup the Manager and Controller. Wrap the Controller Reconcile function so it writes each request to a
// channel when it is finished.
Expand Down Expand Up @@ -158,12 +160,18 @@ func TestReconcile(t *testing.T) {
}

defer func() {
c.Delete(context.TODO(), tc.instance)
err := c.Delete(context.TODO(), tc.instance)
if err != nil {
t.Logf("error deleting %v: %v", tc.instance, err)
}

select {
case recv := <-requests:
if recv != tc.expectedRequest {
t.Error("received request does not match expected request")
}

t.Logf("Last reconcile %v", <-requests)
case <-time.After(timeout):
t.Error("timed out waiting for request")
}
Expand All @@ -172,7 +180,7 @@ func TestReconcile(t *testing.T) {
select {
case recv := <-requests:
if recv != tc.expectedRequest {
t.Error("received request does not match expected request")
t.Errorf("received request does not match expected request. Got: %v, expected: %v", recv, tc.expectedRequest)
}
case <-time.After(timeout):
t.Error("timed out waiting for request")
Expand Down
4 changes: 3 additions & 1 deletion pkg/controller/vsphere/machine_scope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,8 @@ func TestPatchMachine(t *testing.T) {
Namespace: namespace,
},
TypeMeta: metav1.TypeMeta{
Kind: "Machine",
Kind: "Machine",
APIVersion: "machine.openshift.io/v1beta1",
},
Spec: machinev1.MachineSpec{
ProviderSpec: machinev1.ProviderSpec{
Expand Down Expand Up @@ -384,6 +385,7 @@ func TestPatchMachine(t *testing.T) {
t.Fatal(err)
}

expectedMachine.ResourceVersion = "2"
if !equality.Semantic.DeepEqual(gotMachine, expectedMachine) {
t.Errorf("expected: %+v, got: %+v", expectedMachine, gotMachine)
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/operator/operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,15 @@ func TestOperatorSync_NoOp(t *testing.T) {
stopCh := make(<-chan struct{})
optr := newFakeOperator(nil, []runtime.Object{infra}, stopCh)
optr.queue.Add("trigger")
go optr.Run(2, stopCh)
go optr.Run(1, stopCh)

err := wait.PollImmediate(1*time.Second, 5*time.Second, func() (bool, error) {
err := wait.PollImmediate(5*time.Second, 1*time.Second, func() (bool, error) {
_, err := optr.deployLister.Deployments(targetNamespace).Get(deploymentName)
if err != nil {
t.Logf("Failed to get %q deployment: %v", deploymentName, err)
return false, nil
}
t.Logf("Found deployment: %q", deploymentName)
return true, nil
})

Expand Down

0 comments on commit 015378d

Please sign in to comment.