Skip to content

Commit

Permalink
use it.Pend instead of return to skip tests not working with root
Browse files Browse the repository at this point in the history
Co-authored-by: Philipp Stehle <[email protected]>
  • Loading branch information
c0d1ngm0nk3y and phil9909 committed Jan 10, 2024
1 parent f25b563 commit d0507d6
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 27 deletions.
12 changes: 2 additions & 10 deletions certificate_loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,7 @@ func testCertificateLoader(t *testing.T, context spec.G, it spec.S) {
Expect(ks).To(HaveLen(3))
})

if internal.IsRoot() {
return
}

it("does not return error when keystore is read-only", func() {
internal.SkipIfRoot(it, "does not return error when keystore is read-only", func() {
Expect(os.Chmod(path, 0555)).To(Succeed())

c := libjvm.CertificateLoader{
Expand Down Expand Up @@ -256,11 +252,7 @@ func testCertificateLoader(t *testing.T, context spec.G, it spec.S) {
Expect(ks.Aliases()).To(HaveLen(3))
})

if internal.IsRoot() {
return
}

it("does not return error when keystore is read-only", func() {
internal.SkipIfRoot(it, "does not return error when keystore is read-only", func() {
Expect(os.Chmod(path, 0555)).To(Succeed())

c := libjvm.CertificateLoader{
Expand Down
6 changes: 1 addition & 5 deletions helper/link_local_dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,7 @@ networkaddress.cache.negative.ttl=0
`)))
})

if internal.IsRoot() {
return
}

it("warns if file is read-only", func() {
internal.SkipIfRoot(it, "warns if file is read-only", func() {
Expect(os.Chmod(path, 0555)).To(Succeed())

config := &ddns.ClientConfig{Servers: []string{"169.254.0.1"}}
Expand Down
8 changes: 2 additions & 6 deletions helper/openssl_certificate_loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,7 @@ func testOpenSSLCertificateLoader(t *testing.T, context spec.G, it spec.S) {
Expect(ks.Aliases()).To(HaveLen(3))
})

if internal.IsRoot() {
return
}

it("does use temp keystore if keystore is read-only", func() {
internal.SkipIfRoot(it, "does use temp keystore if keystore is read-only", func() {
Expect(os.Chmod(path, 0555)).To(Succeed())

o := helper.OpenSSLCertificateLoader{CertificateLoader: cl, Logger: bard.NewLogger(ioutil.Discard)}
Expand All @@ -122,7 +118,7 @@ func testOpenSSLCertificateLoader(t *testing.T, context spec.G, it spec.S) {
Expect(env).To(HaveKeyWithValue("JAVA_TOOL_OPTIONS", fmt.Sprintf("-Djavax.net.ssl.trustStore=%s", helper.TmpTrustStore)))
})

it("does not return error when keystore and /tmp/truststore are read-only", func() {
internal.SkipIfRoot(it, "does not return error when keystore and /tmp/truststore are read-only", func() {
Expect(os.Chmod(path, 0555)).To(Succeed())
_, err := os.OpenFile(helper.TmpTrustStore, os.O_CREATE, 0)
Expect(err).NotTo(HaveOccurred())
Expand Down
6 changes: 1 addition & 5 deletions helper/security_providers_configurer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,7 @@ security.provider.6=FOXTROT
`)))
})

if internal.IsRoot() {
return
}

it("warns if the file is read-only", func() {
internal.SkipIfRoot(it, "warns if the file is read-only", func() {
Expect(os.Chmod(path, 0555)).To(Succeed())

Expect(helper.SecurityProvidersConfigurer{}.Execute()).To(BeNil())
Expand Down
11 changes: 10 additions & 1 deletion internal/if_not_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,18 @@ package internal

import (
"os/user"

"github.com/sclevine/spec"
)

func IsRoot() bool {
func isRoot() bool {
u, _ := user.Current()
return u.Username == "root"
}

func SkipIfRoot(it spec.S, text string, f func(), options ...spec.Option) {
if isRoot() {
it = it.Pend
}
it(text, f, options...)
}

0 comments on commit d0507d6

Please sign in to comment.