Skip to content

Commit c73fe4a

Browse files
committed
feat(scenario): simplify generation of non complete setups
Allow for single SubGenerator generation for e.g. only create a Deployment to simulate the Che Workspace Deployment.
1 parent 7551094 commit c73fe4a

4 files changed

Lines changed: 79 additions & 21 deletions

File tree

test/cmd/test-scenario/generator/generators.go

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,34 +50,49 @@ type Modifier func(service Entry, object runtime.Object)
5050
// Generate runs and prints the full test scenario generation to sysout.
5151
func Generate(out io.Writer, services []Entry, modifiers ...Modifier) {
5252
sub := []SubGenerator{Deployment, DeploymentConfig, Service, DestinationRule, VirtualService}
53-
modify := func(service Entry, object runtime.Object) {
54-
for _, modifier := range modifiers {
55-
modifier(service, object)
56-
}
57-
}
58-
printObj := func(object runtime.Object) {
59-
b, err := yaml.Marshal(object)
60-
if err != nil {
61-
_, _ = io.WriteString(out, "Marshal error"+err.Error()+"\n")
62-
}
63-
_, _ = out.Write(b)
64-
_, _ = io.WriteString(out, "---\n")
65-
}
6653
for _, service := range services {
6754
func(service Entry) {
6855
for _, subGenerator := range sub {
69-
object := subGenerator(service)
70-
if object == nil {
71-
continue
72-
}
73-
modify(service, object)
74-
printObj(object)
56+
Do(out, service, subGenerator, modifiers...)
7557
}
7658
}(service)
7759
}
7860
gw := Gateway()
79-
modify(Entry{Name: "gateway"}, gw)
80-
printObj(gw)
61+
modify(Entry{Name: "gateway"}, gw, modifiers...)
62+
printObj(out, gw)
63+
}
64+
65+
// Do executes the SubGenerator and applies the Modifiers and prints the object to the io.Writer
66+
func Do(out io.Writer, service Entry, generator SubGenerator, modifiers ...Modifier) {
67+
object := gen(service, generator)
68+
if object == nil {
69+
return
70+
}
71+
modify(service, object, modifiers...)
72+
printObj(out, object)
73+
}
74+
75+
func gen(service Entry, generator SubGenerator) runtime.Object {
76+
object := generator(service)
77+
if object == nil {
78+
return nil
79+
}
80+
return object
81+
}
82+
83+
func modify(service Entry, object runtime.Object, modifiers ...Modifier) {
84+
for _, modifier := range modifiers {
85+
modifier(service, object)
86+
}
87+
}
88+
89+
func printObj(out io.Writer, object runtime.Object) {
90+
b, err := yaml.Marshal(object)
91+
if err != nil {
92+
_, _ = io.WriteString(out, "Marshal error"+err.Error()+"\n")
93+
}
94+
_, _ = out.Write(b)
95+
_, _ = io.WriteString(out, "---\n")
8196
}
8297

8398
// DeploymentConfig basic SubGenerator for the kind DeploymentConfig.

test/cmd/test-scenario/generator/modifiers.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package generator
22

33
import (
4+
"reflect"
5+
46
osappsv1 "github.com/openshift/api/apps/v1"
57
istiov1alpha3 "istio.io/api/networking/v1alpha3"
68
istionetwork "istio.io/client-go/pkg/apis/networking/v1alpha3"
79
appsv1 "k8s.io/api/apps/v1"
810
corev1 "k8s.io/api/core/v1"
11+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
912
"k8s.io/apimachinery/pkg/runtime"
1013
)
1114

@@ -150,3 +153,19 @@ func WithVersion(version string) Modifier {
150153
}
151154
}
152155
}
156+
157+
func WithAnnotations(annotations map[string]string) Modifier {
158+
return func(service Entry, object runtime.Object) {
159+
k8sObjValue := reflect.ValueOf(object).Elem()
160+
objectField := k8sObjValue.FieldByName("ObjectMeta")
161+
if objMeta, ok := objectField.Interface().(metav1.ObjectMeta); ok {
162+
if objMeta.Annotations == nil {
163+
objMeta.Annotations = map[string]string{}
164+
}
165+
for k, v := range annotations {
166+
objMeta.Annotations[k] = v
167+
}
168+
objectField.Set(reflect.ValueOf(objMeta))
169+
}
170+
}
171+
}

test/cmd/test-scenario/generator/scenarios.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,29 @@ func TestScenario2ThreeServicesInSequenceDeploymentConfig(out io.Writer) {
6060
)
6161
}
6262

63+
// TestScenarioMutationHookChe is a basic test setup with a
64+
// few services calling each other in a chain. Similar to the original bookinfo example setup
65+
// and a single Deployment simulating a Che Deployment which should trigger the WebHook.
66+
// Using Deployment.
67+
func TestScenarioMutationHookChe(out io.Writer) {
68+
productpage := Entry{"productpage", "Deployment", Namespace}
69+
reviews := Entry{"reviews", "Deployment", Namespace}
70+
ratings := Entry{"ratings", "Deployment", Namespace}
71+
che := Entry{"che-workspace", "Deployment", Namespace}
72+
73+
Generate(
74+
out,
75+
[]Entry{productpage, reviews, ratings},
76+
ForService(productpage, WithVersion("v1")),
77+
ForService(reviews, WithVersion("v1")),
78+
ForService(ratings, WithVersion("v1")),
79+
ForService(productpage, Call(HTTP(), reviews), ConnectToGateway(GatewayHost)),
80+
ForService(reviews, Call(HTTP(), ratings)),
81+
GatewayOnHost(GatewayHost),
82+
)
83+
Do(out, che, Deployment, WithAnnotations(map[string]string{"ike.target": "reviews-v1"}))
84+
}
85+
6386
// DemoScenario is a simple setup for demo purposes.
6487
func DemoScenario(out io.Writer) {
6588
productpage := Entry{"productpage", "Deployment", Namespace}

test/cmd/test-scenario/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func main() {
2828
"scenario-1": generator.TestScenario1HTTPThreeServicesInSequence,
2929
"scenario-1.1": generator.TestScenario1GRPCThreeServicesInSequence,
3030
"scenario-2": generator.TestScenario2ThreeServicesInSequenceDeploymentConfig,
31+
"scenario-che": generator.TestScenarioMutationHookChe,
3132
"demo": generator.DemoScenario,
3233
}
3334
scenario := os.Args[1]

0 commit comments

Comments
 (0)