etcd_suite_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package etcd_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. stsName string
  18. namespace 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 primary statefulset")
  26. flag.StringVar(&namespace, "namespace", "", "namespace where the application is running")
  27. flag.StringVar(&password, "password", "", "database password for username")
  28. flag.IntVar(&timeoutSeconds, "timeout", 120, "timeout in seconds")
  29. timeout = time.Duration(timeoutSeconds) * time.Second
  30. }
  31. func TestEtcd(t *testing.T) {
  32. RegisterFailHandler(Fail)
  33. RunSpecs(t, "Etcd Persistence Test Suite")
  34. }
  35. func createJob(ctx context.Context, c kubernetes.Interface, name string, port string, image string, args ...string) error {
  36. securityContext := &v1.SecurityContext{
  37. Privileged: &[]bool{false}[0],
  38. AllowPrivilegeEscalation: &[]bool{false}[0],
  39. RunAsNonRoot: &[]bool{true}[0],
  40. Capabilities: &v1.Capabilities{
  41. Drop: []v1.Capability{"ALL"},
  42. },
  43. SeccompProfile: &v1.SeccompProfile{
  44. Type: "RuntimeDefault",
  45. },
  46. }
  47. command := []string{"etcdctl", "--user", "root:$(ETCD_ROOT_PASSWORD)"}
  48. command = append(command, args[:]...)
  49. job := &batchv1.Job{
  50. ObjectMeta: metav1.ObjectMeta{
  51. Name: name,
  52. },
  53. TypeMeta: metav1.TypeMeta{
  54. Kind: "Job",
  55. },
  56. Spec: batchv1.JobSpec{
  57. Template: v1.PodTemplateSpec{
  58. Spec: v1.PodSpec{
  59. RestartPolicy: "Never",
  60. Containers: []v1.Container{
  61. {
  62. Name: "etcd",
  63. Image: image,
  64. Command: command,
  65. Env: []v1.EnvVar{
  66. {
  67. Name: "ETCDCTL_ENDPOINTS",
  68. Value: fmt.Sprintf("%s:%s", stsName, port),
  69. },
  70. {
  71. Name: "ETCD_ROOT_PASSWORD",
  72. Value: password,
  73. },
  74. },
  75. SecurityContext: securityContext,
  76. },
  77. },
  78. },
  79. },
  80. },
  81. }
  82. _, err := c.BatchV1().Jobs(namespace).Create(ctx, job, metav1.CreateOptions{})
  83. return err
  84. }