|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/alecthomas/kong" |
| 5 | + "github.com/confluentinc/confluent-kafka-go/kafka" |
| 6 | + "github.com/grepplabs/mqtt-proxy/pkg/config" |
| 7 | + "github.com/grepplabs/mqtt-proxy/pkg/log" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +func TestDefaultServerConfig(t *testing.T) { |
| 13 | + testCLI, command, err := parseTestCLI([]string{"server"}) |
| 14 | + require.NoError(t, err) |
| 15 | + require.Equal(t, "server", command) |
| 16 | + require.Nil(t, testCLI.Server.MQTT.Handler.Authenticator.Plain.Credentials) |
| 17 | + require.Nil(t, testCLI.Server.MQTT.Publisher.Kafka.ConfArgs.ConfigMap()) |
| 18 | + require.Nil(t, testCLI.Server.MQTT.Publisher.Kafka.TopicMappings.Mappings) |
| 19 | + require.Equal(t, 1, testCLI.Server.MQTT.Publisher.Kafka.Workers) |
| 20 | +} |
| 21 | + |
| 22 | +func TestKafkaConfigOneParam(t *testing.T) { |
| 23 | + testCLI, _, err := parseTestCLI([]string{ |
| 24 | + "server", |
| 25 | + "--mqtt.publisher.kafka.config", "producer.sasl.mechanisms=PLAIN,producer.security.protocol=SASL_SSL,producer.sasl.username=myuser,producer.sasl.password=mypasswd", |
| 26 | + }) |
| 27 | + require.NoError(t, err) |
| 28 | + require.EqualValues(t, map[string]kafka.ConfigValue{ |
| 29 | + "producer.sasl.mechanisms": "PLAIN", |
| 30 | + "producer.security.protocol": "SASL_SSL", |
| 31 | + "producer.sasl.username": "myuser", |
| 32 | + "producer.sasl.password": "mypasswd", |
| 33 | + }, testCLI.Server.MQTT.Publisher.Kafka.ConfArgs.ConfigMap()) |
| 34 | +} |
| 35 | + |
| 36 | +func TestKafkaConfigMultipleParams(t *testing.T) { |
| 37 | + testCLI, _, err := parseTestCLI([]string{ |
| 38 | + "server", |
| 39 | + "--mqtt.publisher.kafka.config", "producer.sasl.mechanisms=PLAIN,producer.security.protocol=SASL_SSL", |
| 40 | + "--mqtt.publisher.kafka.config", "producer.sasl.username=myuser,producer.sasl.password=mypasswd", |
| 41 | + }) |
| 42 | + require.NoError(t, err) |
| 43 | + require.EqualValues(t, map[string]kafka.ConfigValue{ |
| 44 | + "producer.sasl.mechanisms": "PLAIN", |
| 45 | + "producer.security.protocol": "SASL_SSL", |
| 46 | + "producer.sasl.username": "myuser", |
| 47 | + "producer.sasl.password": "mypasswd", |
| 48 | + }, testCLI.Server.MQTT.Publisher.Kafka.ConfArgs.ConfigMap()) |
| 49 | +} |
| 50 | + |
| 51 | +func TestTopicMappingConfig(t *testing.T) { |
| 52 | + testCLI, _, err := parseTestCLI([]string{ |
| 53 | + "server", |
| 54 | + "--mqtt.publisher.kafka.topic-mappings", "temperature=temperature, humidity=.*/humidity,brightness=.*brightness, temperature=^cool$", |
| 55 | + }) |
| 56 | + require.NoError(t, err) |
| 57 | + require.Equal(t, 4, len(testCLI.Server.MQTT.Publisher.Kafka.TopicMappings.Mappings)) |
| 58 | +} |
| 59 | + |
| 60 | +func TestPlainCredentialsConfig(t *testing.T) { |
| 61 | + testCLI, _, err := parseTestCLI([]string{ |
| 62 | + "server", |
| 63 | + "--mqtt.handler.auth.plain.credentials", "alice=test1", |
| 64 | + "--mqtt.handler.auth.plain.credentials", "bob=test2", |
| 65 | + }) |
| 66 | + require.NoError(t, err) |
| 67 | + require.EqualValues(t, map[string]string{ |
| 68 | + "alice": "test1", |
| 69 | + "bob": "test2", |
| 70 | + }, testCLI.Server.MQTT.Handler.Authenticator.Plain.Credentials) |
| 71 | +} |
| 72 | + |
| 73 | +func parseTestCLI(args []string) (*CLI, string, error) { |
| 74 | + testCLI := &CLI{} |
| 75 | + parser, err := kong.New(testCLI, |
| 76 | + kong.Name("mqtt-proxy"), |
| 77 | + kong.Description("MQTT Proxy"), |
| 78 | + log.Vars(), config.ServerVars()) |
| 79 | + if err != nil { |
| 80 | + return nil, "", err |
| 81 | + } |
| 82 | + command, err := parser.Parse(args) |
| 83 | + if err != nil { |
| 84 | + return nil, "", err |
| 85 | + } |
| 86 | + return testCLI, command.Command(), nil |
| 87 | +} |
0 commit comments