Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate mock return an error while trying to parse a imported package name alias #179

Open
FloMedja opened this issue Jun 4, 2024 · 0 comments

Comments

@FloMedja
Copy link

FloMedja commented Jun 4, 2024

Actual behavior

I get a unknow package error during a mock generation due to an import with an a alias name. The error message is :

2024/06/04 12:51:12 Loading input failed: /vendor/k8s.io/client-go/kubernetes/typed/core/v1/pod_expansion.go:44:9: failed parsing returns: /vendor/k8s.io/client-go/kubernetes/typed/core/v1/pod_expansion.go:44:48: unknown package "restclient"
exit status 1

Expected behavior

The mock generation works without errors.

To Reproduce

  1. Create a file with the following content file
//go:generate go run go.uber.org/mock/mockgen -source $GOFILE -package=fake -destination=fake/zz_generated_mock_$GOFILE

package kubernetes

import (
	v1 "k8s.io/client-go/kubernetes/typed/core/v1"
)

type PodPodExpansion interface {
	v1.PodExpansion
}

For information, This is the useful part of the content of the PodExpansion for the current bug :

package v1

import (
	"context"

	v1 "k8s.io/api/core/v1"
	policyv1 "k8s.io/api/policy/v1"
	policyv1beta1 "k8s.io/api/policy/v1beta1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/util/net"
	"k8s.io/client-go/kubernetes/scheme"
	restclient "k8s.io/client-go/rest"
)

// The PodExpansion interface allows manually adding extra methods to the PodInterface.
type PodExpansion interface {
	Bind(ctx context.Context, binding *v1.Binding, opts metav1.CreateOptions) error
	// Evict submits a policy/v1beta1 Eviction request to the pod's eviction subresource.
	// Equivalent to calling EvictV1beta1.
	// Deprecated: Use EvictV1() (supported in 1.22+) or EvictV1beta1().
	Evict(ctx context.Context, eviction *policyv1beta1.Eviction) error
	// EvictV1 submits a policy/v1 Eviction request to the pod's eviction subresource.
	// Supported in 1.22+.
	EvictV1(ctx context.Context, eviction *policyv1.Eviction) error
	// EvictV1beta1 submits a policy/v1beta1 Eviction request to the pod's eviction subresource.
	// Supported in 1.22+.
	EvictV1beta1(ctx context.Context, eviction *policyv1beta1.Eviction) error
	GetLogs(name string, opts *v1.PodLogOptions) *restclient.Request
	ProxyGet(scheme, name, port, path string, params map[string]string) restclient.ResponseWrapper
}
  1. Run the mock generation. You will get a similar error as mentioned earlier
2024/06/04 12:51:12 Loading input failed: /vendor/k8s.io/client-go/kubernetes/typed/core/v1/pod_expansion.go:44:9: failed parsing returns: /vendor/k8s.io/client-go/kubernetes/typed/core/v1/pod_expansion.go:44:48: unknown package "restclient"
exit status 1

Additional Information

  • I tried this generate command :
//go:generate go run go.uber.org/mock/mockgen -source $GOFILE -package=fake -destination=fake/zz_generated_mock_$GOFILE -imports restclient=k8s.io/client-go/rest

It returns the same error.

  • When I copy the functions from the interface I would like to implement , the mock generation works . So the following content generate the mock file without errors.
//go:generate go run go.uber.org/mock/mockgen -source $GOFILE -package=fake -destination=fake/zz_generated_mock_$GOFILE

package kubernetes

import (
	"context"

	v1 "k8s.io/api/core/v1"
	policyv1 "k8s.io/api/policy/v1"
	policyv1beta1 "k8s.io/api/policy/v1beta1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	restclient "k8s.io/client-go/rest"
)

type PodPodExpansion interface {
	Bind(ctx context.Context, binding *v1.Binding, opts metav1.CreateOptions) error
	// Evict submits a policy/v1beta1 Eviction request to the pod's eviction subresource.
	// Equivalent to calling EvictV1beta1.
	// Deprecated: Use EvictV1() (supported in 1.22+) or EvictV1beta1().
	Evict(ctx context.Context, eviction *policyv1beta1.Eviction) error
	// EvictV1 submits a policy/v1 Eviction request to the pod's eviction subresource.
	// Supported in 1.22+.
	EvictV1(ctx context.Context, eviction *policyv1.Eviction) error
	// EvictV1beta1 submits a policy/v1beta1 Eviction request to the pod's eviction subresource.
	// Supported in 1.22+.
	EvictV1beta1(ctx context.Context, eviction *policyv1beta1.Eviction) error
	GetLogs(name string, opts *v1.PodLogOptions) *restclient.Request
	ProxyGet(scheme, name, port, path string, params map[string]string) restclient.ResponseWrapper
}

  • When I change the import from restclient "k8s.io/client-go/rest" to rest "k8s.io/client-go/rest" , in the original files that generate the error, it also works.

I suspect that the import alias name restclient have some issues to be parsed by the mockgen.

Triage Notes for the Maintainers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant