import pytest, re

from kat.utils import ShellCommand
from abstract_tests import AmbassadorTest, ServiceType, HTTP

access_log_entry_regex = re.compile('^MY_REQUEST 200 .*')


class EnvoyLogTest(AmbassadorTest):
    target: ServiceType
    log_path: str

    def init(self):
        self.target = HTTP()
        self.log_path = '/tmp/ambassador/ambassador.log'
        self.log_format = 'MY_REQUEST %RESPONSE_CODE% \"%REQ(:AUTHORITY)%\" \"%REQ(USER-AGENT)%\" \"%REQ(X-REQUEST-ID)%\" \"%UPSTREAM_HOST%\"'

    def config(self):
        yield self, self.format("""
---
apiVersion: ambassador/v1
kind: Module
name: ambassador
ambassador_id: {self.ambassador_id}
config:
  envoy_log_path: {self.log_path}
  envoy_log_format: {self.log_format}
""")

    def check(self):
        cmd = ShellCommand("kubectl", "exec", self.path.k8s, "cat", self.log_path)
        if not cmd.check("check envoy access log"):
            pytest.exit("envoy access log does not exist")

        for line in cmd.stdout.splitlines():
            assert access_log_entry_regex.match(line), f"{line} does not match {access_log_entry_regex}"

import pytest, re

from kat.utils import ShellCommand
from abstract_tests import AmbassadorTest, ServiceType, HTTP

access_log_entry_regex = re.compile('^ACCESS \\[.*?\\] \\\"GET \\/ambassador')


class EnvoyLogPathTest(AmbassadorTest):
    target: ServiceType
    log_path: str

    def init(self):
        self.target = HTTP()
        self.log_path = '/tmp/ambassador/ambassador.log'

    def config(self):
        yield self, self.format("""
---
apiVersion: ambassador/v1
kind: Module
name: ambassador
ambassador_id: {self.ambassador_id}
config:
  envoy_log_path: {self.log_path}
""")

    def check(self):
        cmd = ShellCommand("kubectl", "exec", self.path.k8s, "cat", self.log_path)
        if not cmd.check("check envoy access log"):
            pytest.exit("envoy access log does not exist")

        for line in cmd.stdout.splitlines():
            assert access_log_entry_regex.match(line)

--- 
+++ 
@@ -3,16 +3,17 @@
 from kat.utils import ShellCommand
 from abstract_tests import AmbassadorTest, ServiceType, HTTP
 
-access_log_entry_regex = re.compile('^ACCESS \\[.*?\\] \\\"GET \\/ambassador')
+access_log_entry_regex = re.compile('^MY_REQUEST 200 .*')
 
 
-class EnvoyLogPathTest(AmbassadorTest):
+class EnvoyLogTest(AmbassadorTest):
     target: ServiceType
     log_path: str
 
     def init(self):
         self.target = HTTP()
         self.log_path = '/tmp/ambassador/ambassador.log'
+        self.log_format = 'MY_REQUEST %RESPONSE_CODE% \"%REQ(:AUTHORITY)%\" \"%REQ(USER-AGENT)%\" \"%REQ(X-REQUEST-ID)%\" \"%UPSTREAM_HOST%\"'
 
     def config(self):
         yield self, self.format("""
@@ -23,6 +24,7 @@
 ambassador_id: {self.ambassador_id}
 config:
   envoy_log_path: {self.log_path}
+  envoy_log_format: {self.log_format}
 """)
 
     def check(self):
@@ -31,4 +33,4 @@
             pytest.exit("envoy access log does not exist")
 
         for line in cmd.stdout.splitlines():
-            assert access_log_entry_regex.match(line)
+            assert access_log_entry_regex.match(line), f"{line} does not match {access_log_entry_regex}"
Test for Envoy logs format

Signed-off-by: Alvaro Saurin <5b2d0c210c4a9fd6aeaf2eaedf8273be993c90c2@datawire.io>
