mirror of
https://github.com/portainer/portainer.git
synced 2025-07-19 05:19:39 +02:00
Co-authored-by: James Carppe <85850129+jamescarppe@users.noreply.github.com> Co-authored-by: Anthony Lapenna <anthony.lapenna@portainer.io> Co-authored-by: andres-portainer <91705312+andres-portainer@users.noreply.github.com> Co-authored-by: Oscar Zhou <100548325+oscarzhou-portainer@users.noreply.github.com> Co-authored-by: Yajith Dayarathna <yajith.dayarathna@portainer.io> Co-authored-by: LP B <xAt0mZ@users.noreply.github.com> Co-authored-by: oscarzhou <oscar.zhou@portainer.io> Co-authored-by: testA113 <aliharriss1995@gmail.com>
66 lines
1.8 KiB
Go
66 lines
1.8 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
models "github.com/portainer/portainer/api/http/models/kubernetes"
|
|
batchv1 "k8s.io/api/batch/v1"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
kfake "k8s.io/client-go/kubernetes/fake"
|
|
)
|
|
|
|
// TestFetchCronJobs tests the fetchCronJobs method for both admin and non-admin clients
|
|
// It creates a fake Kubernetes client and passes it to the fetchCronJobs method
|
|
// It then logs the fetched Cron Jobs
|
|
// non-admin client will have access to the default namespace only
|
|
func (kcl *KubeClient) TestFetchCronJobs(t *testing.T) {
|
|
t.Run("admin client can fetch Cron Jobs from all namespaces", func(t *testing.T) {
|
|
kcl.cli = kfake.NewSimpleClientset()
|
|
kcl.instanceID = "test"
|
|
kcl.IsKubeAdmin = true
|
|
|
|
cronJobs, err := kcl.GetCronJobs("")
|
|
if err != nil {
|
|
t.Fatalf("Failed to fetch Cron Jobs: %v", err)
|
|
}
|
|
|
|
t.Logf("Fetched Cron Jobs: %v", cronJobs)
|
|
})
|
|
|
|
t.Run("non-admin client can fetch Cron Jobs from the default namespace only", func(t *testing.T) {
|
|
kcl.cli = kfake.NewSimpleClientset()
|
|
kcl.instanceID = "test"
|
|
kcl.IsKubeAdmin = false
|
|
kcl.NonAdminNamespaces = []string{"default"}
|
|
|
|
cronJobs, err := kcl.GetCronJobs("")
|
|
if err != nil {
|
|
t.Fatalf("Failed to fetch Cron Jobs: %v", err)
|
|
}
|
|
|
|
t.Logf("Fetched Cron Jobs: %v", cronJobs)
|
|
})
|
|
|
|
t.Run("delete Cron Jobs", func(t *testing.T) {
|
|
kcl.cli = kfake.NewSimpleClientset()
|
|
kcl.instanceID = "test"
|
|
|
|
_, err := kcl.cli.BatchV1().CronJobs("default").Create(context.Background(), &batchv1.CronJob{
|
|
ObjectMeta: metav1.ObjectMeta{Name: "test-cronjob"},
|
|
}, metav1.CreateOptions{})
|
|
if err != nil {
|
|
t.Fatalf("Failed to create cron job: %v", err)
|
|
}
|
|
|
|
err = kcl.DeleteCronJobs(models.K8sCronJobDeleteRequests{
|
|
"default": []string{"test-cronjob"},
|
|
})
|
|
|
|
if err != nil {
|
|
t.Fatalf("Failed to delete Cron Jobs: %v", err)
|
|
}
|
|
|
|
t.Logf("Deleted Cron Jobs")
|
|
})
|
|
}
|