Skip to content

fake client unable to List Job objects #524

Closed
@raffaelespazzoli

Description

@raffaelespazzoli

I am getting this error when trying to list job objects using the fake client in my unit tests:

{"level":"error","ts":1563402934.2386599,"logger":"controller_gitopsconfig","msg":"error retrieving the job","error":"item[0]: can't assign or convert unstructured.Unstructured into v1.Job","stacktrace":"github.com/KohlsTechnology/eunomia/vendor/github.com/go-logr/zapr.(*zapLogger).Error\n\t/home/rspazzol/go/src/github.com/KohlsTechnology/eunomia/vendor/github.com/go-logr/zapr/zapr.go:128\ngithub.com/KohlsTechnology/eunomia/test/unit.TestDeleteRemovingFinalizer\n\t/home/rspazzol/go/src/github.com/KohlsTechnology/eunomia/test/unit/gitopsconfig_controller_test.go:372\ntesting.tRunner\n\t/usr/lib/golang/src/testing/testing.go:865"}

the code to reproduce the error is the following:

	// Create the deleteJob
	err = cl.Create(context.TODO(), deleteJob)
	if err != nil {
		log.Error(err, "Create Job - Failed creating Job type action of Delete")
	}

	jobList := &batchv1.JobList{}
	err = cl.List(context.TODO(), &client.ListOptions{
		//Namespace: nsn.Namespace,
	}, jobList)
	if err != nil {
		log.Error(err, "error retrieving the job")
	}

here is some of the initialization that may be relevant:

deleteJob = &batchv1.Job{
		TypeMeta: metav1.TypeMeta{
			Kind:       "Job",
			APIVersion: "batch/v1",
		},
		ObjectMeta: metav1.ObjectMeta{
			Name:      deleteJobName,
			Namespace: namespace,
			Labels:    map[string]string{"action": "delete"},
			OwnerReferences: []metav1.OwnerReference{
				{
					APIVersion:         "eunomia.kohls.io/v1alpha1",
					Kind:               "GitOpsConfig",
					Name:               name,
					Controller:         &controller,
					BlockOwnerDeletion: &blockDelete,
				},
			},
		},
		Spec: batchv1.JobSpec{
			Parallelism:  &parallelism,
			Completions:  &completions,
			BackoffLimit: &backoffLimit,
		},
		Status: batchv1.JobStatus{
			Succeeded: 2,
		},
	}
	s := scheme.Scheme
	apis.AddToScheme(s)
	// Initialize fake client
	cl := fake.NewFakeClient(objs...)

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/fake-clientThis affects the fake implementation of the client (which is a bit undermaintained ATM)help wantedDenotes an issue that needs help from a contributor. Must meet "help wanted" guidelines.kind/bugCategorizes issue or PR as related to a bug.lifecycle/frozenIndicates that an issue or PR should not be auto-closed due to staleness.priority/backlogHigher priority than priority/awaiting-more-evidence.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions