github.com/cilium/cilium@v1.16.2/test/controlplane/pod/hostport/hostport.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package hostport
     5  
     6  import (
     7  	"os"
     8  	"path"
     9  	"testing"
    10  
    11  	"github.com/cilium/cilium/pkg/option"
    12  	"github.com/cilium/cilium/test/controlplane"
    13  	"github.com/cilium/cilium/test/controlplane/services/helpers"
    14  	"github.com/cilium/cilium/test/controlplane/suite"
    15  )
    16  
    17  func init() {
    18  	suite.AddTestCase("Pod/HostPort", testHostPort)
    19  
    20  }
    21  
    22  func testHostPort(t *testing.T) {
    23  	cwd, err := os.Getwd()
    24  	if err != nil {
    25  		t.Fatal(err)
    26  	}
    27  
    28  	abs := func(f string) string { return path.Join(cwd, "pod", "hostport", f) }
    29  
    30  	k8sVersions := controlplane.K8sVersions()
    31  	// We only need to test the last k8s version
    32  	test := suite.NewControlPlaneTest(t, "hostport-control-plane", k8sVersions[len(k8sVersions)-1])
    33  	defer test.StopAgent()
    34  
    35  	// Feed in initial state and start the agent.
    36  	test.
    37  		UpdateObjectsFromFile(abs("init.yaml")).
    38  		SetupEnvironment().
    39  		StartAgent(func(_ *option.DaemonConfig) {}).
    40  		EnsureWatchers("pods").
    41  
    42  		// Step 1: Create the first hostport pod.
    43  		// lbmap1.golden: Hostport service exists in the Datapath with hostport-1 pod as backend.
    44  		UpdateObjectsFromFile(abs("state1.yaml")).
    45  		Eventually(func() error { return helpers.ValidateLBMapGoldenFile(abs("lbmap1.golden"), test.Datapath) }).
    46  
    47  		// Step 2: Mark the first pod as "completed", and create a second hostport pod using the same port
    48  		// lbmap2.golden: Hostport service exists in the Datapath with hostport-2 pod as backend.
    49  		UpdateObjectsFromFile(abs("state2.yaml")).
    50  		Eventually(func() error { return helpers.ValidateLBMapGoldenFile(abs("lbmap2.golden"), test.Datapath) }).
    51  
    52  		// Step 3: Delete the completed pod, and verify that the hostport service doesn't get deleted.
    53  		// lbmap3.golden: Hostport service still exists in the Datapath, with hostport-2 pod as backend.
    54  		UpdateObjectsFromFile(abs("state3.yaml")).
    55  		Eventually(func() error { return helpers.ValidateLBMapGoldenFile(abs("lbmap3.golden"), test.Datapath) })
    56  }