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
This commit is contained in:
Ian Howell 2019-08-08 11:43:01 -05:00
parent bba1bd9d3d
commit 513e3b8701
2 changed files with 37 additions and 17 deletions

View File

@ -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())
})

View File

@ -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)))
})
})