2021-08-10 18:13:26 +00:00
|
|
|
package integration
|
2021-07-28 17:25:34 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
2021-08-26 23:26:19 +00:00
|
|
|
"strings"
|
2021-07-28 17:25:34 +00:00
|
|
|
"testing"
|
|
|
|
|
2022-02-09 16:22:53 +00:00
|
|
|
. "github.com/onsi/ginkgo/v2"
|
2021-07-28 17:25:34 +00:00
|
|
|
. "github.com/onsi/gomega"
|
2021-08-10 18:13:26 +00:00
|
|
|
testutil "github.com/rancher/k3s/tests/util"
|
2021-07-28 17:25:34 +00:00
|
|
|
)
|
|
|
|
|
2021-10-28 19:35:28 +00:00
|
|
|
var localStorageServer *testutil.K3sServer
|
|
|
|
var localStorageServerArgs = []string{"--cluster-init"}
|
2022-02-25 16:42:21 +00:00
|
|
|
var testLock int
|
|
|
|
|
2021-07-28 17:25:34 +00:00
|
|
|
var _ = BeforeSuite(func() {
|
2021-08-26 23:26:19 +00:00
|
|
|
if !testutil.IsExistingServer() {
|
|
|
|
var err error
|
2022-02-25 16:42:21 +00:00
|
|
|
testLock, err = testutil.K3sTestLock()
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2021-10-28 19:35:28 +00:00
|
|
|
localStorageServer, err = testutil.K3sStartServer(localStorageServerArgs...)
|
2021-08-26 23:26:19 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
}
|
2021-07-28 17:25:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
var _ = Describe("local storage", func() {
|
2021-08-26 23:26:19 +00:00
|
|
|
BeforeEach(func() {
|
2021-10-28 19:35:28 +00:00
|
|
|
if testutil.IsExistingServer() && !testutil.ServerArgsPresent(localStorageServerArgs) {
|
|
|
|
Skip("Test needs k3s server with: " + strings.Join(localStorageServerArgs, " "))
|
2021-08-26 23:26:19 +00:00
|
|
|
}
|
|
|
|
})
|
2021-07-28 17:25:34 +00:00
|
|
|
When("a new local storage is created", func() {
|
|
|
|
It("starts up with no problems", func() {
|
|
|
|
Eventually(func() (string, error) {
|
2022-01-06 16:05:56 +00:00
|
|
|
return testutil.K3sCmd("kubectl get pods -A")
|
2021-07-28 17:25:34 +00:00
|
|
|
}, "90s", "1s").Should(MatchRegexp("kube-system.+coredns.+1\\/1.+Running"))
|
|
|
|
})
|
|
|
|
It("creates a new pvc", func() {
|
2022-01-06 16:05:56 +00:00
|
|
|
result, err := testutil.K3sCmd("kubectl create -f ./testdata/localstorage_pvc.yaml")
|
2021-08-30 15:27:59 +00:00
|
|
|
Expect(result).To(ContainSubstring("persistentvolumeclaim/local-path-pvc created"))
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2021-07-28 17:25:34 +00:00
|
|
|
})
|
|
|
|
It("creates a new pod", func() {
|
2022-01-06 16:05:56 +00:00
|
|
|
Expect(testutil.K3sCmd("kubectl create -f ./testdata/localstorage_pod.yaml")).
|
2021-07-28 17:25:34 +00:00
|
|
|
To(ContainSubstring("pod/volume-test created"))
|
|
|
|
})
|
|
|
|
It("shows storage up in kubectl", func() {
|
|
|
|
Eventually(func() (string, error) {
|
2022-01-06 16:05:56 +00:00
|
|
|
return testutil.K3sCmd("kubectl get --namespace=default pvc")
|
2021-08-10 23:22:12 +00:00
|
|
|
}, "45s", "1s").Should(MatchRegexp(`local-path-pvc.+Bound`))
|
|
|
|
Eventually(func() (string, error) {
|
2022-01-06 16:05:56 +00:00
|
|
|
return testutil.K3sCmd("kubectl get --namespace=default pv")
|
2021-12-07 22:31:32 +00:00
|
|
|
}, "10s", "1s").Should(MatchRegexp(`pvc.+1Gi.+Bound`))
|
2021-08-30 15:27:59 +00:00
|
|
|
Eventually(func() (string, error) {
|
2022-01-06 16:05:56 +00:00
|
|
|
return testutil.K3sCmd("kubectl get --namespace=default pod")
|
2021-08-30 15:27:59 +00:00
|
|
|
}, "10s", "1s").Should(MatchRegexp(`volume-test.+Running`))
|
2021-07-28 17:25:34 +00:00
|
|
|
})
|
|
|
|
It("has proper folder permissions", func() {
|
|
|
|
var k3sStorage = "/var/lib/rancher/k3s/storage"
|
|
|
|
fileStat, err := os.Stat(k3sStorage)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(fmt.Sprintf("%04o", fileStat.Mode().Perm())).To(Equal("0701"))
|
|
|
|
|
2022-01-06 16:05:56 +00:00
|
|
|
pvResult, err := testutil.K3sCmd("kubectl get --namespace=default pv")
|
2021-07-28 17:25:34 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
reg, err := regexp.Compile(`pvc[^\s]+`)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
volumeName := reg.FindString(pvResult) + "_default_local-path-pvc"
|
|
|
|
fileStat, err = os.Stat(k3sStorage + "/" + volumeName)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(fmt.Sprintf("%04o", fileStat.Mode().Perm())).To(Equal("0777"))
|
|
|
|
})
|
|
|
|
It("deletes properly", func() {
|
2022-01-06 16:05:56 +00:00
|
|
|
Expect(testutil.K3sCmd("kubectl delete --namespace=default --force pod volume-test")).
|
2021-08-30 15:27:59 +00:00
|
|
|
To(ContainSubstring("pod \"volume-test\" force deleted"))
|
2022-01-06 16:05:56 +00:00
|
|
|
Expect(testutil.K3sCmd("kubectl delete --namespace=default pvc local-path-pvc")).
|
2021-07-28 17:25:34 +00:00
|
|
|
To(ContainSubstring("persistentvolumeclaim \"local-path-pvc\" deleted"))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
var _ = AfterSuite(func() {
|
2021-08-26 23:26:19 +00:00
|
|
|
if !testutil.IsExistingServer() {
|
2022-02-25 16:42:21 +00:00
|
|
|
Expect(testutil.K3sKillServer(localStorageServer)).To(Succeed())
|
|
|
|
Expect(testutil.K3sCleanup(testLock, "")).To(Succeed())
|
2021-08-26 23:26:19 +00:00
|
|
|
}
|
2021-07-28 17:25:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
func Test_IntegrationLocalStorage(t *testing.T) {
|
|
|
|
RegisterFailHandler(Fail)
|
2022-02-09 16:22:53 +00:00
|
|
|
RunSpecs(t, "Local Storage Suite")
|
2021-07-28 17:25:34 +00:00
|
|
|
}
|