github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/pkg/compute/bidstrategy/networking_allowlist_test.go (about)

     1  //go:build unit || !integration
     2  
     3  package bidstrategy
     4  
     5  import (
     6  	"context"
     7  	"fmt"
     8  	"os/exec"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/filecoin-project/bacalhau/pkg/model"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  type networkAllowlistTestCase struct {
    17  	Type      model.Network
    18  	Domains   []string
    19  	ShouldBid bool
    20  }
    21  
    22  func (tc networkAllowlistTestCase) String() string {
    23  	return fmt.Sprintf(
    24  		"should bid %t with %s networking and domains %s",
    25  		tc.ShouldBid,
    26  		tc.Type,
    27  		strings.Join(tc.Domains, " "),
    28  	)
    29  }
    30  
    31  var networkAllowlistTestCases []networkAllowlistTestCase = []networkAllowlistTestCase{
    32  	{model.NetworkNone, []string{}, true},
    33  	{model.NetworkFull, []string{}, false},
    34  	{model.NetworkHTTP, []string{}, true},
    35  	{model.NetworkHTTP, []string{"example.com"}, true},
    36  	{model.NetworkFull, []string{"example.com"}, false},
    37  	{model.NetworkHTTP, []string{"malware.com"}, false},
    38  	{model.NetworkFull, []string{"malware.com"}, false},
    39  	{model.NetworkHTTP, []string{"example.com", "proxy.golang.org"}, true},
    40  	{model.NetworkHTTP, []string{"malware.com", "proxy.golang.org"}, false},
    41  }
    42  
    43  func TestNetworkAllowlistStrategyFiltersDomains(t *testing.T) {
    44  	require.NoError(t, exec.Command("jq", "--help").Run(), "Requires `jq` to be installed.")
    45  
    46  	strategy := NewExternalCommandStrategy(ExternalCommandStrategyParams{
    47  		Command: "../../../ops/terraform/remote_files/scripts/apply-http-allowlist.sh",
    48  	})
    49  
    50  	for _, testCase := range networkAllowlistTestCases {
    51  		t.Run(testCase.String(), func(t *testing.T) {
    52  			resp, err := strategy.ShouldBid(context.Background(), BidStrategyRequest{
    53  				Job: model.Job{
    54  					Spec: model.Spec{
    55  						Network: model.NetworkConfig{
    56  							Type:    testCase.Type,
    57  							Domains: testCase.Domains,
    58  						},
    59  					},
    60  				},
    61  			})
    62  
    63  			require.NoError(t, err)
    64  			require.Equal(t, testCase.ShouldBid, resp.ShouldBid, resp.Reason)
    65  		})
    66  	}
    67  }