...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/webhook/inject/command_test.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/webhook/inject

     1  // Copyright 2021 Chaos Mesh Authors.
     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  
    16  package inject
    17  
    18  import (
    19  	"testing"
    20  
    21  	. "github.com/onsi/gomega"
    22  )
    23  
    24  func TestIsCommonScripts(t *testing.T) {
    25  	g := NewGomegaWithT(t)
    26  
    27  	type TestCase struct {
    28  		name          string
    29  		cmd           string
    30  		expectedValue bool
    31  	}
    32  
    33  	tcs := []TestCase{
    34  		{
    35  			name:          "command scripts: bash",
    36  			cmd:           "bash",
    37  			expectedValue: true,
    38  		},
    39  		{
    40  			name:          "command scripts: bash -c echo 1",
    41  			cmd:           "bash -c echo 1",
    42  			expectedValue: true,
    43  		},
    44  		{
    45  			name:          "command scripts: sh",
    46  			cmd:           "sh",
    47  			expectedValue: true,
    48  		},
    49  		{
    50  			name:          "command scripts: /bin/sh",
    51  			cmd:           "/bin/sh",
    52  			expectedValue: true,
    53  		},
    54  		{
    55  			name:          "command scripts: /bin/bash",
    56  			cmd:           "/bin/bash",
    57  			expectedValue: true,
    58  		},
    59  		{
    60  			name:          "command scripts: /usr/bin/bash",
    61  			cmd:           "/usr/bin/bash",
    62  			expectedValue: true,
    63  		},
    64  		{
    65  			name:          "not command scripts: /usr/bin/echo",
    66  			cmd:           "/usr/bin/echo",
    67  			expectedValue: false,
    68  		},
    69  		{
    70  			name:          "not command scripts: /chaos-mesh",
    71  			cmd:           "/chaos-mesh",
    72  			expectedValue: false,
    73  		},
    74  	}
    75  
    76  	for _, tc := range tcs {
    77  		g.Expect(isCommonScripts(tc.cmd)).To(Equal(tc.expectedValue), tc.name)
    78  	}
    79  }
    80  
    81  func TestIsShellScripts(t *testing.T) {
    82  	g := NewGomegaWithT(t)
    83  
    84  	type TestCase struct {
    85  		name          string
    86  		cmd           string
    87  		expectedValue bool
    88  	}
    89  
    90  	tcs := []TestCase{
    91  		{
    92  			name:          "bash",
    93  			cmd:           "bash",
    94  			expectedValue: true,
    95  		},
    96  		{
    97  			name:          "bash -c echo 1",
    98  			cmd:           "bash -c echo 1",
    99  			expectedValue: true,
   100  		},
   101  		{
   102  			name:          "/usr/bin/bash",
   103  			cmd:           "/usr/bin/bash",
   104  			expectedValue: true,
   105  		},
   106  		{
   107  			name:          "/usr/bin/echo",
   108  			cmd:           "/usr/bin/echo",
   109  			expectedValue: false,
   110  		},
   111  		{
   112  			name:          "/chaos-mesh",
   113  			cmd:           "/chaos-mesh",
   114  			expectedValue: false,
   115  		},
   116  	}
   117  
   118  	for _, tc := range tcs {
   119  		g.Expect(isShellScripts(tc.cmd)).To(Equal(tc.expectedValue), tc.name)
   120  	}
   121  }
   122  
   123  func TestIsPythonScripts(t *testing.T) {
   124  	g := NewGomegaWithT(t)
   125  
   126  	type TestCase struct {
   127  		name          string
   128  		cmd           string
   129  		expectedValue bool
   130  	}
   131  
   132  	tcs := []TestCase{
   133  		{
   134  			name:          "python",
   135  			cmd:           "python",
   136  			expectedValue: true,
   137  		},
   138  		{
   139  			name:          "bash -c echo 1",
   140  			cmd:           "bash -c echo 1",
   141  			expectedValue: false,
   142  		},
   143  		{
   144  			name:          "/bin/python",
   145  			cmd:           "/bin/python",
   146  			expectedValue: true,
   147  		},
   148  		{
   149  			name:          "/usr/bin/bash",
   150  			cmd:           "/usr/bin/bash",
   151  			expectedValue: false,
   152  		},
   153  		{
   154  			name:          "/usr/bin/echo",
   155  			cmd:           "/usr/bin/echo",
   156  			expectedValue: false,
   157  		},
   158  		{
   159  			name:          "/chaos-mesh",
   160  			cmd:           "/chaos-mesh",
   161  			expectedValue: false,
   162  		},
   163  	}
   164  
   165  	for _, tc := range tcs {
   166  		g.Expect(isPythonScripts(tc.cmd)).To(Equal(tc.expectedValue), tc.name)
   167  	}
   168  }
   169  
   170  func TestMergeCommandsAction(t *testing.T) {
   171  	g := NewGomegaWithT(t)
   172  
   173  	type TestCase struct {
   174  		name          string
   175  		commands      []string
   176  		expectedValue string
   177  	}
   178  
   179  	tcs := []TestCase{
   180  		{
   181  			name: "scripts files",
   182  			commands: []string{
   183  				"/bin/sh",
   184  				"start_chaos_mesh.sh",
   185  			},
   186  			expectedValue: "/bin/sh start_chaos_mesh.sh\n",
   187  		},
   188  		{
   189  			name: "common scripts",
   190  			commands: []string{
   191  				"bash",
   192  				"-ec",
   193  				"echo $HOSENAME\n /start_chaos_mesh.sh -s t1 \n -v t2",
   194  			},
   195  			expectedValue: "echo $HOSENAME\n /start_chaos_mesh.sh -s t1 \n -v t2\n",
   196  		},
   197  		{
   198  			name: "common start scripts",
   199  			commands: []string{
   200  				"/chaos-mesh",
   201  				"--c t",
   202  				"--v 2",
   203  				"--log stdout",
   204  			},
   205  			expectedValue: "/chaos-mesh --c t --v 2 --log stdout\n",
   206  		},
   207  		{
   208  			name: "one line",
   209  			commands: []string{
   210  				"/chaos-mesh --c t --v 2 --log stdout",
   211  			},
   212  			expectedValue: "/chaos-mesh --c t --v 2 --log stdout\n",
   213  		},
   214  	}
   215  
   216  	for _, tc := range tcs {
   217  		g.Expect(mergeCommandsAction(tc.commands)).To(Equal(tc.expectedValue), tc.name)
   218  	}
   219  }
   220  
   221  func TestMergeOriginCommandsAndArgs(t *testing.T) {
   222  	g := NewGomegaWithT(t)
   223  
   224  	type TestCase struct {
   225  		name          string
   226  		commands      []string
   227  		args          []string
   228  		expectedValue string
   229  	}
   230  
   231  	tcs := []TestCase{
   232  		{
   233  			name: "only commands",
   234  			commands: []string{
   235  				"bash",
   236  				"-ec",
   237  				"echo $HOSENAME\n /start_chaos_mesh.sh -s t1 \n -v t2",
   238  			},
   239  			expectedValue: "echo $HOSENAME\n /start_chaos_mesh.sh -s t1 \n -v t2\n",
   240  		},
   241  		{
   242  			name: "only args",
   243  			args: []string{
   244  				"bash",
   245  				"-ec",
   246  				"echo $HOSENAME\n /start_chaos_mesh.sh -s t1 \n -v t2",
   247  			},
   248  			expectedValue: "echo $HOSENAME\n /start_chaos_mesh.sh -s t1 \n -v t2\n",
   249  		},
   250  		{
   251  			name: "commands and args",
   252  			commands: []string{
   253  				"/chaos-mesh",
   254  			},
   255  			args: []string{
   256  				"--c t",
   257  				"--v 2",
   258  				"--log stdout",
   259  			},
   260  			expectedValue: "/chaos-mesh --c t --v 2 --log stdout\n",
   261  		},
   262  	}
   263  
   264  	for _, tc := range tcs {
   265  		g.Expect(mergeOriginCommandsAndArgs(tc.commands, tc.args)).To(Equal(tc.expectedValue), tc.name)
   266  	}
   267  }
   268  
   269  func TestMergeCommands(t *testing.T) {
   270  	g := NewGomegaWithT(t)
   271  
   272  	type TestCase struct {
   273  		name          string
   274  		inject        []string
   275  		origin        []string
   276  		args          []string
   277  		expectedValue []string
   278  	}
   279  
   280  	tcs := []TestCase{
   281  		{
   282  			name: "scripts file",
   283  			inject: []string{
   284  				"/check.sh -v 1",
   285  			},
   286  			origin: []string{
   287  				"/bin/sh",
   288  				"/start.sh",
   289  			},
   290  			expectedValue: []string{
   291  				"/bin/sh",
   292  				"-ec",
   293  				"/check.sh -v 1\n/bin/sh /start.sh\n",
   294  			},
   295  		},
   296  		{
   297  			name: "common scripts",
   298  			inject: []string{
   299  				"/check.sh -v 1",
   300  			},
   301  			origin: []string{
   302  				"bash",
   303  				"-c",
   304  				"set -ex\n[[ `hostname` =~ -([0-9]+)$ ]] || exit 1\n/tiflash server --config-file /data/config.toml",
   305  			},
   306  			expectedValue: []string{
   307  				"/bin/sh",
   308  				"-ec",
   309  				"/check.sh -v 1\nset -ex\n[[ `hostname` =~ -([0-9]+)$ ]] || exit 1\n/tiflash server --config-file /data/config.toml\n",
   310  			},
   311  		},
   312  	}
   313  
   314  	for _, tc := range tcs {
   315  		g.Expect(MergeCommands(tc.inject, tc.origin, tc.args)).To(Equal(tc.expectedValue), tc.name)
   316  	}
   317  }
   318