123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- package etcd_test
- import (
- "context"
- "flag"
- "fmt"
- "testing"
- "time"
- . "github.com/onsi/ginkgo/v2"
- . "github.com/onsi/gomega"
- batchv1 "k8s.io/api/batch/v1"
- v1 "k8s.io/api/core/v1"
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
- "k8s.io/client-go/kubernetes"
- )
- var (
- kubeconfig string
- stsName string
- namespace string
- password string
- timeoutSeconds int
- timeout time.Duration
- )
- func init() {
- flag.StringVar(&kubeconfig, "kubeconfig", "", "absolute path to the kubeconfig file")
- flag.StringVar(&stsName, "name", "", "name of the primary statefulset")
- flag.StringVar(&namespace, "namespace", "", "namespace where the application is running")
- flag.StringVar(&password, "password", "", "database password for username")
- flag.IntVar(&timeoutSeconds, "timeout", 120, "timeout in seconds")
- timeout = time.Duration(timeoutSeconds) * time.Second
- }
- func TestEtcd(t *testing.T) {
- RegisterFailHandler(Fail)
- RunSpecs(t, "Etcd Persistence Test Suite")
- }
- func createJob(ctx context.Context, c kubernetes.Interface, name string, port string, image string, args ...string) error {
- securityContext := &v1.SecurityContext{
- Privileged: &[]bool{false}[0],
- AllowPrivilegeEscalation: &[]bool{false}[0],
- RunAsNonRoot: &[]bool{true}[0],
- Capabilities: &v1.Capabilities{
- Drop: []v1.Capability{"ALL"},
- },
- SeccompProfile: &v1.SeccompProfile{
- Type: "RuntimeDefault",
- },
- }
- command := []string{"etcdctl", "--user", "root:$(ETCD_ROOT_PASSWORD)"}
- command = append(command, args[:]...)
- job := &batchv1.Job{
- ObjectMeta: metav1.ObjectMeta{
- Name: name,
- },
- TypeMeta: metav1.TypeMeta{
- Kind: "Job",
- },
- Spec: batchv1.JobSpec{
- Template: v1.PodTemplateSpec{
- Spec: v1.PodSpec{
- RestartPolicy: "Never",
- Containers: []v1.Container{
- {
- Name: "etcd",
- Image: image,
- Command: command,
- Env: []v1.EnvVar{
- {
- Name: "ETCDCTL_ENDPOINTS",
- Value: fmt.Sprintf("%s:%s", stsName, port),
- },
- {
- Name: "ETCD_ROOT_PASSWORD",
- Value: password,
- },
- },
- SecurityContext: securityContext,
- },
- },
- },
- },
- },
- }
- _, err := c.BatchV1().Jobs(namespace).Create(ctx, job, metav1.CreateOptions{})
- return err
- }
|