github.com/cilium/cilium@v1.16.2/test/controlplane/node/nodehandler.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package node
     5  
     6  import (
     7  	"fmt"
     8  	"testing"
     9  
    10  	corev1 "k8s.io/api/core/v1"
    11  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    12  
    13  	"github.com/cilium/cilium/pkg/cidr"
    14  	fakeTypes "github.com/cilium/cilium/pkg/datapath/fake/types"
    15  	"github.com/cilium/cilium/pkg/option"
    16  	"github.com/cilium/cilium/test/controlplane"
    17  	"github.com/cilium/cilium/test/controlplane/suite"
    18  )
    19  
    20  var (
    21  	podCIDR = cidr.MustParseCIDR("10.0.1.0/24")
    22  
    23  	minimalNode = &corev1.Node{
    24  		TypeMeta:   metav1.TypeMeta{Kind: "Node", APIVersion: "v1"},
    25  		ObjectMeta: metav1.ObjectMeta{Name: "minimal"},
    26  		Spec: corev1.NodeSpec{
    27  			PodCIDR:  podCIDR.String(),
    28  			PodCIDRs: []string{podCIDR.String()},
    29  		},
    30  		Status: corev1.NodeStatus{
    31  			Conditions: []corev1.NodeCondition{},
    32  			Addresses: []corev1.NodeAddress{
    33  				{Type: corev1.NodeInternalIP, Address: "10.0.0.1"},
    34  				{Type: corev1.NodeHostName, Address: "minimal"},
    35  			},
    36  		},
    37  	}
    38  )
    39  
    40  func validateNodes(dp *fakeTypes.FakeDatapath) error {
    41  	nodes := dp.FakeNode().Nodes
    42  
    43  	if len(nodes) != 1 {
    44  		return fmt.Errorf("expected 1 node, found %d (%v)", len(nodes), nodes)
    45  	}
    46  
    47  	minimal, ok := nodes["minimal"]
    48  	if !ok {
    49  		return fmt.Errorf("'minimal' node not found from nodes (%v)", nodes)
    50  	}
    51  
    52  	if minimal.Name != "minimal" {
    53  		return fmt.Errorf("name mismatch: %q vs %q", "minimal", minimal.Name)
    54  	}
    55  
    56  	if !podCIDR.Equal(minimal.IPv4AllocCIDR) {
    57  		return fmt.Errorf("cidr mismatch: %q vs %q", podCIDR, minimal)
    58  	}
    59  
    60  	return nil
    61  }
    62  
    63  func init() {
    64  	suite.AddTestCase("NodeHandler", func(t *testing.T) {
    65  		k8sVersions := controlplane.K8sVersions()
    66  		// We only need to test the last k8s version
    67  		test := suite.NewControlPlaneTest(t, "minimal", k8sVersions[len(k8sVersions)-1])
    68  
    69  		test.
    70  			UpdateObjects(minimalNode).
    71  			SetupEnvironment().
    72  			StartAgent(func(*option.DaemonConfig) {}).
    73  			Eventually(func() error { return validateNodes(test.Datapath) }).
    74  			StopAgent().
    75  			ClearEnvironment()
    76  	})
    77  }