github.com/pachyderm/pachyderm@v1.13.4/src/server/pkg/testpachd/real_env.go (about)

     1  package testpachd
     2  
     3  import (
     4  	"net"
     5  	"net/url"
     6  	"os"
     7  	"path"
     8  
     9  	authiface "github.com/pachyderm/pachyderm/src/server/auth"
    10  	authtesting "github.com/pachyderm/pachyderm/src/server/auth/testing"
    11  	pfsiface "github.com/pachyderm/pachyderm/src/server/pfs"
    12  	pfsserver "github.com/pachyderm/pachyderm/src/server/pfs/server"
    13  	"github.com/pachyderm/pachyderm/src/server/pkg/hashtree"
    14  	"github.com/pachyderm/pachyderm/src/server/pkg/serviceenv"
    15  	txnenv "github.com/pachyderm/pachyderm/src/server/pkg/transactionenv"
    16  	txnserver "github.com/pachyderm/pachyderm/src/server/transaction/server"
    17  )
    18  
    19  // RealEnv contains a setup for running end-to-end pachyderm tests locally.  It
    20  // includes the base MockEnv struct as well as a real instance of the API server.
    21  // These calls can still be mocked, but they default to calling into the real
    22  // server endpoints.
    23  type RealEnv struct {
    24  	MockEnv
    25  
    26  	LocalStorageDirectory    string
    27  	treeCache                *hashtree.Cache
    28  	AuthServer               authiface.APIServer
    29  	PFSBlockServer           pfsserver.BlockAPIServer
    30  	PFSServer                pfsiface.APIServer
    31  	TransactionServer        txnserver.APIServer
    32  	MockPPSTransactionServer *MockPPSTransactionServer
    33  }
    34  
    35  const (
    36  	testingTreeCacheSize       = 8
    37  	localBlockServerCacheBytes = 256 * 1024 * 1024
    38  )
    39  
    40  // WithRealEnv constructs a MockEnv, then forwards all API calls to go to API
    41  // server instances for supported operations. PPS requires a kubernetes
    42  // environment in order to spin up pipelines, which is not yet supported by this
    43  // package, but the other API servers work.
    44  func WithRealEnv(cb func(*RealEnv) error, customConfig ...*serviceenv.PachdFullConfiguration) error {
    45  	// TODO: This means StorageV2 tests can not run in parallel with V1 test.
    46  	if len(customConfig) > 0 && customConfig[0].StorageV2 {
    47  		if err := os.Setenv("STORAGE_V2", "true"); err != nil {
    48  			panic(err)
    49  		}
    50  		defer func() {
    51  			if err := os.Unsetenv("STORAGE_V2"); err != nil {
    52  				panic(err)
    53  			}
    54  		}()
    55  	}
    56  
    57  	return WithMockEnv(func(mockEnv *MockEnv) (err error) {
    58  		realEnv := &RealEnv{MockEnv: *mockEnv}
    59  
    60  		defer func() {
    61  			if realEnv.treeCache != nil {
    62  				realEnv.treeCache.Close()
    63  			}
    64  		}()
    65  
    66  		config := serviceenv.NewConfiguration(&serviceenv.PachdFullConfiguration{})
    67  		if len(customConfig) > 0 {
    68  			config = serviceenv.NewConfiguration(customConfig[0])
    69  		}
    70  
    71  		etcdClientURL, err := url.Parse(realEnv.EtcdClient.Endpoints()[0])
    72  		if err != nil {
    73  			return err
    74  		}
    75  		config.EtcdHost = etcdClientURL.Hostname()
    76  		config.EtcdPort = etcdClientURL.Port()
    77  		config.PeerPort = uint16(realEnv.MockPachd.Addr.(*net.TCPAddr).Port)
    78  		servEnv := serviceenv.InitServiceEnv(config)
    79  
    80  		realEnv.LocalStorageDirectory = path.Join(realEnv.Directory, "localStorage")
    81  		config.StorageRoot = realEnv.LocalStorageDirectory
    82  		if err := os.Setenv("STORAGE_BACKEND", "LOCAL"); err != nil {
    83  			return err
    84  		}
    85  		realEnv.PFSBlockServer, err = pfsserver.NewBlockAPIServer(
    86  			realEnv.LocalStorageDirectory,
    87  			localBlockServerCacheBytes,
    88  			pfsserver.LocalBackendEnvVar,
    89  			net.JoinHostPort(config.EtcdHost, config.EtcdPort),
    90  			true, // duplicate
    91  		)
    92  		if err != nil {
    93  			return err
    94  		}
    95  
    96  		etcdPrefix := ""
    97  		realEnv.treeCache, err = hashtree.NewCache(testingTreeCacheSize)
    98  		if err != nil {
    99  			return err
   100  		}
   101  
   102  		txnEnv := &txnenv.TransactionEnv{}
   103  
   104  		realEnv.PFSServer, err = pfsserver.NewAPIServer(
   105  			servEnv,
   106  			txnEnv,
   107  			etcdPrefix,
   108  			realEnv.treeCache,
   109  			realEnv.LocalStorageDirectory,
   110  			64*1024*1024,
   111  			realEnv.PFSBlockServer,
   112  		)
   113  		if err != nil {
   114  			return err
   115  		}
   116  
   117  		realEnv.TransactionServer, err = txnserver.NewAPIServer(servEnv, txnEnv, etcdPrefix)
   118  		if err != nil {
   119  			return err
   120  		}
   121  
   122  		realEnv.MockPPSTransactionServer = NewMockPPSTransactionServer()
   123  
   124  		realEnv.AuthServer = &authtesting.InactiveAPIServer{}
   125  		servEnv.SetAuthServer(realEnv.AuthServer)
   126  		servEnv.SetPfsServer(realEnv.PFSServer)
   127  		servEnv.SetPpsServer(&realEnv.MockPPSTransactionServer.api)
   128  
   129  		txnEnv.Initialize(servEnv, realEnv.TransactionServer, realEnv.AuthServer, realEnv.PFSServer, &realEnv.MockPPSTransactionServer.api)
   130  
   131  		linkServers(&realEnv.MockPachd.Object, realEnv.PFSBlockServer)
   132  		linkServers(&realEnv.MockPachd.PFS, realEnv.PFSServer)
   133  		linkServers(&realEnv.MockPachd.Auth, realEnv.AuthServer)
   134  		linkServers(&realEnv.MockPachd.Transaction, realEnv.TransactionServer)
   135  
   136  		return cb(realEnv)
   137  	})
   138  }