constellation/operators/constellation-node-operator/internal/controlplane/controlplane_test.go
Thomas Tendyck bd63aa3c6b add license headers
sed -i '1i/*\nCopyright (c) Edgeless Systems GmbH\n\nSPDX-License-Identifier: AGPL-3.0-only\n*/\n' `grep -rL --include='*.go' 'DO NOT EDIT'`
gofumpt -w .
2022-09-05 09:17:25 +02:00

84 lines
1.7 KiB
Go

/*
Copyright (c) Edgeless Systems GmbH
SPDX-License-Identifier: AGPL-3.0-only
*/
package controlplane
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func TestListControlPlaneIPs(t *testing.T) {
testCases := map[string]struct {
nodes []corev1.Node
listErr error
wantIPs []string
wantErr bool
}{
"listing works": {
nodes: []corev1.Node{
{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"node-role.kubernetes.io/control-plane": ""},
},
Status: corev1.NodeStatus{Addresses: []corev1.NodeAddress{{
Type: corev1.NodeInternalIP,
Address: "192.0.2.1",
}}},
},
{
Status: corev1.NodeStatus{Addresses: []corev1.NodeAddress{{
Type: corev1.NodeInternalIP,
Address: "192.0.2.2",
}}},
},
},
wantIPs: []string{"192.0.2.1"},
},
"listing fails": {
listErr: errors.New("listing failed"),
wantErr: true,
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
client := &stubClient{
nodes: tc.nodes,
listErr: tc.listErr,
}
gotIPs, err := ListControlPlaneIPs(client)
if tc.wantErr {
assert.Error(err)
return
}
require.NoError(err)
assert.ElementsMatch(tc.wantIPs, gotIPs)
})
}
}
type stubClient struct {
nodes []corev1.Node
listErr error
client.Client
}
func (c *stubClient) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
list.(*corev1.NodeList).Items = c.nodes
return c.listErr
}