Skip to content
This repository was archived by the owner on Jun 29, 2022. It is now read-only.

Commit 56f55b1

Browse files
committed
contour envoy: Verify if hostPort does not exist
Check if the converted config does not contain the hostPort. This commit also adds test to check if a certain JSONPath exists. Signed-off-by: Suraj Deshmukh <suraj@kinvolk.io>
1 parent dc2ceb8 commit 56f55b1

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

pkg/components/contour/component_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,18 @@ func TestConversion(t *testing.T) {
130130
})
131131
}
132132
}
133+
134+
func TestEnvoyHostPort(t *testing.T) {
135+
t.Parallel()
136+
137+
componentCfg := `component "contour" {}`
138+
expectedManifestName := k8sutil.ObjectMetadata{Version: "apps/v1", Kind: "DaemonSet", Name: "envoy"}
139+
jsonPath := "{.spec.template.spec.containers[1].ports[0].hostPort}"
140+
errExpected := "hostPort is not found"
141+
142+
component := NewConfig()
143+
m := testutil.RenderManifests(t, component, Name, componentCfg)
144+
gotConfig := testutil.ConfigFromMap(t, m, expectedManifestName)
145+
146+
testutil.JSONPathExists(t, gotConfig, jsonPath, errExpected)
147+
}

pkg/components/internal/testutil/jsonpath.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,16 @@ func MatchJSONPathInt64Value(t *testing.T, yamlConfig string, jsonPath string, e
102102
t.Fatalf("Expected: %d, Got: %d", expected, got)
103103
}
104104
}
105+
106+
// JSONPathExists checks if the given YAML config has an object at the given JSON path, also provide
107+
// what error to expect.
108+
func JSONPathExists(t *testing.T, yamlConfig string, jsonPath string, errExp string) {
109+
_, err := jsonPathValue(yamlConfig, jsonPath)
110+
if err != nil && errExp == "" { //nolint:gocritic
111+
t.Fatalf("Error not expected and failed with: %v", err)
112+
} else if err != nil && !strings.Contains(err.Error(), errExp) {
113+
t.Fatalf("Extracting JSON path value, expected error: %v to contain: %q", err, errExp)
114+
} else if err == nil && errExp != "" {
115+
t.Fatalf("Expected error %q but got none", errExp)
116+
}
117+
}

0 commit comments

Comments
 (0)