github.com/netdata/go.d.plugin@v0.58.1/modules/unbound/config/config_test.go (about)

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package config
     4  
     5  import (
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestUnboundConfig_Empty(t *testing.T) {
    12  	assert.True(t, UnboundConfig{}.Empty())
    13  	assert.False(t, UnboundConfig{enable: "yes"}.Empty())
    14  }
    15  
    16  func TestUnboundConfig_Cumulative(t *testing.T) {
    17  	tests := []struct {
    18  		input     string
    19  		wantValue bool
    20  		wantOK    bool
    21  	}{
    22  		{input: "yes", wantValue: true, wantOK: true},
    23  		{input: "no", wantValue: false, wantOK: true},
    24  		{input: "", wantValue: false, wantOK: false},
    25  		{input: "some value", wantValue: false, wantOK: true},
    26  	}
    27  
    28  	for _, test := range tests {
    29  		t.Run(test.input, func(t *testing.T) {
    30  			cfg := UnboundConfig{cumulative: test.input}
    31  
    32  			v, ok := cfg.Cumulative()
    33  			assert.Equal(t, test.wantValue, v)
    34  			assert.Equal(t, test.wantOK, ok)
    35  		})
    36  	}
    37  }
    38  
    39  func TestUnboundConfig_ControlEnabled(t *testing.T) {
    40  	tests := []struct {
    41  		input     string
    42  		wantValue bool
    43  		wantOK    bool
    44  	}{
    45  		{input: "yes", wantValue: true, wantOK: true},
    46  		{input: "no", wantValue: false, wantOK: true},
    47  		{input: "", wantValue: false, wantOK: false},
    48  		{input: "some value", wantValue: false, wantOK: true},
    49  	}
    50  
    51  	for _, test := range tests {
    52  		t.Run(test.input, func(t *testing.T) {
    53  			cfg := UnboundConfig{enable: test.input}
    54  
    55  			v, ok := cfg.ControlEnabled()
    56  			assert.Equal(t, test.wantValue, v)
    57  			assert.Equal(t, test.wantOK, ok)
    58  		})
    59  	}
    60  }
    61  
    62  func TestUnboundConfig_ControlInterface(t *testing.T) {
    63  	tests := []struct {
    64  		input     string
    65  		wantValue string
    66  		wantOK    bool
    67  	}{
    68  		{input: "127.0.0.1", wantValue: "127.0.0.1", wantOK: true},
    69  		{input: "/var/run/unbound.sock", wantValue: "/var/run/unbound.sock", wantOK: true},
    70  		{input: "", wantValue: "", wantOK: false},
    71  		{input: "some value", wantValue: "some value", wantOK: true},
    72  	}
    73  
    74  	for _, test := range tests {
    75  		t.Run(test.input, func(t *testing.T) {
    76  			cfg := UnboundConfig{iface: test.input}
    77  
    78  			v, ok := cfg.ControlInterface()
    79  			assert.Equal(t, test.wantValue, v)
    80  			assert.Equal(t, test.wantOK, ok)
    81  		})
    82  	}
    83  }
    84  
    85  func TestUnboundConfig_ControlPort(t *testing.T) {
    86  	tests := []struct {
    87  		input     string
    88  		wantValue string
    89  		wantOK    bool
    90  	}{
    91  		{input: "8953", wantValue: "8953", wantOK: true},
    92  		{input: "", wantValue: "", wantOK: false},
    93  		{input: "some value", wantValue: "some value", wantOK: true},
    94  	}
    95  
    96  	for _, test := range tests {
    97  		t.Run(test.input, func(t *testing.T) {
    98  			cfg := UnboundConfig{port: test.input}
    99  
   100  			v, ok := cfg.ControlPort()
   101  			assert.Equal(t, test.wantValue, v)
   102  			assert.Equal(t, test.wantOK, ok)
   103  		})
   104  	}
   105  }
   106  
   107  func TestUnboundConfig_ControlUseCert(t *testing.T) {
   108  	tests := []struct {
   109  		input     string
   110  		wantValue bool
   111  		wantOK    bool
   112  	}{
   113  		{input: "yes", wantValue: true, wantOK: true},
   114  		{input: "no", wantValue: false, wantOK: true},
   115  		{input: "", wantValue: false, wantOK: false},
   116  		{input: "some value", wantValue: false, wantOK: true},
   117  	}
   118  
   119  	for _, test := range tests {
   120  		t.Run(test.input, func(t *testing.T) {
   121  			cfg := UnboundConfig{useCert: test.input}
   122  
   123  			v, ok := cfg.ControlUseCert()
   124  			assert.Equal(t, test.wantValue, v)
   125  			assert.Equal(t, test.wantOK, ok)
   126  		})
   127  	}
   128  }
   129  
   130  func TestUnboundConfig_ControlKeyFile(t *testing.T) {
   131  	tests := []struct {
   132  		input     string
   133  		wantValue string
   134  		wantOK    bool
   135  	}{
   136  		{input: "/etc/unbound/unbound_control.key", wantValue: "/etc/unbound/unbound_control.key", wantOK: true},
   137  		{input: "", wantValue: "", wantOK: false},
   138  		{input: "some value", wantValue: "some value", wantOK: true},
   139  	}
   140  
   141  	for _, test := range tests {
   142  		t.Run(test.input, func(t *testing.T) {
   143  			cfg := UnboundConfig{keyFile: test.input}
   144  
   145  			v, ok := cfg.ControlKeyFile()
   146  			assert.Equal(t, test.wantValue, v)
   147  			assert.Equal(t, test.wantOK, ok)
   148  		})
   149  	}
   150  }
   151  
   152  func TestUnboundConfig_ControlCertFile(t *testing.T) {
   153  	tests := []struct {
   154  		input     string
   155  		wantValue string
   156  		wantOK    bool
   157  	}{
   158  		{input: "/etc/unbound/unbound_control.pem", wantValue: "/etc/unbound/unbound_control.pem", wantOK: true},
   159  		{input: "", wantValue: "", wantOK: false},
   160  		{input: "some value", wantValue: "some value", wantOK: true},
   161  	}
   162  
   163  	for _, test := range tests {
   164  		t.Run(test.input, func(t *testing.T) {
   165  			cfg := UnboundConfig{certFile: test.input}
   166  
   167  			v, ok := cfg.ControlCertFile()
   168  			assert.Equal(t, test.wantValue, v)
   169  			assert.Equal(t, test.wantOK, ok)
   170  		})
   171  	}
   172  }