github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/k8s/resource_test.go (about)

     1  package k8s
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  	"golang.org/x/sync/errgroup"
    11  	"helm.sh/helm/v3/pkg/kube"
    12  	"k8s.io/client-go/rest"
    13  	"k8s.io/client-go/tools/clientcmd"
    14  )
    15  
    16  // This tests a race condition where if there were two Build()s
    17  // happening in parallel, they would overwrite each other's configs
    18  // due to a race condition.
    19  func TestRESTClientBuilder(t *testing.T) {
    20  	config := &rest.Config{}
    21  	loader, err := clientcmd.NewClientConfigFromBytes(nil)
    22  	require.NoError(t, err)
    23  
    24  	client := &K8sClient{
    25  		restConfig:   config,
    26  		clientLoader: loader,
    27  		drm:          fakeRESTMapper{},
    28  	}
    29  	rClient := newResourceClient(client)
    30  
    31  	g, _ := errgroup.WithContext(context.Background())
    32  	var list1, list2 kube.ResourceList
    33  	g.Go(func() error {
    34  		var err error
    35  		list1, err = rClient.Build(strings.NewReader(`
    36  apiVersion: v1
    37  kind: Pod
    38  metadata:
    39    name: fe
    40  `), false)
    41  		return err
    42  	})
    43  
    44  	g.Go(func() error {
    45  		var err error
    46  		list2, err = rClient.Build(strings.NewReader(`
    47  apiVersion: apps/v1
    48  kind: Deployment
    49  metadata:
    50    name: fe
    51  `), false)
    52  		return err
    53  	})
    54  	require.NoError(t, g.Wait())
    55  
    56  	assert.Equal(t, "v1", list1[0].Client.(*rest.RESTClient).APIVersion().String())
    57  	assert.Equal(t, "apps/v1", list2[0].Client.(*rest.RESTClient).APIVersion().String())
    58  }