from kat.harness import Query
from abstract_tests import AmbassadorTest, ServiceType, HTTP


class LuaTest(AmbassadorTest):
    target: ServiceType

    def init(self):
        self.target = HTTP()

    def manifests(self) -> str:
        return super().manifests() + self.format('''
---
apiVersion: getambassador.io/v1
kind: Module
metadata:
  name: ambassador
spec:
  ambassador_id: {self.ambassador_id}
  config:
    lua_scripts: |
      function envoy_on_response(response_handle)
        response_handle: headers():add("Lua-Scripts-Enabled", "Processed")
      end
---
apiVersion: getambassador.io/v1
kind: Mapping
metadata:
  name: lua-target-mapping
spec:
  ambassador_id: {self.ambassador_id}
  prefix: /target/
  service: {self.target.path.fqdn}
''')

    def queries(self):
        yield Query(self.url("target/"))

    def check(self):
        for r in self.results:
            assert r.headers.get('Lua-Scripts-Enabled', None) == ['Processed']

from kat.harness import Query
from abstract_tests import AmbassadorTest, ServiceType, HTTP


class LuaTest(AmbassadorTest):
    target: ServiceType

    def init(self):
        self.target = HTTP()
        self.env = ["LUA_SCRIPTS_ENABLED=Processed"]

    def manifests(self) -> str:
        return super().manifests() + self.format('''
---
apiVersion: getambassador.io/v1
kind: Module
metadata:
  name: ambassador
spec:
  ambassador_id: {self.ambassador_id}
  config:
    lua_scripts: |
      function envoy_on_response(response_handle)
        response_handle: headers():add("Lua-Scripts-Enabled", "${LUA_SCRIPTS_ENABLED}")
      end
---
apiVersion: getambassador.io/v1
kind: Mapping
metadata:
  name: lua-target-mapping
spec:
  ambassador_id: {self.ambassador_id}
  prefix: /target/
  service: {self.target.path.fqdn}
''')

    def queries(self):
        yield Query(self.url("target/"))

    def check(self):
        for r in self.results:
            assert r.headers.get('Lua-Scripts-Enabled', None) == ['Processed']

Update LUA test to perform interpolation
Update LUA test to perform interpolation

  from kat.harness import Query
  from abstract_tests import AmbassadorTest, ServiceType, HTTP
  
  
  class LuaTest(AmbassadorTest):
      target: ServiceType
  
      def init(self):
          self.target = HTTP()
+         self.env = ["LUA_SCRIPTS_ENABLED=Processed"]
  
      def manifests(self) -> str:
          return super().manifests() + self.format('''
  ---
  apiVersion: getambassador.io/v1
  kind: Module
  metadata:
    name: ambassador
  spec:
    ambassador_id: {self.ambassador_id}
    config:
      lua_scripts: |
        function envoy_on_response(response_handle)
-         response_handle: headers():add("Lua-Scripts-Enabled", "Processed")
+         response_handle: headers():add("Lua-Scripts-Enabled", "${LUA_SCRIPTS_ENABLED}")
        end
  ---
  apiVersion: getambassador.io/v1
  kind: Mapping
  metadata:
    name: lua-target-mapping
  spec:
    ambassador_id: {self.ambassador_id}
    prefix: /target/
    service: {self.target.path.fqdn}
  ''')
  
      def queries(self):
          yield Query(self.url("target/"))
  
      def check(self):
          for r in self.results:
              assert r.headers.get('Lua-Scripts-Enabled', None) == ['Processed']
  
Update LUA test to perform interpolation
## Code Before:
from kat.harness import Query
from abstract_tests import AmbassadorTest, ServiceType, HTTP


class LuaTest(AmbassadorTest):
    target: ServiceType

    def init(self):
        self.target = HTTP()

    def manifests(self) -> str:
        return super().manifests() + self.format('''
---
apiVersion: getambassador.io/v1
kind: Module
metadata:
  name: ambassador
spec:
  ambassador_id: {self.ambassador_id}
  config:
    lua_scripts: |
      function envoy_on_response(response_handle)
        response_handle: headers():add("Lua-Scripts-Enabled", "Processed")
      end
---
apiVersion: getambassador.io/v1
kind: Mapping
metadata:
  name: lua-target-mapping
spec:
  ambassador_id: {self.ambassador_id}
  prefix: /target/
  service: {self.target.path.fqdn}
''')

    def queries(self):
        yield Query(self.url("target/"))

    def check(self):
        for r in self.results:
            assert r.headers.get('Lua-Scripts-Enabled', None) == ['Processed']

## Instruction:
Update LUA test to perform interpolation
## Code After:
from kat.harness import Query
from abstract_tests import AmbassadorTest, ServiceType, HTTP


class LuaTest(AmbassadorTest):
    target: ServiceType

    def init(self):
        self.target = HTTP()
        self.env = ["LUA_SCRIPTS_ENABLED=Processed"]

    def manifests(self) -> str:
        return super().manifests() + self.format('''
---
apiVersion: getambassador.io/v1
kind: Module
metadata:
  name: ambassador
spec:
  ambassador_id: {self.ambassador_id}
  config:
    lua_scripts: |
      function envoy_on_response(response_handle)
        response_handle: headers():add("Lua-Scripts-Enabled", "${LUA_SCRIPTS_ENABLED}")
      end
---
apiVersion: getambassador.io/v1
kind: Mapping
metadata:
  name: lua-target-mapping
spec:
  ambassador_id: {self.ambassador_id}
  prefix: /target/
  service: {self.target.path.fqdn}
''')

    def queries(self):
        yield Query(self.url("target/"))

    def check(self):
        for r in self.results:
            assert r.headers.get('Lua-Scripts-Enabled', None) == ['Processed']
