github.com/cilium/cilium@v1.16.2/test/controlplane/services/graceful-termination/graceful_termination.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package graceful_termination
     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("Services/GracefulTermination", testGracefulTermination)
    19  
    20  }
    21  
    22  func testGracefulTermination(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, "services", "graceful-termination", f) }
    29  
    30  	modConfig := func(cfg *option.DaemonConfig) {
    31  		cfg.EnableK8sTerminatingEndpoint = true
    32  	}
    33  
    34  	k8sVersions := controlplane.K8sVersions()
    35  	// We only need to test the last k8s version
    36  	test := suite.NewControlPlaneTest(t, "graceful-term-control-plane", k8sVersions[len(k8sVersions)-1])
    37  	defer test.StopAgent()
    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  
    46  		// Step 1: Initial creation of the services and backends
    47  		// lbmap1.golden: Shows graceful-term-svc service with an active backend
    48  		UpdateObjectsFromFile(abs("state1.yaml")).
    49  		Eventually(func() error { return helpers.ValidateLBMapGoldenFile(abs("lbmap1.golden"), test.Datapath) }).
    50  
    51  		// Step 2: Pod is being deleted and endpoint is set to terminating state
    52  		// lbmap2.golden: The backend state is 'terminating'
    53  		UpdateObjectsFromFile(abs("state2.yaml")).
    54  		Eventually(func() error { return helpers.ValidateLBMapGoldenFile(abs("lbmap2.golden"), test.Datapath) }).
    55  
    56  		// Step 3: Endpoint has now been removed from the endpoint slice.
    57  		// lbmap3.golden: The graceful-term-svc service no longer has any backeds
    58  		UpdateObjectsFromFile(abs("state3.yaml")).
    59  		Eventually(func() error { return helpers.ValidateLBMapGoldenFile(abs("lbmap3.golden"), test.Datapath) }).
    60  		ClearEnvironment()
    61  }