Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package kafka |
| 17 | |
| 18 | import ( |
| 19 | "github.com/stretchr/testify/assert" |
| 20 | "testing" |
| 21 | ) |
| 22 | |
| 23 | func TestDefaultKafkaProxy(t *testing.T) { |
Kent Hagerman | 3a40230 | 2020-01-31 15:03:53 -0500 | [diff] [blame] | 24 | actualResult := newInterContainerProxy() |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 25 | assert.Equal(t, actualResult.kafkaHost, DefaultKafkaHost) |
| 26 | assert.Equal(t, actualResult.kafkaPort, DefaultKafkaPort) |
| 27 | assert.Equal(t, actualResult.defaultRequestHandlerInterface, interface{}(nil)) |
| 28 | } |
| 29 | |
| 30 | func TestKafkaProxyOptionHost(t *testing.T) { |
Kent Hagerman | 3a40230 | 2020-01-31 15:03:53 -0500 | [diff] [blame] | 31 | actualResult := newInterContainerProxy(InterContainerHost("10.20.30.40")) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 32 | assert.Equal(t, actualResult.kafkaHost, "10.20.30.40") |
| 33 | assert.Equal(t, actualResult.kafkaPort, DefaultKafkaPort) |
| 34 | assert.Equal(t, actualResult.defaultRequestHandlerInterface, interface{}(nil)) |
| 35 | } |
| 36 | |
| 37 | func TestKafkaProxyOptionPort(t *testing.T) { |
Kent Hagerman | 3a40230 | 2020-01-31 15:03:53 -0500 | [diff] [blame] | 38 | actualResult := newInterContainerProxy(InterContainerPort(1020)) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 39 | assert.Equal(t, actualResult.kafkaHost, DefaultKafkaHost) |
| 40 | assert.Equal(t, actualResult.kafkaPort, 1020) |
| 41 | assert.Equal(t, actualResult.defaultRequestHandlerInterface, interface{}(nil)) |
| 42 | } |
| 43 | |
| 44 | func TestKafkaProxyOptionTopic(t *testing.T) { |
Kent Hagerman | 3a40230 | 2020-01-31 15:03:53 -0500 | [diff] [blame] | 45 | actualResult := newInterContainerProxy(DefaultTopic(&Topic{Name: "Adapter"})) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 46 | assert.Equal(t, actualResult.kafkaHost, DefaultKafkaHost) |
| 47 | assert.Equal(t, actualResult.kafkaPort, DefaultKafkaPort) |
| 48 | assert.Equal(t, actualResult.defaultRequestHandlerInterface, interface{}(nil)) |
Matteo Scandolo | f346a2d | 2020-01-24 13:14:54 -0800 | [diff] [blame] | 49 | assert.Equal(t, actualResult.defaultTopic.Name, "Adapter") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | type myInterface struct { |
| 53 | } |
| 54 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 55 | func TestKafkaProxyOptionTargetInterface(t *testing.T) { |
| 56 | var m *myInterface |
Kent Hagerman | 3a40230 | 2020-01-31 15:03:53 -0500 | [diff] [blame] | 57 | actualResult := newInterContainerProxy(RequestHandlerInterface(m)) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 58 | assert.Equal(t, actualResult.kafkaHost, DefaultKafkaHost) |
| 59 | assert.Equal(t, actualResult.kafkaPort, DefaultKafkaPort) |
| 60 | assert.Equal(t, actualResult.defaultRequestHandlerInterface, m) |
| 61 | } |
| 62 | |
| 63 | func TestKafkaProxyChangeAllOptions(t *testing.T) { |
| 64 | var m *myInterface |
Kent Hagerman | 3a40230 | 2020-01-31 15:03:53 -0500 | [diff] [blame] | 65 | actualResult := newInterContainerProxy( |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 66 | InterContainerHost("10.20.30.40"), |
| 67 | InterContainerPort(1020), |
| 68 | DefaultTopic(&Topic{Name: "Adapter"}), |
| 69 | RequestHandlerInterface(m)) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 70 | assert.Equal(t, actualResult.kafkaHost, "10.20.30.40") |
| 71 | assert.Equal(t, actualResult.kafkaPort, 1020) |
| 72 | assert.Equal(t, actualResult.defaultRequestHandlerInterface, m) |
Matteo Scandolo | f346a2d | 2020-01-24 13:14:54 -0800 | [diff] [blame] | 73 | assert.Equal(t, actualResult.defaultTopic.Name, "Adapter") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 74 | } |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 75 | |
| 76 | func TestKafkaProxyEnableLivenessChannel(t *testing.T) { |
| 77 | var m *myInterface |
| 78 | |
| 79 | // Note: This doesn't actually start the client |
| 80 | client := NewSaramaClient() |
| 81 | |
Kent Hagerman | 3a40230 | 2020-01-31 15:03:53 -0500 | [diff] [blame] | 82 | probe := newInterContainerProxy( |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 83 | InterContainerHost("10.20.30.40"), |
| 84 | InterContainerPort(1020), |
| 85 | DefaultTopic(&Topic{Name: "Adapter"}), |
| 86 | RequestHandlerInterface(m), |
| 87 | MsgClient(client), |
| 88 | ) |
| 89 | |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 90 | ch := probe.EnableLivenessChannel(true) |
| 91 | |
| 92 | // The channel should have one "true" message on it |
| 93 | assert.NotEmpty(t, ch) |
| 94 | |
| 95 | select { |
| 96 | case stuff := <-ch: |
| 97 | assert.True(t, stuff) |
| 98 | default: |
| 99 | t.Error("Failed to read from the channel") |
| 100 | } |
| 101 | } |