|
| 1 | +package resources |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 9 | + |
| 10 | + hermesv1 "github.com/paperclipinc/hermes-operator/api/v1" |
| 11 | +) |
| 12 | + |
| 13 | +func TestBuildHTTPRoute_NilWhenUnset(t *testing.T) { |
| 14 | + t.Parallel() |
| 15 | + inst := &hermesv1.HermesInstance{ObjectMeta: metav1.ObjectMeta{Name: "demo", Namespace: "agents"}} |
| 16 | + assert.Nil(t, BuildHTTPRoute(inst), "no HTTPRoute spec means no route") |
| 17 | +} |
| 18 | + |
| 19 | +func TestBuildHTTPRoute_Basics(t *testing.T) { |
| 20 | + t.Parallel() |
| 21 | + ns := "gateways" |
| 22 | + section := "https" |
| 23 | + inst := &hermesv1.HermesInstance{ |
| 24 | + ObjectMeta: metav1.ObjectMeta{Name: "demo", Namespace: "agents"}, |
| 25 | + Spec: hermesv1.HermesInstanceSpec{ |
| 26 | + Networking: hermesv1.NetworkingSpec{ |
| 27 | + HTTPRoute: &hermesv1.HTTPRouteSpec{ |
| 28 | + Enabled: Ptr(true), |
| 29 | + Hostnames: []string{"agent.example.com"}, |
| 30 | + ParentRefs: []hermesv1.HTTPRouteParentRef{ |
| 31 | + {Name: "public-gw", Namespace: &ns, SectionName: §ion}, |
| 32 | + }, |
| 33 | + Annotations: map[string]string{"team": "platform"}, |
| 34 | + }, |
| 35 | + }, |
| 36 | + }, |
| 37 | + } |
| 38 | + |
| 39 | + route := BuildHTTPRoute(inst) |
| 40 | + require.NotNil(t, route) |
| 41 | + assert.Equal(t, "gateway.networking.k8s.io/v1", route.GetAPIVersion()) |
| 42 | + assert.Equal(t, "HTTPRoute", route.GetKind()) |
| 43 | + assert.Equal(t, "demo", route.GetName()) |
| 44 | + assert.Equal(t, "agents", route.GetNamespace()) |
| 45 | + assert.Equal(t, "platform", route.GetAnnotations()["team"]) |
| 46 | + |
| 47 | + spec, _, _ := getNestedMap(route.Object, "spec") |
| 48 | + |
| 49 | + parents := spec["parentRefs"].([]interface{}) |
| 50 | + require.Len(t, parents, 1) |
| 51 | + pr := parents[0].(map[string]interface{}) |
| 52 | + assert.Equal(t, "public-gw", pr["name"]) |
| 53 | + assert.Equal(t, "gateways", pr["namespace"]) |
| 54 | + assert.Equal(t, "https", pr["sectionName"]) |
| 55 | + |
| 56 | + hostnames := spec["hostnames"].([]interface{}) |
| 57 | + assert.Equal(t, "agent.example.com", hostnames[0]) |
| 58 | + |
| 59 | + rules := spec["rules"].([]interface{}) |
| 60 | + require.Len(t, rules, 1) |
| 61 | + rule := rules[0].(map[string]interface{}) |
| 62 | + |
| 63 | + match := rule["matches"].([]interface{})[0].(map[string]interface{}) |
| 64 | + path := match["path"].(map[string]interface{}) |
| 65 | + assert.Equal(t, "PathPrefix", path["type"]) |
| 66 | + assert.Equal(t, "/", path["value"]) |
| 67 | + |
| 68 | + backend := rule["backendRefs"].([]interface{})[0].(map[string]interface{}) |
| 69 | + assert.Equal(t, "demo", backend["name"]) |
| 70 | + assert.Equal(t, int64(GatewayPort), backend["port"], "backendRefs target the Service port by number") |
| 71 | +} |
| 72 | + |
| 73 | +func TestBuildHTTPRoute_CustomPathAndPort(t *testing.T) { |
| 74 | + t.Parallel() |
| 75 | + inst := &hermesv1.HermesInstance{ |
| 76 | + ObjectMeta: metav1.ObjectMeta{Name: "demo", Namespace: "agents"}, |
| 77 | + Spec: hermesv1.HermesInstanceSpec{ |
| 78 | + Networking: hermesv1.NetworkingSpec{ |
| 79 | + Service: hermesv1.ServiceSpec{ |
| 80 | + Ports: []hermesv1.NamedServicePort{{Name: "web", Port: 9000}}, |
| 81 | + }, |
| 82 | + HTTPRoute: &hermesv1.HTTPRouteSpec{ |
| 83 | + Enabled: Ptr(true), |
| 84 | + Path: "/api", |
| 85 | + ServicePortName: "web", |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + } |
| 90 | + route := BuildHTTPRoute(inst) |
| 91 | + require.NotNil(t, route) |
| 92 | + spec, _, _ := getNestedMap(route.Object, "spec") |
| 93 | + rule := spec["rules"].([]interface{})[0].(map[string]interface{}) |
| 94 | + path := rule["matches"].([]interface{})[0].(map[string]interface{})["path"].(map[string]interface{}) |
| 95 | + assert.Equal(t, "/api", path["value"]) |
| 96 | + backend := rule["backendRefs"].([]interface{})[0].(map[string]interface{}) |
| 97 | + assert.Equal(t, int64(9000), backend["port"], "named port resolves to its Service port number") |
| 98 | +} |
| 99 | + |
| 100 | +func TestHTTPRouteName(t *testing.T) { |
| 101 | + t.Parallel() |
| 102 | + inst := &hermesv1.HermesInstance{ObjectMeta: metav1.ObjectMeta{Name: "demo"}} |
| 103 | + assert.Equal(t, "demo", HTTPRouteName(inst)) |
| 104 | +} |
0 commit comments