-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(test): add tests to vcluster controller (#49)
- Loading branch information
Showing
513 changed files
with
231,001 additions
and
17 deletions.
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
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,115 @@ | ||
package controllerstest | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/loft-sh/cluster-api-provider-vcluster/api/v1alpha1" | ||
"github.com/loft-sh/cluster-api-provider-vcluster/controllers" | ||
"github.com/onsi/ginkgo/v2" | ||
"github.com/onsi/gomega" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/types" | ||
|
||
fakeclientset "k8s.io/client-go/kubernetes/fake" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
) | ||
|
||
var ( | ||
kubeconfigBytes = []byte(` | ||
kind: Config | ||
apiVersion: v1 | ||
clusters: | ||
- cluster: | ||
api-version: v1 | ||
server: https://test:443 | ||
certificate-authority: test.crt | ||
name: kube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VRRME16TXoKV2pBak1TRXdId1lEVlFRRERCaHJNM010WTJ4cFpXNTBMV05oUURFM01EZ3dNRGd5TVRNd1dUQVRCZ2NxaGtqTwpQUUlCQmdncWhrak9QUU1CQndOQ0FBVCtZbTVnL0o4TzIwQ0llSFB4Z2hRWTBXajl3QVZzc0QxdHRzS0VnMFFRCjA3UDNLZEttV3AzS3BvV3FkdkN4dTNFMkp4ZDBGVDh5eG1IOVJiamVXRW90bzBJd1FEQU9CZ05WSFE4QkFmOEUKQkFNQ0FxUXdEd1lEVlIwVEFRSC9CQVV3QXdFQi96QWRCZ05WSFE0RUZnUVV2cmRKbXRFNzNiTXNIWjA0MThBTgpXZnVNVlNJd0NnWUlLb1pJemowRUF3SURSd0F3UkFJZ1VldS9yVnBmc1NoUUZmSjIyb05CMVhwY1djUWFPY2FBCnF4ZGg0dzhGdHBRQ0lIdmVTRE00clN2V3ZGZktROXRWTDRFZkpUdDc2cWliMFMyY2FBdDQwUHNGCi0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0K | ||
client-key-data: LS0tLS1CRUdJTiBFQyBQUklWQVRFIEtFWS0tLS0tCk1IY0NBUUVFSVBWS2JlQzJua2JaZ1UxZUNaS2NxUHpnSXd0MWxtOGcxZFNRaENoaHRURWVvQW9HQ0NxR1NNNDkKQXdFSG9VUURRZ0FFS2o3WWJpd0lseUxsZzFMWW5OVHZKK3JjZ08yMHlhdTdxV1FWWndzWm9naE5KUG82VmF5SAp0bHNGQ2lxUTBqMnpCTDM0RmZPWlpvMEpDd1c0SVZuOEN3PT0KLS0tLS1FTkQgRUMgUFJJVkFURSBLRVktLS0tLQo= | ||
`) | ||
) | ||
|
||
func TestRunControllersTests(t *testing.T) { | ||
gomega.RegisterFailHandler(ginkgo.Fail) | ||
ginkgo.RunSpecs(t, "controllers suite") | ||
} | ||
|
||
var _ = ginkgo.Describe("Vcluster Controller test", func() { | ||
ginkgo.Context("Reconcile", func() { | ||
var ( | ||
reconciler *controllers.VClusterReconciler | ||
ctx context.Context | ||
scheme *runtime.Scheme | ||
hemlClient *MockHelmClient | ||
) | ||
|
||
ginkgo.BeforeEach(func() { | ||
scheme = runtime.NewScheme() | ||
err := v1alpha1.AddToScheme(scheme) | ||
gomega.Expect(err).NotTo(gomega.HaveOccurred()) | ||
|
||
err = corev1.AddToScheme(scheme) | ||
gomega.Expect(err).NotTo(gomega.HaveOccurred()) | ||
|
||
ctx = context.Background() | ||
hemlClient = &MockHelmClient{} | ||
}) | ||
|
||
ginkgo.It("reconcile successfully", func() { | ||
vCluster := &v1alpha1.VCluster{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-vcluster", | ||
Namespace: "default", | ||
}, | ||
Spec: v1alpha1.VClusterSpec{}, | ||
} | ||
secret := &corev1.Secret{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: vCluster.Namespace, | ||
Name: "vc-test-vcluster", | ||
}, | ||
Data: map[string][]byte{ | ||
"config": kubeconfigBytes, | ||
}, | ||
} | ||
hemlClient.On("Upgrade").Return(nil) | ||
f := fakeclientset.NewSimpleClientset() | ||
|
||
_, err := f.CoreV1().ServiceAccounts("default").Create(context.Background(), &corev1.ServiceAccount{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "default", | ||
Namespace: "default", | ||
}, | ||
}, metav1.CreateOptions{}) | ||
gomega.Expect(err).NotTo(gomega.HaveOccurred()) | ||
|
||
reconciler = &controllers.VClusterReconciler{ | ||
Client: fakeclient.NewClientBuilder().WithScheme(scheme).WithObjects(vCluster, secret).Build(), | ||
HelmClient: hemlClient, | ||
Scheme: scheme, | ||
ClientConfigGetter: &fakeConfigGetter{ | ||
fake: f, | ||
}, | ||
HTTPClientGetter: &fakeHTTPClientGetter{}, | ||
} | ||
req := ctrl.Request{ | ||
NamespacedName: types.NamespacedName{ | ||
Name: vCluster.Name, | ||
Namespace: vCluster.Namespace, | ||
}, | ||
} | ||
result, err := reconciler.Reconcile(ctx, req) | ||
gomega.Expect(err).NotTo(gomega.HaveOccurred()) | ||
gomega.Expect(result.RequeueAfter).Should(gomega.Equal(time.Minute)) | ||
}) | ||
}) | ||
|
||
}) |
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,33 @@ | ||
package controllerstest | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"net/http/httptest" | ||
|
||
"k8s.io/client-go/kubernetes" | ||
fakeclientset "k8s.io/client-go/kubernetes/fake" | ||
"k8s.io/client-go/rest" | ||
restfake "k8s.io/client-go/rest/fake" | ||
) | ||
|
||
type fakeConfigGetter struct { | ||
fake *fakeclientset.Clientset | ||
} | ||
|
||
func (f *fakeConfigGetter) NewForConfig(_ *rest.Config) (kubernetes.Interface, error) { | ||
return f.fake, nil | ||
} | ||
|
||
type fakeHTTPClientGetter struct { | ||
} | ||
|
||
func (f *fakeHTTPClientGetter) ClientFor(_ http.RoundTripper, _ time.Duration) *http.Client { | ||
return restfake.CreateHTTPClient(func(*http.Request) (*http.Response, error) { | ||
recorder := httptest.NewRecorder() | ||
fmt.Fprint(recorder, "ok") | ||
return recorder.Result(), nil | ||
}) | ||
} |
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 controllerstest | ||
|
||
import ( | ||
"github.com/loft-sh/cluster-api-provider-vcluster/pkg/helm" | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
type MockHelmClient struct { | ||
mock.Mock | ||
} | ||
|
||
func (m *MockHelmClient) Install(_, _ string, _ helm.UpgradeOptions) error { | ||
args := m.Called() | ||
return args.Error(0) | ||
} | ||
|
||
func (m *MockHelmClient) Upgrade(_, _ string, _ helm.UpgradeOptions) error { | ||
args := m.Called() | ||
return args.Error(0) | ||
} | ||
|
||
func (m *MockHelmClient) Rollback(_, _ string, _ string) error { | ||
args := m.Called() | ||
return args.Error(0) | ||
} | ||
|
||
func (m *MockHelmClient) Delete(_, _ string) error { | ||
args := m.Called() | ||
return args.Error(0) | ||
} | ||
|
||
func (m *MockHelmClient) Exists(_, _ string) (bool, error) { | ||
args := m.Called() | ||
return args.Bool(0), args.Error(1) | ||
} |
Oops, something went wrong.