|
| 1 | +package e2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "golang.org/x/sync/errgroup" |
| 12 | + |
| 13 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 14 | + "sigs.k8s.io/controller-runtime/pkg/client/config" |
| 15 | + |
| 16 | + "github.com/app-sre/dba-operator/pkg/dbadmin" |
| 17 | +) |
| 18 | + |
| 19 | +var () |
| 20 | + |
| 21 | +type finalizerFn func() error |
| 22 | + |
| 23 | +type TestFramework struct { |
| 24 | + client.Client |
| 25 | + admin dbadmin.DbAdmin |
| 26 | +} |
| 27 | + |
| 28 | +func New(operatorImage string) (*TestFramework, error) { |
| 29 | + c, err := client.New(config.GetConfigOrDie(), client.Options{}) |
| 30 | + if err != nil { |
| 31 | + return nil, nil |
| 32 | + } |
| 33 | + |
| 34 | + return &TestFramework{ |
| 35 | + Client: c, |
| 36 | + }, nil |
| 37 | +} |
| 38 | + |
| 39 | +// Create a new test context with the test's name and the current time as its ID. |
| 40 | +// The ID will be used for creating objects, such as creating a namespace for the test. |
| 41 | +func (tf *TestFramework) NewTestCtx(t *testing.T) TestCtx { |
| 42 | + prefix := strings.TrimPrefix( |
| 43 | + strings.ReplaceAll( |
| 44 | + strings.ToLower(t.Name()), |
| 45 | + "/", |
| 46 | + "-", |
| 47 | + ), |
| 48 | + "test", |
| 49 | + ) |
| 50 | + |
| 51 | + id := prefix + "-" + strconv.FormatInt(time.Now().Unix(), 36) |
| 52 | + return TestCtx{ |
| 53 | + ID: id, |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +type TestCtx struct { |
| 58 | + ID string |
| 59 | + cleanupFns []finalizerFn |
| 60 | + ctx context.Context |
| 61 | +} |
| 62 | + |
| 63 | +func (tctx *TestCtx) Cleanup(t *testing.T) { |
| 64 | + var eg errgroup.Group |
| 65 | + |
| 66 | + for i := len(tctx.cleanupFns) - 1; i >= 0; i-- { |
| 67 | + eg.Go(tctx.cleanupFns[i]) |
| 68 | + } |
| 69 | + |
| 70 | + if err := eg.Wait(); err != nil { |
| 71 | + t.Fatal(err) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func (tctx *TestCtx) AddFinalizerFn(fn finalizerFn) { |
| 76 | + tctx.cleanupFns = append(tctx.cleanupFns, fn) |
| 77 | +} |
| 78 | + |
| 79 | +func (tctx *TestCtx) CreateNamespace(t *testing.T, c client.Client) string { |
| 80 | + name := tctx.ID |
| 81 | + if err := CreateNamespace(tctx.ctx, c, name); err != nil { |
| 82 | + t.Fatal(err) |
| 83 | + } |
| 84 | + |
| 85 | + namespaceCleanupFn := func() error { |
| 86 | + return DeleteNamespace(tctx.ctx, c, name) |
| 87 | + } |
| 88 | + |
| 89 | + tctx.AddFinalizerFn(namespaceCleanupFn) |
| 90 | + |
| 91 | + return name |
| 92 | +} |
| 93 | + |
| 94 | +func (tctx *TestCtx) CreateDbaOperator(c client.Client, operatorImage string) error { |
| 95 | + deployment, err := MakeDeployment("../../deploy/dba-operator.yaml") |
| 96 | + if err != nil { |
| 97 | + return err |
| 98 | + } |
| 99 | + |
| 100 | + if operatorImage != "" { |
| 101 | + repoTag := strings.Split(operatorImage, ":") |
| 102 | + if len(repoTag) != 2 { |
| 103 | + return fmt.Errorf("invalid operator image '%s'", operatorImage) |
| 104 | + } |
| 105 | + |
| 106 | + deployment.Spec.Template.Spec.Containers[0].Image = operatorImage |
| 107 | + } |
| 108 | + |
| 109 | + err = CreateDeployment(tctx.ctx, c, tctx.ID, deployment) |
| 110 | + if err != nil { |
| 111 | + return err |
| 112 | + } |
| 113 | + |
| 114 | + return nil |
| 115 | +} |
| 116 | + |
| 117 | +func (tctx *TestCtx) CreateDB(c client.Client, image string) error { |
| 118 | + deployment, err := MakeDeployment("../../deploy/debug.yaml") |
| 119 | + if err != nil { |
| 120 | + return err |
| 121 | + } |
| 122 | + |
| 123 | + if image != "" { |
| 124 | + repoTag := strings.Split(image, ":") |
| 125 | + if len(repoTag) != 2 { |
| 126 | + return fmt.Errorf("invalid operator image '%s'", image) |
| 127 | + } |
| 128 | + |
| 129 | + deployment.Spec.Template.Spec.Containers[0].Image = image |
| 130 | + } |
| 131 | + |
| 132 | + err = CreateDeployment(tctx.ctx, c, tctx.ID, deployment) |
| 133 | + if err != nil { |
| 134 | + return err |
| 135 | + } |
| 136 | + |
| 137 | + return nil |
| 138 | +} |
0 commit comments