Skip to content

Commit a57a0a9

Browse files
Merge pull request #238 from splunk/test_service_account
Smoke test to verify service account attaches to standalone.
2 parents bc17a99 + f90fe3f commit a57a0a9

File tree

5 files changed

+131
-1
lines changed

5 files changed

+131
-1
lines changed

test/smoke/smoke_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ import (
2222
. "github.com/onsi/gomega"
2323

2424
"github.com/splunk/splunk-operator/test/testenv"
25+
26+
enterprisev1 "github.com/splunk/splunk-operator/pkg/apis/enterprise/v1beta1"
27+
splcommon "github.com/splunk/splunk-operator/pkg/splunk/common"
28+
corev1 "k8s.io/api/core/v1"
2529
)
2630

2731
func dumpGetPods(ns string) {
@@ -167,4 +171,36 @@ var _ = Describe("Smoke test", func() {
167171
testenv.VerifyLMConfiguredOnPod(deployment, standalonePodName)
168172
})
169173
})
174+
175+
Context("Standalone deployment (S1) with Service Account", func() {
176+
It("smoke: can deploy a standalone instance attached to a service account", func() {
177+
// Create Service Account
178+
serviceAccountName := "smoke-service-account"
179+
testenvInstance.CreateServiceAccount(serviceAccountName)
180+
181+
standaloneSpec := enterprisev1.StandaloneSpec{
182+
CommonSplunkSpec: enterprisev1.CommonSplunkSpec{
183+
Spec: splcommon.Spec{
184+
ImagePullPolicy: "IfNotPresent",
185+
},
186+
Volumes: []corev1.Volume{},
187+
ServiceAccount: serviceAccountName,
188+
},
189+
}
190+
191+
// Create standalone Deployment with License Master
192+
standalone, err := deployment.DeployStandalonewithGivenSpec(deployment.GetName(), standaloneSpec)
193+
Expect(err).To(Succeed(), "Unable to deploy standalone instance with LM")
194+
195+
// Wait for Standalone to be in READY status
196+
testenv.StandaloneReady(deployment, deployment.GetName(), standalone, testenvInstance)
197+
198+
// Verify MC Pod is Ready
199+
testenv.MCPodReady(testenvInstance.GetName(), deployment)
200+
201+
// Verify serviceAccount is configured on Pod
202+
standalonePodName := fmt.Sprintf(testenv.StandalonePod, deployment.GetName(), 0)
203+
testenv.VerifyServiceAccountConfiguredOnPod(deployment, testenvInstance.GetName(), standalonePodName, serviceAccountName)
204+
})
205+
})
170206
})

test/testenv/deployment.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,3 +417,13 @@ func (d *Deployment) DeployStandaloneWithLM(name string) (*enterprisev1.Standalo
417417
}
418418
return deployed.(*enterprisev1.Standalone), err
419419
}
420+
421+
// DeployStandalonewithGivenSpec deploys a standalone with given spec
422+
func (d *Deployment) DeployStandalonewithGivenSpec(name string, spec enterprisev1.StandaloneSpec) (*enterprisev1.Standalone, error) {
423+
standalone := newStandaloneWithGivenSpec(name, d.testenv.namespace, spec)
424+
deployed, err := d.deployCR(name, standalone)
425+
if err != nil {
426+
return nil, err
427+
}
428+
return deployed.(*enterprisev1.Standalone), err
429+
}

test/testenv/testenv.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,41 @@ func (testenv *TestEnv) createLicenseConfigMap() error {
493493
return nil
494494
}
495495

496+
// Create a service account config
497+
func newServiceAccount(ns string, serviceAccountName string) *corev1.ServiceAccount {
498+
499+
new := corev1.ServiceAccount{
500+
TypeMeta: metav1.TypeMeta{
501+
Kind: "ServiceAccount",
502+
},
503+
ObjectMeta: metav1.ObjectMeta{
504+
Name: serviceAccountName,
505+
Namespace: ns,
506+
},
507+
}
508+
509+
return &new
510+
}
511+
512+
// CreateServiceAccount Create a service account with given name
513+
func (testenv *TestEnv) CreateServiceAccount(name string) error {
514+
serviceAccountConfig := newServiceAccount(testenv.namespace, name)
515+
if err := testenv.GetKubeClient().Create(context.TODO(), serviceAccountConfig); err != nil {
516+
testenv.Log.Error(err, "Unable to create service account")
517+
return err
518+
}
519+
520+
testenv.pushCleanupFunc(func() error {
521+
err := testenv.GetKubeClient().Delete(context.TODO(), serviceAccountConfig)
522+
if err != nil {
523+
testenv.Log.Error(err, "Unable to delete service account")
524+
return err
525+
}
526+
return nil
527+
})
528+
return nil
529+
}
530+
496531
// NewDeployment creates a new deployment
497532
func (testenv *TestEnv) NewDeployment(name string) (*Deployment, error) {
498533
d := Deployment{

test/testenv/util.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,24 @@ func newStandalone(name, ns string) *enterprisev1.Standalone {
8686
return &new
8787
}
8888

89+
// newStandalone creates and initializes CR for Standalone Kind
90+
func newStandaloneWithGivenSpec(name, ns string, spec enterprisev1.StandaloneSpec) *enterprisev1.Standalone {
91+
92+
new := enterprisev1.Standalone{
93+
TypeMeta: metav1.TypeMeta{
94+
Kind: "Standalone",
95+
},
96+
ObjectMeta: metav1.ObjectMeta{
97+
Name: name,
98+
Namespace: ns,
99+
Finalizers: []string{"enterprise.splunk.com/delete-pvc"},
100+
},
101+
102+
Spec: spec,
103+
}
104+
return &new
105+
}
106+
89107
func newLicenseMaster(name, ns, licenseConfigMapName string) *enterprisev1.LicenseMaster {
90108
new := enterprisev1.LicenseMaster{
91109
TypeMeta: metav1.TypeMeta{
@@ -225,7 +243,7 @@ func newRole(name, ns string) *rbacv1.Role {
225243
Rules: []rbacv1.PolicyRule{
226244
{
227245
APIGroups: []string{""},
228-
Resources: []string{"services", "endpoints", "persistentvolumeclaims", "configmaps", "secrets", "pods"},
246+
Resources: []string{"services", "endpoints", "persistentvolumeclaims", "configmaps", "secrets", "pods", "serviceaccounts", "pods/exec"},
229247
Verbs: []string{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
230248
},
231249
{

test/testenv/verificationutils.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,24 @@ package testenv
1717
import (
1818
"encoding/json"
1919
"fmt"
20+
"os/exec"
21+
"strings"
2022

2123
gomega "github.com/onsi/gomega"
2224

2325
enterprisev1 "github.com/splunk/splunk-operator/pkg/apis/enterprise/v1beta1"
2426
splcommon "github.com/splunk/splunk-operator/pkg/splunk/common"
27+
logf "sigs.k8s.io/controller-runtime/pkg/log"
2528
)
2629

30+
// PodDetailsStruct captures output of kubectl get pods podname -o json
31+
type PodDetailsStruct struct {
32+
Spec struct {
33+
ServiceAccount string `json:"serviceAccount"`
34+
ServiceAccountName string `json:"serviceAccountName"`
35+
}
36+
}
37+
2738
// StandaloneReady verify Standlone is in ReadyStatus and does not flip-flop
2839
func StandaloneReady(deployment *Deployment, deploymentName string, standalone *enterprisev1.Standalone, testenvInstance *TestEnv) {
2940
gomega.Eventually(func() splcommon.Phase {
@@ -223,3 +234,23 @@ func VerifyLMConfiguredOnPod(deployment *Deployment, podName string) {
223234
return lmConfigured
224235
}, deployment.GetTimeout(), PollInterval).Should(gomega.Equal(true))
225236
}
237+
238+
// VerifyServiceAccountConfiguredOnPod check if given service account is configured on given pod
239+
func VerifyServiceAccountConfiguredOnPod(deployment *Deployment, ns string, podName string, serviceAccount string) {
240+
gomega.Eventually(func() bool {
241+
output, err := exec.Command("kubectl", "get", "pods", "-n", ns, podName, "-o", "json").Output()
242+
if err != nil {
243+
cmd := fmt.Sprintf("kubectl get pods -n %s %s -o json", ns, podName)
244+
logf.Log.Error(err, "Failed to execute command", "command", cmd)
245+
return false
246+
}
247+
restResponse := PodDetailsStruct{}
248+
err = json.Unmarshal([]byte(output), &restResponse)
249+
if err != nil {
250+
logf.Log.Error(err, "Failed to parse cluster searchheads")
251+
return false
252+
}
253+
logf.Log.Info("Service Account on Pod", "FOUND", restResponse.Spec.ServiceAccount, "EXPECTED", serviceAccount)
254+
return strings.Contains(serviceAccount, restResponse.Spec.ServiceAccount)
255+
}, deployment.GetTimeout(), PollInterval).Should(gomega.Equal(true))
256+
}

0 commit comments

Comments
 (0)