github.com/Mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/pkg/imagetranslation/translator_test.go (about)

     1  /*
     2  Copyright 2017 Mirantis
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package imagetranslation
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"github.com/Mirantis/virtlet/pkg/api/virtlet.k8s/v1"
    24  )
    25  
    26  // TestTranslations tests how image names are translated with various translation rules
    27  func TestTranslations(t *testing.T) {
    28  	configs := map[string]v1.ImageTranslation{
    29  		"config1": {
    30  			Rules: []v1.TranslationRule{
    31  				{
    32  					Regex: `^image(\d+)`,
    33  					URL:   "http://example.net/image_$1.qcow2",
    34  				},
    35  				{
    36  					Regex: `image(\d+)`,
    37  					URL:   "http://example.net/alt_$1.qcow2",
    38  				},
    39  				{
    40  					Name: "image1",
    41  					URL:  "https://example.net/base.qcow2",
    42  				},
    43  			},
    44  		},
    45  		"config2": {
    46  			Prefix: "prod",
    47  			Rules: []v1.TranslationRule{
    48  				{
    49  					Regex: `^linux/(\d+\.\d+)`,
    50  					URL:   "http://acme.org/linux_$1.qcow2",
    51  				},
    52  				{
    53  					Name: "linux/1",
    54  					URL:  "https://acme.org/linux.qcow2",
    55  				},
    56  			},
    57  		},
    58  	}
    59  
    60  	for _, tc := range []struct {
    61  		name        string
    62  		allowRegexp bool
    63  		imageName   string
    64  		expectedURL string
    65  	}{
    66  		{
    67  			name:        "strict translation",
    68  			allowRegexp: false,
    69  			imageName:   "image1",
    70  			expectedURL: "https://example.net/base.qcow2",
    71  		},
    72  		{
    73  			name:        "negative strict translation",
    74  			allowRegexp: false,
    75  			imageName:   "image2",
    76  			expectedURL: "image2",
    77  		},
    78  		{
    79  			name:        "strict translation precedes regexps",
    80  			allowRegexp: true,
    81  			imageName:   "image1",
    82  			expectedURL: "https://example.net/base.qcow2",
    83  		},
    84  		{
    85  			name:        "regexp translation",
    86  			allowRegexp: true,
    87  			imageName:   "image2",
    88  			expectedURL: "http://example.net/image_2.qcow2",
    89  		},
    90  		{
    91  			name:        "negative regexp translation",
    92  			allowRegexp: true,
    93  			imageName:   "image",
    94  			expectedURL: "image",
    95  		},
    96  		{
    97  			name:        "translation with prefix",
    98  			allowRegexp: false,
    99  			imageName:   "prod/linux/1",
   100  			expectedURL: "https://acme.org/linux.qcow2",
   101  		},
   102  		{
   103  			name:        "regexp translation with prefix",
   104  			allowRegexp: true,
   105  			imageName:   "prod/linux/2.11",
   106  			expectedURL: "http://acme.org/linux_2.11.qcow2",
   107  		},
   108  		{
   109  			name:        "negative translation with prefix",
   110  			allowRegexp: false,
   111  			imageName:   "prod/image1",
   112  			expectedURL: "prod/image1",
   113  		},
   114  		{
   115  			name:        "empty string translation",
   116  			allowRegexp: true,
   117  			imageName:   "",
   118  			expectedURL: "",
   119  		},
   120  		{
   121  			name:        "misleading translation with prefix",
   122  			allowRegexp: true,
   123  			imageName:   "prod/image1",
   124  			expectedURL: "http://example.net/alt_1.qcow2",
   125  		},
   126  	} {
   127  		t.Run(tc.name, func(t *testing.T) {
   128  			translator := NewImageNameTranslator(tc.allowRegexp).(*imageNameTranslator)
   129  			translator.LoadConfigs(context.Background(), NewFakeConfigSource(configs))
   130  			endpoint := translator.Translate(tc.imageName)
   131  			if tc.expectedURL != endpoint.URL {
   132  				t.Errorf("expected URL %q, but got %q", tc.expectedURL, endpoint.URL)
   133  			}
   134  		})
   135  	}
   136  }