github.com/operator-framework/operator-lifecycle-manager@v0.30.0/pkg/lib/catalogsource/image_template_test.go (about)

     1  package catalogsource
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/sirupsen/logrus"
     8  	"github.com/stretchr/testify/assert"
     9  	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    10  	"k8s.io/apimachinery/pkg/version"
    11  
    12  	"github.com/operator-framework/api/pkg/operators/v1alpha1"
    13  )
    14  
    15  func TestImageTemplateRegex(t *testing.T) {
    16  	var table = []struct {
    17  		description string
    18  		imageName   string
    19  		isMatch     bool
    20  		foundItems  []string
    21  	}{
    22  		{
    23  			description: "no templates used",
    24  			imageName:   "foo",
    25  			isMatch:     false,
    26  		},
    27  		{
    28  			description: "one static template used",
    29  			imageName:   fmt.Sprintf("foo%s", TemplKubeMajorV),
    30  			isMatch:     true,
    31  			foundItems:  []string{TemplKubeMajorV},
    32  		},
    33  		{
    34  			description: "multiple templates used",
    35  			imageName:   fmt.Sprintf("%sfoo%s", TemplKubeMajorV, TemplKubeMinorV),
    36  			isMatch:     true,
    37  			foundItems:  []string{TemplKubeMajorV, TemplKubeMinorV},
    38  		},
    39  	}
    40  
    41  	for _, tt := range table {
    42  		t.Run(tt.description, func(t *testing.T) {
    43  
    44  			assert.Equal(t, tt.isMatch, regexImageTemplates.MatchString(tt.imageName), "Expected image name %q to find a match", tt.imageName)
    45  
    46  			actualFoundItems := regexImageTemplates.FindAllString(tt.imageName, -1)
    47  			assert.Equal(t, tt.foundItems, actualFoundItems, "Expected to find all strings within image name %q", tt.imageName)
    48  		})
    49  	}
    50  }
    51  
    52  // TestImageTemplateFlow simulates the image template process.
    53  func TestImageTemplateFlow(t *testing.T) {
    54  
    55  	defaultServerVersion := version.Info{
    56  		Major:      "1",
    57  		Minor:      "20",
    58  		GitVersion: "v1.20.0+bd9e442",
    59  	}
    60  
    61  	serverVersionNonReleaseBuild := version.Info{
    62  		Major:      "1",
    63  		Minor:      "20+",
    64  		GitVersion: "v1.20.1+bd9e442",
    65  	}
    66  
    67  	var table = []struct {
    68  		description                 string                 // description of test case
    69  		catsrc                      v1alpha1.CatalogSource // fake catalog source for testing
    70  		serverVersion               *version.Info          // simulated kube version information
    71  		expectedCatalogTemplate     string                 // expected results after calling api
    72  		expectedUnresolvedTemplates []string               // expected results after calling api
    73  	}{
    74  		{
    75  			description:                 "no image templates",
    76  			catsrc:                      v1alpha1.CatalogSource{},
    77  			serverVersion:               &defaultServerVersion,
    78  			expectedCatalogTemplate:     "",
    79  			expectedUnresolvedTemplates: []string{},
    80  		},
    81  		{
    82  			description: "kube image template",
    83  			catsrc: v1alpha1.CatalogSource{
    84  				ObjectMeta: v1.ObjectMeta{
    85  					Annotations: map[string]string{
    86  						CatalogImageTemplateAnnotation: fmt.Sprintf("foo/v%s", TemplKubeMajorV),
    87  					},
    88  				},
    89  			},
    90  			serverVersion:               &defaultServerVersion,
    91  			expectedCatalogTemplate:     "foo/v1",
    92  			expectedUnresolvedTemplates: []string{},
    93  		},
    94  		{
    95  			description: "multiple kube image template",
    96  			catsrc: v1alpha1.CatalogSource{
    97  				ObjectMeta: v1.ObjectMeta{
    98  					Annotations: map[string]string{
    99  						CatalogImageTemplateAnnotation: fmt.Sprintf("foo/v%s.%s", TemplKubeMajorV, TemplKubeMinorV),
   100  					},
   101  				},
   102  			},
   103  			serverVersion:               &defaultServerVersion,
   104  			expectedCatalogTemplate:     "foo/v1.20",
   105  			expectedUnresolvedTemplates: []string{},
   106  		},
   107  		{
   108  			description: "kube image template but no server version",
   109  			catsrc: v1alpha1.CatalogSource{
   110  				ObjectMeta: v1.ObjectMeta{
   111  					Annotations: map[string]string{
   112  						CatalogImageTemplateAnnotation: fmt.Sprintf("foo/v%s", TemplKubeMajorV),
   113  					},
   114  				},
   115  			},
   116  			serverVersion:               nil,
   117  			expectedCatalogTemplate:     "foo/vmissing",
   118  			expectedUnresolvedTemplates: []string{"{kube_major_version}"},
   119  		},
   120  		{
   121  			description: "garbage image template",
   122  			catsrc: v1alpha1.CatalogSource{
   123  				ObjectMeta: v1.ObjectMeta{
   124  					Annotations: map[string]string{
   125  						CatalogImageTemplateAnnotation: fmt.Sprintf("foo/v%s", "{foobar}"),
   126  					},
   127  				},
   128  			},
   129  			serverVersion:               &defaultServerVersion,
   130  			expectedCatalogTemplate:     "foo/v{foobar}",
   131  			expectedUnresolvedTemplates: []string{},
   132  		},
   133  		{
   134  			description: "multiple kube image template with patch and nonrelease build version",
   135  			catsrc: v1alpha1.CatalogSource{
   136  				ObjectMeta: v1.ObjectMeta{
   137  					Annotations: map[string]string{
   138  						CatalogImageTemplateAnnotation: fmt.Sprintf("foo/v%s.%s.%s", TemplKubeMajorV, TemplKubeMinorV, TemplKubePatchV),
   139  					},
   140  				},
   141  			},
   142  			serverVersion:               &serverVersionNonReleaseBuild,
   143  			expectedCatalogTemplate:     "foo/v1.20.1",
   144  			expectedUnresolvedTemplates: []string{},
   145  		},
   146  	}
   147  
   148  	logger := logrus.New()
   149  
   150  	for _, tt := range table {
   151  		t.Run(tt.description, func(t *testing.T) {
   152  			// make sure test case is initialized to default to simulate real usage when operator starts up
   153  			resetMaps()
   154  
   155  			// simulate kube watch updates
   156  			UpdateKubeVersion(tt.serverVersion, logger)
   157  			// get the template
   158  			catalogImageTemplate := GetCatalogTemplateAnnotation(&tt.catsrc)
   159  			// perform the template replacement
   160  			processedCatalogTemplate, unresolveTemplates := ReplaceTemplates(catalogImageTemplate)
   161  			assert.Equal(t, tt.expectedCatalogTemplate, processedCatalogTemplate, "the processed template did not match expected value")
   162  			assert.Equal(t, tt.expectedUnresolvedTemplates, unresolveTemplates, "the unresolved templates list did not match expected values")
   163  
   164  		})
   165  	}
   166  }