mirror of
https://github.com/fluxcd/flux2.git
synced 2025-11-01 18:26:25 +08:00
Add tests for flux trace command
Add tests for flux trace command that fake out the kubernetes client, load objects from a yaml file and create them in the client, and assert on the output of the trace command to an expected golden file. This is a follow up from the suggestions in PR https://github.com/fluxcd/flux2/pull/1626 which suggested that additional testing would be helpful. This test approach is modeled after the helm command tests. This required some changes to the kubernetes client setup to make it possible to use a fake. If we agree this pattern makes sense, it can be applied to other commands. Signed-off-by: Allen Porter <allen@thebends.org>
This commit is contained in:
@ -131,12 +131,39 @@ func KubeConfig(kubeConfigPath string, kubeContext string) (*rest.Config, error)
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func KubeClient(kubeConfigPath string, kubeContext string) (client.Client, error) {
|
||||
// KubeManger creates a Kubernetes client.Client. This interface exists to
|
||||
// facilitate unit testing and provide a fake client.
|
||||
type KubeManager interface {
|
||||
NewClient(string, string) (client.Client, error)
|
||||
}
|
||||
|
||||
type defaultKubeManager struct{}
|
||||
|
||||
func DefaultKubeManager() KubeManager {
|
||||
var manager defaultKubeManager
|
||||
return manager
|
||||
}
|
||||
|
||||
func (m defaultKubeManager) NewClient(kubeConfigPath string, kubeContext string) (client.Client, error) {
|
||||
cfg, err := KubeConfig(kubeConfigPath, kubeContext)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("kubernetes client initialization failed: %w", err)
|
||||
}
|
||||
|
||||
scheme := NewScheme()
|
||||
kubeClient, err := client.New(cfg, client.Options{
|
||||
Scheme: scheme,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("kubernetes client initialization failed: %w", err)
|
||||
}
|
||||
|
||||
return kubeClient, nil
|
||||
}
|
||||
|
||||
// Create the Scheme, methods for serializing and deserializing API objects
|
||||
// which can be shared by tests.
|
||||
func NewScheme() *apiruntime.Scheme {
|
||||
scheme := apiruntime.NewScheme()
|
||||
_ = apiextensionsv1.AddToScheme(scheme)
|
||||
_ = corev1.AddToScheme(scheme)
|
||||
@ -149,15 +176,13 @@ func KubeClient(kubeConfigPath string, kubeContext string) (client.Client, error
|
||||
_ = notificationv1.AddToScheme(scheme)
|
||||
_ = imagereflectv1.AddToScheme(scheme)
|
||||
_ = imageautov1.AddToScheme(scheme)
|
||||
return scheme
|
||||
}
|
||||
|
||||
kubeClient, err := client.New(cfg, client.Options{
|
||||
Scheme: scheme,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("kubernetes client initialization failed: %w", err)
|
||||
}
|
||||
|
||||
return kubeClient, nil
|
||||
func KubeClient(kubeConfigPath string, kubeContext string) (client.Client, error) {
|
||||
m := DefaultKubeManager()
|
||||
kubeClient, err := m.NewClient(kubeConfigPath, kubeContext)
|
||||
return kubeClient, err
|
||||
}
|
||||
|
||||
// SplitKubeConfigPath splits the given KUBECONFIG path based on the runtime OS
|
||||
|
||||
Reference in New Issue
Block a user