Skip to content

Commit

Permalink
up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
samirtahir91 committed Apr 1, 2024
1 parent 233b8c7 commit 891ed74
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 42 deletions.
42 changes: 0 additions & 42 deletions internal/controller/githubapp_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -637,46 +637,4 @@ func (r *GithubAppReconciler) SetupWithManager(mgr ctrl.Manager) error {
// Watch access token secrets owned by GithubApps.
Owns(&corev1.Secret{}, builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}, accessTokenSecretPredicate())).
Complete(r)

Context("When encountering errors while restarting pods", func() {
It("Should return an error when listing pods fails", func() {
ctx := context.Background()

// Create a GithubApp instance with the RestartPods field initialized
githubApp := &githubappv1.GithubApp{
ObjectMeta: metav1.ObjectMeta{
Name: githubAppName,
Namespace: sourceNamespace,
},
Spec: githubappv1.GithubAppSpec{
AppId: appId,
InstallId: installId,
PrivateKeySecret: privateKeySecret,
RestartPods: &githubappv1.RestartPodsSpec{
Labels: map[string]string{
"foo": "bar",
},
},
},
}

// Mock the List method of the client to return an error
mockErr := fmt.Errorf("mock error: failed to list pods")
r := &GithubAppReconciler{
Client: &fakeClient{
ListFunc: func(ctx context.Context, list runtime.Object, opts ...client.ListOption) error {
return mockErr
},
},
}

// Call the restartPods function
err := r.restartPods(ctx, githubApp)

// Check if the error is as expected
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal(fmt.Sprintf("failed to list pods with label foo=bar: %v", mockErr)))
})
})

}
42 changes: 42 additions & 0 deletions internal/controller/githubapp_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,4 +395,46 @@ var _ = Describe("GithubApp controller", func() {
}, "60s", "5s").Should(BeTrue(), "Failed to delete GitHubApp within timeout")
})
})

Context("When encountering errors while restarting pods", func() {
It("Should return an error when listing pods fails", func() {
ctx := context.Background()

// Create a GithubApp instance with the RestartPods field initialized
githubApp := &githubappv1.GithubApp{
ObjectMeta: metav1.ObjectMeta{
Name: githubAppName2,
Namespace: sourceNamespace,
},
Spec: githubappv1.GithubAppSpec{
AppId: appId,
InstallId: installId,
PrivateKeySecret: privateKeySecret,
RestartPods: &githubappv1.RestartPodsSpec{
Labels: map[string]string{
"foo": "bar",
},
},
},
}

// Mock the List method of the client to return an error
mockErr := fmt.Errorf("mock error: failed to list pods")
r := &GithubAppReconciler{
Client: &fakeClient{

Check failure on line 424 in internal/controller/githubapp_controller_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint and staticcheck

undefined: fakeClient
ListFunc: func(ctx context.Context, list runtime.Object, opts ...client.ListOption) error {

Check failure on line 425 in internal/controller/githubapp_controller_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint and staticcheck

undefined: runtime

Check failure on line 425 in internal/controller/githubapp_controller_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint and staticcheck

undefined: client (typecheck)
return mockErr
},
},
}

// Call the restartPods function
err := r.restartPods(ctx, githubApp)

// Check if the error is as expected
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal(fmt.Sprintf("failed to list pods with label foo=bar: %v", mockErr)))
})
})

})

0 comments on commit 891ed74

Please sign in to comment.