Files
my-rook-config/pkg/operator/ceph/config/network_test.go
T
rohan47 f5bbc8c5c5 ceph: fix networkAttachmentConfig to be compatible with whereabouts cni
In the whereabouts config, the network range is determined by range
option unlike other cni plugins that use Subnet option.

Signed-off-by: rohan47 <rohgupta@redhat.com>
2020-08-05 21:17:13 +05:30

197 lines
4.9 KiB
Go

/*
Copyright 2020 The Rook Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package config
import (
"fmt"
"testing"
networkv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
fakenetclient "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/client/clientset/versioned/fake"
"github.com/rook/rook/pkg/clusterd"
"github.com/rook/rook/pkg/operator/k8sutil"
testop "github.com/rook/rook/pkg/operator/test"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func TestGenerateNetworkSettings(t *testing.T) {
ns := "rook-ceph"
clientset := testop.New(t, 1)
ctx := &clusterd.Context{
Clientset: clientset,
NetworkClient: fakenetclient.NewSimpleClientset().K8sCniCncfIoV1(),
}
//
// TEST 1: network definition does not exist
//
netSelector := map[string]string{
"public": "public-network-attach-def",
}
cephNetwork, err := generateNetworkSettings(ctx, ns, netSelector)
assert.Error(t, err)
//
// TEST 2: single dedicated networks
//
expectedNetworks := []Option{
{
Who: "global",
Option: "public_network",
Value: "192.168.0.0/24",
},
{
Who: "global",
Option: "cluster_network",
Value: "192.168.0.0/24",
},
}
// this nad uses whereabouts cni
network := &networkv1.NetworkAttachmentDefinition{
ObjectMeta: metav1.ObjectMeta{
Name: "public-network-attach-def",
Namespace: ns,
},
Spec: networkv1.NetworkAttachmentDefinitionSpec{
Config: `{
"cniVersion": "0.3.0",
"type": "macvlan",
"master": "eth2",
"mode": "bridge",
"ipam": {
"type": "host-local",
"subnet": "192.168.0.0/24",
"gateway": "172.18.8.1"
}
}`,
},
}
// Create public network definition
ctx.NetworkClient.NetworkAttachmentDefinitions(ns).Create(network)
cephNetwork, err = generateNetworkSettings(ctx, ns, netSelector)
assert.NoError(t, err)
assert.ElementsMatch(t, cephNetwork, expectedNetworks, fmt.Sprintf("networks: %+v", cephNetwork))
//
// TEST 3: two dedicated networks
//
expectedNetworks = []Option{
{
Who: "global",
Option: "public_network",
Value: "192.168.0.0/24",
},
{
Who: "global",
Option: "cluster_network",
Value: "172.18.0.0/16",
},
}
netSelector = map[string]string{
"public": "public-network-attach-def",
"cluster": "cluster-network-attach-def",
}
network2 := &networkv1.NetworkAttachmentDefinition{
ObjectMeta: metav1.ObjectMeta{
Name: "cluster-network-attach-def",
Namespace: ns,
},
Spec: networkv1.NetworkAttachmentDefinitionSpec{
Config: `{
"cniVersion": "0.3.0",
"type": "macvlan",
"master": "eth2",
"mode": "bridge",
"ipam": {
"type": "host-local",
"subnet": "172.18.0.0/16",
"gateway": "172.18.0.1"
}
}`,
},
}
// Create cluster network definition
ctx.NetworkClient.NetworkAttachmentDefinitions(ns).Create(network2)
cephNetwork, err = generateNetworkSettings(ctx, ns, netSelector)
assert.NoError(t, err)
assert.ElementsMatch(t, cephNetwork, expectedNetworks, fmt.Sprintf("networks: %+v", cephNetwork))
}
func TestGetNetworkRange(t *testing.T) {
ns := "rook-ceph"
nad := &networkv1.NetworkAttachmentDefinition{
ObjectMeta: metav1.ObjectMeta{
Name: "public-network-attach-def",
Namespace: ns,
},
Spec: networkv1.NetworkAttachmentDefinitionSpec{
Config: `{
"cniVersion": "0.3.0",
"type": "macvlan",
"master": "eth2",
"mode": "bridge",
"ipam": {
"type": "host-local",
"subnet": "",
"gateway": "172.18.8.1"
}
}`,
},
}
netConfig, err := k8sutil.GetNetworkAttachmentConfig(*nad)
assert.NoError(t, err)
//
// TEST 1: subnet/range is empty
//
networkRange := getNetworkRange(netConfig)
assert.Empty(t, networkRange)
//
// TEST 2: subnet is not empty
//
netConfig.Ipam.Type = "host-local"
netConfig.Ipam.Subnet = "192.168.0.0/24"
networkRange = getNetworkRange(netConfig)
assert.Equal(t, "192.168.0.0/24", networkRange)
//
// TEST 3: range is empty and ipam type is whereabouts
//
netConfig.Ipam.Type = "whereabouts"
netConfig.Ipam.Subnet = ""
networkRange = getNetworkRange(netConfig)
assert.Empty(t, networkRange)
//
// TEST 4: range is not empty and ipam type is whereabouts
//
netConfig.Ipam.Type = "whereabouts"
netConfig.Ipam.Range = "192.168.0.0/24"
networkRange = getNetworkRange(netConfig)
assert.Equal(t, "192.168.0.0/24", networkRange)
}