github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/swarmkit/manager/allocator/allocator_linux_test.go (about)

     1  package allocator
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/docker/swarmkit/api"
     8  	"github.com/docker/swarmkit/manager/state"
     9  	"github.com/docker/swarmkit/manager/state/store"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestIPAMNotNil(t *testing.T) {
    14  	s := store.NewMemoryStore(nil)
    15  	assert.NotNil(t, s)
    16  	defer s.Close()
    17  
    18  	a, err := New(s, nil, nil)
    19  	assert.NoError(t, err)
    20  	assert.NotNil(t, a)
    21  
    22  	// Predefined node-local network
    23  	p := &api.Network{
    24  		ID: "one_unIque_id",
    25  		Spec: api.NetworkSpec{
    26  			Annotations: api.Annotations{
    27  				Name: "pred_bridge_network",
    28  				Labels: map[string]string{
    29  					"com.docker.swarm.predefined": "true",
    30  				},
    31  			},
    32  			DriverConfig: &api.Driver{Name: "bridge"},
    33  		},
    34  	}
    35  
    36  	// Node-local swarm scope network
    37  	nln := &api.Network{
    38  		ID: "another_unIque_id",
    39  		Spec: api.NetworkSpec{
    40  			Annotations: api.Annotations{
    41  				Name: "swarm-macvlan",
    42  			},
    43  			DriverConfig: &api.Driver{Name: "macvlan"},
    44  		},
    45  	}
    46  
    47  	// Try adding some objects to store before allocator is started
    48  	assert.NoError(t, s.Update(func(tx store.Tx) error {
    49  		// populate ingress network
    50  		in := &api.Network{
    51  			ID: "ingress-nw-id",
    52  			Spec: api.NetworkSpec{
    53  				Annotations: api.Annotations{
    54  					Name: "default-ingress",
    55  				},
    56  				Ingress: true,
    57  			},
    58  		}
    59  		assert.NoError(t, store.CreateNetwork(tx, in))
    60  
    61  		// Create the predefined node-local network with one service
    62  		assert.NoError(t, store.CreateNetwork(tx, p))
    63  
    64  		// Create the the swarm level node-local network with one service
    65  		assert.NoError(t, store.CreateNetwork(tx, nln))
    66  
    67  		return nil
    68  	}))
    69  
    70  	netWatch, cancel := state.Watch(s.WatchQueue(), api.EventUpdateNetwork{}, api.EventDeleteNetwork{})
    71  	defer cancel()
    72  
    73  	// Start allocator
    74  	go func() {
    75  		assert.NoError(t, a.Run(context.Background()))
    76  	}()
    77  	defer a.Stop()
    78  
    79  	// Now verify if we get network and tasks updated properly
    80  	watchNetwork(t, netWatch, false, func(t assert.TestingT, n *api.Network) bool { return true })
    81  	watchNetwork(t, netWatch, false, func(t assert.TestingT, n *api.Network) bool { return true })
    82  	watchNetwork(t, netWatch, false, func(t assert.TestingT, n *api.Network) bool { return true })
    83  
    84  	// Verify no allocation was done for the node-local networks
    85  	var (
    86  		ps *api.Network
    87  		sn *api.Network
    88  	)
    89  	s.View(func(tx store.ReadTx) {
    90  		ps = store.GetNetwork(tx, p.ID)
    91  		sn = store.GetNetwork(tx, nln.ID)
    92  
    93  	})
    94  	assert.NotNil(t, ps)
    95  	assert.NotNil(t, sn)
    96  	assert.NotNil(t, ps.IPAM)
    97  	assert.NotNil(t, sn.IPAM)
    98  }