Skip to content

Commit

Permalink
add kube-* server flags integration tests
Browse files Browse the repository at this point in the history
This commit adds SearchK3sLog to find specific strings in integration tests log file and also removes FindStringInCmdAsync function since it was not being used.

Signed-off-by: Ian Cardoso <[email protected]>
  • Loading branch information
osodracnai committed May 3, 2023
1 parent a3ddff2 commit a7a5d4b
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 3 deletions.
23 changes: 20 additions & 3 deletions tests/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,21 @@ func GetPersistentVolume(name string) (*corev1.PersistentVolume, error) {
return client.CoreV1().PersistentVolumes().Get(context.Background(), name, metav1.GetOptions{})
}

func FindStringInCmdAsync(scanner *bufio.Scanner, target string) bool {
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 Expand Up @@ -346,6 +354,15 @@ func K3sSaveLog(server *K3sServer, dump bool) error {
return nil
}

func ReadK3sLogFile(server *K3sServer) (string, error) {
bufio.NewScanner(server.log)
log, err := os.ReadFile("./k3log.txt")
if err != nil {
return "", err
}
return string(log), err
}

// RunCommand Runs command on the host
func RunCommand(cmd string) (string, error) {
c := exec.Command("bash", "-c", cmd)
Expand Down
107 changes: 107 additions & 0 deletions tests/integration/kubeflags/kubeflags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package kubeflags

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

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",
}
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())
})
})
})

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")
}

0 comments on commit a7a5d4b

Please sign in to comment.