github.com/smartcontractkit/chainlink-testing-framework/libs@v0.0.0-20240227141906-ec710b4eb1a3/docs/setup/code.md (about)

     1  ---
     2  layout: default
     3  title: Test Setup Code
     4  nav_order: 3
     5  parent: Setup
     6  ---
     7  
     8  # Test Setup Code
     9  
    10  Now that we've got our config and Kubernetes sorted, we can write a bit of code that will deploy an environment for our test to run. To deploy our simulated geth, mock-server, and Chainlink instances, we rely on `env` package. This package handles deploying everything our test needs to the Kubernetes cluster.
    11  
    12  ```go
    13  // We use the env package to make and handle deployed resources
    14  import "github.com/smartcontractkit/chainlink-testing-framework/libs/k8s/environment"
    15  
    16  // Deploy a testing environment, and receive it as the `env` variable. This is used to connect to resources.
    17  e = environment.New(nil)
    18  err := e.
    19      AddHelm(mockservercfg.New(nil)).
    20      AddHelm(mockserver.New(nil)).
    21      AddHelm(geth.New(nil)).
    22      AddHelm(chainlink.New(nil)).
    23      Run()
    24  Expect(err).ShouldNot(HaveOccurred(), "Environment deployment shouldn't fail")
    25  // Connect to all networks specified in the networks.yaml file
    26  networkRegistry := client.NewDefaultNetworkRegistry()
    27  // Retrieve these networks
    28  networks, err := networkRegistry.GetNetworks(env)
    29  // Get the default network (the first one in your listed selected_networks)
    30  defaultNetwork := networks.Default
    31  ```
    32  
    33  Most of the setup code will be the same for all your tests. Here's a more detailed explanation as to what some of the deployment code is doing to launch a few common test resources.
    34  
    35  ```go
    36  e := environment.New(&environment.Config{
    37      Labels: []string{fmt.Sprintf("envType=%s", pkg.EnvTypeEVM5)}, // set more additional labels
    38  })
    39  err := e.
    40      AddHelm(mockservercfg.New(nil)). // add more Helm charts, all charts got merged in a manifest and deployed with kubectl when you call Run()
    41      AddHelm(mockserver.New(nil)).
    42      Run()
    43  Expect(err).ShouldNot(HaveOccurred(), "Environment deployment shouldn't fail")
    44  // do some other stuff with deployed charts if you need to interact with deployed services
    45  err = e.
    46      AddChart(blockscout.New(&blockscout.Props{})). // you can also add cdk8s charts if you like Go code
    47      AddHelm(geth.New(nil)).
    48      AddHelm(chainlink.New(nil)).
    49      Run()
    50  // Connect to all networks specified in the networks.yaml file
    51  networkRegistry := client.NewDefaultNetworkRegistry()
    52  // Retrieve these networks
    53  networks, err := networkRegistry.GetNetworks(env)
    54  // Get the default network (the first one in your listed selected_networks)
    55  defaultNetwork := networks.Default
    56  ```
    57  
    58  These common resources consist of
    59  
    60  * A simulated Geth instance
    61  * A basic mock server that serves as a mock adapter for Chainlink nodes
    62  * A specified number of chainlink nodes
    63  
    64  ## Test Tear Down
    65  
    66  When your test is done, you'll want to have a way to tear down the test environment you launched. You'll also want to be able to see the logs from your test environment. Below is a typical test flow.
    67  
    68  ```go
    69  // Launch our environment
    70  env, err := environment.DeployOrLoadEnvironment( 
    71    environment.NewChainlinkConfig(environment.ChainlinkReplicas(1, nil), "chainlink-test-setup"),
    72    tools.ChartsRoot,
    73  )
    74  
    75  // Put test logic here
    76  
    77  // Tear down the test environment
    78  // Prints some handy stats on Gas usage for the test, if you'd like to see that info.
    79  networks.Default.GasStats().PrintStats()
    80  // Tears down the test environment, according to options you selected in the `framework.yaml` config file
    81  err = actions.TeardownSuite(
    82    env,                // The test environment object
    83    networks,           // The list of networks obtained from `networks, err := networkRegistry.GetNetworks(env)`
    84    utils.ProjectRoot,  // The folder location you'd like logs (on test failure) to be dumped to
    85    nil,                // An optional test reporter for more custom test statistics (we'll get to that later)
    86  )
    87  ```