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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
package config
import (
"os"
"path/filepath"
"strconv"
"sync"
. "github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
)
var _ = Describe("Connections conf", func() {
var (
connectionsConfFile string
containersConfFile string
)
BeforeEach(func() {
t := GinkgoT()
dir := t.TempDir()
connectionsConfFile = filepath.Join(dir, "connections.json")
t.Setenv("PODMAN_CONNECTIONS_CONF", connectionsConfFile)
containersConfFile = filepath.Join(dir, "containers.conf")
t.Setenv(containersConfEnv, containersConfFile)
})
It("read non existent file", func() {
path, err := connectionsConfigFile()
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(path).To(gomega.Equal(connectionsConfFile))
conf, err := readConnectionConf(path)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(conf).To(gomega.Equal(&ConnectionsFile{}))
})
It("read empty file", func() {
err := os.WriteFile(connectionsConfFile, []byte("{}"), os.ModePerm)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
path, err := connectionsConfigFile()
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(path).To(gomega.Equal(connectionsConfFile))
conf, err := readConnectionConf(path)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(conf).To(gomega.Equal(&ConnectionsFile{}))
})
It("write and read file", func() {
orgConf := &ConnectionsFile{Connection: ConnectionConfig{
Default: "test",
Connections: map[string]Destination{
"test": {URI: "ssh://podman.io"},
},
}, Farm: FarmConfig{
Default: "farm1",
List: map[string][]string{
"farm1": {"test"},
},
}}
err := writeConnectionConf(connectionsConfFile, orgConf)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
path, err := connectionsConfigFile()
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(path).To(gomega.Equal(connectionsConfFile))
conf, err := readConnectionConf(path)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(conf).To(gomega.Equal(orgConf))
})
It("parallel EditConnectionConfig", func() {
// race test for EditConnectionConfig
// Basic idea spawn a bunch of goroutines and call EditConnectionConfig at the same time.
// We read a int from one field and then +1 one it each time so at the end we must have
// the number in the filed for how many times we called EditConnectionConfig. If it is
// less than it is racy.
count := 50
wg := sync.WaitGroup{}
wg.Add(count)
for i := 0; i < count; i++ {
go func() {
defer wg.Done()
err := EditConnectionConfig(func(cfg *ConnectionsFile) error {
if cfg.Connection.Default == "" {
cfg.Connection.Default = "1"
return nil
}
// basic idea just add 1
i, err := strconv.Atoi(cfg.Connection.Default)
if err != nil {
return err
}
i++
cfg.Connection.Default = strconv.Itoa(i)
return nil
})
gomega.Expect(err).ToNot(gomega.HaveOccurred())
}()
}
wg.Wait()
path, err := connectionsConfigFile()
gomega.Expect(err).ToNot(gomega.HaveOccurred())
conf, err := readConnectionConf(path)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(conf.Connection.Default).To(gomega.Equal("50"))
})
Context("GetConnection/Farm", func() {
const defConnectionsConf = `{"Connection":{"Default":"test","Connections":{"test":{"URI":"ssh://podman.io"},"QA":{"URI":"ssh://test","Identity":".ssh/id","IsMachine":true}}},"farm":{"Default":"farm1","List":{"farm1":["test"]}}}`
const defContainersConf = `
[engine]
active_service = "containers"
[engine.service_destinations]
[engine.service_destinations.containers]
uri = "unix:///tmp/test.sock"
is_machine = true
[farms]
default = "farm2"
[farms.list]
farm2 = ["containers"]
`
BeforeEach(func() {
err := os.WriteFile(connectionsConfFile, []byte(defConnectionsConf), os.ModePerm)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
err = os.WriteFile(containersConfFile, []byte(defContainersConf), os.ModePerm)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
})
It("GetConnection()", func() {
conf, err := New(nil)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
con, err := conf.GetConnection("", true)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(con).To(gomega.Equal(&Connection{
Name: "test",
Default: true,
ReadWrite: true,
Destination: Destination{URI: "ssh://podman.io"},
}))
con, err = conf.GetConnection("test", false)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(con).To(gomega.Equal(&Connection{
Name: "test",
Default: true,
ReadWrite: true,
Destination: Destination{URI: "ssh://podman.io"},
}))
con, err = conf.GetConnection("QA", false)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(con).To(gomega.Equal(&Connection{
Name: "QA",
Default: false,
ReadWrite: true,
Destination: Destination{URI: "ssh://test", Identity: ".ssh/id", IsMachine: true},
}))
con, err = conf.GetConnection("containers", false)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(con).To(gomega.Equal(&Connection{
Name: "containers",
Default: false,
ReadWrite: false,
Destination: Destination{URI: "unix:///tmp/test.sock", IsMachine: true},
}))
})
It("GetAllConnections()", func() {
conf, err := New(nil)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
cons, err := conf.GetAllConnections()
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(cons).To(gomega.ContainElements(
Connection{
Name: "test",
Default: true,
ReadWrite: true,
Destination: Destination{URI: "ssh://podman.io"},
},
Connection{
Name: "QA",
Default: false,
ReadWrite: true,
Destination: Destination{URI: "ssh://test", Identity: ".ssh/id", IsMachine: true},
},
Connection{
Name: "containers",
Default: false,
ReadWrite: false,
Destination: Destination{URI: "unix:///tmp/test.sock", IsMachine: true},
},
))
})
It("GetFarmConnections()", func() {
conf, err := New(nil)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
name, cons, err := conf.GetDefaultFarmConnections()
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(name).To(gomega.Equal("farm1"))
gomega.Expect(cons).To(gomega.ContainElements(
Connection{
Name: "test",
Default: false,
ReadWrite: false,
Destination: Destination{URI: "ssh://podman.io"},
},
))
cons, err = conf.GetFarmConnections("farm1")
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(cons).To(gomega.ContainElements(
Connection{
Name: "test",
Default: false,
ReadWrite: false,
Destination: Destination{URI: "ssh://podman.io"},
},
))
cons, err = conf.GetFarmConnections("farm2")
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(cons).To(gomega.ContainElements(
Connection{
Name: "containers",
Default: false,
ReadWrite: false,
Destination: Destination{URI: "unix:///tmp/test.sock", IsMachine: true},
},
))
})
It("GetAllFarms()", func() {
conf, err := New(nil)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
farms, err := conf.GetAllFarms()
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(farms).To(gomega.ContainElements(
Farm{
Name: "farm1",
Connections: []string{"test"},
Default: true,
ReadWrite: true,
},
Farm{
Name: "farm2",
Connections: []string{"containers"},
Default: false,
ReadWrite: false,
},
))
})
})
})
|