Skip to content

Commit 77b238a

Browse files
committed
Added crudl integration tests
1 parent f45d2bb commit 77b238a

File tree

4 files changed

+101
-105
lines changed

4 files changed

+101
-105
lines changed

integration/client_crudl_test.go

+13-82
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
package main
1+
package integration
22

33
import (
44
"encoding/json"
5-
"flag"
65
"fmt"
7-
"os/exec"
8-
"regexp"
96
"strings"
107
"testing"
118
)
@@ -20,36 +17,6 @@ type MobileClientJson struct {
2017
Spec MobileClientSpec
2118
}
2219

23-
var namespace = flag.String("namespace", "", "Openshift namespace (most often Project) to run our integration tests in")
24-
var name = flag.String("name", "", "Client name to be created")
25-
var executable = flag.String("executable", "", "Executable under test")
26-
27-
type ValidationFunction = func(output []byte, err error) (bool, []string)
28-
29-
func EmptyValidation(output []byte, err error) (bool, []string) {
30-
return true, []string{}
31-
}
32-
33-
func VNoErr(output []byte, err error) (bool, []string) {
34-
return err == nil, []string{fmt.Sprintf("%s", err)}
35-
}
36-
37-
func VIsErr(output []byte, err error) (bool, []string) {
38-
return err != nil, []string{fmt.Sprintf("%s", err)}
39-
}
40-
41-
func VRegex(pattern string) func(output []byte, err error) (bool, []string) {
42-
return func(output []byte, err error) (bool, []string) {
43-
matched, errMatch := regexp.MatchString(pattern, fmt.Sprintf("%s", output))
44-
if errMatch != nil {
45-
return false, []string{fmt.Sprintf("Error in regexp %s when trying to match %s", errMatch, pattern)}
46-
}
47-
if !matched {
48-
return false, []string{fmt.Sprintf("Expected combined output matching %s", pattern)}
49-
}
50-
return true, []string{}
51-
}
52-
}
5320
func VMobileClientJson(name string, clientType string) func(output []byte, err error) (bool, []string) {
5421
return func(output []byte, err error) (bool, []string) {
5522
var parsed MobileClientJson
@@ -67,68 +34,32 @@ func VMobileClientJson(name string, clientType string) func(output []byte, err e
6734
}
6835
}
6936

70-
func All(vs ...ValidationFunction) ValidationFunction {
71-
return func(output []byte, err error) (bool, []string) {
72-
for _, v := range vs {
73-
r, o := v(output, err)
74-
if !r {
75-
return r, o
76-
}
77-
}
78-
return true, []string{}
79-
}
80-
}
81-
82-
type CmdDesc struct {
83-
executable string
84-
Arg []string
85-
Validator ValidationFunction
86-
}
87-
88-
func (c CmdDesc) Add(arg ...string) CmdDesc {
89-
return CmdDesc{c.executable, append(c.Arg, arg...), c.Validator}
90-
}
91-
92-
func (c CmdDesc) Complying(validator ValidationFunction) CmdDesc {
93-
return CmdDesc{c.executable, c.Arg, All(c.Validator, validator)}
94-
}
95-
96-
func (c CmdDesc) Run(t *testing.T) ([]byte, error) {
97-
t.Log(c.Arg)
98-
cmd := exec.Command(c.executable, c.Arg...)
99-
output, err := cmd.CombinedOutput()
100-
t.Log(fmt.Sprintf("%s\n", output))
101-
v, errs := c.Validator(output, err)
102-
if !v {
103-
t.Fatal(errs)
104-
}
105-
return output, err
106-
}
107-
108-
func ValidatedCmd(executable string, arg ...string) CmdDesc {
109-
return CmdDesc{executable, arg, EmptyValidation}
110-
}
111-
112-
func TestPositive(t *testing.T) {
37+
func TestClientJson(t *testing.T) {
11338

11439
clientTypes := []string{
11540
"cordova",
11641
"iOS",
11742
"android",
11843
}
11944

45+
name := fmt.Sprintf("%s-mobile-crud-test-entity", *prefix)
12046
m := ValidatedCmd(*executable, fmt.Sprintf("--namespace=%s", *namespace), "-o=json")
47+
oc := ValidatedCmd("oc", fmt.Sprintf("--namespace=%s", *namespace), "-o=json")
12148
for _, clientType := range clientTypes {
12249
t.Run(clientType, func(t *testing.T) {
123-
expectedId := strings.ToLower(fmt.Sprintf("%s-%s", *name, clientType))
124-
notExists := All(VIsErr, VRegex(".*Error: failed to get.*"))
125-
exists := All(VNoErr, VMobileClientJson(*name, clientType))
50+
expectedId := strings.ToLower(fmt.Sprintf("%s-%s", name, clientType))
51+
52+
notExists := All(VIsErr, VRegex(fmt.Sprintf(".*\"%s\" not found.*", expectedId)))
53+
exists := All(VNoErr, VMobileClientJson(name, clientType))
54+
12655
m.Add("get", "client", expectedId).Complying(notExists).Run(t)
127-
m.Add("create", "client", *name, clientType).Complying(exists).Run(t)
56+
oc.Add("get", "mobileclient", expectedId).Complying(notExists).Run(t)
57+
m.Add("create", "client", name, clientType).Complying(exists).Run(t)
12858
m.Add("get", "client", expectedId).Complying(exists).Run(t)
59+
oc.Add("get", "mobileclient", expectedId).Complying(exists).Run(t)
12960
m.Add("delete", "client", expectedId).Complying(VNoErr).Run(t)
13061
m.Add("get", "client", expectedId).Complying(notExists).Run(t)
131-
62+
oc.Add("get", "mobileclient", expectedId).Complying(notExists).Run(t)
13263
})
13364
}
13465
}

integration/client_get_services_test.go

+1-23
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
11
package integration
22

33
import (
4-
"flag"
5-
"fmt"
6-
"os"
74
"os/exec"
8-
"path"
95
"regexp"
106
"testing"
117
)
128

13-
var namespace = flag.String("namespace", "myproject", "Openshift namespace (most often Project) to run our integration tests in")
14-
var executable = flag.String("executable", "mobile", "Executable under test")
15-
var update = flag.Bool("update", false, "update golden files")
16-
179
const testPath = "getServicesTestData/"
1810

1911
func TestGetServices(t *testing.T) {
@@ -36,17 +28,12 @@ func TestGetServices(t *testing.T) {
3628

3729
for _, test := range tests {
3830
t.Run(test.name, func(t *testing.T) {
39-
dir, err := os.Getwd()
40-
if err != nil {
41-
t.Fatal(err)
42-
}
43-
cmd := exec.Command(path.Join(dir, *executable), test.args...)
31+
cmd := exec.Command(*executable, test.args...)
4432

4533
output, err := cmd.CombinedOutput()
4634
if err != nil {
4735
t.Fatal(err)
4836
}
49-
5037
if *update {
5138
WriteSnapshot(t, testPath+test.fixture, output)
5239
}
@@ -74,12 +61,3 @@ func cleanStringByRegex(input string, regexes []*regexp.Regexp) string {
7461
}
7562
return input
7663
}
77-
78-
func TestMain(m *testing.M) {
79-
err := os.Chdir("..")
80-
if err != nil {
81-
fmt.Printf("could not change dir: %v", err)
82-
os.Exit(1)
83-
}
84-
os.Exit(m.Run())
85-
}

integration/flags.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package integration
2+
3+
import (
4+
"flag"
5+
)
6+
7+
var namespace = flag.String("namespace", "", "Openshift namespace (most often Project) to run our integration tests in")
8+
var prefix = flag.String("prefix", "", "Client name to be created")
9+
var executable = flag.String("executable", "", "Executable under test")
10+
var update = flag.Bool("update", false, "update golden files")

integration/validatedCommandUtils.go

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package integration
2+
3+
import (
4+
"fmt"
5+
"os/exec"
6+
"regexp"
7+
"testing"
8+
)
9+
10+
type ValidationFunction = func(output []byte, err error) (bool, []string)
11+
12+
func EmptyValidation(output []byte, err error) (bool, []string) {
13+
return true, []string{}
14+
}
15+
16+
func VNoErr(output []byte, err error) (bool, []string) {
17+
return err == nil, []string{fmt.Sprintf("%s", err)}
18+
}
19+
20+
func VIsErr(output []byte, err error) (bool, []string) {
21+
return err != nil, []string{fmt.Sprintf("%s", err)}
22+
}
23+
24+
func VRegex(pattern string) func(output []byte, err error) (bool, []string) {
25+
return func(output []byte, err error) (bool, []string) {
26+
matched, errMatch := regexp.MatchString(pattern, fmt.Sprintf("%s", output))
27+
if errMatch != nil {
28+
return false, []string{fmt.Sprintf("Error in regexp %s when trying to match %s", errMatch, pattern)}
29+
}
30+
if !matched {
31+
return false, []string{fmt.Sprintf("Expected combined output matching %s", pattern)}
32+
}
33+
return true, []string{}
34+
}
35+
}
36+
37+
func All(vs ...ValidationFunction) ValidationFunction {
38+
return func(output []byte, err error) (bool, []string) {
39+
for _, v := range vs {
40+
r, o := v(output, err)
41+
if !r {
42+
return r, o
43+
}
44+
}
45+
return true, []string{}
46+
}
47+
}
48+
49+
type CmdDesc struct {
50+
executable string
51+
Arg []string
52+
Validator ValidationFunction
53+
}
54+
55+
func (c CmdDesc) Add(arg ...string) CmdDesc {
56+
return CmdDesc{c.executable, append(c.Arg, arg...), c.Validator}
57+
}
58+
59+
func (c CmdDesc) Complying(validator ValidationFunction) CmdDesc {
60+
return CmdDesc{c.executable, c.Arg, All(c.Validator, validator)}
61+
}
62+
63+
func (c CmdDesc) Run(t *testing.T) ([]byte, error) {
64+
t.Log(c.Arg)
65+
cmd := exec.Command(c.executable, c.Arg...)
66+
output, err := cmd.CombinedOutput()
67+
t.Log(fmt.Sprintf("%s\n", output))
68+
v, errs := c.Validator(output, err)
69+
if !v {
70+
t.Fatal(errs)
71+
}
72+
return output, err
73+
}
74+
75+
func ValidatedCmd(executable string, arg ...string) CmdDesc {
76+
return CmdDesc{executable, arg, EmptyValidation}
77+
}

0 commit comments

Comments
 (0)