-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(test): create a struct and interface to allow kube client mocks…
… in handlers
- Loading branch information
Showing
3 changed files
with
46 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package kubeclient | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/replicatedhq/kots/pkg/k8sutil" | ||
kbclient "sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
var _ KubeClientBuilder = (*Builder)(nil) | ||
var _ KubeClientBuilder = (*MockBuilder)(nil) | ||
|
||
// KubeClientBuilder interface is used as an abstraction to get a kube client. Useful to mock the client in tests. | ||
type KubeClientBuilder interface { | ||
GetKubeClient(ctx context.Context) (kbclient.Client, error) | ||
} | ||
|
||
// Builder is the default implementation of KubeClientBuilder. It returns a regular kube client. | ||
type Builder struct{} | ||
|
||
// GetKubeClient returns a regular kube client. | ||
func (b *Builder) GetKubeClient(ctx context.Context) (kbclient.Client, error) { | ||
return k8sutil.GetKubeClient(ctx) | ||
} | ||
|
||
// MockBuilder is a mock implementation of KubeClientBuilder. It returns the client that was set in the struct allowing | ||
// you to set a fakeClient for example. | ||
type MockBuilder struct { | ||
client kbclient.Client | ||
} | ||
|
||
// GetKubeClient returns the client that was set in the struct. | ||
func (b *MockBuilder) GetKubeClient(ctx context.Context) (kbclient.Client, error) { | ||
return b.client, nil | ||
} |