Skip to content

Commit

Permalink
chore: make common json test function
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra authored and moshloop committed Feb 28, 2023
1 parent 1679a80 commit c4f4861
Showing 1 changed file with 22 additions and 86 deletions.
108 changes: 22 additions & 86 deletions topology_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,131 +28,67 @@ func prettytree(mytree []*models.Component) {
}
}

var _ = ginkgo.Describe("Topology behavior", ginkgo.Ordered, func() {
func testTopologyJSON(opts TopologyOptions, path string) {
tree, err := QueryTopology(opts)
Expect(err).ToNot(HaveOccurred())

ginkgo.It("Should create root tree", func() {
tree, err := QueryTopology(TopologyOptions{})
Expect(err).ToNot(HaveOccurred())
treeJSON, err := json.Marshal(tree)
Expect(err).ToNot(HaveOccurred())

expected := readTestFile(path)
Expect(expected).Should(MatchJSON(string(treeJSON)))
}

treeJSON, err := json.Marshal(tree)
Expect(err).ToNot(HaveOccurred())
var _ = ginkgo.Describe("Topology behavior", ginkgo.Ordered, func() {

expected := readTestFile("fixtures/expectations/topology_root_tree.json")
Expect(expected).Should(MatchJSON(string(treeJSON)))
ginkgo.It("Should create root tree", func() {
testTopologyJSON(TopologyOptions{}, "fixtures/expectations/topology_root_tree.json")
})

ginkgo.It("Should create child tree", func() {
tree, err := QueryTopology(TopologyOptions{ID: dummy.NodeA.ID.String()})
Expect(err).ToNot(HaveOccurred())

treeJSON, err := json.Marshal(tree)
Expect(err).ToNot(HaveOccurred())

expected := readTestFile("fixtures/expectations/topology_child_tree.json")
Expect(expected).Should(MatchJSON(string(treeJSON)))
testTopologyJSON(TopologyOptions{ID: dummy.NodeA.ID.String()}, "fixtures/expectations/topology_child_tree.json")
})

ginkgo.It("Should test depth 1 root tree", func() {
tree, err := QueryTopology(TopologyOptions{Depth: 1})
Expect(err).ToNot(HaveOccurred())

treeJSON, err := json.Marshal(tree)
Expect(err).ToNot(HaveOccurred())

expected := readTestFile("fixtures/expectations/topology_depth_1_root_tree.json")
Expect(expected).Should(MatchJSON(string(treeJSON)))
testTopologyJSON(TopologyOptions{Depth: 1}, "fixtures/expectations/topology_depth_1_root_tree.json")
})

ginkgo.It("Should test depth 2 root tree", func() {
tree, err := QueryTopology(TopologyOptions{Depth: 2})
Expect(err).ToNot(HaveOccurred())

treeJSON, err := json.Marshal(tree)
Expect(err).ToNot(HaveOccurred())

expected := readTestFile("fixtures/expectations/topology_depth_2_root_tree.json")
Expect(expected).Should(MatchJSON(string(treeJSON)))
testTopologyJSON(TopologyOptions{Depth: 2}, "fixtures/expectations/topology_depth_2_root_tree.json")
})

ginkgo.It("Should test depth 1 tree child tree", func() {
tree, err := QueryTopology(TopologyOptions{ID: dummy.LogisticsAPI.ID.String(), Depth: 1})
Expect(err).ToNot(HaveOccurred())

treeJSON, err := json.Marshal(tree)
Expect(err).ToNot(HaveOccurred())

expected := readTestFile("fixtures/expectations/topology_depth_1_child_tree.json")
Expect(expected).Should(MatchJSON(string(treeJSON)))
testTopologyJSON(TopologyOptions{ID: dummy.LogisticsAPI.ID.String(), Depth: 1}, "fixtures/expectations/topology_depth_1_child_tree.json")
})

ginkgo.It("Should test depth 2 tree child tree", func() {
// TODO: Current query with a component_id defined does not return the children if
// they are linked via parent_id of that component
ginkgo.Skip("SQL Query needs to be fixed for this to work")
tree, err := QueryTopology(TopologyOptions{ID: dummy.LogisticsAPI.ID.String(), Depth: 2})
Expect(err).ToNot(HaveOccurred())

treeJSON, err := json.Marshal(tree)
Expect(err).ToNot(HaveOccurred())

expected := readTestFile("fixtures/expectations/topology_depth_2_child_tree.json")
Expect(expected).Should(MatchJSON(string(treeJSON)))
testTopologyJSON(TopologyOptions{ID: dummy.LogisticsAPI.ID.String(), Depth: 1}, "fixtures/expectations/topology_depth_2_child_tree.json")
})

ginkgo.It("Should test tree with labels", func() {
tree, err := QueryTopology(TopologyOptions{Labels: map[string]string{"telemetry": "enabled"}})
Expect(err).ToNot(HaveOccurred())

treeJSON, err := json.Marshal(tree)
Expect(err).ToNot(HaveOccurred())

expected := readTestFile("fixtures/expectations/topology_tree_with_label_filter.json")
Expect(expected).Should(MatchJSON(string(treeJSON)))
testTopologyJSON(TopologyOptions{Labels: map[string]string{"telemetry": "enabled"}}, "fixtures/expectations/topology_tree_with_label_filter.json")
})

ginkgo.It("Should test tree with owner", func() {
tree, err := QueryTopology(TopologyOptions{Owner: "logistics-team"})
Expect(err).ToNot(HaveOccurred())

treeJSON, err := json.Marshal(tree)
Expect(err).ToNot(HaveOccurred())

expected := readTestFile("fixtures/expectations/topology_tree_with_owner_filter.json")
Expect(expected).Should(MatchJSON(string(treeJSON)))
testTopologyJSON(TopologyOptions{Owner: "logistics-team"}, "fixtures/expectations/topology_tree_with_owner_filter.json")
})

ginkgo.It("Should test tree with type filter", func() {
tree, err := QueryTopology(TopologyOptions{Types: []string{"Entity"}})
Expect(err).ToNot(HaveOccurred())

treeJSON, err := json.Marshal(tree)
Expect(err).ToNot(HaveOccurred())

expected := readTestFile("fixtures/expectations/topology_tree_with_type_filter.json")
Expect(expected).Should(MatchJSON(string(treeJSON)))
testTopologyJSON(TopologyOptions{Types: []string{"Entity"}}, "fixtures/expectations/topology_tree_with_type_filter.json")
})

ginkgo.It("Should test tree with negative type filter", func() {
// TODO: Change implementation of matchItems to fix this
ginkgo.Skip("Current implementation does not filter negative types correctly")
tree, err := QueryTopology(TopologyOptions{Types: []string{"!KubernetesCluster"}})
Expect(err).ToNot(HaveOccurred())

treeJSON, err := json.Marshal(tree)
Expect(err).ToNot(HaveOccurred())

expected := readTestFile("fixtures/expectations/topology_tree_with_negative_type_filter.json")
Expect(expected).Should(MatchJSON(string(treeJSON)))
testTopologyJSON(TopologyOptions{Types: []string{"!KubernetesCluster"}}, "fixtures/expectations/topology_tree_with_negative_type_filter.json")
})

ginkgo.It("Should test tree with status filter", func() {
tree, err := QueryTopology(TopologyOptions{Status: []string{string(models.ComponentStatusWarning)}})
Expect(err).ToNot(HaveOccurred())

treeJSON, err := json.Marshal(tree)
Expect(err).ToNot(HaveOccurred())

expected := readTestFile("fixtures/expectations/topology_tree_with_status_filter.json")
Expect(expected).Should(MatchJSON(string(treeJSON)))
testTopologyJSON(TopologyOptions{Status: []string{string(models.ComponentStatusWarning)}}, "fixtures/expectations/topology_tree_with_status_filter.json")
})
})

0 comments on commit c4f4861

Please sign in to comment.