hs-test: fill configuration files at runtime 67/38367/3
authorMaros Ondrejicka <mondreji@cisco.com>
Tue, 28 Feb 2023 11:49:43 +0000 (12:49 +0100)
committerFlorin Coras <florin.coras@gmail.com>
Tue, 28 Feb 2023 22:13:21 +0000 (22:13 +0000)
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

extras/hs-test/container.go
extras/hs-test/proxy_test.go
extras/hs-test/resources/envoy/proxy.yaml
extras/hs-test/resources/nginx/nginx_proxy_mirroring.conf
extras/hs-test/suite_nginx_test.go
extras/hs-test/topo-containers/ns.yaml

index 5def278..40dc082 100644 (file)
@@ -5,6 +5,7 @@ import (
        "os"
        "os/exec"
        "strings"
+       "text/template"
 
        "github.com/edwarnicke/exechelper"
 )
@@ -301,3 +302,19 @@ func (c *Container) stop() error {
        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)
+}
index f121866..ce0e7ad 100644 (file)
@@ -49,7 +49,7 @@ func testProxyHttpTcp(s *NsSuite) error {
        return nil
 }
 
-func configureVppProxy(s *NsSuite) error {
+func configureVppProxy(s *NsSuite) {
        serverVeth := s.netInterfaces[serverInterface]
        clientVeth := s.netInterfaces[clientInterface]
 
@@ -60,24 +60,35 @@ func configureVppProxy(s *NsSuite) error {
                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)
 }
index 2093b56..77da80d 100644 (file)
@@ -44,7 +44,7 @@ static_resources:
             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
index 03af8b7..bf15d1c 100644 (file)
@@ -29,21 +29,21 @@ http {
   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;
 
index d950cf8..e66eca7 100644 (file)
@@ -45,7 +45,19 @@ func (s *NginxSuite) SetupTest() {
 
        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)
index fddf2d8..2b1902b 100644 (file)
@@ -12,8 +12,6 @@ containers:
         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