-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: added e2e test with kind and testscript
- Loading branch information
Showing
6 changed files
with
284 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
// Copyright 2024 The Atlas Operator Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package e2e_test | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/rogpeppe/go-internal/testscript" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
const nsController = "atlas-operator-system" | ||
|
||
func TestOperator(t *testing.T) { | ||
img := os.Getenv("IMG") | ||
if img == "" { | ||
img = "ariga/atlas-operator:e2e" | ||
} | ||
dir, err := getProjectDir() | ||
require.NoError(t, err) | ||
// Creating kubeconfig for the kind cluster | ||
kubeconfig := filepath.Join(t.TempDir(), "kubeconfig") | ||
err = pipeFile(kubeconfig, func(f io.Writer) error { | ||
cmd := exec.Command("kind", "get", "kubeconfig") | ||
cmd.Stdout, cmd.Stderr = f, os.Stderr | ||
return cmd.Run() | ||
}) | ||
require.NoError(t, err) | ||
// Kind run the command with kubeconfig set to kind cluster | ||
kind := func(name string, args ...string) (string, error) { | ||
cmd := exec.Command(name, args...) | ||
cmd.Dir = dir | ||
cmd.Env = append(os.Environ(), "KUBECONFIG="+kubeconfig) | ||
command := strings.Join(cmd.Args, " ") | ||
fmt.Fprintf(os.Stdout, "running: %s\n", command) | ||
output, err := cmd.CombinedOutput() | ||
if err != nil { | ||
return "", fmt.Errorf("%s failed with error: (%v) %s", command, err, string(output)) | ||
} | ||
return string(output), nil | ||
} | ||
// Deploying the controller-manager | ||
_, err = kind("make", "deploy", "install", fmt.Sprintf("IMG=%s", img)) | ||
require.NoError(t, err) | ||
// Running the test script | ||
testscript.Run(t, testscript.Params{ | ||
Dir: filepath.Join("testdata", "atlas-schema"), | ||
Setup: func(e *testscript.Env) (err error) { | ||
e.Setenv("CONTROLLER", "deployment/atlas-operator-controller-manager") | ||
// Ensure the test in running in the right kube context | ||
e.Setenv("KUBECONFIG", kubeconfig) | ||
// Creating a namespace for the test | ||
ns := fmt.Sprintf("e2e-%s-%d", strings.ToLower(t.Name()), time.Now().UnixMicro()) | ||
e.Setenv("NAMESPACE", ns) | ||
_, err = kind("kubectl", "create", "namespace", ns) | ||
if err != nil { | ||
return err | ||
} | ||
e.Defer(func() { | ||
// Deleting the namespace after the test | ||
kind("kubectl", "delete", "namespace", ns) | ||
}) | ||
return nil | ||
}, | ||
Cmds: map[string]func(ts *testscript.TestScript, neg bool, args []string){ | ||
// expand reads a file and expands it with the environment variables | ||
"expand": func(ts *testscript.TestScript, neg bool, args []string) { | ||
if neg { | ||
ts.Fatalf("unsupported: ! stdin") | ||
} | ||
if len(args) != 1 { | ||
ts.Fatalf("usage: expand filename") | ||
} | ||
content := os.Expand(ts.ReadFile(args[0]), ts.Getenv) | ||
_, err := ts.Stdout().Write([]byte(content)) | ||
ts.Check(err) | ||
}, | ||
// kubectl runs kubectl with the namespace set to the test namespace | ||
"kubectl": func(ts *testscript.TestScript, neg bool, args []string) { | ||
err := ts.Exec("kubectl", append([]string{ | ||
"--namespace", ts.Getenv("NAMESPACE"), | ||
}, args...)...) | ||
if !neg { | ||
ts.Check(err) | ||
} else if err == nil { | ||
ts.Fatalf("unexpected success") | ||
} | ||
}, | ||
// atlas runs the atlas binary in the controller-manager pod | ||
"atlas": func(ts *testscript.TestScript, neg bool, args []string) { | ||
err := ts.Exec("kubectl", append([]string{ | ||
"--namespace", nsController, | ||
"exec", ts.Getenv("CONTROLLER"), | ||
"--", "/atlas", | ||
}, args...)...) | ||
if !neg { | ||
ts.Check(err) | ||
} else if err == nil { | ||
ts.Fatalf("unexpected success") | ||
} | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func pipeFile(p string, fn func(w io.Writer) error) error { | ||
fs, err := os.OpenFile(p, os.O_CREATE|os.O_WRONLY, 0644) | ||
if err != nil { | ||
return err | ||
} | ||
defer fs.Close() | ||
return fn(fs) | ||
} | ||
|
||
func getProjectDir() (string, error) { | ||
wd, err := os.Getwd() | ||
if err != nil { | ||
return wd, err | ||
} | ||
wd = strings.Replace(wd, "/test/e2e", "", -1) | ||
return wd, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
kubectl apply -f database.yaml | ||
expand secret.yaml | ||
stdin stdout | ||
kubectl apply -f - | ||
|
||
# Wait for the DB ready before creating the schema | ||
kubectl wait --for=condition=ready --timeout=60s -l app=postgres pods | ||
kubectl apply -f schema.yaml | ||
kubectl wait --for=condition=ready --timeout=120s --all atlasschemas | ||
|
||
# Inspect the schema to ensure it's correct | ||
atlas schema inspect -u postgres://root:pass@postgres.${NAMESPACE}:5432/postgres?sslmode=disable | ||
cmp stdout schema.hcl | ||
-- schema.hcl -- | ||
table "users2" { | ||
schema = schema.public | ||
column "id" { | ||
null = false | ||
type = integer | ||
} | ||
primary_key { | ||
columns = [column.id] | ||
} | ||
} | ||
schema "public" { | ||
comment = "standard public schema" | ||
} | ||
-- schema.yaml -- | ||
apiVersion: db.atlasgo.io/v1alpha1 | ||
kind: AtlasSchema | ||
metadata: | ||
name: atlasschema-postgres | ||
spec: | ||
urlFrom: | ||
secretKeyRef: | ||
name: postgres-credentials | ||
key: url | ||
schema: | ||
sql: | | ||
create table users2 ( | ||
id int not null, | ||
primary key (id) | ||
); | ||
-- secret.yaml -- | ||
apiVersion: v1 | ||
kind: Secret | ||
metadata: | ||
name: postgres-credentials | ||
type: Opaque | ||
stringData: | ||
url: "postgres://root:pass@postgres.${NAMESPACE}:5432/postgres?sslmode=disable" | ||
-- database.yaml -- | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
name: postgres | ||
spec: | ||
selector: | ||
app: postgres | ||
ports: | ||
- name: postgres | ||
port: 5432 | ||
targetPort: postgres | ||
type: ClusterIP | ||
--- | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: postgres | ||
spec: | ||
selector: | ||
matchLabels: | ||
app: postgres | ||
replicas: 1 | ||
template: | ||
metadata: | ||
labels: | ||
app: postgres | ||
spec: | ||
securityContext: | ||
runAsNonRoot: true | ||
runAsUser: 999 | ||
containers: | ||
- name: postgres | ||
image: postgres:15.4 | ||
securityContext: | ||
allowPrivilegeEscalation: false | ||
capabilities: | ||
drop: | ||
- all | ||
env: | ||
- name: POSTGRES_PASSWORD | ||
value: pass | ||
- name: POSTGRES_USER | ||
value: root | ||
ports: | ||
- containerPort: 5432 | ||
name: postgres | ||
readinessProbe: | ||
initialDelaySeconds: 5 | ||
periodSeconds: 2 | ||
timeoutSeconds: 1 | ||
exec: | ||
command: [ "pg_isready", "-U", "postgres" ] |