Skip to content

Commit

Permalink
Moved testing utils into tests directory. Improved gotests template. (k…
Browse files Browse the repository at this point in the history
…3s-io#3805)

* Moved testing utils into tests directory. Improved gotests template.
* Updated cgroups2 with util folder rename

Signed-off-by: dereknola <[email protected]>
  • Loading branch information
dereknola authored Aug 10, 2021
1 parent dcf0657 commit 4cc781b
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 56 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/cgroup2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
path: ./tests/cgroup2
- name: "Boot Fedora VM"
run: |
cp -r k3s.service k3s-rootless.service ./tests/testutil ./tests/cgroup2
cp -r k3s.service k3s-rootless.service ./tests/util ./tests/cgroup2
cd ./tests/cgroup2
vagrant up
vagrant ssh-config >> ~/.ssh/config
Expand All @@ -51,7 +51,7 @@ jobs:
# Sonobuoy requires CoreDNS to be ready
- name: "Waiting for CoreDNS to be ready"
run: |
ssh default -- sudo KUBECONFIG=/etc/rancher/k3s/k3s.yaml /vagrant/testutil/wait-for-coredns.sh
ssh default -- sudo KUBECONFIG=/etc/rancher/k3s/k3s.yaml /vagrant/util/wait-for-coredns.sh
# Vagrant is slow, so we set --mode=quick here
- name: "Run Sonobuoy (--mode=quick)"
run: |
Expand All @@ -69,7 +69,7 @@ jobs:
ssh default -- systemctl --user start k3s-rootless
- name: "[Rootless] Waiting for CoreDNS to be ready"
run: |
ssh default -- KUBECONFIG=/home/vagrant/.kube/k3s.yaml /vagrant/testutil/wait-for-coredns.sh
ssh default -- KUBECONFIG=/home/vagrant/.kube/k3s.yaml /vagrant/util/wait-for-coredns.sh
- name: "[Rootless] Run Sonobuoy (--mode=quick)"
run: |
ssh default -- KUBECONFIG=/home/vagrant/.kube/k3s.yaml sonobuoy run --mode=quick --wait
2 changes: 1 addition & 1 deletion contrib/gotests_templates/function.tmpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{define "function"}}
{{- $f := .}}

func {{.TestName}}(t *testing.T) {
func Test_Unit{{.FullName}}(t *testing.T) {
{{- with .Receiver}}
{{- if .IsStruct}}
{{- if .Fields}}
Expand Down
42 changes: 21 additions & 21 deletions pkg/etcd/etcd_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,76 +9,76 @@ import (

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

var serverCmd *exec.Cmd
var _ = BeforeSuite(func() {
var err error
serverCmd, _, err = tests.K3sCmdAsync("server", "--cluster-init")
serverCmd, _, err = testutil.K3sCmdAsync("server", "--cluster-init")
Expect(err).ToNot(HaveOccurred())
})

var _ = Describe("etcd snapshots", func() {
When("a new etcd is created", func() {
It("starts up with no problems", func() {
Eventually(func() (string, error) {
return tests.K3sCmd("kubectl", "get", "pods", "-A")
return testutil.K3sCmd("kubectl", "get", "pods", "-A")
}, "90s", "1s").Should(MatchRegexp("kube-system.+coredns.+1\\/1.+Running"))
})
It("saves an etcd snapshot", func() {
Expect(tests.K3sCmd("etcd-snapshot", "save")).
Expect(testutil.K3sCmd("etcd-snapshot", "save")).
To(ContainSubstring("Saving current etcd snapshot set to k3s-etcd-snapshots"))
})
It("list snapshots", func() {
Expect(tests.K3sCmd("etcd-snapshot", "ls")).
Expect(testutil.K3sCmd("etcd-snapshot", "ls")).
To(MatchRegexp(`:///var/lib/rancher/k3s/server/db/snapshots/on-demand`))
})
It("deletes a snapshot", func() {
lsResult, err := tests.K3sCmd("etcd-snapshot", "ls")
lsResult, err := testutil.K3sCmd("etcd-snapshot", "ls")
Expect(err).ToNot(HaveOccurred())
reg, err := regexp.Compile(`on-demand[^\s]+`)
Expect(err).ToNot(HaveOccurred())
snapshotName := reg.FindString(lsResult)
Expect(tests.K3sCmd("etcd-snapshot", "delete", snapshotName)).
Expect(testutil.K3sCmd("etcd-snapshot", "delete", snapshotName)).
To(ContainSubstring("Removing the given locally stored etcd snapshot"))
})
})
When("saving a custom name", func() {
It("starts with no snapshots", func() {
Expect(tests.K3sCmd("etcd-snapshot", "ls")).To(BeEmpty())
Expect(testutil.K3sCmd("etcd-snapshot", "ls")).To(BeEmpty())
})
It("saves an etcd snapshot with a custom name", func() {
Expect(tests.K3sCmd("etcd-snapshot", "save", "--name", "ALIVEBEEF")).
Expect(testutil.K3sCmd("etcd-snapshot", "save", "--name", "ALIVEBEEF")).
To(ContainSubstring("Saving etcd snapshot to /var/lib/rancher/k3s/server/db/snapshots/ALIVEBEEF"))
})
It("deletes that snapshot", func() {
lsResult, err := tests.K3sCmd("etcd-snapshot", "ls")
lsResult, err := testutil.K3sCmd("etcd-snapshot", "ls")
Expect(err).ToNot(HaveOccurred())
reg, err := regexp.Compile(`ALIVEBEEF[^\s]+`)
Expect(err).ToNot(HaveOccurred())
snapshotName := reg.FindString(lsResult)
Expect(tests.K3sCmd("etcd-snapshot", "delete", snapshotName)).
Expect(testutil.K3sCmd("etcd-snapshot", "delete", snapshotName)).
To(ContainSubstring("Removing the given locally stored etcd snapshot"))
})
})
When("using etcd snapshot prune", func() {
It("starts with no snapshots", func() {
Expect(tests.K3sCmd("etcd-snapshot", "ls")).To(BeEmpty())
Expect(testutil.K3sCmd("etcd-snapshot", "ls")).To(BeEmpty())
})
It("saves 3 different snapshots", func() {
Expect(tests.K3sCmd("etcd-snapshot", "save", "-name", "PRUNE_TEST")).
Expect(testutil.K3sCmd("etcd-snapshot", "save", "-name", "PRUNE_TEST")).
To(ContainSubstring("Saving current etcd snapshot set to k3s-etcd-snapshots"))
time.Sleep(1 * time.Second)
Expect(tests.K3sCmd("etcd-snapshot", "save", "-name", "PRUNE_TEST")).
Expect(testutil.K3sCmd("etcd-snapshot", "save", "-name", "PRUNE_TEST")).
To(ContainSubstring("Saving current etcd snapshot set to k3s-etcd-snapshots"))
time.Sleep(1 * time.Second)
Expect(tests.K3sCmd("etcd-snapshot", "save", "-name", "PRUNE_TEST")).
Expect(testutil.K3sCmd("etcd-snapshot", "save", "-name", "PRUNE_TEST")).
To(ContainSubstring("Saving current etcd snapshot set to k3s-etcd-snapshots"))
time.Sleep(1 * time.Second)
})
It("lists all 3 snapshots", func() {
lsResult, err := tests.K3sCmd("etcd-snapshot", "ls")
lsResult, err := testutil.K3sCmd("etcd-snapshot", "ls")
Expect(err).ToNot(HaveOccurred())
sepLines := strings.FieldsFunc(lsResult, func(c rune) bool {
return c == '\n'
Expand All @@ -87,9 +87,9 @@ var _ = Describe("etcd snapshots", func() {
Expect(sepLines).To(HaveLen(3))
})
It("prunes snapshots down to 2", func() {
Expect(tests.K3sCmd("etcd-snapshot", "prune", "--snapshot-retention", "2", "--name", "PRUNE_TEST")).
Expect(testutil.K3sCmd("etcd-snapshot", "prune", "--snapshot-retention", "2", "--name", "PRUNE_TEST")).
To(BeEmpty())
lsResult, err := tests.K3sCmd("etcd-snapshot", "ls")
lsResult, err := testutil.K3sCmd("etcd-snapshot", "ls")
Expect(err).ToNot(HaveOccurred())
sepLines := strings.FieldsFunc(lsResult, func(c rune) bool {
return c == '\n'
Expand All @@ -98,20 +98,20 @@ var _ = Describe("etcd snapshots", func() {
Expect(sepLines).To(HaveLen(2))
})
It("cleans up remaining snapshots", func() {
lsResult, err := tests.K3sCmd("etcd-snapshot", "ls")
lsResult, err := testutil.K3sCmd("etcd-snapshot", "ls")
Expect(err).ToNot(HaveOccurred())
reg, err := regexp.Compile(`PRUNE_TEST[^\s]+`)
Expect(err).ToNot(HaveOccurred())
for _, snapshotName := range reg.FindAllString(lsResult, -1) {
Expect(tests.K3sCmd("etcd-snapshot", "delete", snapshotName)).
Expect(testutil.K3sCmd("etcd-snapshot", "delete", snapshotName)).
To(ContainSubstring("Removing the given locally stored etcd snapshot"))
}
})
})
})

var _ = AfterSuite(func() {
Expect(tests.K3sKillAsync(serverCmd)).To(Succeed())
Expect(testutil.K3sKillAsync(serverCmd)).To(Succeed())
})

func Test_IntegrationEtcd(t *testing.T) {
Expand Down
30 changes: 15 additions & 15 deletions pkg/etcd/etcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/rancher/k3s/pkg/clientaccess"
"github.com/rancher/k3s/pkg/daemons/config"
"github.com/rancher/k3s/pkg/util/tests"
testutil "github.com/rancher/k3s/tests/util"
"github.com/robfig/cron/v3"
etcd "go.etcd.io/etcd/clientv3"
)
Expand Down Expand Up @@ -62,13 +62,13 @@ func Test_UnitETCD_IsInitialized(t *testing.T) {
config: generateTestConfig(),
},
setup: func(cnf *config.Control) error {
if err := tests.GenerateDataDir(cnf); err != nil {
if err := testutil.GenerateDataDir(cnf); err != nil {
return err
}
return os.MkdirAll(walDir(cnf), 0700)
},
teardown: func(cnf *config.Control) error {
tests.CleanupDataDir(cnf)
testutil.CleanupDataDir(cnf)
return os.Remove(walDir(cnf))
},
wantErr: false,
Expand All @@ -81,15 +81,15 @@ func Test_UnitETCD_IsInitialized(t *testing.T) {
config: generateTestConfig(),
},
setup: func(cnf *config.Control) error {
if err := tests.GenerateDataDir(cnf); err != nil {
if err := testutil.GenerateDataDir(cnf); err != nil {
return err
}
// We don't care if removal fails to find the dir
os.Remove(walDir(cnf))
return nil
},
teardown: func(cnf *config.Control) error {
tests.CleanupDataDir(cnf)
testutil.CleanupDataDir(cnf)
return nil
},
wantErr: false,
Expand Down Expand Up @@ -138,10 +138,10 @@ func Test_UnitETCD_Register(t *testing.T) {
handler: generateTestHandler(),
},
setup: func(cnf *config.Control) error {
return tests.GenerateRuntime(cnf)
return testutil.GenerateRuntime(cnf)
},
teardown: func(cnf *config.Control) error {
tests.CleanupDataDir(cnf)
testutil.CleanupDataDir(cnf)
return nil
},
},
Expand All @@ -153,7 +153,7 @@ func Test_UnitETCD_Register(t *testing.T) {
handler: generateTestHandler(),
},
setup: func(cnf *config.Control) error {
if err := tests.GenerateRuntime(cnf); err != nil {
if err := testutil.GenerateRuntime(cnf); err != nil {
return err
}
if err := os.MkdirAll(etcdDBDir(cnf), 0700); err != nil {
Expand All @@ -168,7 +168,7 @@ func Test_UnitETCD_Register(t *testing.T) {
teardown: func(cnf *config.Control) error {
tombstoneFile := filepath.Join(etcdDBDir(cnf), "tombstone")
os.Remove(tombstoneFile)
tests.CleanupDataDir(cnf)
testutil.CleanupDataDir(cnf)
return nil
},
},
Expand Down Expand Up @@ -225,10 +225,10 @@ func Test_UnitETCD_Start(t *testing.T) {
},
setup: func(cnf *config.Control) error {
cnf.EtcdDisableSnapshots = true
return tests.GenerateRuntime(cnf)
return testutil.GenerateRuntime(cnf)
},
teardown: func(cnf *config.Control) error {
tests.CleanupDataDir(cnf)
testutil.CleanupDataDir(cnf)
return nil
},
},
Expand All @@ -244,10 +244,10 @@ func Test_UnitETCD_Start(t *testing.T) {
clientAccessInfo: nil,
},
setup: func(cnf *config.Control) error {
return tests.GenerateRuntime(cnf)
return testutil.GenerateRuntime(cnf)
},
teardown: func(cnf *config.Control) error {
tests.CleanupDataDir(cnf)
testutil.CleanupDataDir(cnf)
return nil
},
},
Expand All @@ -263,13 +263,13 @@ func Test_UnitETCD_Start(t *testing.T) {
clientAccessInfo: nil,
},
setup: func(cnf *config.Control) error {
if err := tests.GenerateRuntime(cnf); err != nil {
if err := testutil.GenerateRuntime(cnf); err != nil {
return err
}
return os.MkdirAll(walDir(cnf), 0700)
},
teardown: func(cnf *config.Control) error {
tests.CleanupDataDir(cnf)
testutil.CleanupDataDir(cnf)
os.Remove(walDir(cnf))
return nil
},
Expand Down
2 changes: 1 addition & 1 deletion tests/cgroup2/.gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
k3s
k3s.service
k3s-rootless.service
testutil/
util/
.vagrant/
2 changes: 1 addition & 1 deletion tests/cgroup2/Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# - k3s
# - k3s.service
# - k3s-rootless.service
# - testutil/
# - util/
Vagrant.configure("2") do |config|
config.vm.box = "fedora/34-cloud-base"
memory = 2048
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tests
package integration

import (
"fmt"
Expand All @@ -10,38 +10,38 @@ import (

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

var serverCmd *exec.Cmd
var _ = BeforeSuite(func() {
var err error
serverCmd, _, err = tests.K3sCmdAsync("server", "--cluster-init")
serverCmd, _, err = testutil.K3sCmdAsync("server", "--cluster-init")
Expect(err).ToNot(HaveOccurred())
})

var _ = Describe("local storage", func() {
When("a new local storage is created", func() {
It("starts up with no problems", func() {
Eventually(func() (string, error) {
return tests.K3sCmd("kubectl", "get", "pods", "-A")
return testutil.K3sCmd("kubectl", "get", "pods", "-A")
}, "90s", "1s").Should(MatchRegexp("kube-system.+coredns.+1\\/1.+Running"))
})
It("creates a new pvc", func() {
Expect(tests.K3sCmd("kubectl", "create", "-f", "testdata/localstorage_pvc.yaml")).
Expect(testutil.K3sCmd("kubectl", "create", "-f", "../testdata/localstorage_pvc.yaml")).
To(ContainSubstring("persistentvolumeclaim/local-path-pvc created"))
})
It("creates a new pod", func() {
Expect(tests.K3sCmd("kubectl", "create", "-f", "testdata/localstorage_pod.yaml")).
Expect(testutil.K3sCmd("kubectl", "create", "-f", "../testdata/localstorage_pod.yaml")).
To(ContainSubstring("pod/volume-test created"))
})
time.Sleep(30 * time.Second)
It("shows storage up in kubectl", func() {
Eventually(func() (string, error) {
return tests.K3sCmd("kubectl", "get", "pv")
return testutil.K3sCmd("kubectl", "get", "pv")
}, "30s", "1s").Should(MatchRegexp(`pvc.+2Gi.+Bound`))
Eventually(func() (string, error) {
return tests.K3sCmd("kubectl", "get", "pvc")
return testutil.K3sCmd("kubectl", "get", "pvc")
}, "10s", "1s").Should(MatchRegexp(`local-path-pvc.+Bound`))
})
It("has proper folder permissions", func() {
Expand All @@ -50,7 +50,7 @@ var _ = Describe("local storage", func() {
Expect(err).ToNot(HaveOccurred())
Expect(fmt.Sprintf("%04o", fileStat.Mode().Perm())).To(Equal("0701"))

pvResult, err := tests.K3sCmd("kubectl", "get", "pv")
pvResult, err := testutil.K3sCmd("kubectl", "get", "pv")
Expect(err).ToNot(HaveOccurred())
reg, err := regexp.Compile(`pvc[^\s]+`)
Expect(err).ToNot(HaveOccurred())
Expand All @@ -60,16 +60,16 @@ var _ = Describe("local storage", func() {
Expect(fmt.Sprintf("%04o", fileStat.Mode().Perm())).To(Equal("0777"))
})
It("deletes properly", func() {
Expect(tests.K3sCmd("kubectl", "delete", "pod", "volume-test")).
Expect(testutil.K3sCmd("kubectl", "delete", "pod", "volume-test")).
To(ContainSubstring("pod \"volume-test\" deleted"))
Expect(tests.K3sCmd("kubectl", "delete", "pvc", "local-path-pvc")).
Expect(testutil.K3sCmd("kubectl", "delete", "pvc", "local-path-pvc")).
To(ContainSubstring("persistentvolumeclaim \"local-path-pvc\" deleted"))
})
})
})

var _ = AfterSuite(func() {
Expect(tests.K3sKillAsync(serverCmd)).To(Succeed())
Expect(testutil.K3sKillAsync(serverCmd)).To(Succeed())
})

func Test_IntegrationLocalStorage(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/tests/cmd.go → tests/util/cmd.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tests
package util

import (
"bufio"
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/tests/runtime.go → tests/util/runtime.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tests
package util

import (
"os"
Expand Down
File renamed without changes.

0 comments on commit 4cc781b

Please sign in to comment.