|
| 1 | +// Copyright 2019 HAProxy Technologies LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, softwarehaproxyConfig |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package customresources |
| 16 | + |
| 17 | +import ( |
| 18 | + "io/ioutil" |
| 19 | + "path/filepath" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/haproxytech/client-native/v2/models" |
| 23 | + corev1alpha1 "github.com/haproxytech/kubernetes-ingress/crs/api/core/v1alpha1" |
| 24 | + c "github.com/haproxytech/kubernetes-ingress/pkg/controller" |
| 25 | + "github.com/haproxytech/kubernetes-ingress/pkg/haproxy/config" |
| 26 | + "github.com/haproxytech/kubernetes-ingress/pkg/k8s" |
| 27 | + "github.com/haproxytech/kubernetes-ingress/pkg/store" |
| 28 | + "github.com/haproxytech/kubernetes-ingress/pkg/utils" |
| 29 | + "github.com/jessevdk/go-flags" |
| 30 | + "github.com/stretchr/testify/suite" |
| 31 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 32 | + "k8s.io/apimachinery/pkg/watch" |
| 33 | +) |
| 34 | + |
| 35 | +type CustomResourceSuite struct { |
| 36 | + suite.Suite |
| 37 | + test Test |
| 38 | +} |
| 39 | + |
| 40 | +func TestCustomResource(t *testing.T) { |
| 41 | + suite.Run(t, new(CustomResourceSuite)) |
| 42 | +} |
| 43 | + |
| 44 | +type Test struct { |
| 45 | + TempDir string |
| 46 | + Controller *c.HAProxyController |
| 47 | +} |
| 48 | + |
| 49 | +func (suite *CustomResourceSuite) BeforeTest(suiteName, testName string) { |
| 50 | + tempDir, err := ioutil.TempDir("", "ut-"+testName+"-*") |
| 51 | + if err != nil { |
| 52 | + suite.T().Fatalf("Suite '%s': Test '%s' : error : %s", suiteName, testName, err) |
| 53 | + } |
| 54 | + suite.test.TempDir = tempDir |
| 55 | +} |
| 56 | + |
| 57 | +var haproxyConfig = `global |
| 58 | +daemon |
| 59 | +master-worker |
| 60 | +pidfile /var/run/haproxy.pid |
| 61 | +stats socket /var/run/haproxy-runtime-api.sock level admin expose-fd listeners |
| 62 | +default-path config |
| 63 | +
|
| 64 | +peers localinstance |
| 65 | + peer local 127.0.0.1:10000 |
| 66 | +
|
| 67 | +frontend https |
| 68 | +mode http |
| 69 | +bind 127.0.0.1:8080 name v4 |
| 70 | +http-request set-var(txn.base) base |
| 71 | +use_backend %[var(txn.path_match),field(1,.)] |
| 72 | +
|
| 73 | +frontend http |
| 74 | +mode http |
| 75 | +bind 127.0.0.1:4443 name v4 |
| 76 | +http-request set-var(txn.base) base |
| 77 | +use_backend %[var(txn.path_match),field(1,.)] |
| 78 | +
|
| 79 | +frontend healthz |
| 80 | +bind 127.0.0.1:1042 name v4 |
| 81 | +mode http |
| 82 | +monitor-uri /healthz |
| 83 | +option dontlog-normal |
| 84 | +
|
| 85 | +frontend stats |
| 86 | + mode http |
| 87 | + bind *:1024 |
| 88 | + http-request set-var(txn.base) base |
| 89 | + http-request use-service prometheus-exporter if { path /metrics } |
| 90 | + stats enable |
| 91 | + stats uri / |
| 92 | + stats refresh 10s` |
| 93 | + |
| 94 | +func (suite *CustomResourceSuite) GlobalCRFixture() (eventChan chan k8s.SyncDataEvent, s store.K8s, globalCREvt k8s.SyncDataEvent) { |
| 95 | + var osArgs utils.OSArgs |
| 96 | + parser := flags.NewParser(&osArgs, flags.IgnoreUnknown) |
| 97 | + _, _ = parser.Parse() |
| 98 | + osArgs.Test = true |
| 99 | + eventChan = make(chan k8s.SyncDataEvent, watch.DefaultChanSize*6) |
| 100 | + s = store.NewK8sStore(osArgs) |
| 101 | + globalCREvt = k8s.SyncDataEvent{ |
| 102 | + SyncType: k8s.CR_GLOBAL, |
| 103 | + Data: &corev1alpha1.Global{ |
| 104 | + ObjectMeta: metav1.ObjectMeta{ |
| 105 | + Name: "fake", |
| 106 | + }, |
| 107 | + Spec: corev1alpha1.GlobalSpec{ |
| 108 | + Config: &models.Global{}, |
| 109 | + LogTargets: models.LogTargets{}, |
| 110 | + }, |
| 111 | + }, |
| 112 | + Name: "globalcrjob", |
| 113 | + } |
| 114 | + |
| 115 | + haproxyEnv := config.Env{ |
| 116 | + Binary: "/usr/local/sbin/haproxy", |
| 117 | + MainCFGFile: filepath.Join(suite.test.TempDir, "haproxy.cfg"), |
| 118 | + CfgDir: suite.test.TempDir, |
| 119 | + RuntimeDir: filepath.Join(suite.test.TempDir, "run"), |
| 120 | + StateDir: filepath.Join(suite.test.TempDir, "state/haproxy/"), |
| 121 | + Proxies: config.Proxies{ |
| 122 | + FrontHTTP: "http", |
| 123 | + FrontHTTPS: "https", |
| 124 | + FrontSSL: "ssl", |
| 125 | + BackSSL: "ssl", |
| 126 | + }, |
| 127 | + } |
| 128 | + |
| 129 | + controller := c.NewBuilder(). |
| 130 | + WithHaproxyCfgFile([]byte(haproxyConfig)). |
| 131 | + WithEventChan(eventChan). |
| 132 | + WithStore(s). |
| 133 | + WithHaproxyEnv(haproxyEnv). |
| 134 | + WithArgs(osArgs).Build() |
| 135 | + |
| 136 | + go controller.Start() |
| 137 | + return |
| 138 | +} |
0 commit comments