Skip to content

Commit

Permalink
Add functions to retrieve ServiceAccounts
Browse files Browse the repository at this point in the history
  • Loading branch information
sutaakar authored and openshift-merge-bot[bot] committed Jan 17, 2025
1 parent 222395d commit 5748d67
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
32 changes: 32 additions & 0 deletions support/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,38 @@ func CreateServiceAccount(t Test, namespace string) *corev1.ServiceAccount {
return serviceAccount
}

func ServiceAccount(t Test, namespace, name string) func(g gomega.Gomega) *corev1.ServiceAccount {
return func(g gomega.Gomega) *corev1.ServiceAccount {
sa, err := t.Client().Core().CoreV1().ServiceAccounts(namespace).Get(t.Ctx(), name, metav1.GetOptions{})
g.Expect(err).NotTo(gomega.HaveOccurred())
return sa
}
}

func GetServiceAccount(t Test, namespace, name string) *corev1.ServiceAccount {
t.T().Helper()
return ServiceAccount(t, namespace, name)(t)
}

func ServiceAccounts(t Test, namespace string) func(g gomega.Gomega) []*corev1.ServiceAccount {
return func(g gomega.Gomega) []*corev1.ServiceAccount {
sas, err := t.Client().Core().CoreV1().ServiceAccounts(namespace).List(t.Ctx(), metav1.ListOptions{})
g.Expect(err).NotTo(gomega.HaveOccurred())

sasp := []*corev1.ServiceAccount{}
for _, v := range sas.Items {
sasp = append(sasp, &v)
}

return sasp
}
}

func GetServiceAccounts(t Test, namespace string) []*corev1.ServiceAccount {
t.T().Helper()
return ServiceAccounts(t, namespace)(t)
}

func CreatePersistentVolumeClaim(t Test, namespace string, storageSize string, accessMode ...corev1.PersistentVolumeAccessMode) *corev1.PersistentVolumeClaim {
t.T().Helper()

Expand Down
35 changes: 35 additions & 0 deletions support/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,38 @@ func TestResourceName(t *testing.T) {
test.Expect(err).To(gomega.BeNil(), "Expected no error, but got '%v'", err)
test.Expect(resourceName).To(gomega.Equal("test-resource"), "Expected resource name 'test-resource', but got '%s'", resourceName)
}

func TestGetServiceAccount(t *testing.T) {
test := NewTest(t)

createdSa := &corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: "my-sa",
Namespace: "my-namespace",
},
}

test.client.Core().CoreV1().ServiceAccounts("my-namespace").Create(test.ctx, createdSa, metav1.CreateOptions{})
sa := GetServiceAccount(test, "my-namespace", "my-sa")

test.Expect(sa.Name).To(gomega.Equal("my-sa"))
test.Expect(sa.Namespace).To(gomega.Equal("my-namespace"))
}

func TestGetServiceAccounts(t *testing.T) {
test := NewTest(t)

createdSa := &corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: "my-sa-1",
Namespace: "my-namespace",
},
}

test.client.Core().CoreV1().ServiceAccounts("my-namespace").Create(test.ctx, createdSa, metav1.CreateOptions{})
sas := GetServiceAccounts(test, "my-namespace")

test.Expect(len(sas)).To(gomega.Equal(1))
test.Expect(sas[0].Name).To(gomega.Equal("my-sa-1"))
test.Expect(sas[0].Namespace).To(gomega.Equal("my-namespace"))
}

0 comments on commit 5748d67

Please sign in to comment.