From 513e3b8701f70f17a1323e55d4804ad06fcde250 Mon Sep 17 00:00:00 2001 From: Ian Howell Date: Thu, 8 Aug 2019 11:43:01 -0500 Subject: [PATCH] This isolates several unit tests Prior to this change, the outcome from several unit tests could be modified dependent on certain preconditions. As an example, if the tests were run as the root user, socket_test would surely fail, since it depended on the user not having permissions to a file. Change-Id: I8e4b96ceb2d12209ff5908e4b69fc0b701e3bcde --- dependencies/config/config_test.go | 10 +++++-- dependencies/socket/socket_test.go | 44 ++++++++++++++++++++---------- 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/dependencies/config/config_test.go b/dependencies/config/config_test.go index 3d6c1c6..2d0cd46 100644 --- a/dependencies/config/config_test.go +++ b/dependencies/config/config_test.go @@ -65,8 +65,12 @@ func setupConfigTemplate(templatePath string) error { return nil } -func teardownConfigTemplate(templatePath string) (err error) { - if err := os.RemoveAll(templatePath); err != nil { +func teardownConfigTemplate() (err error) { + if err := os.RemoveAll(templatePrefix); err != nil { + return err + } + + if err := os.RemoveAll(testConfigPath); err != nil { return err } @@ -89,7 +93,7 @@ var _ = Describe("Config", func() { err := teardownOsEnvironment() Expect(err).NotTo(HaveOccurred()) - err = teardownConfigTemplate(testTemplatePath) + err = teardownConfigTemplate() Expect(err).NotTo(HaveOccurred()) }) diff --git a/dependencies/socket/socket_test.go b/dependencies/socket/socket_test.go index 934ed6f..058654e 100644 --- a/dependencies/socket/socket_test.go +++ b/dependencies/socket/socket_test.go @@ -2,7 +2,9 @@ package socket import ( "fmt" + "io/ioutil" "os" + "path/filepath" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" @@ -12,21 +14,45 @@ import ( ) const ( - existingSocketPath = "/tmp/k8s-existing-socket" - nonExistingSocketPath = "/tmp/k8s-nonexisting-socket" - noPermsSocketPath = "/root/k8s-no-permission-socket" + tempPathSuffix = "k8s-entrypoint" + + existingSocket = "existing-socket" + nonExistingSocket = "nonexisting-socket" +) + +var ( + testDir string + + existingSocketPath string + nonExistingSocketPath string ) var testEntrypoint entrypoint.EntrypointInterface var _ = Describe("Socket", func() { + // NOTE: It is impossible for a user to create a file that he does not + // have access to, and thus it is impossible to write an isolated unit + // test that checks for permission errors. That test is omitted from + // this suite + BeforeEach(func() { testEntrypoint = mocks.NewEntrypoint() - _, err := os.Create(existingSocketPath) + var err error + testDir, err = ioutil.TempDir("", tempPathSuffix) Expect(err).NotTo(HaveOccurred()) + existingSocketPath = filepath.Join(testDir, existingSocket) + nonExistingSocketPath = filepath.Join(testDir, nonExistingSocket) + + _, err = os.Create(existingSocketPath) + Expect(err).NotTo(HaveOccurred()) + }) + + AfterEach(func() { + err := os.RemoveAll(testDir) + Expect(err).NotTo(HaveOccurred()) }) It("checks the name of a newly created socket", func() { @@ -53,14 +79,4 @@ var _ = Describe("Socket", func() { Expect(err).To(HaveOccurred()) Expect(err.Error()).To(Equal(fmt.Sprintf(NonExistingErrorFormat, socket))) }) - - It("fails on trying to resolve a socket without permissions", func() { - socket := NewSocket(noPermsSocketPath) - - isResolved, err := socket.IsResolved(testEntrypoint) - - Expect(isResolved).To(Equal(false)) - Expect(err).To(HaveOccurred()) - Expect(err.Error()).To(Equal(fmt.Sprintf(NoPermsErrorFormat, socket))) - }) })