Skip to content

Commit

Permalink
chore: add test for forEach component flow
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra committed Oct 12, 2023
1 parent 9111066 commit 9714108
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 26 deletions.
4 changes: 2 additions & 2 deletions fixtures/k8s/kubernetes_bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ spec:
'pass': k8s.is_healthy(r.Object),
'message': k8s.health(r.Object).message,
'error': k8s.health(r.Object).message,
}).marshalJSON()
}).toJSON()
- kind: Pod
ready: true
name: pod-bundle
Expand All @@ -28,4 +28,4 @@ spec:
'pass': k8s.is_healthy(r.Object),
'message': k8s.health(r.Object).message,
'error': k8s.health(r.Object).message,
}).marshalJSON()
}).toJSON()
75 changes: 75 additions & 0 deletions fixtures/topology/component-with-for-each.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
apiVersion: canaries.flanksource.com/v1
kind: Topology
metadata:
name: test-topology-for-each
spec:
schedule: "@every 10m"
components:
- name: GroupA
type: virtual
icon: server
lookup:
http:
- endpoint: https://httpbin.demo.aws.flanksource.com/status/200
name: http-lookup
display:
expr: |
[
{
'name': 'component-a',
'type': 'API',
'properties': [{'name': 'owner', 'text': 'team-a'}, {'name': 'processor'}]
},
{
'name': 'component-b',
'type': 'Frontend',
'properties': [{'name': 'owner', 'text': 'team-b'}, {'name': 'processor'}]
},
{
'name': 'component-c',
'type': 'Database',
'properties': [{'name': 'owner', 'text': 'team-b'}, {'name': 'processor'}]
},
].toJSON()
forEach:
configs:
- name: ${.component.name}
type: Service
components:
- name: Child-A
- name: Child-B
properties:
# Test property lookup merge as components
- name: processor
lookup:
http:
- endpoint: https://httpbin.demo.aws.flanksource.com/status/200
name: processor_lookup
display:
expr: |
[
{
'name': 'component-a',
'properties': [{'name': 'processor', 'text': 'intel'}]
},
{
'name': 'component-b',
'properties': [{'name': 'processor', 'text': 'intel'}]
},
{
'name': 'component-c',
'properties': [{'name': 'processor', 'text': 'amd'}]
},
].toJSON()
# Test property lookup merge as properties
- name: generic
lookup:
http:
- endpoint: https://httpbin.demo.aws.flanksource.com/status/200
name: generic_lookup
display:
expr: |
[
{'name': 'company', 'text': 'Acme'},
{'name': 'location', 'text': 'Mars'},
].toJSON()
9 changes: 4 additions & 5 deletions fixtures/topology/component-with-properties.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#yaml-language-server: $schema=../../generate-schemas/schemas/system.schema.json
apiVersion: canaries.flanksource.com/v1
kind: Topology
metadata:
Expand Down Expand Up @@ -31,7 +30,7 @@ spec:
'type': 'Database',
'properties': [{'name': 'error_percentage', 'min': 0, 'max': 100}, {'name': 'owner'}]
},
].marshalJSON()
].toJSON()
properties:
# Test property lookup merge as components
- name: error_percentage
Expand All @@ -54,7 +53,7 @@ spec:
'name': 'component-c',
'properties': [{'name': 'error_percentage', 'value': 50}]
},
].marshalJSON()
].toJSON()
# Test property lookup merge as components
- name: owner
lookup:
Expand All @@ -76,7 +75,7 @@ spec:
'name': 'component-c',
'properties': [{'name': 'owner', 'text': 'team-b'}]
},
].marshalJSON()
].toJSON()
# Test property lookup merge as properties
- name: generic
lookup:
Expand All @@ -88,4 +87,4 @@ spec:
[
{'name': 'company', 'text': 'Acme'},
{'name': 'location', 'text': 'Mars'},
].marshalJSON()
].toJSON()
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ require (
github.com/fergusstrange/embedded-postgres v1.24.0
github.com/flanksource/commons v1.12.0
github.com/flanksource/duty v1.0.191
github.com/flanksource/gomplate/v3 v3.20.14
github.com/flanksource/gomplate/v3 v3.20.16
github.com/flanksource/is-healthy v0.0.0-20231003215854-76c51e3a3ff7
github.com/flanksource/kommons v0.31.4
github.com/friendsofgo/errors v0.9.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -824,8 +824,8 @@ github.com/flanksource/commons v1.12.0/go.mod h1:zYEhi6E2+diQ+loVcROUHo/Bgv+Tn61
github.com/flanksource/duty v1.0.191 h1:acnvyTeQlfqmtyXxWprNFGK/vBTUlqkYwxEPLtXSPrk=
github.com/flanksource/duty v1.0.191/go.mod h1:ikyl/TcRy6Cc0R5b0wEHT7CecV7gyJvrDGq/4oIZHoc=
github.com/flanksource/gomplate/v3 v3.20.4/go.mod h1:27BNWhzzSjDed1z8YShO6W+z6G9oZXuxfNFGd/iGSdc=
github.com/flanksource/gomplate/v3 v3.20.14 h1:mtxwRbBaAwGr35T2OxCaXFEqovfyMGZ6vFjDJFk625I=
github.com/flanksource/gomplate/v3 v3.20.14/go.mod h1:1N1aptaAo0XUaGsyU5CWiwn9GMRpbIKX1AdsypfmZYo=
github.com/flanksource/gomplate/v3 v3.20.16 h1:Bfn+nbD0iK0iGQcu6alV8Nr7O5+KpeDo8OD9WOu831Q=
github.com/flanksource/gomplate/v3 v3.20.16/go.mod h1:2GgHZ2vWmtDspJMBfUIryOuzJSwc8jU7Kw9fDLr0TMA=
github.com/flanksource/is-healthy v0.0.0-20230705092916-3b4cf510c5fc/go.mod h1:4pQhmF+TnVqJroQKY8wSnSp+T18oLson6YQ2M0qPHfQ=
github.com/flanksource/is-healthy v0.0.0-20231003215854-76c51e3a3ff7 h1:s6jf6P1pRfdvksVFjIXFRfnimvEYUR0/Mmla1EIjiRM=
github.com/flanksource/is-healthy v0.0.0-20231003215854-76c51e3a3ff7/go.mod h1:BH5gh9JyEAuuWVP6Q5y9h43VozS0RfKyjNpM9L4v4hw=
Expand Down
2 changes: 1 addition & 1 deletion hack/generate-schemas/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
cloud.google.com/go/compute v1.23.0 // indirect
cloud.google.com/go/iam v1.1.2 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/flanksource/gomplate/v3 v3.20.14 // indirect
github.com/flanksource/gomplate/v3 v3.20.16 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.1 // indirect
github.com/gosimple/unidecode v1.0.1 // indirect
Expand Down
4 changes: 2 additions & 2 deletions hack/generate-schemas/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -706,8 +706,8 @@ github.com/flanksource/commons v1.12.0/go.mod h1:zYEhi6E2+diQ+loVcROUHo/Bgv+Tn61
github.com/flanksource/duty v1.0.191 h1:acnvyTeQlfqmtyXxWprNFGK/vBTUlqkYwxEPLtXSPrk=
github.com/flanksource/duty v1.0.191/go.mod h1:ikyl/TcRy6Cc0R5b0wEHT7CecV7gyJvrDGq/4oIZHoc=
github.com/flanksource/gomplate/v3 v3.20.4/go.mod h1:27BNWhzzSjDed1z8YShO6W+z6G9oZXuxfNFGd/iGSdc=
github.com/flanksource/gomplate/v3 v3.20.14 h1:mtxwRbBaAwGr35T2OxCaXFEqovfyMGZ6vFjDJFk625I=
github.com/flanksource/gomplate/v3 v3.20.14/go.mod h1:1N1aptaAo0XUaGsyU5CWiwn9GMRpbIKX1AdsypfmZYo=
github.com/flanksource/gomplate/v3 v3.20.16 h1:Bfn+nbD0iK0iGQcu6alV8Nr7O5+KpeDo8OD9WOu831Q=
github.com/flanksource/gomplate/v3 v3.20.16/go.mod h1:2GgHZ2vWmtDspJMBfUIryOuzJSwc8jU7Kw9fDLr0TMA=
github.com/flanksource/is-healthy v0.0.0-20230705092916-3b4cf510c5fc/go.mod h1:4pQhmF+TnVqJroQKY8wSnSp+T18oLson6YQ2M0qPHfQ=
github.com/flanksource/is-healthy v0.0.0-20231003215854-76c51e3a3ff7 h1:s6jf6P1pRfdvksVFjIXFRfnimvEYUR0/Mmla1EIjiRM=
github.com/flanksource/is-healthy v0.0.0-20231003215854-76c51e3a3ff7/go.mod h1:BH5gh9JyEAuuWVP6Q5y9h43VozS0RfKyjNpM9L4v4hw=
Expand Down
13 changes: 0 additions & 13 deletions pkg/topology/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,6 @@ func forEachComponent(ctx *ComponentContext, spec *v1.ComponentSpec, component *
if err := mergeComponentProperties(pkg.Components{component}, props); err != nil {
continue
}

var properties pkg.Properties
if err := json.Unmarshal(props, &properties); err != nil {
return err
}
for _, p := range properties {
found := component.Properties.Find(p.Name)
if found != nil {
found.Merge(p)
continue
}
component.Properties = append(component.Properties, p)
}
}
ctx.SetCurrentComponent(component) // component properties may have changed

Expand Down
45 changes: 45 additions & 0 deletions pkg/topology/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,49 @@ var _ = ginkgo.Describe("Test topology run", ginkgo.Ordered, func() {
Expect(string(componentB.Properties.AsJSON())).To(MatchJSON(`[{"name":"error_percentage","value":10,"min":0,"max":100},{"name":"owner","text":"team-b"},{"name":"company","text":"Acme"},{"name":"location","text":"Mars"}]`))
Expect(string(componentC.Properties.AsJSON())).To(MatchJSON(`[{"name":"error_percentage","value":50,"min":0,"max":100},{"name":"owner","text":"team-b"},{"name":"company","text":"Acme"},{"name":"location","text":"Mars"}]`))
})

ginkgo.It("should create component with forEach functionality", func() {
t, err := yamlFileToTopology("../../fixtures/topology/component-with-for-each.yml")
if err != nil {
ginkgo.Fail("Error converting yaml to v1.Topology:" + err.Error())
}

rootComponent := Run(opts, t)
Expect(len(rootComponent[0].Components)).To(Equal(3))

componentA := rootComponent[0].Components[0]
componentB := rootComponent[0].Components[1]
componentC := rootComponent[0].Components[2]

// Test correct merging of properties
Expect(string(componentA.Properties.AsJSON())).To(MatchJSON(`[{"name":"owner","text":"team-a"},{"name":"processor","text":"intel"},{"name":"company","text":"Acme"},{"name":"location","text":"Mars"}]`))
Expect(string(componentB.Properties.AsJSON())).To(MatchJSON(`[{"name":"owner","text":"team-b"},{"name":"processor","text":"intel"},{"name":"company","text":"Acme"},{"name":"location","text":"Mars"}]`))
Expect(string(componentC.Properties.AsJSON())).To(MatchJSON(`[{"name":"owner","text":"team-b"},{"name":"processor","text":"amd"},{"name":"company","text":"Acme"},{"name":"location","text":"Mars"}]`))

// Each component should have 2 children named Child-A and Child-B
Expect(len(componentA.Components)).To(Equal(2))
Expect(componentA.Components[0].Name).To(Equal("Child-A"))
Expect(componentA.Components[1].Name).To(Equal("Child-B"))

Expect(len(componentB.Components)).To(Equal(2))
Expect(componentB.Components[0].Name).To(Equal("Child-A"))
Expect(componentB.Components[1].Name).To(Equal("Child-B"))

Expect(len(componentC.Components)).To(Equal(2))
Expect(componentC.Components[0].Name).To(Equal("Child-A"))
Expect(componentC.Components[1].Name).To(Equal("Child-B"))

// Each component should have a templated config linked
Expect(len(componentA.Configs)).To(Equal(1))
Expect(componentA.Configs[0].Name).To(Equal(componentA.Name))
Expect(componentA.Configs[0].Type).To(Equal("Service"))

Expect(len(componentB.Configs)).To(Equal(1))
Expect(componentB.Configs[0].Name).To(Equal(componentB.Name))
Expect(componentB.Configs[0].Type).To(Equal("Service"))

Expect(len(componentC.Configs)).To(Equal(1))
Expect(componentC.Configs[0].Name).To(Equal(componentC.Name))
Expect(componentC.Configs[0].Type).To(Equal("Service"))
})
})

0 comments on commit 9714108

Please sign in to comment.