...

Source file src/github.com/Microsoft/hcsshim/cmd/ncproxy/utilities_test.go

Documentation: github.com/Microsoft/hcsshim/cmd/ncproxy

     1  package main
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  
     7  	ncproxystore "github.com/Microsoft/hcsshim/internal/ncproxy/store"
     8  	ncproxygrpc "github.com/Microsoft/hcsshim/pkg/ncproxy/ncproxygrpc/v1"
     9  	bolt "go.etcd.io/bbolt"
    10  )
    11  
    12  func exists(target string, list []string) bool {
    13  	for _, v := range list {
    14  		if v == target {
    15  			return true
    16  		}
    17  	}
    18  	return false
    19  }
    20  
    21  func networkExists(targetName string, networks []*ncproxygrpc.GetNetworkResponse) bool {
    22  	for _, resp := range networks {
    23  		n := resp.Network.Settings
    24  		switch network := n.(type) {
    25  		case *ncproxygrpc.Network_HcnNetwork:
    26  			if network.HcnNetwork != nil && network.HcnNetwork.Name == targetName {
    27  				return true
    28  			}
    29  		case *ncproxygrpc.Network_NcproxyNetwork:
    30  			if network.NcproxyNetwork != nil && network.NcproxyNetwork.Name == targetName {
    31  				return true
    32  			}
    33  		}
    34  	}
    35  	return false
    36  }
    37  
    38  func endpointExists(targetName string, endpoints []*ncproxygrpc.GetEndpointResponse) bool {
    39  	for _, resp := range endpoints {
    40  		ep := resp.Endpoint.Settings
    41  		switch endpt := ep.(type) {
    42  		case *ncproxygrpc.EndpointSettings_HcnEndpoint:
    43  			if endpt.HcnEndpoint != nil && endpt.HcnEndpoint.Name == targetName {
    44  				return true
    45  			}
    46  		case *ncproxygrpc.EndpointSettings_NcproxyEndpoint:
    47  			if resp.ID == targetName {
    48  				return true
    49  			}
    50  		}
    51  	}
    52  	return false
    53  }
    54  
    55  func createTestNetworkingStore() (store *ncproxystore.NetworkingStore, closer func(), err error) {
    56  	tempDir, err := os.MkdirTemp("", "")
    57  	if err != nil {
    58  		return nil, nil, err
    59  	}
    60  
    61  	defer func() {
    62  		if err != nil {
    63  			_ = os.RemoveAll(tempDir)
    64  		}
    65  	}()
    66  
    67  	db, err := bolt.Open(filepath.Join(tempDir, "networkproxy.db.test"), 0600, nil)
    68  	if err != nil {
    69  		return nil, nil, err
    70  	}
    71  
    72  	closer = func() {
    73  		_ = os.RemoveAll(tempDir)
    74  		_ = db.Close()
    75  	}
    76  
    77  	return ncproxystore.NewNetworkingStore(db), closer, nil
    78  }
    79  

View as plain text