|
| 1 | +package tests |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + netv1 "k8s.io/api/networking/v1" |
| 7 | + "k8s.io/utils/pointer" |
| 8 | + |
| 9 | + "github.com/jinzhu/copier" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +func TestIngress(t *testing.T) { |
| 14 | + chart := LoadChart(t) |
| 15 | + |
| 16 | + pathTypePrefix := netv1.PathTypePrefix |
| 17 | + coderdIngressRule := netv1.IngressRuleValue{ |
| 18 | + HTTP: &netv1.HTTPIngressRuleValue{ |
| 19 | + Paths: []netv1.HTTPIngressPath{ |
| 20 | + { |
| 21 | + Path: "/", |
| 22 | + PathType: &pathTypePrefix, |
| 23 | + Backend: netv1.IngressBackend{ |
| 24 | + Service: &netv1.IngressServiceBackend{ |
| 25 | + Name: "coderd", |
| 26 | + Port: netv1.ServiceBackendPort{ |
| 27 | + Name: "tcp-coderd", |
| 28 | + }, |
| 29 | + }, |
| 30 | + }, |
| 31 | + }, |
| 32 | + }, |
| 33 | + }, |
| 34 | + } |
| 35 | + |
| 36 | + tests := []struct { |
| 37 | + Name string |
| 38 | + // ValuesFunc is called to configure the values used in this test. |
| 39 | + // The function should override the CoderValues as appropriate for |
| 40 | + // the test in question. |
| 41 | + ValuesFunc func(v *CoderValues) |
| 42 | + // AssertFunc is called after rendering the chart, with the resulting |
| 43 | + // Ingress object. You can use it to assert properties about the |
| 44 | + // Ingress object. |
| 45 | + AssertFunc func(t *testing.T, ingress *netv1.Ingress) |
| 46 | + }{ |
| 47 | + { |
| 48 | + Name: "simple-ingress", |
| 49 | + ValuesFunc: func(v *CoderValues) { |
| 50 | + v.Ingress.Enable = pointer.Bool(true) |
| 51 | + v.Ingress.Host = pointer.String("install.coder.com") |
| 52 | + v.Coderd.DevURLsHost = pointer.String("*.install.coder.app") |
| 53 | + }, |
| 54 | + AssertFunc: func(t *testing.T, ingress *netv1.Ingress) { |
| 55 | + defaultAnnotations := map[string]string{ |
| 56 | + "nginx.ingress.kubernetes.io/proxy-body-size": "0", |
| 57 | + } |
| 58 | + require.Equal(t, defaultAnnotations, ingress.Annotations) |
| 59 | + |
| 60 | + expectedRules := []netv1.IngressRule{ |
| 61 | + { |
| 62 | + Host: "install.coder.com", |
| 63 | + IngressRuleValue: coderdIngressRule, |
| 64 | + }, |
| 65 | + { |
| 66 | + Host: "*.install.coder.app", |
| 67 | + IngressRuleValue: coderdIngressRule, |
| 68 | + }, |
| 69 | + } |
| 70 | + require.Equal(t, expectedRules, ingress.Spec.Rules, "expected ingress spec to match") |
| 71 | + }, |
| 72 | + }, |
| 73 | + { |
| 74 | + Name: "devurl-suffix", |
| 75 | + ValuesFunc: func(v *CoderValues) { |
| 76 | + v.Ingress.Enable = pointer.Bool(true) |
| 77 | + v.Ingress.Host = pointer.String("install.coder.com") |
| 78 | + v.Coderd.DevURLsHost = pointer.String("*-dev.install.coder.app") |
| 79 | + }, |
| 80 | + AssertFunc: func(t *testing.T, ingress *netv1.Ingress) { |
| 81 | + defaultAnnotations := map[string]string{ |
| 82 | + "nginx.ingress.kubernetes.io/proxy-body-size": "0", |
| 83 | + } |
| 84 | + require.Equal(t, defaultAnnotations, ingress.Annotations) |
| 85 | + expectedRules := []netv1.IngressRule{ |
| 86 | + { |
| 87 | + Host: "install.coder.com", |
| 88 | + IngressRuleValue: coderdIngressRule, |
| 89 | + }, |
| 90 | + { |
| 91 | + Host: "*.install.coder.app", |
| 92 | + IngressRuleValue: coderdIngressRule, |
| 93 | + }, |
| 94 | + } |
| 95 | + require.Equal(t, expectedRules, ingress.Spec.Rules, "expected ingress spec to match") |
| 96 | + }, |
| 97 | + }, |
| 98 | + } |
| 99 | + |
| 100 | + for _, test := range tests { |
| 101 | + test := test |
| 102 | + |
| 103 | + t.Run(test.Name, func(t *testing.T) { |
| 104 | + // Clone the original values |
| 105 | + values := &CoderValues{} |
| 106 | + copier.Copy(values, chart.OriginalValues) |
| 107 | + |
| 108 | + // Run function to perform test-specific modifications of defaults |
| 109 | + test.ValuesFunc(values) |
| 110 | + |
| 111 | + // Verify the results using AssertFunc |
| 112 | + objs, err := chart.Render(values, nil, nil) |
| 113 | + require.NoError(t, err, "chart render failed") |
| 114 | + |
| 115 | + var found bool |
| 116 | + for _, obj := range objs { |
| 117 | + ingress, ok := obj.(*netv1.Ingress) |
| 118 | + if ok && ingress.Name == "coderd-ingress" { |
| 119 | + found = true |
| 120 | + test.AssertFunc(t, ingress) |
| 121 | + break |
| 122 | + } |
| 123 | + } |
| 124 | + require.True(t, found, "expected ingress in manifests") |
| 125 | + }) |
| 126 | + } |
| 127 | +} |
0 commit comments