...

Source file src/github.com/linkerd/linkerd2/cli/cmd/install-cni-plugin_test.go

Documentation: github.com/linkerd/linkerd2/cli/cmd

     1  package cmd
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"helm.sh/helm/v3/pkg/cli/values"
     9  )
    10  
    11  func TestRenderCNIPlugin(t *testing.T) {
    12  	defaultOptions, err := newCNIInstallOptionsWithDefaults()
    13  	if err != nil {
    14  		t.Fatalf("Unexpected error from newCNIInstallOptionsWithDefaults(): %v", err)
    15  	}
    16  
    17  	image := cniPluginImage{
    18  		name:       "my-docker-registry.io/awesome/cni-plugin-test-image",
    19  		version:    "v1.4.0",
    20  		pullPolicy: nil,
    21  	}
    22  	fullyConfiguredOptions := &cniPluginOptions{
    23  		linkerdVersion:      "awesome-linkerd-version.1",
    24  		dockerRegistry:      "cr.l5d.io/linkerd",
    25  		proxyControlPort:    5190,
    26  		proxyAdminPort:      5191,
    27  		inboundPort:         5143,
    28  		outboundPort:        5140,
    29  		ignoreInboundPorts:  make([]string, 0),
    30  		ignoreOutboundPorts: make([]string, 0),
    31  		proxyUID:            12102,
    32  		proxyGID:            12102,
    33  		image:               image,
    34  		logLevel:            "debug",
    35  		destCNINetDir:       "/etc/kubernetes/cni/net.d",
    36  		destCNIBinDir:       "/opt/my-cni/bin",
    37  		priorityClassName:   "system-node-critical",
    38  	}
    39  
    40  	fullyConfiguredOptionsEqualDsts := &cniPluginOptions{
    41  		linkerdVersion:      "awesome-linkerd-version.1",
    42  		dockerRegistry:      "cr.l5d.io/linkerd",
    43  		proxyControlPort:    5190,
    44  		proxyAdminPort:      5191,
    45  		inboundPort:         5143,
    46  		outboundPort:        5140,
    47  		ignoreInboundPorts:  make([]string, 0),
    48  		ignoreOutboundPorts: make([]string, 0),
    49  		proxyUID:            12102,
    50  		proxyGID:            12102,
    51  		image:               image,
    52  		logLevel:            "debug",
    53  		destCNINetDir:       "/etc/kubernetes/cni/net.d",
    54  		destCNIBinDir:       "/etc/kubernetes/cni/net.d",
    55  		priorityClassName:   "system-node-critical",
    56  	}
    57  
    58  	fullyConfiguredOptionsNoNamespace := &cniPluginOptions{
    59  		linkerdVersion:      "awesome-linkerd-version.1",
    60  		dockerRegistry:      "cr.l5d.io/linkerd",
    61  		proxyControlPort:    5190,
    62  		proxyAdminPort:      5191,
    63  		inboundPort:         5143,
    64  		outboundPort:        5140,
    65  		ignoreInboundPorts:  make([]string, 0),
    66  		ignoreOutboundPorts: make([]string, 0),
    67  		proxyUID:            12102,
    68  		proxyGID:            12102,
    69  		image:               image,
    70  		logLevel:            "debug",
    71  		destCNINetDir:       "/etc/kubernetes/cni/net.d",
    72  		destCNIBinDir:       "/opt/my-cni/bin",
    73  		priorityClassName:   "system-node-critical",
    74  	}
    75  
    76  	defaultOptionsWithSkipPorts, err := newCNIInstallOptionsWithDefaults()
    77  	if err != nil {
    78  		t.Fatalf("Unexpected error from newCNIInstallOptionsWithDefaults(): %v", err)
    79  	}
    80  
    81  	defaultOptionsWithSkipPorts.ignoreInboundPorts = append(defaultOptionsWithSkipPorts.ignoreInboundPorts, []string{"80", "8080"}...)
    82  	defaultOptionsWithSkipPorts.ignoreOutboundPorts = append(defaultOptionsWithSkipPorts.ignoreOutboundPorts, []string{"443", "1000"}...)
    83  
    84  	valOpts := values.Options{
    85  		Values: []string{"resources.cpu.limit=1m"},
    86  	}
    87  
    88  	testCases := []struct {
    89  		*cniPluginOptions
    90  		valOpts        values.Options
    91  		goldenFileName string
    92  	}{
    93  		{defaultOptions, valOpts, "install-cni-plugin_default.golden"},
    94  		{fullyConfiguredOptions, values.Options{}, "install-cni-plugin_fully_configured.golden"},
    95  		{fullyConfiguredOptionsEqualDsts, values.Options{}, "install-cni-plugin_fully_configured_equal_dsts.golden"},
    96  		{fullyConfiguredOptionsNoNamespace, values.Options{}, "install-cni-plugin_fully_configured_no_namespace.golden"},
    97  		{defaultOptionsWithSkipPorts, values.Options{}, "install-cni-plugin_skip_ports.golden"},
    98  	}
    99  
   100  	for i, tc := range testCases {
   101  		tc := tc // pin
   102  		t.Run(fmt.Sprintf("%d: %s", i, tc.goldenFileName), func(t *testing.T) {
   103  			var buf bytes.Buffer
   104  			err := renderCNIPlugin(&buf, tc.valOpts, tc.cniPluginOptions)
   105  			if err != nil {
   106  				t.Fatalf("Unexpected error: %v", err)
   107  			}
   108  			if err = testDataDiffer.DiffTestYAML(tc.goldenFileName, buf.String()); err != nil {
   109  				t.Error(err)
   110  			}
   111  		})
   112  	}
   113  }
   114  

View as plain text