mirror of
https://github.com/fluxcd/flux2.git
synced 2025-10-29 07:19:07 +08:00
Remove fakeclient and use testenv for flux cmd tests
Remove use of the fake client, and replace with a real client connected to the testEnv. This required fixes to the yaml files as the testEnv has stricter verifcation of objects. This also meant it was not possible to test a GitRepository with a missing artifact since that is not a valid state. The tests are slower than before, taking around 7-10 seconds each because the testEnv is setup and destroyed for every test. These will be sped up in a follow up PR. Signed-off-by: Allen Porter <allen@thebends.org>
This commit is contained in:
@ -131,36 +131,6 @@ func KubeConfig(kubeConfigPath string, kubeContext string) (*rest.Config, error)
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
// 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.WithWatch, error)
|
||||
}
|
||||
|
||||
type defaultKubeManager struct{}
|
||||
|
||||
func DefaultKubeManager() KubeManager {
|
||||
var manager defaultKubeManager
|
||||
return manager
|
||||
}
|
||||
|
||||
func (m defaultKubeManager) NewClient(kubeConfigPath string, kubeContext string) (client.WithWatch, error) {
|
||||
cfg, err := KubeConfig(kubeConfigPath, kubeContext)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("kubernetes client initialization failed: %w", err)
|
||||
}
|
||||
|
||||
scheme := NewScheme()
|
||||
kubeClient, err := client.NewWithWatch(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 {
|
||||
@ -180,9 +150,20 @@ func NewScheme() *apiruntime.Scheme {
|
||||
}
|
||||
|
||||
func KubeClient(kubeConfigPath string, kubeContext string) (client.WithWatch, error) {
|
||||
m := DefaultKubeManager()
|
||||
kubeClient, err := m.NewClient(kubeConfigPath, kubeContext)
|
||||
return kubeClient, err
|
||||
cfg, err := KubeConfig(kubeConfigPath, kubeContext)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("kubernetes client initialization failed: %w", err)
|
||||
}
|
||||
|
||||
scheme := NewScheme()
|
||||
kubeClient, err := client.NewWithWatch(cfg, client.Options{
|
||||
Scheme: scheme,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("kubernetes client initialization failed: %w", err)
|
||||
}
|
||||
|
||||
return kubeClient, nil
|
||||
}
|
||||
|
||||
// SplitKubeConfigPath splits the given KUBECONFIG path based on the runtime OS
|
||||
|
||||
Reference in New Issue
Block a user