Skip to content

Commit

Permalink
More test framework
Browse files Browse the repository at this point in the history
  • Loading branch information
marccampbell committed May 11, 2019
1 parent 775afe1 commit cab2064
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 19 deletions.
File renamed without changes.
22 changes: 13 additions & 9 deletions integration/pkg/runner/cluster.go
Expand Up @@ -80,7 +80,7 @@ func (c Cluster) delete() error {
return ctx.Delete()
}

func (c Cluster) apply(manifests []byte) error {
func (c Cluster) apply(manifests []byte, showStdOut bool) error {
cli, err := client.NewEnvClient()
if err != nil {
return err
Expand Down Expand Up @@ -152,19 +152,23 @@ func (c Cluster) apply(manifests []byte) error {
return err
}

if exitCode != 0 {
data, err := cli.ContainerLogs(ctx, resp.ID, types.ContainerLogsOptions{ShowStdout: true, ShowStderr: true})
if err != nil {
return err
}
data, err := cli.ContainerLogs(ctx, resp.ID, types.ContainerLogsOptions{ShowStdout: true, ShowStderr: true})
if err != nil {
return err
}

stdOut := new(bytes.Buffer)
stdErr := new(bytes.Buffer)
stdOut := new(bytes.Buffer)
stdErr := new(bytes.Buffer)

stdcopy.StdCopy(stdOut, stdErr, data)
stdcopy.StdCopy(stdOut, stdErr, data)

if exitCode != 0 {
return fmt.Errorf("unexpected exit code running kubectl: %d\nstderr:%s\b\bstdout:%s", exitCode, stdErr, stdOut)
}

if showStdOut {
fmt.Printf("%s\n", stdOut)
}

return nil
}
38 changes: 35 additions & 3 deletions integration/pkg/runner/crd.go
Expand Up @@ -3,16 +3,48 @@ package runner
import (
"fmt"
"io/ioutil"
"path/filepath"
"strings"
)

func getApplyableCRD(managerImageName string) ([]byte, error) {
fmt.Printf(managerImageName)
crd, err := ioutil.ReadFile("manifests/crd.yaml")
func getApplyableOperator(managerImageName string) ([]byte, error) {
manager, err := getApplyableManager(managerImageName)
if err != nil {
return nil, err
}
crds, err := getApplyableCrds()
if err != nil {
return nil, err
}

return append(manager, crds...), nil
}

func getApplyableManager(managerImageName string) ([]byte, error) {
crd, err := ioutil.ReadFile("manifests/manager.yaml")
if err != nil {
return nil, err
}

updatedCrd := strings.Replace(string(crd), "IMAGE_URL", managerImageName, -1)
return []byte(updatedCrd), nil
}

func getApplyableCrds() ([]byte, error) {
crds, err := ioutil.ReadDir("../config/crds")
if err != nil {
return nil, err
}

manifests := "---"
for _, crd := range crds {
manifest, err := ioutil.ReadFile(filepath.Join("../config/crds/", crd.Name()))
if err != nil {
return nil, err
}

manifests = fmt.Sprintf("%s\n%s\n---", manifests, manifest)
}

return []byte(manifests), nil
}
29 changes: 25 additions & 4 deletions integration/pkg/runner/runner.go
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"time"

"github.com/spf13/viper"
)
Expand Down Expand Up @@ -59,25 +60,45 @@ func (r *Runner) RunSync() error {
return err
}

if err := cluster.apply(databaseManifests); err != nil {
if err := cluster.apply(databaseManifests, false); err != nil {
return err
}
}

fmt.Printf("(%s) -----> Applying SchemaHero Operator\n", test.Cluster.Name)
crd, err := getApplyableCRD(r.Viper.GetString("manager-image-name"))
operator, err := getApplyableOperator(r.Viper.GetString("manager-image-name"))
if err != nil {
return nil
}
if err := cluster.apply(crd); err != nil {
if err := cluster.apply(operator, false); err != nil {
return err
}

fmt.Printf("(%s) -----> Applying database connection\n", test.Cluster.Name)
// Give the cluster 2 seconds to register the CRDs. This shouldn't be necessary
// And for production code this would be better handled in almost any other way
// But this test flow is a very specific pattern and this is working for now.
time.Sleep(time.Second * 2)

fmt.Printf("(%s) -----> Applying database connections\n", test.Cluster.Name)
for _, connection := range test.Connections {
fmt.Printf("(%s) -----> ... %s\n", test.Cluster.Name, connection)
connectionManifests, err := ioutil.ReadFile(filepath.Join(root, connection))
if err != nil {
return err
}

if err := cluster.apply(connectionManifests, false); err != nil {
return err
}
}

fmt.Printf("(%s) -----> Setting up test\n", test.Cluster.Name)
// TODO

fmt.Printf("(%s) -----> Running test(s)\n", test.Cluster.Name)
for _, testStep := range test.Steps {
fmt.Printf("(%s) -----> ... %s\n", test.Cluster.Name, testStep.Name)
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions integration/pkg/runner/types.go
Expand Up @@ -10,12 +10,22 @@ type Test struct {
Cluster TestCluster `yaml:"cluster"`
Databases []string `yaml:"databases"`
Connections []string `yaml:"connections"`
Steps []*TestStep `yaml:"steps"`
}

type TestCluster struct {
Name string `json:"name"`
}

type TestStep struct {
Name string `yaml:"name"`
Table *TestStepTable `yaml:"table"`
}

type TestStepTable struct {
Source string `yaml:"source"`
}

func unmarshalTestFile(filename string) (*Test, error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions integration/tests/postgres-create/test.yaml
Expand Up @@ -9,9 +9,10 @@ connections:

setup: []

test:
- table:
- source: users-table.yaml
steps:
- name: create users table
table:
source: users-table.yaml



Expand Down

0 comments on commit cab2064

Please sign in to comment.