diff --git a/.github/workflows/cgroup2.yaml b/.github/workflows/cgroup2.yaml index 6ae79c9561..ae6153187c 100644 --- a/.github/workflows/cgroup2.yaml +++ b/.github/workflows/cgroup2.yaml @@ -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 diff --git a/contrib/gotests_templates/function.tmpl b/contrib/gotests_templates/function.tmpl index e850666855..f53d0eedf9 100644 --- a/contrib/gotests_templates/function.tmpl +++ b/contrib/gotests_templates/function.tmpl @@ -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}} diff --git a/pkg/etcd/etcd_int_test.go b/pkg/etcd/etcd_int_test.go index e5e0e0f1d5..4407528a5c 100644 --- a/pkg/etcd/etcd_int_test.go +++ b/pkg/etcd/etcd_int_test.go @@ -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) { diff --git a/pkg/etcd/etcd_test.go b/pkg/etcd/etcd_test.go index 6040b1afcf..bb1a09c36c 100644 --- a/pkg/etcd/etcd_test.go +++ b/pkg/etcd/etcd_test.go @@ -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 }, diff --git a/tests/cgroup2/.gitignore b/tests/cgroup2/.gitignore index 6108fd1e3c..7d75037630 100644 --- a/tests/cgroup2/.gitignore +++ b/tests/cgroup2/.gitignore @@ -1,5 +1,5 @@ k3s k3s.service k3s-rootless.service -testutil/ +util/ .vagrant/ diff --git a/tests/cgroup2/Vagrantfile b/tests/cgroup2/Vagrantfile index d8ac4cc0fb..3d0fef70eb 100644 --- a/tests/cgroup2/Vagrantfile +++ b/tests/cgroup2/Vagrantfile @@ -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 diff --git a/tests/localstorage_int_test.go b/tests/integration/localstorage_int_test.go similarity index 72% rename from tests/localstorage_int_test.go rename to tests/integration/localstorage_int_test.go index 099b7de159..e77344e378 100644 --- a/tests/localstorage_int_test.go +++ b/tests/integration/localstorage_int_test.go @@ -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) { diff --git a/pkg/util/tests/cmd.go b/tests/util/cmd.go similarity index 99% rename from pkg/util/tests/cmd.go rename to tests/util/cmd.go index 005e46a004..15094da708 100644 --- a/pkg/util/tests/cmd.go +++ b/tests/util/cmd.go @@ -1,4 +1,4 @@ -package tests +package util import ( "bufio" diff --git a/pkg/util/tests/runtime.go b/tests/util/runtime.go similarity index 99% rename from pkg/util/tests/runtime.go rename to tests/util/runtime.go index 7af97c9a2b..49f9c31d9d 100644 --- a/pkg/util/tests/runtime.go +++ b/tests/util/runtime.go @@ -1,4 +1,4 @@ -package tests +package util import ( "os" diff --git a/tests/testutil/wait-for-coredns.sh b/tests/util/wait-for-coredns.sh similarity index 100% rename from tests/testutil/wait-for-coredns.sh rename to tests/util/wait-for-coredns.sh