Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions ats_caching/atscachingpolicy.yaml
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
apiVersion: k8s.trafficserver.apache.com/v1
apiVersion: k8s.trafficserver.apache.com/v1alpha1
kind: ATSCachingPolicy
metadata:
name: my-app-caching
namespace: trafficserver-test
namespace: caching-ats-new
spec:
rules:
- name: home-endpoint
- name: static-assets
primarySpecifier:
type: url_regex
pattern: ".*/app1"
pattern: "^/app2"
action: cache
ttl: "12s"
- name: static-assets
ttl: "23s"
- name: cache-endpoint
primarySpecifier:
type: url_regex
pattern: "^/app2"
pattern: ".*/cache-test"
action: cache
ttl: "20s"
ttl: "12s"
3 changes: 1 addition & 2 deletions ats_caching/crd-atscachingpolicy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ spec:
shortNames:
- atscp
versions:
- name: v1
- name: v1alpha1
served: true
storage: true
schema:
Expand Down Expand Up @@ -65,4 +65,3 @@ spec:
ttl:
type: string
description: Cache time to live (e.g., "10s", "1h")

69 changes: 69 additions & 0 deletions tests/data/caching-app/cache-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@

apiVersion: v1
kind: Namespace
metadata:
name: cache-test-ns
---

apiVersion: apps/v1
kind: Deployment
metadata:
name: cache-test
namespace: cache-test-ns
spec:
replicas: 1
selector:
matchLabels:
app: cache-test
template:
metadata:
labels:
app: cache-test
spec:
containers:
- name: cache-test
image: localhost:5000/caching-app
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is defined yet?

imagePullPolicy: Always
env:
- name: AUTHOR
value: cache-test
ports:
- containerPort: 8080
name: http
protocol: TCP

---
apiVersion: v1
kind: Service
metadata:
name: cachingsvc
namespace: cache-test-ns
spec:
ports:
- port: 8080
name: "cachingsvchttp"
protocol: TCP
targetPort: 8080

selector:
app: cache-test

---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: app-ingress
namespace: cache-test-ns
spec:
#ingressClassName: ats
rules:
- host: test.media.com
http:
paths:
- path: /cache-test
pathType: Exact
backend:
service:
name: cachingsvc
port:
number: 8080
46 changes: 24 additions & 22 deletions tests/suite/test_ingress.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,22 @@ def misc_command(command):

def setup_module(module):
misc_command('openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout tls.key -out tls.crt -subj "/CN=atssvc/O=atssvc"')

kubectl_create('namespace trafficserver-test')
kubectl_create('secret tls tls-secret --key tls.key --cert tls.crt -n trafficserver-test --dry-run=client -o yaml | kubectl apply -f -')
kubectl_apply('data/setup/configmaps/')
kubectl_apply('data/setup/traffic-server/')
kubectl_apply('data/setup/apps/')
kubectl_apply('data/setup/ingresses/')

#Applying here as it takes some time for controller to get notification from kubernetes.
kubectl_apply('data/setup/ats_caching/ats-cachingpolicy-role.yaml')
kubectl_apply('data/setup/ats_caching/ats-cachingpolicy-binding.yaml')
kubectl_apply('data/setup/ats_caching/crd-atscachingpolicy.yaml')
kubectl_apply('data/setup/ats_caching/atscachingpolicy.yaml')
kubectl_apply('data/caching-app/')

time.sleep(90)
kubectl_apply('../ats_caching/ats-cachingpolicy-role.yaml')
kubectl_apply('../ats_caching/ats-cachingpolicy-binding.yaml')
time.sleep(5)
misc_command('kubectl get all -A')
misc_command('kubectl get pod -A -o wide')
misc_command('kubectl logs $(kubectl get pod -n trafficserver-test-2 -o name | head -1) -n trafficserver-test-2')
Expand All @@ -62,9 +68,15 @@ def setup_module(module):
# misc_command('kubectl exec $(kubectl get pod -n trafficserver-test -o name) -n trafficserver-test -- curl -v $(kubectl get service/appsvc2 -n trafficserver-test-2 -o jsonpath={.spec.clusterIP}):8080/app1')

def teardown_module(module):

kubectl_delete('crd atscachingpolicies.k8s.trafficserver.apache.com')
kubectl_delete('-f data/setup/ats_caching/ats-cachingpolicy-role.yaml')
kubectl_delete('-f data/setup/ats_caching/ats-cachingpolicy-binding.yaml')
kubectl_delete('namespace trafficserver-test-3')
kubectl_delete('namespace trafficserver-test-2')
kubectl_delete('namespace trafficserver-test')
kubectl_delete('namespace cache-test-ns')


def get_expected_response_app1():
resp = """<!DOCTYPE html>
Expand Down Expand Up @@ -168,11 +180,7 @@ def test_basic_routing_edge_app2_https(self, minikubeip):
assert ' '.join(resp.text.split()) == get_expected_response_app2()

def test_cache_app1(self, minikubeip):
kubectl_apply('../ats_caching/crd-atscachingpolicy.yaml')
kubectl_apply('../ats_caching/atscachingpolicy.yaml')
time.sleep(15)

command = f'curl -i -v -H "Host: test.media.com" http://{minikubeip}:30080/app1'
command = f'curl -i -v -H "Host: test.media.com" http://{minikubeip}:30080/cache-test'
response_1 = subprocess.run(command, shell=True, capture_output=True, text=True)
response1 = response_1.stdout.strip()
response1_list = response1.split('\n')
Expand All @@ -185,7 +193,6 @@ def test_cache_app1(self, minikubeip):
response_2 = subprocess.run(command, shell=True, capture_output=True, text=True)
response2 = response_2.stdout.strip()
response2_list = response2.split('\n')
kubectl_delete('crd atscachingpolicies.k8s.trafficserver.apache.com')
for resp in response2_list:
if resp.__contains__("Age"):
age2 = resp
Expand All @@ -194,11 +201,10 @@ def test_cache_app1(self, minikubeip):
assert mod_time1 == mod_time2 and age1 != age2, "Expected Date provided by both responses to be same and the Age mentioned in second response to be more than 0"

def test_cache_app1_beyond_ttl(self, minikubeip):
kubectl_apply('../ats_caching/crd-atscachingpolicy.yaml')
kubectl_apply('../ats_caching/atscachingpolicy.yaml')
time.sleep(15)
# waiting for cache from previous test case to expire
time.sleep(13)

command = f'curl -i -v -H "Host: test.media.com" http://{minikubeip}:30080/app1'
command = f'curl -i -v -H "Host: test.media.com" http://{minikubeip}:30080/cache-test'
response_1 = subprocess.run(command, shell=True, capture_output=True, text=True)
response1 = response_1.stdout.strip()
response1_list = response1.split('\n')
Expand All @@ -215,16 +221,12 @@ def test_cache_app1_beyond_ttl(self, minikubeip):
if resp.__contains__("Age"):
age2 = resp
if resp.__contains__("Date"):
mod_time2 = resp
kubectl_delete('crd atscachingpolicies.k8s.trafficserver.apache.com')
mod_time2 = resp
expected_age = "Age: 0"
assert mod_time1 != mod_time2 and age1 == age2 and age2 == expected_age, "Expected Date provided by both responses to be different and the Age mentioned in both responses to be 0"
assert mod_time1 != mod_time2 and age1 == age2 and age2 == expected_age, "Expected Date provided by both responses should be different and the Age mentioned in both responses should be 0"

def test_cache_app2(self, minikubeip):
kubectl_apply('../ats_caching/crd-atscachingpolicy.yaml')
kubectl_apply('../ats_caching/atscachingpolicy.yaml')
time.sleep(15)

# here caching will not work because url will not match with information in caching crd
command = f'curl -i -v -H "Host: test.edge.com" http://{minikubeip}:30080/app2'
response_1 = subprocess.run(command, shell=True, capture_output=True, text=True)
response1 = response_1.stdout.strip()
Expand All @@ -243,7 +245,6 @@ def test_cache_app2(self, minikubeip):
age2 = resp
if resp.__contains__("Date"):
mod_time2 = resp
kubectl_delete('crd atscachingpolicies.k8s.trafficserver.apache.com')
assert mod_time1 != mod_time2 and age1 == age2, "Expected Date provided by both the responses to be different and the Age to be 0 in both the responses"


Expand Down Expand Up @@ -285,4 +286,5 @@ def test_snippet_edge_app2(self, minikubeip):
assert resp.status_code == 301,\
f"Expected: 301 response code for test_snippet_edge_app2"
assert resp.headers['Location'] == 'https://test.edge.com/app2'



2 changes: 1 addition & 1 deletion watcher/handlerCache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func newTestHandler(t *testing.T) (*AtsCacheHandler, string) {
func newCachingPolicy(name string, rules []interface{}) *unstructured.Unstructured {
u := &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "example.com/v1",
"apiVersion": "example.com/v1alpha1",
"kind": "ATSCachingPolicy",
"metadata": map[string]interface{}{
"name": name,
Expand Down
2 changes: 1 addition & 1 deletion watcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (w *Watcher) inNamespacesWatchFor(h EventHandler, c cache.Getter,
}

func (w *Watcher) WatchAtsCachingPolicy(path string) error {
gvr := schema.GroupVersionResource{Group: "k8s.trafficserver.apache.com", Version: "v1", Resource: "atscachingpolicies"}
gvr := schema.GroupVersionResource{Group: "k8s.trafficserver.apache.com", Version: "v1alpha1", Resource: "atscachingpolicies"}
dynamicFactory := dynamicinformer.NewFilteredDynamicSharedInformerFactory(w.DynamicClient, w.ResyncPeriod, metav1.NamespaceAll, nil)
informer := dynamicFactory.ForResource(gvr).Informer()
cachehandler := NewAtsCacheHandler("atscaching", w.Ep, path)
Expand Down
18 changes: 8 additions & 10 deletions watcher/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import (
framework "k8s.io/client-go/tools/cache/testing"
)

// --- Existing endpoint/configmap/endpoint-watcher tests left intact ---

func TestAllNamespacesWatchFor_Add(t *testing.T) {
w, fc := getTestWatcher()

Expand Down Expand Up @@ -397,7 +395,7 @@ func getTestWatcherForCache() (Watcher, *framework.FakeControllerSource) {

gvr := schema.GroupVersionResource{
Group: "k8s.trafficserver.apache.com",
Version: "v1",
Version: "v1alpha1",
Resource: "atscachingpolicies",
}

Expand All @@ -411,7 +409,7 @@ func getTestWatcherForCache() (Watcher, *framework.FakeControllerSource) {

clientset := fake.NewSimpleClientset()
fc := framework.NewFakeControllerSource()
exampleEndpoint := createExampleEndpointWithFakeATS()
exampleEndpoint := createExampleEndpointWithFakeATSCache()
stopChan := make(chan struct{})

ingressWatcher := Watcher{
Expand Down Expand Up @@ -456,15 +454,15 @@ func TestWatchAtsCachingPolicy_Add(t *testing.T) {

gvr := schema.GroupVersionResource{
Group: "k8s.trafficserver.apache.com",
Version: "v1",
Version: "v1alpha1",
Resource: "atscachingpolicies",
}
dynClient := w.DynamicClient.Resource(gvr).Namespace("default")

// Create a new caching policy
policy := &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "k8s.trafficserver.apache.com/v1",
"apiVersion": "k8s.trafficserver.apache.com/v1alpha1",
"kind": "ATSCachingPolicy",
"metadata": map[string]interface{}{
"name": "policy-add",
Expand Down Expand Up @@ -509,15 +507,15 @@ func TestWatchAtsCachingPolicy_Update(t *testing.T) {

gvr := schema.GroupVersionResource{
Group: "k8s.trafficserver.apache.com",
Version: "v1",
Version: "v1alpha1",
Resource: "atscachingpolicies",
}
dynClient := w.DynamicClient.Resource(gvr).Namespace("default")

// Create a policy first
policy := &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "k8s.trafficserver.apache.com/v1",
"apiVersion": "k8s.trafficserver.apache.com/v1alpha1",
"kind": "ATSCachingPolicy",
"metadata": map[string]interface{}{
"name": "policy-update",
Expand Down Expand Up @@ -577,15 +575,15 @@ func TestWatchAtsCachingPolicy_Delete(t *testing.T) {

gvr := schema.GroupVersionResource{
Group: "k8s.trafficserver.apache.com",
Version: "v1",
Version: "v1alpha1",
Resource: "atscachingpolicies",
}
dynClient := w.DynamicClient.Resource(gvr).Namespace("default")

// Create a policy first
policy := &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "k8s.trafficserver.apache.com/v1",
"apiVersion": "k8s.trafficserver.apache.com/v1alpha1",
"kind": "ATSCachingPolicy",
"metadata": map[string]interface{}{
"name": "policy-delete",
Expand Down
Loading