Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Release-1.24] Kube flags and longhorn tests 1.24 #7467

Merged
merged 2 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 37 additions & 5 deletions tests/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,12 @@ func CheckDeployments(deployments []string) error {
return nil
}

func ParsePods(opts metav1.ListOptions) ([]corev1.Pod, error) {
func ParsePods(namespace string, opts metav1.ListOptions) ([]corev1.Pod, error) {
clientSet, err := k8sClient()
if err != nil {
return nil, err
}
pods, err := clientSet.CoreV1().Pods("").List(context.Background(), opts)
pods, err := clientSet.CoreV1().Pods(namespace).List(context.Background(), opts)
if err != nil {
return nil, err
}
Expand All @@ -189,13 +189,45 @@ func ParseNodes() ([]corev1.Node, error) {
return nodes.Items, nil
}

func FindStringInCmdAsync(scanner *bufio.Scanner, target string) bool {
func GetPod(namespace, name string) (*corev1.Pod, error) {
client, err := k8sClient()
if err != nil {
return nil, err
}
return client.CoreV1().Pods(namespace).Get(context.Background(), name, metav1.GetOptions{})
}

func GetPersistentVolumeClaim(namespace, name string) (*corev1.PersistentVolumeClaim, error) {
client, err := k8sClient()
if err != nil {
return nil, err
}
return client.CoreV1().PersistentVolumeClaims(namespace).Get(context.Background(), name, metav1.GetOptions{})
}

func GetPersistentVolume(name string) (*corev1.PersistentVolume, error) {
client, err := k8sClient()
if err != nil {
return nil, err
}
return client.CoreV1().PersistentVolumes().Get(context.Background(), name, metav1.GetOptions{})
}

func SearchK3sLog(k3s *K3sServer, target string) (bool, error) {
file, err := os.Open(k3s.log.Name())
if err != nil {
return false, err
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if strings.Contains(scanner.Text(), target) {
return true
return true, nil
}
}
return false
if scanner.Err() != nil {
return false, scanner.Err()
}
return false, nil
}

func K3sTestLock() (int, error) {
Expand Down
135 changes: 135 additions & 0 deletions tests/integration/kubeflags/kubeflags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package kubeflags

import (
"strings"
"testing"

testutil "github.com/k3s-io/k3s/tests/integration"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/pkg/errors"
)

var server *testutil.K3sServer
var serverArgs = []string{"--cluster-init",
"--kube-apiserver-arg", "advertise-port=1234",
"--kube-controller-manager-arg", "allocate-node-cidrs=false",
"--kube-scheduler-arg", "authentication-kubeconfig=test",
"--kube-cloud-controller-manager-arg", "allocate-node-cidrs=false",
"--kubelet-arg", "address=127.0.0.1",
"--kube-proxy-arg", "cluster-cidr=127.0.0.1/16",
}
var testLock int

var _ = BeforeSuite(func() {
if !testutil.IsExistingServer() {
var err error
testLock, err = testutil.K3sTestLock()
Expect(err).ToNot(HaveOccurred())
server, err = testutil.K3sStartServer(serverArgs...)
Expect(err).ToNot(HaveOccurred())
}
})

var _ = Describe("create a new cluster with kube-* flags", Ordered, func() {
BeforeEach(func() {
if testutil.IsExistingServer() && !testutil.ServerArgsPresent(serverArgs) {
Skip("Test needs k3s server with: " + strings.Join(serverArgs, " "))
}
})
When("should print the args on the console", func() {
It("should find cloud-controller-manager starting", func() {
Eventually(func() error {
match, err := testutil.SearchK3sLog(server, "Running cloud-controller-manager --allocate-node-cidrs=false")
if err != nil {
return err
}
if match {
return nil
}
return errors.New("error finding cloud-controller-manager")
}, "30s", "2s").Should(Succeed())
})
It("should find kube-scheduler starting", func() {
Eventually(func() error {
match, err := testutil.SearchK3sLog(server, "Running kube-scheduler --authentication-kubeconfig=test")
if err != nil {
return err
}
if match {
return nil
}
return errors.New("error finding kube-scheduler")
}, "30s", "2s").Should(Succeed())
})
It("should find kube-apiserver starting", func() {
Eventually(func() error {
match, err := testutil.SearchK3sLog(server, "Running kube-apiserver --advertise-port=1234")
if err != nil {
return err
}
if match {
return nil
}
return errors.New("error finding kube-apiserver")
}, "30s", "2s").Should(Succeed())
})
It("should find kube-controller-manager starting", func() {
Eventually(func() error {
match, err := testutil.SearchK3sLog(server, "Running kube-controller-manager --allocate-node-cidrs=false")
if err != nil {
return err
}
if match {
return nil
}
return errors.New("error finding kube-controller-manager")
}, "30s", "2s").Should(Succeed())
})
It("should find kubelet starting", func() {
Eventually(func() error {
match, err := testutil.SearchK3sLog(server, "Running kubelet --address=127.0.0.1")
if err != nil {
return err
}
if match {
return nil
}
return errors.New("error finding kubelet")
}, "120s", "15s").Should(Succeed())
})
It("should find kube-proxy starting", func() {
Eventually(func() error {
match, err := testutil.SearchK3sLog(server, "Running kube-proxy --cluster-cidr=127.0.0.1/16")
if err != nil {
return err
}
if match {
return nil
}
return errors.New("error finding kube-proxy")
}, "120s", "15s").Should(Succeed())
})

})
})

var failed bool
var _ = AfterEach(func() {
failed = failed || CurrentSpecReport().Failed()
})

var _ = AfterSuite(func() {
if !testutil.IsExistingServer() {
if failed {
testutil.K3sSaveLog(server, false)
}
Expect(testutil.K3sKillServer(server)).To(Succeed())
Expect(testutil.K3sCleanup(testLock, "")).To(Succeed())
}
})

func Test_IntegrationEtcdSnapshot(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Etcd Snapshot Suite")
}
154 changes: 154 additions & 0 deletions tests/integration/longhorn/longhorn_int_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package longhorn

import (
"fmt"
"os/exec"
"strings"
"testing"

testutil "github.com/k3s-io/k3s/tests/integration"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var server *testutil.K3sServer
var serverArgs = []string{"--cluster-init"}
var testLock int

var _ = BeforeSuite(func() {
if _, err := exec.LookPath("iscsiadm"); err != nil {
Skip("Test needs open-iscsi to be installed")
} else if !testutil.IsExistingServer() {
var err error
testLock, err = testutil.K3sTestLock()
Expect(err).ToNot(HaveOccurred())
server, err = testutil.K3sStartServer(serverArgs...)
Expect(err).ToNot(HaveOccurred())
}
})

var _ = Describe("longhorn", Ordered, func() {
BeforeEach(func() {
if testutil.IsExistingServer() && !testutil.ServerArgsPresent(serverArgs) {
Skip("Test needs k3s server with: " + strings.Join(serverArgs, " "))
}
})

When("a new cluster is created", func() {
It("starts up with no problems", func() {
Eventually(func() error {
return testutil.K3sDefaultDeployments()
}, "120s", "5s").Should(Succeed())
})
})

When("longhorn is installed", func() {
It("installs components into the longhorn-system namespace", func() {
result, err := testutil.K3sCmd("kubectl apply -f ./testdata/longhorn.yaml")
Expect(err).NotTo(HaveOccurred())
Expect(result).To(ContainSubstring("namespace/longhorn-system created"))
Expect(result).To(ContainSubstring("daemonset.apps/longhorn-manager created"))
Expect(result).To(ContainSubstring("deployment.apps/longhorn-driver-deployer created"))
Expect(result).To(ContainSubstring("deployment.apps/longhorn-recovery-backend created"))
Expect(result).To(ContainSubstring("deployment.apps/longhorn-ui created"))
Expect(result).To(ContainSubstring("deployment.apps/longhorn-conversion-webhook created"))
Expect(result).To(ContainSubstring("deployment.apps/longhorn-admission-webhook created"))
})
It("starts the longhorn pods with no problems", func() {
Eventually(func() error {
pods, err := testutil.ParsePods("longhorn-system", metav1.ListOptions{})
if err != nil {
return err
}
for _, pod := range pods {
if pod.Status.Phase != "Running" && pod.Status.Phase != "Succeeded" {
return fmt.Errorf("pod %s failing", pod.Name)
}
}
return nil
}, "120s", "5s").Should(Succeed())
})
})

When("persistent volume claim is created", func() {
It("creates the pv and pvc", func() {
result, err := testutil.K3sCmd("kubectl create -f ./testdata/pvc.yaml")
Expect(err).NotTo(HaveOccurred())
Expect(result).To(ContainSubstring("persistentvolumeclaim/longhorn-volv-pvc created"))
Eventually(func() error {
pvc, err := testutil.GetPersistentVolumeClaim("default", "longhorn-volv-pvc")
if err != nil {
return fmt.Errorf("failed to get pvc longhorn-volv-pvc")
}
if pvc.Status.Phase != "Bound" {
return fmt.Errorf("pvc longhorn-volv-pvc not bound")
}
pv, err := testutil.GetPersistentVolume(pvc.Spec.VolumeName)
if err != nil {
return fmt.Errorf("failed to get pv %s", pvc.Spec.VolumeName)
}
if pv.Status.Phase != "Bound" {
return fmt.Errorf("pv %s not bound", pv.Name)
}
return nil
}, "300s", "5s").Should(Succeed())
})
It("creates a pod with the pvc", func() {
result, err := testutil.K3sCmd("kubectl create -f ./testdata/pod.yaml")
Expect(err).NotTo(HaveOccurred())
Expect(result).To(ContainSubstring("pod/volume-test created"))
Eventually(func() error {
pod, err := testutil.GetPod("default", "volume-test")
if err != nil {
return fmt.Errorf("failed to get pod volume-test")
}
if pod.Status.Phase != "Running" {
return fmt.Errorf("pod volume-test \"%s\" reason: \"%s\" message \"%s\"", pod.Status.Phase, pod.Status.Reason, pod.Status.Message)
}
return nil
}, "60s", "5s").Should(Succeed())
})
})

When("the pvc is deleted", func() {
It("the pv is deleted according to the default reclaim policy", func() {
result, err := testutil.K3sCmd("kubectl delete pod volume-test")
Expect(err).NotTo(HaveOccurred())
Expect(result).To(ContainSubstring("pod \"volume-test\" deleted"))
result, err = testutil.K3sCmd("kubectl delete pvc longhorn-volv-pvc")
Expect(err).NotTo(HaveOccurred())
Expect(result).To(ContainSubstring("persistentvolumeclaim \"longhorn-volv-pvc\" deleted"))
Eventually(func() error {
result, err = testutil.K3sCmd("kubectl get pv")
if err != nil {
return fmt.Errorf("failed get persistent volumes")
}
if !strings.Contains(result, "No resources found") {
return fmt.Errorf("persistent volumes still exist")
}
return nil
}, "60s", "5s").Should(Succeed())
})
})
})

var failed bool
var _ = AfterEach(func() {
failed = failed || CurrentSpecReport().Failed()
})

var _ = AfterSuite(func() {
if !testutil.IsExistingServer() {
if failed {
testutil.K3sSaveLog(server, false)
}
Expect(testutil.K3sKillServer(server)).To(Succeed())
Expect(testutil.K3sCleanup(testLock, "")).To(Succeed())
}
})

func Test_IntegrationLonghorn(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Longhorn Suite")
}
Loading