github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/oci/oci_test.go (about)

     1  // Copyright 2023 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 oci
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/assert"
    21  	"github.com/stretchr/testify/require"
    22  )
    23  
    24  func TestSplitIGDomain(t *testing.T) {
    25  	t.Parallel()
    26  
    27  	type testDefinition struct {
    28  		name              string
    29  		expectedDomain    string
    30  		expectedRemainder string
    31  	}
    32  
    33  	tests := map[string]testDefinition{
    34  		"no_domain_and_remainder": {
    35  			name:              "trace_exec",
    36  			expectedDomain:    defaultDomain,
    37  			expectedRemainder: officialRepoPrefix + "trace_exec",
    38  		},
    39  		"no_domain_and_remainder_with_tag": {
    40  			name:              "trace_exec:v0.42.0",
    41  			expectedDomain:    defaultDomain,
    42  			expectedRemainder: officialRepoPrefix + "trace_exec:v0.42.0",
    43  		},
    44  		"no_domain": {
    45  			name:              "xyz/gadget/trace_exec",
    46  			expectedDomain:    defaultDomain,
    47  			expectedRemainder: "xyz/gadget/trace_exec",
    48  		},
    49  		"full": {
    50  			name:              "foobar.baz/xyz/gadget/trace_exec",
    51  			expectedDomain:    "foobar.baz",
    52  			expectedRemainder: "xyz/gadget/trace_exec",
    53  		},
    54  		"full_with_port": {
    55  			name:              "foobar.baz:443/xyz/gadget/trace_exec",
    56  			expectedDomain:    "foobar.baz:443",
    57  			expectedRemainder: "xyz/gadget/trace_exec",
    58  		},
    59  		"full_with_port_with_tag": {
    60  			name:              "foobar.baz:443/xyz/gadget/trace_exec:v0.42.0",
    61  			expectedDomain:    "foobar.baz:443",
    62  			expectedRemainder: "xyz/gadget/trace_exec:v0.42.0",
    63  		},
    64  		"localhost": {
    65  			name:              "localhost/trace_exec",
    66  			expectedDomain:    "localhost",
    67  			expectedRemainder: "trace_exec",
    68  		},
    69  		"localhost_with_long_remainder": {
    70  			name:              "localhost/a/b/c/e/d/g/r/trace_exec",
    71  			expectedDomain:    "localhost",
    72  			expectedRemainder: "a/b/c/e/d/g/r/trace_exec",
    73  		},
    74  		"localhost_with_port": {
    75  			name:              "localhost:5000/trace_exec",
    76  			expectedDomain:    "localhost:5000",
    77  			expectedRemainder: "trace_exec",
    78  		},
    79  		"localhost_with_port_with_tag": {
    80  			name:              "localhost:5000/trace_exec:v1.0.3",
    81  			expectedDomain:    "localhost:5000",
    82  			expectedRemainder: "trace_exec:v1.0.3",
    83  		},
    84  	}
    85  
    86  	for name, test := range tests {
    87  		test := test
    88  		t.Run(name, func(t *testing.T) {
    89  			t.Parallel()
    90  
    91  			actualDomain, actualRemainder := splitIGDomain(test.name)
    92  			assert.Equal(t, test.expectedDomain, actualDomain)
    93  			assert.Equal(t, test.expectedRemainder, actualRemainder)
    94  		})
    95  	}
    96  }
    97  
    98  func TestNormalizeImage(t *testing.T) {
    99  	t.Parallel()
   100  
   101  	type testDefinition struct {
   102  		image         string
   103  		imageExpected string
   104  		err           bool
   105  	}
   106  
   107  	tests := map[string]testDefinition{
   108  		"empty": {
   109  			image: "",
   110  			err:   true,
   111  		},
   112  		"badtag": {
   113  			image: "inspektor-gadget/ig:~½¬",
   114  			err:   true,
   115  		},
   116  		"image": {
   117  			image:         "ig",
   118  			imageExpected: "ghcr.io/inspektor-gadget/gadget/ig:latest",
   119  		},
   120  		"image_and_tag": {
   121  			image:         "ig:latest",
   122  			imageExpected: "ghcr.io/inspektor-gadget/gadget/ig:latest",
   123  		},
   124  		"image_and_tag_2": {
   125  			image:         "ig:latestttt",
   126  			imageExpected: "ghcr.io/inspektor-gadget/gadget/ig:latestttt",
   127  		},
   128  		"host_image_and_tag": {
   129  			image:         "inspektor-gadget/ig:foobar",
   130  			imageExpected: "ghcr.io/inspektor-gadget/ig:foobar",
   131  		},
   132  		"schema_host_image_and_tag": {
   133  			image: "https://inspektor-gadget/ig:baz",
   134  			err:   true,
   135  		},
   136  		"host_port_image_and_tag": {
   137  			image:         "ghcr.io:443/inspektor-gadget/ig:baz",
   138  			imageExpected: "ghcr.io:443/inspektor-gadget/ig:baz",
   139  		},
   140  		"schema_host_port_image_and_tag": {
   141  			image: "https://ghcr.io:443/inspektor-gadget/ig:latest",
   142  			err:   true,
   143  		},
   144  	}
   145  
   146  	for name, test := range tests {
   147  		test := test
   148  		t.Run(name, func(t *testing.T) {
   149  			t.Parallel()
   150  
   151  			imageRef, err := normalizeImageName(test.image)
   152  			if test.err {
   153  				require.Error(t, err)
   154  				return
   155  			}
   156  
   157  			require.NoError(t, err)
   158  			require.Equal(t, test.imageExpected, imageRef.String())
   159  		})
   160  	}
   161  }
   162  
   163  func TestGetHostString(t *testing.T) {
   164  	t.Parallel()
   165  
   166  	type testDefinition struct {
   167  		image string
   168  		host  string
   169  		err   bool
   170  	}
   171  
   172  	tests := map[string]testDefinition{
   173  		"empty": {
   174  			image: "",
   175  			err:   true,
   176  		},
   177  		"badtag": {
   178  			image: "inspektor-gadget/ig:~½¬",
   179  			err:   true,
   180  		},
   181  		"image": {
   182  			image: "ig",
   183  			host:  "",
   184  		},
   185  		"host": {
   186  			image: "ghcr.io",
   187  			host:  "",
   188  		},
   189  		"host_image_and_tag": {
   190  			image: "inspektor-gadget/ig:latest",
   191  			host:  "inspektor-gadget",
   192  		},
   193  		"schema_host_image_and_tag": {
   194  			image: "https://inspektor-gadget/ig:latest",
   195  			err:   true,
   196  		},
   197  		"host_port_image_and_tag": {
   198  			image: "ghcr.io:443/inspektor-gadget/ig:latest",
   199  			host:  "ghcr.io:443",
   200  		},
   201  		"schema_host_port_image_and_tag": {
   202  			image: "https://ghcr.io:443/inspektor-gadget/ig:latest",
   203  			err:   true,
   204  		},
   205  	}
   206  
   207  	for name, test := range tests {
   208  		test := test
   209  		t.Run(name, func(t *testing.T) {
   210  			t.Parallel()
   211  
   212  			host, err := getHostString(test.image)
   213  			if test.err {
   214  				require.Error(t, err)
   215  				return
   216  			}
   217  
   218  			require.NoError(t, err)
   219  			require.Equal(t, test.host, host)
   220  		})
   221  	}
   222  }