github.com/argoproj/argo-cd@v1.8.7/cmd/argocd-util/commands/project_allowlist_test.go (about)

     1  package commands
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/undefinedlabs/go-mpatch"
     9  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    10  	"k8s.io/client-go/discovery"
    11  	restclient "k8s.io/client-go/rest"
    12  	"k8s.io/client-go/tools/clientcmd"
    13  )
    14  
    15  func TestProjectAllowListGen(t *testing.T) {
    16  	useMock := true
    17  	rules := clientcmd.NewDefaultClientConfigLoadingRules()
    18  	overrides := &clientcmd.ConfigOverrides{}
    19  	clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(rules, overrides)
    20  
    21  	if useMock {
    22  		var patchClientConfig *mpatch.Patch
    23  		patchClientConfig, err := mpatch.PatchInstanceMethodByName(reflect.TypeOf(clientConfig), "ClientConfig", func(*clientcmd.DeferredLoadingClientConfig) (*restclient.Config, error) {
    24  			return nil, nil
    25  		})
    26  		assert.NoError(t, err)
    27  
    28  		patch, err := mpatch.PatchMethod(discovery.NewDiscoveryClientForConfig, func(c *restclient.Config) (*discovery.DiscoveryClient, error) {
    29  			return &discovery.DiscoveryClient{LegacyPrefix: "/api"}, nil
    30  		})
    31  		assert.NoError(t, err)
    32  
    33  		var patchSeverPreferedResources *mpatch.Patch
    34  		discoClient := &discovery.DiscoveryClient{}
    35  		patchSeverPreferedResources, err = mpatch.PatchInstanceMethodByName(reflect.TypeOf(discoClient), "ServerPreferredResources", func(*discovery.DiscoveryClient) ([]*metav1.APIResourceList, error) {
    36  			res := metav1.APIResource{
    37  				Name: "services",
    38  				Kind: "Service",
    39  			}
    40  			resourceList := []*metav1.APIResourceList{{APIResources: []metav1.APIResource{res}}}
    41  			return resourceList, nil
    42  		})
    43  		assert.NoError(t, err)
    44  
    45  		defer func() {
    46  			err = patchClientConfig.Unpatch()
    47  			assert.NoError(t, err)
    48  			err = patch.Unpatch()
    49  			assert.NoError(t, err)
    50  			err = patchSeverPreferedResources.Unpatch()
    51  			err = patch.Unpatch()
    52  		}()
    53  	}
    54  
    55  	globalProj := generateProjectAllowList(clientConfig, "testdata/test_clusterrole.yaml", "testproj")
    56  	assert.True(t, len(globalProj.Spec.NamespaceResourceWhitelist) > 0)
    57  }