nats_suite_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package nats_test
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "testing"
  7. "time"
  8. . "github.com/onsi/ginkgo/v2"
  9. . "github.com/onsi/gomega"
  10. batchv1 "k8s.io/api/batch/v1"
  11. v1 "k8s.io/api/core/v1"
  12. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  13. "k8s.io/client-go/kubernetes"
  14. )
  15. var (
  16. kubeconfig string
  17. releaseName string
  18. namespace string
  19. username string
  20. password string
  21. timeoutSeconds int
  22. timeout time.Duration
  23. )
  24. func init() {
  25. flag.StringVar(&kubeconfig, "kubeconfig", "", "absolute path to the kubeconfig file")
  26. flag.StringVar(&releaseName, "name", "", "name of the chart release")
  27. flag.StringVar(&namespace, "namespace", "", "namespace where the application is running")
  28. flag.StringVar(&username, "username", "", "nats user")
  29. flag.StringVar(&password, "password", "", "password for nats user")
  30. flag.IntVar(&timeoutSeconds, "timeout", 300, "timeout in seconds")
  31. timeout = time.Duration(timeoutSeconds) * time.Second
  32. }
  33. func TestNATS(t *testing.T) {
  34. RegisterFailHandler(Fail)
  35. RunSpecs(t, "NATS Persistence Test Suite")
  36. }
  37. func createJob(ctx context.Context, c kubernetes.Interface, name string, port string, args ...string) error {
  38. securityContext := &v1.SecurityContext{
  39. Privileged: &[]bool{false}[0],
  40. AllowPrivilegeEscalation: &[]bool{false}[0],
  41. RunAsNonRoot: &[]bool{true}[0],
  42. Capabilities: &v1.Capabilities{
  43. Drop: []v1.Capability{"ALL"},
  44. },
  45. SeccompProfile: &v1.SeccompProfile{
  46. Type: "RuntimeDefault",
  47. },
  48. }
  49. command := []string{"nats", "kv"}
  50. command = append(command, args[:]...)
  51. job := &batchv1.Job{
  52. ObjectMeta: metav1.ObjectMeta{
  53. Name: name,
  54. },
  55. TypeMeta: metav1.TypeMeta{
  56. Kind: "Job",
  57. },
  58. Spec: batchv1.JobSpec{
  59. Template: v1.PodTemplateSpec{
  60. Spec: v1.PodSpec{
  61. RestartPolicy: "Never",
  62. Containers: []v1.Container{
  63. {
  64. Name: "nats",
  65. Image: "bitnami/natscli:latest",
  66. Command: command,
  67. Env: []v1.EnvVar{
  68. {
  69. Name: "NATS_URL",
  70. Value: fmt.Sprintf("nats://%s:%s", releaseName, port),
  71. },
  72. {
  73. Name: "NATS_USER",
  74. Value: username,
  75. },
  76. {
  77. Name: "NATS_PASSWORD",
  78. Value: password,
  79. },
  80. {
  81. Name: "NATS_CERT",
  82. Value: "/certs/client/tls.crt",
  83. },
  84. {
  85. Name: "NATS_KEY",
  86. Value: "/certs/client/tls.key",
  87. },
  88. {
  89. Name: "NATS_CA",
  90. Value: "/certs/ca/tls.crt",
  91. },
  92. },
  93. SecurityContext: securityContext,
  94. VolumeMounts: []v1.VolumeMount{
  95. {
  96. Name: "ca-cert",
  97. MountPath: "/certs/ca",
  98. },
  99. {
  100. Name: "client-cert",
  101. MountPath: "/certs/client",
  102. },
  103. },
  104. },
  105. },
  106. Volumes: []v1.Volume{
  107. {
  108. Name: "ca-cert",
  109. VolumeSource: v1.VolumeSource{
  110. Secret: &v1.SecretVolumeSource{
  111. SecretName: fmt.Sprintf("%s-ca-crt", releaseName),
  112. },
  113. },
  114. },
  115. {
  116. Name: "client-cert",
  117. VolumeSource: v1.VolumeSource{
  118. Secret: &v1.SecretVolumeSource{
  119. SecretName: fmt.Sprintf("%s-client-crt", releaseName),
  120. },
  121. },
  122. },
  123. },
  124. },
  125. },
  126. },
  127. }
  128. _, err := c.BatchV1().Jobs(namespace).Create(ctx, job, metav1.CreateOptions{})
  129. return err
  130. }