influxdb_suite_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package influxdb_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. deployName string
  18. namespace string
  19. database 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(&deployName, "name", "", "name of the Influxdb deployment")
  26. flag.StringVar(&namespace, "namespace", "", "namespace where the application is running")
  27. flag.StringVar(&database, "database", "", "name of the database to be used")
  28. flag.IntVar(&timeoutSeconds, "timeout", 120, "timeout in seconds")
  29. timeout = time.Duration(timeoutSeconds) * time.Second
  30. }
  31. func TestInfluxdb(t *testing.T) {
  32. RegisterFailHandler(Fail)
  33. RunSpecs(t, "Influxdb Persistence Test Suite")
  34. }
  35. func createJob(ctx context.Context, c kubernetes.Interface, name, port, image, action, token, query 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. job := &batchv1.Job{
  48. ObjectMeta: metav1.ObjectMeta{
  49. Name: name,
  50. },
  51. TypeMeta: metav1.TypeMeta{
  52. Kind: "Job",
  53. },
  54. Spec: batchv1.JobSpec{
  55. Template: v1.PodTemplateSpec{
  56. Spec: v1.PodSpec{
  57. RestartPolicy: "Never",
  58. Containers: []v1.Container{
  59. {
  60. Name: "influxdb",
  61. Image: image,
  62. Command: []string{
  63. "bash", "-ec",
  64. fmt.Sprintf("influxdb3 %s --database %s --host $INFLUX_HOST --token $ADMIN_TOKEN '%s'", action, database, query),
  65. },
  66. Env: []v1.EnvVar{
  67. {
  68. Name: "INFLUX_HOST",
  69. Value: fmt.Sprintf("http://%s:%s", deployName, port),
  70. },
  71. {
  72. Name: "ADMIN_TOKEN",
  73. Value: token,
  74. },
  75. },
  76. SecurityContext: securityContext,
  77. },
  78. },
  79. },
  80. },
  81. },
  82. }
  83. _, err := c.BatchV1().Jobs(namespace).Create(ctx, job, metav1.CreateOptions{})
  84. return err
  85. }