aboutsummaryrefslogtreecommitdiff
path: root/libpod/in_memory_state.go
diff options
context:
space:
mode:
authorMatthew Heon <matthew.heon@pm.me>2020-10-29 14:27:36 -0400
committerMatthew Heon <mheon@redhat.com>2020-11-03 16:26:42 -0500
commit844d540d042fe7477e62f8cf28e524fcd756f6c8 (patch)
tree42ff7cfba8d1216e56e7791560c21230333411d1 /libpod/in_memory_state.go
parent63efde15f14b18f5af385e89936b4ab0868cb357 (diff)
downloadpodman-844d540d042fe7477e62f8cf28e524fcd756f6c8.tar.gz
podman-844d540d042fe7477e62f8cf28e524fcd756f6c8.tar.bz2
podman-844d540d042fe7477e62f8cf28e524fcd756f6c8.zip
Add tests for network aliases
As part of this, we need two new functions, for retrieving all aliases for a network and removing all aliases for a network, both required to test. Also, rework handling for some things the tests discovered were broken (notably conflicts between container name and existing aliases). Signed-off-by: Matthew Heon <matthew.heon@pm.me>
Diffstat (limited to 'libpod/in_memory_state.go')
-rw-r--r--libpod/in_memory_state.go113
1 files changed, 113 insertions, 0 deletions
diff --git a/libpod/in_memory_state.go b/libpod/in_memory_state.go
index 3b88c9664..ba4c70c6b 100644
--- a/libpod/in_memory_state.go
+++ b/libpod/in_memory_state.go
@@ -285,6 +285,13 @@ func (s *InMemoryState) AddContainer(ctr *Container) error {
return err
}
+ // Check networks
+ for _, net := range ctr.config.Networks {
+ if net == "" {
+ return errors.Wrapf(define.ErrInvalidArg, "network names cannot be empty")
+ }
+ }
+
// Check network aliases
for network, aliases := range ctr.config.NetworkAliases {
inNet := false
@@ -301,6 +308,10 @@ func (s *InMemoryState) AddContainer(ctr *Container) error {
allNetAliases, ok := s.networkAliases[network]
if ok {
for _, alias := range aliases {
+ // Check if alias is a name
+ if _, err := s.nameIndex.Get(alias); err == nil {
+ return define.ErrInvalidArg
+ }
if _, ok := allNetAliases[alias]; ok {
return define.ErrAliasExists
}
@@ -364,6 +375,34 @@ func (s *InMemoryState) AddContainer(ctr *Container) error {
s.addCtrToVolDependsMap(ctr.ID(), vol.Name)
}
+ for _, network := range ctr.config.Networks {
+ allNetAliases, ok := s.networkAliases[network]
+ if !ok {
+ continue
+ }
+ otherCtrID, ok := allNetAliases[ctr.Name()]
+ if !ok {
+ continue
+ }
+ delete(allNetAliases, ctr.Name())
+
+ otherCtrAliases, ok := s.ctrNetworkAliases[otherCtrID]
+ if !ok {
+ continue
+ }
+ otherCtrNetAliases, ok := otherCtrAliases[network]
+ if !ok {
+ continue
+ }
+ newAliases := []string{}
+ for _, alias := range otherCtrNetAliases {
+ if alias != ctr.Name() {
+ newAliases = append(newAliases, alias)
+ }
+ }
+ otherCtrAliases[network] = newAliases
+ }
+
// Add network aliases
for network, aliases := range ctr.config.NetworkAliases {
allNetAliases, ok := s.networkAliases[network]
@@ -696,6 +735,41 @@ func (s *InMemoryState) RemoveNetworkAliases(ctr *Container, network string) err
return nil
}
+// GetAllAliasesForNetwork gets all the aliases for a single network.
+func (s *InMemoryState) GetAllAliasesForNetwork(network string) (map[string]string, error) {
+ if network == "" {
+ return nil, errors.Wrapf(define.ErrInvalidArg, "network names must not be empty")
+ }
+
+ allAliases, ok := s.networkAliases[network]
+ if !ok {
+ // Can't tell if the network exists.
+ // Assume it does.
+ return map[string]string{}, nil
+ }
+
+ return allAliases, nil
+}
+
+// RemoveAllAliasesForNetwork removes all the aliases for a given network.
+func (s *InMemoryState) RemoveAllAliasesForNetwork(network string) error {
+ if network == "" {
+ return errors.Wrapf(define.ErrInvalidArg, "network names must not be empty")
+ }
+
+ if _, ok := s.networkAliases[network]; ok {
+ delete(s.networkAliases, network)
+ }
+
+ for _, ctrAliases := range s.ctrNetworkAliases {
+ if _, ok := ctrAliases[network]; ok {
+ delete(ctrAliases, network)
+ }
+ }
+
+ return nil
+}
+
// GetContainerConfig returns a container config from the database by full ID
func (s *InMemoryState) GetContainerConfig(id string) (*ContainerConfig, error) {
ctr, err := s.LookupContainer(id)
@@ -1340,6 +1414,13 @@ func (s *InMemoryState) AddContainerToPod(pod *Pod, ctr *Container) error {
return err
}
+ // Check networks
+ for _, net := range ctr.config.Networks {
+ if net == "" {
+ return errors.Wrapf(define.ErrInvalidArg, "network names cannot be empty")
+ }
+ }
+
// Check network aliases
for network, aliases := range ctr.config.NetworkAliases {
inNet := false
@@ -1356,6 +1437,10 @@ func (s *InMemoryState) AddContainerToPod(pod *Pod, ctr *Container) error {
allNetAliases, ok := s.networkAliases[network]
if ok {
for _, alias := range aliases {
+ // Check if alias is a name
+ if _, err := s.nameIndex.Get(alias); err == nil {
+ return define.ErrInvalidArg
+ }
if _, ok := allNetAliases[alias]; ok {
return define.ErrAliasExists
}
@@ -1440,6 +1525,34 @@ func (s *InMemoryState) AddContainerToPod(pod *Pod, ctr *Container) error {
s.addCtrToVolDependsMap(ctr.ID(), vol.Name)
}
+ for _, network := range ctr.config.Networks {
+ allNetAliases, ok := s.networkAliases[network]
+ if !ok {
+ continue
+ }
+ otherCtrID, ok := allNetAliases[ctr.Name()]
+ if !ok {
+ continue
+ }
+ delete(allNetAliases, ctr.Name())
+
+ otherCtrAliases, ok := s.ctrNetworkAliases[otherCtrID]
+ if !ok {
+ continue
+ }
+ otherCtrNetAliases, ok := otherCtrAliases[network]
+ if !ok {
+ continue
+ }
+ newAliases := []string{}
+ for _, alias := range otherCtrNetAliases {
+ if alias != ctr.Name() {
+ newAliases = append(newAliases, alias)
+ }
+ }
+ otherCtrAliases[network] = newAliases
+ }
+
// Add network aliases
for network, aliases := range ctr.config.NetworkAliases {
allNetAliases, ok := s.networkAliases[network]