mirror of
https://github.com/k3s-io/k3s.git
synced 2024-06-07 19:41:36 +00:00
Moved testing utils into tests directory. Improved gotests template. (#3805)
* Moved testing utils into tests directory. Improved gotests template. * Updated cgroups2 with util folder rename Signed-off-by: dereknola <derek.nola@suse.com>
This commit is contained in:
parent
dcf0657b20
commit
4cc781b5e3
6
.github/workflows/cgroup2.yaml
vendored
6
.github/workflows/cgroup2.yaml
vendored
@ -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
|
||||
@ -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: |
|
||||
@ -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
|
||||
|
@ -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}}
|
||||
|
@ -9,13 +9,13 @@ 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())
|
||||
})
|
||||
|
||||
@ -23,62 +23,62 @@ 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'
|
||||
@ -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'
|
||||
@ -98,12 +98,12 @@ 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"))
|
||||
}
|
||||
})
|
||||
@ -111,7 +111,7 @@ var _ = Describe("etcd snapshots", func() {
|
||||
})
|
||||
|
||||
var _ = AfterSuite(func() {
|
||||
Expect(tests.K3sKillAsync(serverCmd)).To(Succeed())
|
||||
Expect(testutil.K3sKillAsync(serverCmd)).To(Succeed())
|
||||
})
|
||||
|
||||
func Test_IntegrationEtcd(t *testing.T) {
|
||||
|
@ -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"
|
||||
)
|
||||
@ -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,
|
||||
@ -81,7 +81,7 @@ 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
|
||||
@ -89,7 +89,7 @@ func Test_UnitETCD_IsInitialized(t *testing.T) {
|
||||
return nil
|
||||
},
|
||||
teardown: func(cnf *config.Control) error {
|
||||
tests.CleanupDataDir(cnf)
|
||||
testutil.CleanupDataDir(cnf)
|
||||
return nil
|
||||
},
|
||||
wantErr: false,
|
||||
@ -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
|
||||
},
|
||||
},
|
||||
@ -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 {
|
||||
@ -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
|
||||
},
|
||||
},
|
||||
@ -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
|
||||
},
|
||||
},
|
||||
@ -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
|
||||
},
|
||||
},
|
||||
@ -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
|
||||
},
|
||||
|
2
tests/cgroup2/.gitignore
vendored
2
tests/cgroup2/.gitignore
vendored
@ -1,5 +1,5 @@
|
||||
k3s
|
||||
k3s.service
|
||||
k3s-rootless.service
|
||||
testutil/
|
||||
util/
|
||||
.vagrant/
|
||||
|
2
tests/cgroup2/Vagrantfile
vendored
2
tests/cgroup2/Vagrantfile
vendored
@ -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
|
||||
|
@ -1,4 +1,4 @@
|
||||
package tests
|
||||
package integration
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@ -10,13 +10,13 @@ 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())
|
||||
})
|
||||
|
||||
@ -24,24 +24,24 @@ 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() {
|
||||
@ -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())
|
||||
@ -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) {
|
@ -1,4 +1,4 @@
|
||||
package tests
|
||||
package util
|
||||
|
||||
import (
|
||||
"bufio"
|
@ -1,4 +1,4 @@
|
||||
package tests
|
||||
package util
|
||||
|
||||
import (
|
||||
"os"
|
Loading…
Reference in New Issue
Block a user