|
| 1 | +// +build ignore |
| 2 | + |
| 3 | +// end-to-end tests. |
| 4 | +// A kubernetes cluster must be reachable by kubectl and katafygio. |
| 5 | +package main |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "fmt" |
| 10 | + "os" |
| 11 | + "os/exec" |
| 12 | + "path" |
| 13 | + "strings" |
| 14 | + "testing" |
| 15 | + "time" |
| 16 | +) |
| 17 | + |
| 18 | +var ( |
| 19 | + checkTimeout = 30 * time.Second |
| 20 | + checkInterval = 1 * time.Second |
| 21 | + dumpPath = "/tmp/kf-e2e-test" |
| 22 | +) |
| 23 | + |
| 24 | +type dumpStep struct { |
| 25 | + // a kubectl command to create a dumpable object |
| 26 | + cmd string |
| 27 | + // the counter-command, to purge the created object |
| 28 | + teardown string |
| 29 | + // the file that this command will eventually trigger katafygio to generate or delete |
| 30 | + relpath string |
| 31 | + // wether the file should be created or deleted |
| 32 | + shouldExist bool |
| 33 | +} |
| 34 | + |
| 35 | +var testsTable = []dumpStep{ |
| 36 | + {"kubectl create ns kf-e2e-test-1", "kubectl delete ns kf-e2e-test-1", "namespace-kf-e2e-test-1.yaml", true}, |
| 37 | + {"kubectl run kf-e2e-test-2 --image=gcr.io/google_containers/pause-amd64:3.0", "kubectl delete deploy kf-e2e-test-2", "default/deployment-kf-e2e-test-2.yaml", true}, |
| 38 | + {"kubectl expose deployment kf-e2e-test-2 --port=80 --target-port=8000 --name=kf-e2e-test-3", "kubectl delete svc kf-e2e-test-3", "default/service-kf-e2e-test-3.yaml", true}, |
| 39 | + {"kubectl delete service kf-e2e-test-3", "", "default/service-kf-e2e-test-3.yaml", false}, |
| 40 | + {"kubectl delete namespace kf-e2e-test-1", "", "namespace-kf-e2e-test-1.yaml", false}, |
| 41 | + {"kubectl create configmap kf-e2e-test-4 --from-literal=key1=config1", "kubectl delete configmap kf-e2e-test-4", "default/configmap-kf-e2e-test-4.yaml", true}, |
| 42 | +} |
| 43 | + |
| 44 | +func TestE2E(t *testing.T) { |
| 45 | + for _, tt := range testsTable { |
| 46 | + t.Run(tt.cmd, func(t *testing.T) { |
| 47 | + err := tt.fileExists() |
| 48 | + if err != nil { |
| 49 | + t.Errorf("%s test failed (%v)", tt.cmd, err) |
| 50 | + } |
| 51 | + }) |
| 52 | + |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func TestMain(m *testing.M) { |
| 57 | + deleteResources() |
| 58 | + _ = exec.Command("rm", "-rf", dumpPath) |
| 59 | + |
| 60 | + ctx, cancel := context.WithCancel(context.Background()) |
| 61 | + |
| 62 | + go func() { |
| 63 | + cmd := exec.CommandContext(ctx, "katafygio", "-e", dumpPath) |
| 64 | + _ = cmd.Run() |
| 65 | + }() |
| 66 | + |
| 67 | + ret := m.Run() |
| 68 | + cancel() |
| 69 | + deleteResources() |
| 70 | + os.Exit(ret) |
| 71 | +} |
| 72 | + |
| 73 | +func deleteResources() { |
| 74 | + for _, d := range testsTable { |
| 75 | + if len(d.teardown) == 0 { |
| 76 | + continue |
| 77 | + } |
| 78 | + |
| 79 | + command := strings.Split(d.teardown, " ") |
| 80 | + _ = exec.Command(command[0], command[1:]...).Run() |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func (d *dumpStep) fileExists() error { |
| 85 | + checkTick := time.NewTicker(checkInterval) |
| 86 | + timeoutTick := time.NewTicker(checkTimeout) |
| 87 | + defer checkTick.Stop() |
| 88 | + defer timeoutTick.Stop() |
| 89 | + |
| 90 | + command := strings.Split(d.cmd, " ") |
| 91 | + cmd := exec.Command(command[0], command[1:]...) |
| 92 | + |
| 93 | + out, err := cmd.CombinedOutput() |
| 94 | + if err != nil { |
| 95 | + return fmt.Errorf("%s failed with code %v: %s", d.cmd, err, out) |
| 96 | + } |
| 97 | + |
| 98 | + for { |
| 99 | + select { |
| 100 | + case <-checkTick.C: |
| 101 | + _, err := os.Stat(path.Join(dumpPath, d.relpath)) |
| 102 | + |
| 103 | + if err == nil && d.shouldExist { |
| 104 | + return nil |
| 105 | + } |
| 106 | + |
| 107 | + if os.IsNotExist(err) && !d.shouldExist { |
| 108 | + return nil |
| 109 | + } |
| 110 | + |
| 111 | + case <-timeoutTick.C: |
| 112 | + return fmt.Errorf("timeout waiting for %s", d.relpath) |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments