github.com/rkt/rkt@v1.30.1-0.20200224141603-171c416fac02/common/cgroup_util_test.go (about)

     1  // Copyright 2015 The rkt 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  //+build linux
    16  
    17  package common
    18  
    19  import (
    20  	"testing"
    21  )
    22  
    23  func TestCgEscape(t *testing.T) {
    24  	tests := []struct {
    25  		input  string
    26  		output string
    27  	}{
    28  		{
    29  			input:  "",
    30  			output: "",
    31  		},
    32  		{
    33  			input:  "_abc",
    34  			output: "__abc",
    35  		},
    36  		{
    37  			input:  ".abc",
    38  			output: "_.abc",
    39  		},
    40  		{
    41  			input:  "notify_on_release",
    42  			output: "_notify_on_release",
    43  		},
    44  		{
    45  			input:  "tasks",
    46  			output: "_tasks",
    47  		},
    48  		{
    49  			input:  "cgroup.abc.slice",
    50  			output: "_cgroup.abc.slice",
    51  		},
    52  		{
    53  			input:  "abc.mount",
    54  			output: "abc.mount",
    55  		},
    56  		{
    57  			input:  "a/bc.mount",
    58  			output: "_a/bc.mount",
    59  		},
    60  	}
    61  
    62  	for i, tt := range tests {
    63  		o := cgEscape(tt.input)
    64  		if o != tt.output {
    65  			t.Errorf("#%d: expected `%v` got `%v`", i, tt.output, o)
    66  		}
    67  	}
    68  }
    69  
    70  func TestSliceToPath(t *testing.T) {
    71  	tests := []struct {
    72  		input  string
    73  		output string
    74  		werr   bool
    75  	}{
    76  		{
    77  			input:  "",
    78  			output: "",
    79  			werr:   true,
    80  		},
    81  		{
    82  			input:  "abc.service",
    83  			output: "",
    84  			werr:   true,
    85  		},
    86  		{
    87  			input:  "ab/c.slice",
    88  			output: "",
    89  			werr:   true,
    90  		},
    91  		{
    92  			input:  "-abc.slice",
    93  			output: "",
    94  			werr:   true,
    95  		},
    96  		{
    97  			input:  "ab--c.slice",
    98  			output: "",
    99  			werr:   true,
   100  		},
   101  		{
   102  			input:  "abc-.slice",
   103  			output: "",
   104  			werr:   true,
   105  		},
   106  		{
   107  			input:  "abc.slice",
   108  			output: "abc.slice",
   109  			werr:   false,
   110  		},
   111  		{
   112  			input:  "foo-bar.slice",
   113  			output: "foo.slice/foo-bar.slice",
   114  			werr:   false,
   115  		},
   116  		{
   117  			input:  "foo-bar-baz.slice",
   118  			output: "foo.slice/foo-bar.slice/foo-bar-baz.slice",
   119  			werr:   false,
   120  		},
   121  	}
   122  
   123  	for i, tt := range tests {
   124  		o, err := SliceToPath(tt.input)
   125  		gerr := (err != nil)
   126  		if o != tt.output {
   127  			t.Errorf("#%d: expected `%v` got `%v`", i, tt.output, o)
   128  		}
   129  		if gerr != tt.werr {
   130  			t.Errorf("#%d: gerr=%t, want %t (err=%v)", i, gerr, tt.werr, err)
   131  		}
   132  	}
   133  }