hs-test: fill configuration files at runtime

Treat certain configuration files, which contain runtime-dependent
information, as templates. The information is filled at runtime and the
files are copied into containers.
This allows to avoid hard-coding IP addresses into configuration files.

Type: test
Signed-off-by: Maros Ondrejicka <mondreji@cisco.com>
Change-Id: I1dae8f15f4f76c0bf1779d7c68b7f3859bf5a861
diff --git a/extras/hs-test/container.go b/extras/hs-test/container.go
index 5def278..40dc082 100644
--- a/extras/hs-test/container.go
+++ b/extras/hs-test/container.go
@@ -5,6 +5,7 @@
 	"os"
 	"os/exec"
 	"strings"
+	"text/template"
 
 	"github.com/edwarnicke/exechelper"
 )
@@ -301,3 +302,19 @@
 	c.saveLogs()
 	return exechelper.Run("docker stop " + c.name + " -t 0")
 }
+
+func (c *Container) createConfig(targetConfigName string, templateName string, values any) {
+	template := template.Must(template.ParseFiles(templateName))
+
+	f, err := os.CreateTemp("/tmp/hs-test/", "hst-config")
+	c.Suite().assertNil(err)
+	defer os.Remove(f.Name())
+
+	err = template.Execute(f, values)
+	c.Suite().assertNil(err)
+
+	err = f.Close()
+	c.Suite().assertNil(err)
+
+	c.copy(f.Name(), targetConfigName)
+}
diff --git a/extras/hs-test/proxy_test.go b/extras/hs-test/proxy_test.go
index f121866..ce0e7ad 100644
--- a/extras/hs-test/proxy_test.go
+++ b/extras/hs-test/proxy_test.go
@@ -49,7 +49,7 @@
 	return nil
 }
 
-func configureVppProxy(s *NsSuite) error {
+func configureVppProxy(s *NsSuite) {
 	serverVeth := s.netInterfaces[serverInterface]
 	clientVeth := s.netInterfaces[clientInterface]
 
@@ -60,24 +60,35 @@
 		serverVeth.Peer().IP4AddressString(),
 	)
 	s.log("proxy configured...", output)
-	return nil
 }
 
 func (s *NsSuite) TestVppProxyHttpTcp() {
-	err := configureVppProxy(s)
-	s.assertNil(err)
-	err = testProxyHttpTcp(s)
+	configureVppProxy(s)
+	err := testProxyHttpTcp(s)
 	s.assertNil(err)
 }
 
-func configureEnvoyProxy(s *NsSuite) error {
+func configureEnvoyProxy(s *NsSuite) {
 	envoyContainer := s.getContainerByName("envoy")
-	return envoyContainer.run()
+	envoyContainer.create()
+
+	serverVeth := s.netInterfaces[serverInterface]
+	address := struct {
+		Server string
+	}{
+		Server: serverVeth.Peer().IP4AddressString(),
+	}
+	envoyContainer.createConfig(
+		"/etc/envoy/envoy.yaml",
+		"resources/envoy/proxy.yaml",
+		address,
+	)
+
+	envoyContainer.start()
 }
 
 func (s *NsSuite) TestEnvoyProxyHttpTcp() {
-	err := configureEnvoyProxy(s)
-	s.assertNil(err)
-	err = testProxyHttpTcp(s)
+	configureEnvoyProxy(s)
+	err := testProxyHttpTcp(s)
 	s.assertNil(err)
 }
diff --git a/extras/hs-test/resources/envoy/proxy.yaml b/extras/hs-test/resources/envoy/proxy.yaml
index 2093b56..77da80d 100644
--- a/extras/hs-test/resources/envoy/proxy.yaml
+++ b/extras/hs-test/resources/envoy/proxy.yaml
@@ -44,7 +44,7 @@
             address:
               socket_address:
                 # following address will be generated by Addresser during test run
-                address: 10.10.2.1
+                address: {{.Server}}
                 port_value: 666
 bootstrap_extensions:
   - name: envoy.extensions.vcl.vcl_socket_interface
diff --git a/extras/hs-test/resources/nginx/nginx_proxy_mirroring.conf b/extras/hs-test/resources/nginx/nginx_proxy_mirroring.conf
index 03af8b7..bf15d1c 100644
--- a/extras/hs-test/resources/nginx/nginx_proxy_mirroring.conf
+++ b/extras/hs-test/resources/nginx/nginx_proxy_mirroring.conf
@@ -29,21 +29,21 @@
   gzip on;
 
   upstream bk {
-    server 10.10.2.1:8091;
+    server {{.Server}}:8091;
     keepalive 30000;
   }
   upstream bk1 {
-    server 10.10.2.1:8092;
+    server {{.Server}}:8092;
     keepalive 30000;
   }
   upstream bk2 {
-    server 10.10.2.1:8093;
+    server {{.Server}}:8093;
     keepalive 30000;
   }
 
   server {
     listen 80;
-    server_name 10.10.1.2;
+    server_name {{.Proxy}};
 
     server_tokens off;
 
diff --git a/extras/hs-test/suite_nginx_test.go b/extras/hs-test/suite_nginx_test.go
index d950cf8..e66eca7 100644
--- a/extras/hs-test/suite_nginx_test.go
+++ b/extras/hs-test/suite_nginx_test.go
@@ -45,7 +45,19 @@
 
 	nginxContainer := s.getTransientContainerByName(nginxProxyContainerName)
 	nginxContainer.create()
-	nginxContainer.copy("./resources/nginx/nginx_proxy_mirroring.conf", "/nginx.conf")
+
+	values := struct {
+		Proxy  string
+		Server string
+	}{
+		Proxy:  clientInterface.Peer().IP4AddressString(),
+		Server: serverInterface.IP4AddressString(),
+	}
+	nginxContainer.createConfig(
+		"/nginx.conf",
+		"./resources/nginx/nginx_proxy_mirroring.conf",
+		values,
+	)
 	nginxContainer.start()
 
 	proxyVpp.waitForApp("-app", 5)
diff --git a/extras/hs-test/topo-containers/ns.yaml b/extras/hs-test/topo-containers/ns.yaml
index fddf2d8..2b1902b 100644
--- a/extras/hs-test/topo-containers/ns.yaml
+++ b/extras/hs-test/topo-containers/ns.yaml
@@ -12,8 +12,6 @@
         is-default-work-dir: true
   - name: "envoy"
     volumes:
-      - host-dir: "$HST_DIR/resources/envoy/proxy.yaml"
-        container-dir: "/etc/envoy/envoy.yaml"
       - <<: *shared-vol
         container-dir: "/tmp/vpp-envoy"
         is-default-work-dir: true