github.phpd.cn/cilium/cilium@v1.6.12/pkg/workloads/runtimes_test.go (about)

     1  // Copyright 2017-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 workloads
    18  
    19  import (
    20  	"reflect"
    21  	"sync"
    22  	"testing"
    23  
    24  	. "gopkg.in/check.v1"
    25  )
    26  
    27  // Hook up gocheck into the "go test" runner.
    28  func Test(t *testing.T) {
    29  	TestingT(t)
    30  }
    31  
    32  type WorkloadsTestSuite struct{}
    33  
    34  var _ = Suite(&WorkloadsTestSuite{})
    35  
    36  func (s *WorkloadsTestSuite) TestSetupWithoutStatusCheck(c *C) {
    37  	// backup registered workload since None will unregister them all
    38  	bakRegisteredWorkloads := map[WorkloadRuntimeType]workloadModule{}
    39  	for k, v := range registeredWorkloads {
    40  		bakRegisteredWorkloads[k] = v
    41  	}
    42  	defer func() {
    43  		registeredWorkloads = bakRegisteredWorkloads
    44  	}()
    45  
    46  	dockerOpts := map[string]string{
    47  		EpOpt:           "unix:///docker.sock",
    48  		DatapathModeOpt: "ipvlan",
    49  	}
    50  
    51  	type args struct {
    52  		containerRuntimes     []string
    53  		containerRuntimesOpts map[WorkloadRuntimeType]map[string]string
    54  	}
    55  	tests := []struct {
    56  		name    string
    57  		args    args
    58  		wantErr bool
    59  	}{
    60  		{
    61  			name: "Use Docker container runtime",
    62  			args: args{
    63  				containerRuntimes: []string{string(Docker)},
    64  				containerRuntimesOpts: map[WorkloadRuntimeType]map[string]string{
    65  					Docker: dockerOpts,
    66  				},
    67  			},
    68  			wantErr: false,
    69  		},
    70  	}
    71  	for _, tt := range tests {
    72  		if err := setup(nil, tt.args.containerRuntimes, tt.args.containerRuntimesOpts, true); (err != nil) != tt.wantErr {
    73  			c.Errorf("setup() for %s error = %v, wantErr %v", tt.name, err, tt.wantErr)
    74  		}
    75  		setupOnce = sync.Once{}
    76  	}
    77  
    78  	if !reflect.DeepEqual(getWorkload(Docker).getConfig(), dockerOpts) {
    79  		c.Errorf("setup() = %v, want %v", getWorkload(Docker).getConfig(), dockerOpts)
    80  	}
    81  
    82  	// Since None will unregister the backends we need to execute it on a
    83  	// different set of tests
    84  	tests = []struct {
    85  		name    string
    86  		args    args
    87  		wantErr bool
    88  	}{
    89  		{
    90  			name: "Do not use any container runtime",
    91  			args: args{
    92  				containerRuntimes: []string{string(ContainerD), string(None)},
    93  				containerRuntimesOpts: map[WorkloadRuntimeType]map[string]string{
    94  					Docker: {EpOpt: "unix:///foo.sock"},
    95  				},
    96  			},
    97  			wantErr: false,
    98  		},
    99  		{
   100  			name: "Do not use any container runtime (error)",
   101  			args: args{
   102  				containerRuntimes: []string{"does-not-exist", string(None)},
   103  			},
   104  			wantErr: true,
   105  		},
   106  	}
   107  	for _, tt := range tests {
   108  		if err := setup(nil, tt.args.containerRuntimes, tt.args.containerRuntimesOpts, true); (err != nil) != tt.wantErr {
   109  			c.Errorf("setup() for %s error = %v, wantErr %v", tt.name, err, tt.wantErr)
   110  		}
   111  		setupOnce = sync.Once{}
   112  	}
   113  }
   114  
   115  func (s *WorkloadsTestSuite) Test_parseRuntimeType(c *C) {
   116  	type args struct {
   117  		str string
   118  	}
   119  	tests := []struct {
   120  		name    string
   121  		args    args
   122  		want    WorkloadRuntimeType
   123  		wantErr bool
   124  	}{
   125  		{
   126  			name: "containerd",
   127  			args: args{
   128  				str: "containerd",
   129  			},
   130  			want:    ContainerD,
   131  			wantErr: false,
   132  		},
   133  		{
   134  			name: "containerD",
   135  			args: args{
   136  				str: "containerD",
   137  			},
   138  			want:    ContainerD,
   139  			wantErr: false,
   140  		},
   141  		{
   142  			name: "docker",
   143  			args: args{
   144  				str: "docker",
   145  			},
   146  			want:    Docker,
   147  			wantErr: false,
   148  		},
   149  		{
   150  			name: "cri",
   151  			args: args{
   152  				str: "cri",
   153  			},
   154  			want:    None,
   155  			wantErr: true,
   156  		},
   157  		{
   158  			name: "none",
   159  			args: args{
   160  				str: "none",
   161  			},
   162  			want:    None,
   163  			wantErr: false,
   164  		},
   165  		{
   166  			name: "auto",
   167  			args: args{
   168  				str: "auto",
   169  			},
   170  			want:    Auto,
   171  			wantErr: false,
   172  		},
   173  	}
   174  	for _, tt := range tests {
   175  		got, err := parseRuntimeType(tt.args.str)
   176  		if (err != nil) != tt.wantErr {
   177  			c.Errorf("parseRuntimeType() for %s error = %v, wantErr %v", tt.name, err, tt.wantErr)
   178  			return
   179  		}
   180  		if got != tt.want {
   181  			c.Errorf("parseRuntimeType() for %s = %v, want %v", tt.name, got, tt.want)
   182  		}
   183  	}
   184  }
   185  
   186  func (s *WorkloadsTestSuite) Test_unregisterWorkloads(c *C) {
   187  	// backup registered workloads since they will unregistered
   188  	bakRegisteredWorkloads := map[WorkloadRuntimeType]workloadModule{}
   189  	for k, v := range registeredWorkloads {
   190  		bakRegisteredWorkloads[k] = v
   191  	}
   192  	defer func() {
   193  		registeredWorkloads = bakRegisteredWorkloads
   194  	}()
   195  
   196  	if len(registeredWorkloads) == 0 {
   197  		c.Errorf("number of registeredWorkloads should not be 0")
   198  	}
   199  	unregisterWorkloads()
   200  	if len(registeredWorkloads) != 0 {
   201  		c.Errorf("number of registeredWorkloads should be 0")
   202  	}
   203  }
   204  
   205  func (s *WorkloadsTestSuite) Test_getWorkload(c *C) {
   206  	type args struct {
   207  		name WorkloadRuntimeType
   208  	}
   209  	tests := []struct {
   210  		name string
   211  		args args
   212  		want workloadModule
   213  	}{
   214  		{
   215  			name: "containerD",
   216  			args: args{
   217  				name: ContainerD,
   218  			},
   219  			want: registeredWorkloads[ContainerD],
   220  		},
   221  	}
   222  	for _, tt := range tests {
   223  		if got := getWorkload(tt.args.name); !reflect.DeepEqual(got, tt.want) {
   224  			c.Errorf("getWorkload() fot %s = %v, want %v", tt.name, got, tt.want)
   225  		}
   226  	}
   227  }
   228  
   229  func (s *WorkloadsTestSuite) TestGetRuntimeDefaultOpt(c *C) {
   230  	type args struct {
   231  		crt WorkloadRuntimeType
   232  		opt string
   233  	}
   234  	tests := []struct {
   235  		name string
   236  		args args
   237  		want string
   238  	}{
   239  		{
   240  			name: "containerd",
   241  			args: args{
   242  				crt: ContainerD,
   243  				opt: EpOpt + "=" + containerDInstance.opts[EpOpt].value,
   244  			},
   245  		},
   246  		{
   247  			name: "docker",
   248  			args: args{
   249  				crt: Docker,
   250  				opt: EpOpt + "=" + dockerInstance.opts[EpOpt].value,
   251  			},
   252  		},
   253  	}
   254  	for _, tt := range tests {
   255  		if got := GetRuntimeDefaultOpt(tt.args.crt, tt.args.opt); got != tt.want {
   256  			c.Errorf("GetRuntimeDefaultOpt() for %s = %v, want %v", tt.name, got, tt.want)
   257  		}
   258  	}
   259  }
   260  
   261  func (s *WorkloadsTestSuite) TestGetRuntimeOptions(c *C) {
   262  	tests := []struct {
   263  		name string
   264  		want string
   265  	}{
   266  		{
   267  			name: "default options",
   268  			want: EpOpt + "=" + containerDInstance.opts[EpOpt].value + "," +
   269  				EpOpt + "=" + criOInstance.opts[EpOpt].value + "," +
   270  				DatapathModeOpt + "=" + dockerInstance.opts[DatapathModeOpt].value + "," +
   271  				EpOpt + "=" + dockerInstance.opts[EpOpt].value,
   272  		},
   273  	}
   274  	for _, tt := range tests {
   275  		if got := GetRuntimeOptions(); got != tt.want {
   276  			c.Errorf("GetRuntimeOptions() for %s = %v, want %v", tt.name, got, tt.want)
   277  		}
   278  	}
   279  }
   280  
   281  func (s *WorkloadsTestSuite) TestGetDefaultEPOptsStringWithPrefix(c *C) {
   282  	type args struct {
   283  		prefix string
   284  	}
   285  	tests := []struct {
   286  		name string
   287  		args args
   288  		want string
   289  	}{
   290  		{
   291  			name: "default ep options",
   292  			args: args{
   293  				prefix: "--container-runtime-endpoint=",
   294  			},
   295  			want: `--container-runtime-endpoint=` + string(ContainerD) + "=" + containerDInstance.opts[EpOpt].value + ", " +
   296  				`--container-runtime-endpoint=` + string(CRIO) + "=" + criOInstance.opts[EpOpt].value + ", " +
   297  				`--container-runtime-endpoint=` + string(Docker) + "=" + dockerInstance.opts[EpOpt].value,
   298  		},
   299  	}
   300  	for _, tt := range tests {
   301  		if got := GetDefaultEPOptsStringWithPrefix(tt.args.prefix); got != tt.want {
   302  			c.Errorf("GetDefaultEPOptsStringWithPrefix() for %s = %v, want %v", tt.name, got, tt.want)
   303  		}
   304  	}
   305  }