github.com/cilium/cilium@v1.16.2/test/controlplane/services/dualstack/dualstack.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package dualstack
     5  
     6  import (
     7  	"os"
     8  	"path"
     9  	"testing"
    10  
    11  	lb "github.com/cilium/cilium/pkg/loadbalancer"
    12  	"github.com/cilium/cilium/pkg/option"
    13  	"github.com/cilium/cilium/test/controlplane"
    14  	"github.com/cilium/cilium/test/controlplane/services/helpers"
    15  	"github.com/cilium/cilium/test/controlplane/suite"
    16  )
    17  
    18  func init() {
    19  	suite.AddTestCase("Services/DualStack", testDualStack)
    20  }
    21  
    22  func testDualStack(t *testing.T) {
    23  	cwd, err := os.Getwd()
    24  	if err != nil {
    25  		t.Fatal(err)
    26  	}
    27  
    28  	modConfig := func(cfg *option.DaemonConfig) {
    29  		cfg.EnableIPv6 = true
    30  		cfg.EnableNodePort = true
    31  	}
    32  
    33  	for _, version := range controlplane.K8sVersions() {
    34  		abs := func(f string) string { return path.Join(cwd, "services", "dualstack", "v"+version, f) }
    35  
    36  		t.Run("v"+version, func(t *testing.T) {
    37  			test := suite.NewControlPlaneTest(t, "dual-stack-worker", version)
    38  
    39  			// Feed in initial state and start the agent.
    40  			test.
    41  				UpdateObjectsFromFile(abs("init.yaml")).
    42  				SetupEnvironment().
    43  				StartAgent(modConfig).
    44  				EnsureWatchers("endpointslices", "services").
    45  				UpdateObjectsFromFile(abs("state1.yaml")).
    46  				Eventually(func() error { return validate(abs("lbmap1.golden"), test) }).
    47  				StopAgent().
    48  				ClearEnvironment()
    49  		})
    50  	}
    51  }
    52  
    53  func validate(file string, test *suite.ControlPlaneTest) error {
    54  	if err := helpers.ValidateLBMapGoldenFile(file, test.Datapath); err != nil {
    55  		return err
    56  	}
    57  
    58  	assert := helpers.NewLBMapAssert(test.Datapath.LBMockMap())
    59  
    60  	// Verify that default/echo-dualstack service exists
    61  	// for both NodePort and ClusterIP, and that it has backends
    62  	// for udp:69, and tcp:80 for both IPv4 and IPv6.
    63  	err := assert.ServicesExist(
    64  		"default/echo-dualstack",
    65  		[]lb.SVCType{lb.SVCTypeNodePort, lb.SVCTypeClusterIP},
    66  		[]helpers.SVCL3Type{helpers.SVCIPv4, helpers.SVCIPv6},
    67  		lb.UDP,
    68  		69)
    69  	if err != nil {
    70  		return err
    71  	}
    72  
    73  	err = assert.ServicesExist(
    74  		"default/echo-dualstack",
    75  		[]lb.SVCType{lb.SVCTypeNodePort, lb.SVCTypeClusterIP},
    76  		[]helpers.SVCL3Type{helpers.SVCIPv4, helpers.SVCIPv6},
    77  		lb.TCP,
    78  		80)
    79  	if err != nil {
    80  		return err
    81  	}
    82  
    83  	return nil
    84  
    85  }