Skip to content

Commit

Permalink
chore: add more tests to store_test.go
Browse files Browse the repository at this point in the history
  • Loading branch information
shaneutt authored and rainest committed Jul 12, 2021
1 parent d08f68c commit cb27c4b
Showing 1 changed file with 85 additions and 9 deletions.
94 changes: 85 additions & 9 deletions pkg/store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ package store

import (
"reflect"
"strings"
"testing"

"github.com/sirupsen/logrus"
core "k8s.io/api/core/v1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
extensions "k8s.io/api/extensions/v1beta1"
netv1 "k8s.io/api/networking/v1"
networking "k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
Expand All @@ -31,11 +36,11 @@ func Test_networkingIngressV1Beta1(t *testing.T) {
{
name: "returns nil if a non-ingress object is passed in",
args: args{
&core.Service{
Spec: core.ServiceSpec{
Type: core.ServiceTypeClusterIP,
&corev1.Service{
Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeClusterIP,
ClusterIP: "1.1.1.1",
Ports: []core.ServicePort{
Ports: []corev1.ServicePort{
{
Name: "default",
TargetPort: intstr.FromString("port-1"),
Expand Down Expand Up @@ -75,8 +80,8 @@ func Test_networkingIngressV1Beta1(t *testing.T) {
},
},
Status: extensions.IngressStatus{
LoadBalancer: core.LoadBalancerStatus{
Ingress: []core.LoadBalancerIngress{{IP: "1.2.3.4"}},
LoadBalancer: corev1.LoadBalancerStatus{
Ingress: []corev1.LoadBalancerIngress{{IP: "1.2.3.4"}},
},
},
},
Expand Down Expand Up @@ -107,8 +112,8 @@ func Test_networkingIngressV1Beta1(t *testing.T) {
},
},
Status: networking.IngressStatus{
LoadBalancer: core.LoadBalancerStatus{
Ingress: []core.LoadBalancerIngress{{IP: "1.2.3.4"}},
LoadBalancer: corev1.LoadBalancerStatus{
Ingress: []corev1.LoadBalancerIngress{{IP: "1.2.3.4"}},
},
},
},
Expand All @@ -125,3 +130,74 @@ func Test_networkingIngressV1Beta1(t *testing.T) {
})
}
}

func TestCacheStoresGet(t *testing.T) {
t.Log("configuring some yaml objects to store in the cache")
svcYAML := []byte(`---
apiVersion: v1
kind: Service
metadata:
name: httpbin-deployment
namespace: default
labels:
app: httpbin
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: httpbin
type: ClusterIP
`)
ingYAML := []byte(`---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: httpbin-ingress
namespace: default
annotations:
httpbin.ingress.kubernetes.io/rewrite-target: /
kubernetes.io/ingress.class: "kong"
spec:
rules:
- http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: httpbin-deployment
port:
number: 80
`)

t.Log("creating a new cache store from object yaml files")
cs, err := NewCacheStoresFromObjYAML(svcYAML, ingYAML)
require.NoError(t, err)

t.Log("verifying that the cache store doesnt try to retrieve unsupported object types")
_, exists, err := cs.Get(new(appsv1.Deployment))
assert.Error(t, err)
assert.True(t, strings.Contains(err.Error(), "Deployment is not a supported cache object type"))
assert.False(t, exists)

t.Log("verifying the integrity of the cache store")
assert.Len(t, cs.IngressV1.List(), 1)
assert.Len(t, cs.Service.List(), 1)
assert.Len(t, cs.IngressV1beta1.List(), 0)
assert.Len(t, cs.KongIngress.List(), 0)
_, exists, err = cs.Get(&corev1.Service{ObjectMeta: metav1.ObjectMeta{Namespace: "doesntexist", Name: "doesntexist"}})
assert.NoError(t, err)
assert.False(t, exists)

t.Log("ensuring that we can Get() the objects back out of the cache store")
svc := &corev1.Service{ObjectMeta: metav1.ObjectMeta{Namespace: "default", Name: "httpbin-deployment"}}
ing := &netv1.Ingress{ObjectMeta: metav1.ObjectMeta{Namespace: "default", Name: "httpbin-ingress"}}
_, exists, err = cs.Get(svc)
assert.NoError(t, err)
assert.True(t, exists)
_, exists, err = cs.Get(ing)
assert.NoError(t, err)
assert.True(t, exists)
}

0 comments on commit cb27c4b

Please sign in to comment.