mirror of
https://github.com/edgelesssys/constellation.git
synced 2024-12-29 01:16:12 -05:00
bd63aa3c6b
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 .
84 lines
1.7 KiB
Go
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
|
|
}
|