istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/test/framework/components/echo/kube/deployment_test.go (about)

     1  // Copyright Istio 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  package kube
    15  
    16  import (
    17  	"testing"
    18  
    19  	"istio.io/api/annotation"
    20  	testutil "istio.io/istio/pilot/test/util"
    21  	"istio.io/istio/pkg/config/protocol"
    22  	"istio.io/istio/pkg/test/framework/components/cluster"
    23  	"istio.io/istio/pkg/test/framework/components/cluster/clusterboot"
    24  	"istio.io/istio/pkg/test/framework/components/echo"
    25  	"istio.io/istio/pkg/test/framework/components/namespace"
    26  	"istio.io/istio/pkg/test/framework/config"
    27  	"istio.io/istio/pkg/test/framework/resource"
    28  )
    29  
    30  func TestDeploymentYAML(t *testing.T) {
    31  	testCase := []struct {
    32  		name          string
    33  		wantFilePath  string
    34  		config        echo.Config
    35  		revVerMap     resource.RevVerMap
    36  		settings      func(*resource.Settings)
    37  		compatibility bool
    38  	}{
    39  		{
    40  			name:         "basic",
    41  			wantFilePath: "testdata/basic.yaml",
    42  			config: echo.Config{
    43  				Service: "foo",
    44  				Version: "bar",
    45  				Ports: []echo.Port{
    46  					{
    47  						Name:         "http",
    48  						Protocol:     protocol.HTTP,
    49  						WorkloadPort: 8090,
    50  						ServicePort:  8090,
    51  					},
    52  				},
    53  			},
    54  		},
    55  		{
    56  			name:         "two-workloads-one-nosidecar",
    57  			wantFilePath: "testdata/two-workloads-one-nosidecar.yaml",
    58  			config: echo.Config{
    59  				Service: "foo",
    60  				Ports: []echo.Port{
    61  					{
    62  						Name:         "http",
    63  						Protocol:     protocol.HTTP,
    64  						WorkloadPort: 8090,
    65  						ServicePort:  8090,
    66  					},
    67  				},
    68  				Subsets: []echo.SubsetConfig{
    69  					{
    70  						Version: "v1",
    71  					},
    72  					{
    73  						Version:     "nosidecar",
    74  						Annotations: map[string]string{annotation.SidecarInject.Name: "false"},
    75  					},
    76  				},
    77  			},
    78  		},
    79  		{
    80  			name:         "healthcheck-rewrite",
    81  			wantFilePath: "testdata/healthcheck-rewrite.yaml",
    82  			config: echo.Config{
    83  				Service: "healthcheck",
    84  				Ports: []echo.Port{{
    85  					Name:         "http-8080",
    86  					Protocol:     protocol.HTTP,
    87  					ServicePort:  8080,
    88  					WorkloadPort: 8080,
    89  				}},
    90  				Subsets: []echo.SubsetConfig{
    91  					{
    92  						Annotations: map[string]string{annotation.SidecarRewriteAppHTTPProbers.Name: "true"},
    93  					},
    94  				},
    95  			},
    96  		},
    97  		{
    98  			name:         "multiversion",
    99  			wantFilePath: "testdata/multiversion.yaml",
   100  			config: echo.Config{
   101  				Service: "multiversion",
   102  				Subsets: []echo.SubsetConfig{
   103  					{
   104  						Version: "v-istio",
   105  					},
   106  					{
   107  						Version:     "v-legacy",
   108  						Annotations: map[string]string{annotation.SidecarInject.Name: "false"},
   109  					},
   110  				},
   111  				Ports: []echo.Port{
   112  					{
   113  						Name:         "http",
   114  						Protocol:     protocol.HTTP,
   115  						WorkloadPort: 8090,
   116  						ServicePort:  8090,
   117  					},
   118  					{
   119  						Name:         "tcp",
   120  						Protocol:     protocol.TCP,
   121  						WorkloadPort: 9000,
   122  						ServicePort:  9000,
   123  					},
   124  					{
   125  						Name:         "grpc",
   126  						Protocol:     protocol.GRPC,
   127  						WorkloadPort: 9090,
   128  						ServicePort:  9090,
   129  					},
   130  				},
   131  			},
   132  		},
   133  		{
   134  			name:         "multiple-istio-versions",
   135  			wantFilePath: "testdata/multiple-istio-versions.yaml",
   136  			config: echo.Config{
   137  				Service: "foo",
   138  				Version: "bar",
   139  				Ports: []echo.Port{
   140  					{
   141  						Name:         "http",
   142  						Protocol:     protocol.HTTP,
   143  						WorkloadPort: 8090,
   144  						ServicePort:  8090,
   145  					},
   146  				},
   147  			},
   148  			revVerMap: resource.RevVerMap{
   149  				"rev-a": resource.IstioVersion("1.9.0"),
   150  				"rev-b": resource.IstioVersion("1.10.0"),
   151  			},
   152  			compatibility: true,
   153  		},
   154  		{
   155  			name:         "multiple-istio-versions-no-proxy",
   156  			wantFilePath: "testdata/multiple-istio-versions-no-proxy.yaml",
   157  			config: echo.Config{
   158  				Service: "foo",
   159  				Version: "bar",
   160  				Ports: []echo.Port{
   161  					{
   162  						Name:         "http",
   163  						Protocol:     protocol.HTTP,
   164  						WorkloadPort: 8090,
   165  						ServicePort:  8090,
   166  					},
   167  				},
   168  			},
   169  			revVerMap: resource.RevVerMap{
   170  				"rev-a": resource.IstioVersion("1.8.2"),
   171  				"rev-b": resource.IstioVersion("1.9.0"),
   172  			},
   173  			compatibility: true,
   174  		},
   175  		{
   176  			name:         "proxyless",
   177  			wantFilePath: "testdata/proxyless.yaml",
   178  			config: echo.Config{
   179  				Service: "foo",
   180  				Version: "bar",
   181  				Subsets: []echo.SubsetConfig{{
   182  					Annotations: map[string]string{annotation.InjectTemplates.Name: "grpc-agent"},
   183  				}},
   184  				Ports: []echo.Port{
   185  					{
   186  						Name:         "grpc",
   187  						Protocol:     protocol.GRPC,
   188  						WorkloadPort: 7070,
   189  						ServicePort:  7070,
   190  					},
   191  				},
   192  			},
   193  		},
   194  		{
   195  			name:         "proxyless-custom-image",
   196  			wantFilePath: "testdata/proxyless-custom-image.yaml",
   197  			settings: func(s *resource.Settings) {
   198  				s.CustomGRPCEchoImage = "grpc/echo:cpp"
   199  			},
   200  			config: echo.Config{
   201  				Service: "foo",
   202  				Version: "bar",
   203  				Subsets: []echo.SubsetConfig{{
   204  					Annotations: map[string]string{annotation.InjectTemplates.Name: "grpc-agent"},
   205  				}},
   206  				Ports: []echo.Port{
   207  					{
   208  						Name:         "grpc",
   209  						Protocol:     protocol.GRPC,
   210  						WorkloadPort: 7070,
   211  						ServicePort:  7070,
   212  					},
   213  				},
   214  			},
   215  		},
   216  		{
   217  			name:         "disable-automount-sa",
   218  			wantFilePath: "testdata/disable-automount-sa.yaml",
   219  			config: echo.Config{
   220  				Service: "foo",
   221  				Version: "bar",
   222  				Ports: []echo.Port{
   223  					{
   224  						Name:         "http",
   225  						Protocol:     protocol.HTTP,
   226  						WorkloadPort: 8090,
   227  						ServicePort:  8090,
   228  					},
   229  				},
   230  				ServiceAccount:          true,
   231  				DisableAutomountSAToken: true,
   232  			},
   233  		},
   234  	}
   235  	for _, tc := range testCase {
   236  		t.Run(tc.name, func(t *testing.T) {
   237  			clusters, err := clusterboot.NewFactory().With(cluster.Config{
   238  				Kind: cluster.Fake, Name: "cluster-0",
   239  				Meta: config.Map{"majorVersion": 1, "minorVersion": 16},
   240  			}).Build()
   241  			if err != nil {
   242  				t.Fatal(err)
   243  			}
   244  			tc.config.Cluster = clusters[0]
   245  			if tc.config.Namespace == nil {
   246  				tc.config.Namespace = namespace.Static("echo-ns")
   247  			}
   248  			if err := tc.config.FillDefaults(nil); err != nil {
   249  				t.Errorf("failed filling in defaults: %v", err)
   250  			}
   251  			if !config.Parsed() {
   252  				config.Parse()
   253  			}
   254  			settings := &resource.Settings{
   255  				Revisions:     tc.revVerMap,
   256  				Compatibility: tc.compatibility,
   257  				Image: resource.ImageSettings{
   258  					Hub:        "testing.hub",
   259  					Tag:        "latest",
   260  					PullPolicy: "Always",
   261  					PullSecret: "testdata/secret.yaml",
   262  				},
   263  			}
   264  			if tc.settings != nil {
   265  				tc.settings(settings)
   266  			}
   267  			serviceYAML, err := GenerateService(tc.config)
   268  			if err != nil {
   269  				t.Errorf("failed to generate service %v", err)
   270  			}
   271  			deploymentYAML, err := GenerateDeployment(nil, tc.config, settings)
   272  			if err != nil {
   273  				t.Errorf("failed to generate deployment %v", err)
   274  			}
   275  			gotBytes := []byte(serviceYAML + "---" + deploymentYAML)
   276  			wantedBytes := testutil.ReadGoldenFile(t, gotBytes, tc.wantFilePath)
   277  
   278  			wantBytes := testutil.StripVersion(wantedBytes)
   279  			gotBytes = testutil.StripVersion(gotBytes)
   280  
   281  			testutil.RefreshGoldenFile(t, gotBytes, tc.wantFilePath)
   282  
   283  			testutil.CompareBytes(t, gotBytes, wantBytes, tc.wantFilePath)
   284  		})
   285  	}
   286  }