github.com/elfadel/cilium@v1.6.12/pkg/option/config_test.go (about)

     1  // Copyright 2018 Authors of Cilium
     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, software
    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  // +build !privileged_tests
    16  
    17  package option
    18  
    19  import (
    20  	"fmt"
    21  	"io/ioutil"
    22  	"net"
    23  	"os"
    24  	"path/filepath"
    25  	"testing"
    26  
    27  	flag "github.com/spf13/pflag"
    28  	"github.com/spf13/viper"
    29  	. "gopkg.in/check.v1"
    30  )
    31  
    32  func (s *OptionSuite) TestValidateIPv6ClusterAllocCIDR(c *C) {
    33  	valid1 := &DaemonConfig{IPv6ClusterAllocCIDR: "fdfd::/64"}
    34  	c.Assert(valid1.validateIPv6ClusterAllocCIDR(), IsNil)
    35  	c.Assert(valid1.IPv6ClusterAllocCIDRBase, Equals, "fdfd::")
    36  
    37  	valid2 := &DaemonConfig{IPv6ClusterAllocCIDR: "fdfd:fdfd:fdfd:fdfd:aaaa::/64"}
    38  	c.Assert(valid2.validateIPv6ClusterAllocCIDR(), IsNil)
    39  	c.Assert(valid2.IPv6ClusterAllocCIDRBase, Equals, "fdfd:fdfd:fdfd:fdfd::")
    40  
    41  	invalid1 := &DaemonConfig{IPv6ClusterAllocCIDR: "foo"}
    42  	c.Assert(invalid1.validateIPv6ClusterAllocCIDR(), Not(IsNil))
    43  
    44  	invalid2 := &DaemonConfig{IPv6ClusterAllocCIDR: "fdfd"}
    45  	c.Assert(invalid2.validateIPv6ClusterAllocCIDR(), Not(IsNil))
    46  
    47  	invalid3 := &DaemonConfig{IPv6ClusterAllocCIDR: "fdfd::/32"}
    48  	c.Assert(invalid3.validateIPv6ClusterAllocCIDR(), Not(IsNil))
    49  
    50  	invalid4 := &DaemonConfig{}
    51  	c.Assert(invalid4.validateIPv6ClusterAllocCIDR(), Not(IsNil))
    52  }
    53  
    54  func TestGetEnvName(t *testing.T) {
    55  	type args struct {
    56  		option string
    57  	}
    58  	tests := []struct {
    59  		name string
    60  		args args
    61  		want string
    62  	}{
    63  		{
    64  			name: "Normal option",
    65  			args: args{
    66  				option: "foo",
    67  			},
    68  			want: "CILIUM_FOO",
    69  		},
    70  		{
    71  			name: "Capital option",
    72  			args: args{
    73  				option: "FOO",
    74  			},
    75  			want: "CILIUM_FOO",
    76  		},
    77  		{
    78  			name: "with numbers",
    79  			args: args{
    80  				option: "2222",
    81  			},
    82  			want: "CILIUM_2222",
    83  		},
    84  		{
    85  			name: "mix numbers small letters",
    86  			args: args{
    87  				option: "22ada22",
    88  			},
    89  			want: "CILIUM_22ADA22",
    90  		},
    91  		{
    92  			name: "mix numbers small letters and dashes",
    93  			args: args{
    94  				option: "22ada2------2",
    95  			},
    96  			want: "CILIUM_22ADA2______2",
    97  		},
    98  		{
    99  			name: "normal option",
   100  			args: args{
   101  				option: "conntrack-garbage-collector-interval",
   102  			},
   103  			want: "CILIUM_CONNTRACK_GARBAGE_COLLECTOR_INTERVAL",
   104  		},
   105  	}
   106  	for _, tt := range tests {
   107  		t.Run(tt.name, func(t *testing.T) {
   108  			if got := getEnvName(tt.args.option); got != tt.want {
   109  				t.Errorf("getEnvName() = %v, want %v", got, tt.want)
   110  			}
   111  		})
   112  	}
   113  }
   114  
   115  func (s *OptionSuite) TestReadDirConfig(c *C) {
   116  	var dirName string
   117  	type args struct {
   118  		dirName string
   119  	}
   120  	type want struct {
   121  		allSettings        map[string]interface{}
   122  		allSettingsChecker Checker
   123  		err                error
   124  		errChecker         Checker
   125  	}
   126  	tests := []struct {
   127  		name        string
   128  		setupArgs   func() args
   129  		setupWant   func() want
   130  		preTestRun  func()
   131  		postTestRun func()
   132  	}{
   133  		{
   134  			name: "empty configuration",
   135  			preTestRun: func() {
   136  				dirName = c.MkDir()
   137  
   138  				fs := flag.NewFlagSet("empty configuration", flag.ContinueOnError)
   139  				viper.BindPFlags(fs)
   140  			},
   141  			setupArgs: func() args {
   142  				return args{
   143  					dirName: dirName,
   144  				}
   145  			},
   146  			setupWant: func() want {
   147  				return want{
   148  					allSettings:        map[string]interface{}{},
   149  					allSettingsChecker: DeepEquals,
   150  					err:                nil,
   151  					errChecker:         Equals,
   152  				}
   153  			},
   154  			postTestRun: func() {
   155  				os.RemoveAll(dirName)
   156  			},
   157  		},
   158  		{
   159  			name: "single file configuration",
   160  			preTestRun: func() {
   161  				dirName = c.MkDir()
   162  
   163  				fullPath := filepath.Join(dirName, "test")
   164  				err := ioutil.WriteFile(fullPath, []byte(`"1"
   165  `), os.FileMode(0644))
   166  				c.Assert(err, IsNil)
   167  				fs := flag.NewFlagSet("single file configuration", flag.ContinueOnError)
   168  				fs.String("test", "", "")
   169  				BindEnv("test")
   170  				viper.BindPFlags(fs)
   171  
   172  				fmt.Println(fullPath)
   173  			},
   174  			setupArgs: func() args {
   175  				return args{
   176  					dirName: dirName,
   177  				}
   178  			},
   179  			setupWant: func() want {
   180  				return want{
   181  					allSettings:        map[string]interface{}{"test": `"1"`},
   182  					allSettingsChecker: DeepEquals,
   183  					err:                nil,
   184  					errChecker:         Equals,
   185  				}
   186  			},
   187  			postTestRun: func() {
   188  				os.RemoveAll(dirName)
   189  			},
   190  		},
   191  	}
   192  	for _, tt := range tests {
   193  		tt.preTestRun()
   194  		args := tt.setupArgs()
   195  		want := tt.setupWant()
   196  		m, err := ReadDirConfig(args.dirName)
   197  		c.Assert(err, want.errChecker, want.err, Commentf("Test Name: %s", tt.name))
   198  		err = MergeConfig(m)
   199  		c.Assert(err, IsNil)
   200  		c.Assert(viper.AllSettings(), want.allSettingsChecker, want.allSettings, Commentf("Test Name: %s", tt.name))
   201  		tt.postTestRun()
   202  	}
   203  }
   204  
   205  func (s *OptionSuite) TestWorkloadsEnabled(c *C) {
   206  	type testDefinition struct {
   207  		config   *DaemonConfig
   208  		expected bool
   209  	}
   210  
   211  	tests := []testDefinition{
   212  		{&DaemonConfig{Workloads: []string{}}, false},
   213  		{&DaemonConfig{Workloads: []string{"none"}}, false},
   214  		{&DaemonConfig{Workloads: []string{"none"}}, false},
   215  		{&DaemonConfig{Workloads: []string{"docker", "none"}}, false},
   216  		{&DaemonConfig{Workloads: []string{"docker"}}, true},
   217  		{&DaemonConfig{Workloads: []string{"docker", "crio"}}, true},
   218  		{&DaemonConfig{Workloads: []string{"docker", "nonefalse"}}, true},
   219  	}
   220  
   221  	for _, test := range tests {
   222  		if test.config.WorkloadsEnabled() != test.expected {
   223  			c.Errorf("%#v != %#v", test.config.Workloads, test.expected)
   224  		}
   225  	}
   226  }
   227  
   228  func (s *OptionSuite) TestBindEnv(c *C) {
   229  	optName1 := "foo-bar"
   230  	os.Setenv("LEGACY_FOO_BAR", "legacy")
   231  	os.Setenv(getEnvName(optName1), "new")
   232  	BindEnvWithLegacyEnvFallback(optName1, "LEGACY_FOO_BAR")
   233  	c.Assert(viper.GetString(optName1), Equals, "new")
   234  
   235  	optName2 := "bar-foo"
   236  	BindEnvWithLegacyEnvFallback(optName2, "LEGACY_FOO_BAR")
   237  	c.Assert(viper.GetString(optName2), Equals, "legacy")
   238  
   239  	viper.Reset()
   240  }
   241  
   242  func (s *OptionSuite) TestLocalAddressExclusion(c *C) {
   243  	d := &DaemonConfig{}
   244  	err := d.parseExcludedLocalAddresses([]string{"1.1.1.1/32", "3.3.3.0/24", "f00d::1/128"})
   245  	c.Assert(err, IsNil)
   246  
   247  	c.Assert(d.IsExcludedLocalAddress(net.ParseIP("1.1.1.1")), Equals, true)
   248  	c.Assert(d.IsExcludedLocalAddress(net.ParseIP("1.1.1.2")), Equals, false)
   249  	c.Assert(d.IsExcludedLocalAddress(net.ParseIP("3.3.3.1")), Equals, true)
   250  	c.Assert(d.IsExcludedLocalAddress(net.ParseIP("f00d::1")), Equals, true)
   251  	c.Assert(d.IsExcludedLocalAddress(net.ParseIP("f00d::2")), Equals, false)
   252  }