github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/gadget-collection/gadgets/advise/seccomp/gadget_test.go (about)

     1  // Copyright 2019-2021 The Inspektor Gadget 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  package seccomp
    16  
    17  import (
    18  	"testing"
    19  
    20  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    21  	seccompprofile "sigs.k8s.io/security-profiles-operator/api/seccompprofile/v1beta1"
    22  )
    23  
    24  func TestGetSeccompProfileNextName(t *testing.T) {
    25  	// Empty profile list
    26  	profileList := []seccompprofile.SeccompProfile{}
    27  	podName := "podname"
    28  	expectedNextName := "podname"
    29  	nextName := getSeccompProfileNextName(profileList, podName)
    30  	if nextName != expectedNextName {
    31  		t.Fatalf("Invalid computation of next name '%s'. Expecting '%s' from an empty profile list",
    32  			nextName, expectedNextName)
    33  	}
    34  
    35  	// There do not exist profiles with podname or podname-X as name.
    36  	profileList = []seccompprofile.SeccompProfile{
    37  		{
    38  			ObjectMeta: metav1.ObjectMeta{
    39  				Name: "another-name",
    40  			},
    41  		},
    42  		{
    43  			ObjectMeta: metav1.ObjectMeta{
    44  				Name: "prefix-podname",
    45  			},
    46  		},
    47  	}
    48  	podName = "podname"
    49  	expectedNextName = "podname"
    50  	nextName = getSeccompProfileNextName(profileList, podName)
    51  	if nextName != expectedNextName {
    52  		t.Fatalf("Invalid computation of next name '%s'. Expecting '%s' from the given profile list",
    53  			nextName, expectedNextName)
    54  	}
    55  
    56  	// There exist a profile with the podname but no one with podname-X.
    57  	profileList = []seccompprofile.SeccompProfile{
    58  		{
    59  			ObjectMeta: metav1.ObjectMeta{
    60  				Name: "another-name",
    61  			},
    62  		},
    63  		{
    64  			ObjectMeta: metav1.ObjectMeta{
    65  				Name: "prefix-podname",
    66  			},
    67  		},
    68  		{
    69  			ObjectMeta: metav1.ObjectMeta{
    70  				Name: "podname",
    71  			},
    72  		},
    73  	}
    74  	podName = "podname"
    75  	expectedNextName = "podname-2"
    76  	nextName = getSeccompProfileNextName(profileList, podName)
    77  	if nextName != expectedNextName {
    78  		t.Fatalf("Invalid computation of next name '%s'. Expecting '%s' from the given profile list",
    79  			nextName, expectedNextName)
    80  	}
    81  
    82  	// There exist a profile with the podname and another with podname-X.
    83  	profileList = []seccompprofile.SeccompProfile{
    84  		{
    85  			ObjectMeta: metav1.ObjectMeta{
    86  				Name: "another-name",
    87  			},
    88  		},
    89  		{
    90  			ObjectMeta: metav1.ObjectMeta{
    91  				Name: "prefix-podname",
    92  			},
    93  		},
    94  		{
    95  			ObjectMeta: metav1.ObjectMeta{
    96  				Name: "podname",
    97  			},
    98  		},
    99  		{
   100  			ObjectMeta: metav1.ObjectMeta{
   101  				Name: "podname-2",
   102  			},
   103  		},
   104  	}
   105  	podName = "podname"
   106  	expectedNextName = "podname-3"
   107  	nextName = getSeccompProfileNextName(profileList, podName)
   108  	if nextName != expectedNextName {
   109  		t.Fatalf("Invalid computation of next name '%s'. Expecting '%s' from the given profile list",
   110  			nextName, expectedNextName)
   111  	}
   112  
   113  	// There exist at least one profile with podname-X.
   114  	profileList = []seccompprofile.SeccompProfile{
   115  		{
   116  			ObjectMeta: metav1.ObjectMeta{
   117  				Name: "another-name",
   118  			},
   119  		},
   120  		{
   121  			ObjectMeta: metav1.ObjectMeta{
   122  				Name: "prefix-podname",
   123  			},
   124  		},
   125  		{
   126  			ObjectMeta: metav1.ObjectMeta{
   127  				Name: "podname-10",
   128  			},
   129  		},
   130  	}
   131  	podName = "podname"
   132  	expectedNextName = "podname-11"
   133  	nextName = getSeccompProfileNextName(profileList, podName)
   134  	if nextName != expectedNextName {
   135  		t.Fatalf("Invalid computation of next name '%s'. Expecting '%s' from the given profile list",
   136  			nextName, expectedNextName)
   137  	}
   138  
   139  	// There exist multiple profiles with podname-X.
   140  	profileList = []seccompprofile.SeccompProfile{
   141  		{
   142  			ObjectMeta: metav1.ObjectMeta{
   143  				Name: "another-name",
   144  			},
   145  		},
   146  		{
   147  			ObjectMeta: metav1.ObjectMeta{
   148  				Name: "prefix-podname",
   149  			},
   150  		},
   151  		{
   152  			ObjectMeta: metav1.ObjectMeta{
   153  				Name: "podname-2",
   154  			},
   155  		},
   156  		{
   157  			ObjectMeta: metav1.ObjectMeta{
   158  				Name: "podname-7",
   159  			},
   160  		},
   161  	}
   162  	podName = "podname"
   163  	expectedNextName = "podname-8"
   164  	nextName = getSeccompProfileNextName(profileList, podName)
   165  	if nextName != expectedNextName {
   166  		t.Fatalf("Invalid computation of next name '%s'. Expecting '%s' from the given profile list",
   167  			nextName, expectedNextName)
   168  	}
   169  
   170  	// Ignoring profiles with sintax podname-X where X is not a number.
   171  	profileList = []seccompprofile.SeccompProfile{
   172  		{
   173  			ObjectMeta: metav1.ObjectMeta{
   174  				Name: "another-name",
   175  			},
   176  		},
   177  		{
   178  			ObjectMeta: metav1.ObjectMeta{
   179  				Name: "podname-xa4b5",
   180  			},
   181  		},
   182  	}
   183  	podName = "podname"
   184  	expectedNextName = "podname"
   185  	nextName = getSeccompProfileNextName(profileList, podName)
   186  	if nextName != expectedNextName {
   187  		t.Fatalf("Invalid computation of next name '%s'. Expecting '%s' from the given profile list",
   188  			nextName, expectedNextName)
   189  	}
   190  
   191  	// Another case where function must ignore the profiles with
   192  	// syntax podname-X where X is not a number.
   193  	profileList = []seccompprofile.SeccompProfile{
   194  		{
   195  			ObjectMeta: metav1.ObjectMeta{
   196  				Name: "another-name",
   197  			},
   198  		},
   199  		{
   200  			ObjectMeta: metav1.ObjectMeta{
   201  				Name: "podname-xa4b5",
   202  			},
   203  		},
   204  		{
   205  			ObjectMeta: metav1.ObjectMeta{
   206  				Name: "podname-5",
   207  			},
   208  		},
   209  	}
   210  	podName = "podname"
   211  	expectedNextName = "podname-6"
   212  	nextName = getSeccompProfileNextName(profileList, podName)
   213  	if nextName != expectedNextName {
   214  		t.Fatalf("Invalid computation of next name '%s'. Expecting '%s' from the given profile list",
   215  			nextName, expectedNextName)
   216  	}
   217  }