scylladb_suite_test.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package scylladb_test
  2. import (
  3. "context"
  4. "flag"
  5. "testing"
  6. "time"
  7. . "github.com/onsi/ginkgo/v2"
  8. . "github.com/onsi/gomega"
  9. batchv1 "k8s.io/api/batch/v1"
  10. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  11. v1 "k8s.io/api/core/v1"
  12. "k8s.io/client-go/kubernetes"
  13. )
  14. var (
  15. kubeconfig string
  16. stsName string
  17. namespace string
  18. username string
  19. password string
  20. timeoutSeconds int
  21. timeout time.Duration
  22. )
  23. func init() {
  24. flag.StringVar(&kubeconfig, "kubeconfig", "", "absolute path to the kubeconfig file")
  25. flag.StringVar(&stsName, "name", "", "name of the statefulset")
  26. flag.StringVar(&namespace, "namespace", "", "namespace where the application is running")
  27. flag.StringVar(&username, "username", "", "database user")
  28. flag.StringVar(&password, "password", "", "database password for username")
  29. flag.IntVar(&timeoutSeconds, "timeout", 500, "timeout in seconds")
  30. timeout = time.Duration(timeoutSeconds) * time.Second
  31. }
  32. func TestScylladb(t *testing.T) {
  33. RegisterFailHandler(Fail)
  34. RunSpecs(t, "Scylladb Persistence Test Suite")
  35. }
  36. func createJob(ctx context.Context, c kubernetes.Interface, name, port, image, stmt string) error {
  37. securityContext := &v1.SecurityContext{
  38. Privileged: &[]bool{false}[0],
  39. AllowPrivilegeEscalation: &[]bool{false}[0],
  40. RunAsNonRoot: &[]bool{true}[0],
  41. Capabilities: &v1.Capabilities{
  42. Drop: []v1.Capability{"ALL"},
  43. },
  44. SeccompProfile: &v1.SeccompProfile{
  45. Type: "RuntimeDefault",
  46. },
  47. }
  48. job := &batchv1.Job{
  49. ObjectMeta: metav1.ObjectMeta{
  50. Name: name,
  51. },
  52. TypeMeta: metav1.TypeMeta{
  53. Kind: "Job",
  54. },
  55. Spec: batchv1.JobSpec{
  56. Template: v1.PodTemplateSpec{
  57. Spec: v1.PodSpec{
  58. RestartPolicy: "Never",
  59. Containers: []v1.Container{
  60. {
  61. Name: "scylladb",
  62. Image: image,
  63. Command: []string{"cqlsh", "-u", username, "-p", password, "-e", stmt},
  64. Env: []v1.EnvVar{
  65. {
  66. Name: "CQLSH_HOST",
  67. Value: stsName,
  68. },
  69. {
  70. Name: "CQLSH_PORT",
  71. Value: port,
  72. },
  73. },
  74. SecurityContext: securityContext,
  75. },
  76. },
  77. },
  78. },
  79. },
  80. }
  81. _, err := c.BatchV1().Jobs(namespace).Create(ctx, job, metav1.CreateOptions{})
  82. return err
  83. }