Skip to content

Commit

Permalink
Add tests to the dual-stack PR and enable dual-stack with flannel bac…
Browse files Browse the repository at this point in the history
…kend

Signed-off-by: Manuel Buil <mbuil@suse.com>
  • Loading branch information
manuelbuil committed Sep 15, 2021
1 parent a181746 commit 262b397
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 1 deletion.
83 changes: 83 additions & 0 deletions pkg/agent/flannel/setup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package flannel

import (
"io/ioutil"
"net"
"regexp"
"strings"
"testing"

"github.com/rancher/k3s/pkg/daemons/config"
)

func stringToCIDR(s string) []*net.IPNet {
var netCidrs []*net.IPNet
for _, v := range strings.Split(s, ",") {
_, parsed, _ := net.ParseCIDR(v)
netCidrs = append(netCidrs, parsed)
}
return netCidrs
}

func Test_findNetMode(t *testing.T) {
tests := []struct {
name string
args string
want int
wantErr bool
}{
{"dual-stack", "10.42.0.0/16,2001:cafe:22::/56", ipv4 + ipv6, false},
{"ipv4 only", "10.42.0.0/16", ipv4, false},
{"ipv6 only", "2001:cafe:42:0::/56", ipv6, false},
{"wrong input", "wrong", 0, true},
}
for _, tt := range tests {

t.Run(tt.name, func(t *testing.T) {
netCidrs := stringToCIDR(tt.args)
got, err := findNetMode(netCidrs)
if (err != nil) != tt.wantErr {
t.Errorf("findNetMode() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("findNetMode() = %v, want %v", got, tt.want)
}
})
}
}

func Test_createFlannelConf(t *testing.T) {
tests := []struct {
name string
args string
wantConfig []string
wantErr bool
}{
{"dual-stack", "10.42.0.0/16,2001:cafe:22::/56", []string{"\"Network\": \"10.42.0.0/16\"", "\"IPv6Network\": \"2001:cafe:22::/56\"", "\"EnableIPv6\": true"}, false},
{"ipv4 only", "10.42.0.0/16", []string{"\"Network\": \"10.42.0.0/16\"", "\"IPv6Network\": \"::/0\"", "\"EnableIPv6\": false"}, false},
}
var containerd = config.Containerd{}
for _, tt := range tests {
var agent = config.Agent{}
agent.ClusterCIDR = stringToCIDR(tt.args)[0]
agent.ClusterCIDRs = stringToCIDR(tt.args)
var nodeConfig = &config.Node{Docker: false, ContainerRuntimeEndpoint: "", NoFlannel: false, SELinux: false, FlannelBackend: "vxlan", FlannelConf: "test_file", FlannelConfOverride: false, FlannelIface: nil, Containerd: containerd, Images: "", AgentConfig: agent, Token: "", Certificate: nil, ServerHTTPSPort: 0}

t.Run(tt.name, func(t *testing.T) {
if err := createFlannelConf(nodeConfig); (err != nil) != tt.wantErr {
t.Errorf("createFlannelConf() error = %v, wantErr %v", err, tt.wantErr)
}
data, err := ioutil.ReadFile("test_file")
if err != nil {
t.Errorf("Something went wrong when reading the flannel config file")
}
for _, config := range tt.wantConfig {
isExist, _ := regexp.Match(config, data)
if !isExist {
t.Errorf("Config is wrong, %s is not present", config)
}
}
})
}
}
2 changes: 1 addition & 1 deletion pkg/cli/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ func validateNetworkConfiguration(serverConfig server.Config) error {
return errors.Wrap(err, "failed to validate cluster-dns")
}

if (serverConfig.ControlConfig.FlannelBackend != "none" || serverConfig.ControlConfig.DisableNPC == false) && (dualCluster || dualService) {
if (serverConfig.ControlConfig.DisableNPC == false) && (dualCluster || dualService) {
return errors.New("flannel CNI and network policy enforcement are not compatible with dual-stack operation; server must be restarted with --flannel-backend=none --disable-network-policy and an alternative CNI plugin deployed")
}
if dualDNS == true {
Expand Down
55 changes: 55 additions & 0 deletions tests/integration/dual_stack_int_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package integration

import (
"strings"
"testing"

. "github.com/onsi/ginkgo"
"github.com/onsi/ginkgo/reporters"
. "github.com/onsi/gomega"
testutil "github.com/rancher/k3s/tests/util"
)

var server *testutil.K3sServer
var serverArgs = []string{"--cluster-init", "--cluster-cidr 10.42.0.0/16,2001:cafe:42:0::/56", "--service-cidr 10.43.0.0/16,2001:cafe:42:1::/112"}
var _ = BeforeSuite(func() {
if !testutil.IsExistingServer() {
var err error
server, err = testutil.K3sStartServer(serverArgs...)
Expect(err).ToNot(HaveOccurred())
}
})

var _ = Describe("dual stack", func() {
BeforeEach(func() {
if testutil.IsExistingServer() && !testutil.ServerArgsPresent(serverArgs) {
Skip("Test needs k3s server with: " + strings.Join(serverArgs, " "))
}
})
When("a ipv4 and ipv6 cidr is present", func() {
It("starts up with no problems", func() {
Eventually(func() (string, error) {
return testutil.K3sCmd("kubectl", "get", "pods", "-A")
}, "90s", "1s").Should(MatchRegexp("kube-system.+traefik.+1\\/1.+Running"))
})
It("creates pods with two IPs", func() {
podname, err := testutil.K3sCmd("kubectl", "get", "pods", "-nkube-system", "-ojsonpath={.items[?(@.metadata.labels.app\\.kubernetes\\.io/name==\"traefik\")].metadata.name}")
result, err := testutil.K3sCmd("kubectl", "exec", podname, "-nkube-system", "--", "ip", "a")
Expect(result).To(ContainSubstring("2001:cafe:42:"))
Expect(err).NotTo(HaveOccurred())
})
})
})

var _ = AfterSuite(func() {
if !testutil.IsExistingServer() {
Expect(testutil.K3sKillServer(server)).To(Succeed())
}
})

func Test_IntegrationDualStack(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecsWithDefaultAndCustomReporters(t, "Dual-Stack Suite", []Reporter{
reporters.NewJUnitReporter("/tmp/results/junit-ls.xml"),
})
}

0 comments on commit 262b397

Please sign in to comment.