1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
package config
import (
"os"
"path/filepath"
"github.com/containers/storage/pkg/unshare"
. "github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
)
const (
testBaseHome = "testdata/modules/home/.config"
testBaseEtc = "testdata/modules/etc"
testBaseUsr = "testdata/modules/usr/share"
)
func testSetModulePaths() {
t := GinkgoT()
wd, err := os.Getwd()
gomega.Expect(err).ToNot(gomega.HaveOccurred())
t.Setenv("XDG_CONFIG_HOME", filepath.Join(wd, testBaseHome))
oldEtc := moduleBaseEtc
oldUsr := moduleBaseUsr
moduleBaseEtc = filepath.Join(wd, testBaseEtc)
moduleBaseUsr = filepath.Join(wd, testBaseUsr)
DeferCleanup(func() {
moduleBaseEtc = oldEtc
moduleBaseUsr = oldUsr
})
}
var _ = Describe("Config Modules", func() {
It("module directories", func() {
dirs, err := ModuleDirectories()
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(dirs).NotTo(gomega.BeNil())
if unshare.IsRootless() {
gomega.Expect(dirs).To(gomega.HaveLen(3))
} else {
gomega.Expect(dirs).To(gomega.HaveLen(2))
}
})
It("resolve modules", func() {
// This test makes sure that the correct module is being
// returned.
testSetModulePaths()
dirs, err := ModuleDirectories()
gomega.Expect(err).ToNot(gomega.HaveOccurred())
if unshare.IsRootless() {
gomega.Expect(dirs).To(gomega.HaveLen(3))
gomega.Expect(dirs[0]).To(gomega.ContainSubstring(testBaseHome))
gomega.Expect(dirs[1]).To(gomega.ContainSubstring(testBaseEtc))
gomega.Expect(dirs[2]).To(gomega.ContainSubstring(testBaseUsr))
} else {
gomega.Expect(dirs).To(gomega.HaveLen(2))
gomega.Expect(dirs[0]).To(gomega.ContainSubstring(testBaseEtc))
gomega.Expect(dirs[1]).To(gomega.ContainSubstring(testBaseUsr))
}
for _, test := range []struct {
input string
expectedDir string
mustFail bool
rootless bool
}{
// Rootless
{"first.conf", testBaseHome, false, true},
{"second.conf", testBaseHome, false, true},
{"third.conf", testBaseHome, false, true},
{"sub/first.conf", testBaseHome, false, true},
// Root + Rootless
{"fourth.conf", testBaseEtc, false, false},
{"sub/etc-only.conf", testBaseEtc, false, false},
{"fifth.conf", testBaseUsr, false, false},
{"sub/share-only.conf", testBaseUsr, false, false},
{"none.conf", "", true, false},
} {
if test.rootless && !unshare.IsRootless() {
continue
}
result, err := resolveModule(test.input, dirs)
if test.mustFail {
gomega.Expect(err).To(gomega.HaveOccurred())
continue
}
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(result).To(gomega.HaveSuffix(filepath.Join(test.expectedDir, moduleSubdir, test.input)))
}
})
It("new config with modules", func() {
testSetModulePaths()
wd, err := os.Getwd()
gomega.Expect(err).ToNot(gomega.HaveOccurred())
options := &Options{Modules: []string{"none.conf"}}
_, err = New(options)
gomega.Expect(err).To(gomega.HaveOccurred()) // must error out
options = &Options{}
c, err := New(options)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(options.additionalConfigs).To(gomega.BeEmpty()) // no module is getting loaded!
gomega.Expect(c).NotTo(gomega.BeNil())
gomega.Expect(c.LoadedModules()).To(gomega.BeEmpty())
options = &Options{Modules: []string{"fourth.conf"}}
c, err = New(options)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(options.additionalConfigs).To(gomega.HaveLen(1)) // 1 module is getting loaded!
gomega.Expect(c.Containers.InitPath).To(gomega.Equal("etc four"))
gomega.Expect(c.LoadedModules()).To(gomega.HaveLen(1))
// Make sure the returned module path is absolute.
gomega.Expect(c.LoadedModules()).To(gomega.Equal([]string{filepath.Join(wd, "testdata/modules/etc/containers/containers.conf.modules/fourth.conf")}))
options = &Options{Modules: []string{"fourth.conf"}}
c, err = New(options)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(options.additionalConfigs).To(gomega.HaveLen(1)) // 1 module is getting loaded!
gomega.Expect(c.Containers.InitPath).To(gomega.Equal("etc four"))
gomega.Expect(c.LoadedModules()).To(gomega.HaveLen(1))
options = &Options{Modules: []string{"fourth.conf", "sub/share-only.conf", "sub/etc-only.conf"}}
c, err = New(options)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(options.additionalConfigs).To(gomega.HaveLen(3)) // 3 modules are getting loaded!
gomega.Expect(c.Containers.InitPath).To(gomega.Equal("etc four"))
gomega.Expect(c.Containers.Env.Get()).To(gomega.Equal([]string{"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "usr share only"}))
gomega.Expect(c.Network.DefaultNetwork).To(gomega.Equal("etc only conf"))
gomega.Expect(c.LoadedModules()).To(gomega.HaveLen(3))
options = &Options{Modules: []string{"third.conf"}}
c, err = New(options)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(options.additionalConfigs).To(gomega.HaveLen(1)) // 1 module is getting loaded!
gomega.Expect(c.LoadedModules()).To(gomega.HaveLen(1))
if unshare.IsRootless() {
gomega.Expect(c.Network.DefaultNetwork).To(gomega.Equal("home third"))
} else {
gomega.Expect(c.Network.DefaultNetwork).To(gomega.Equal("etc third"))
}
})
It("new config with modules and env variables", func() {
testSetModulePaths()
t := GinkgoT()
t.Setenv(containersConfOverrideEnv, "testdata/modules/override.conf")
// Also make sure that absolute paths are loaded as is.
wd, err := os.Getwd()
gomega.Expect(err).ToNot(gomega.HaveOccurred())
absConf := filepath.Join(wd, "testdata/modules/home/.config/containers/containers.conf.modules/second.conf")
options := &Options{Modules: []string{"fourth.conf", "sub/share-only.conf", absConf}}
c, err := New(options)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(options.additionalConfigs).To(gomega.HaveLen(4)) // 2 modules + abs path + override conf are getting loaded!
gomega.Expect(c.Containers.InitPath).To(gomega.Equal("etc four"))
gomega.Expect(c.Containers.Env.Get()).To(gomega.Equal([]string{"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "usr share only", "override conf always wins"}))
gomega.Expect(c.Containers.Volumes.Get()).To(gomega.Equal([]string{"volume four", "home second"}))
})
})
|