github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/kube/envrolebindings_test.go (about)

     1  // +build unit
     2  
     3  package kube_test
     4  
     5  import (
     6  	"testing"
     7  
     8  	v1 "github.com/jenkins-x/jx-api/pkg/apis/jenkins.io/v1"
     9  	"github.com/olli-ai/jx/v2/pkg/kube"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestEnvironmentFilters(t *testing.T) {
    14  	t.Parallel()
    15  	environments := []*v1.Environment{
    16  		kube.NewPermanentEnvironment("staging"),
    17  		kube.NewPermanentEnvironment("production"),
    18  		kube.NewPreviewEnvironment("jstrachan-demo96-pr-1"),
    19  		kube.NewPreviewEnvironment("jstrachan-another-pr-3"),
    20  	}
    21  
    22  	assertEnvironmentsFilter(t, environments,
    23  		nil,
    24  		"staging", "production", "jstrachan-demo96-pr-1", "jstrachan-another-pr-3")
    25  
    26  	assertEnvironmentsFilter(t, environments,
    27  		[]v1.EnvironmentFilter{{}},
    28  		"staging", "production", "jstrachan-demo96-pr-1", "jstrachan-another-pr-3")
    29  
    30  	assertEnvironmentsFilter(t, environments,
    31  		[]v1.EnvironmentFilter{{
    32  			Kind: v1.EnvironmentKindTypePreview,
    33  		}},
    34  		"jstrachan-demo96-pr-1", "jstrachan-another-pr-3")
    35  
    36  	assertEnvironmentsFilter(t, environments,
    37  		[]v1.EnvironmentFilter{{
    38  			Kind: v1.EnvironmentKindTypePermanent,
    39  		}},
    40  		"staging", "production")
    41  
    42  	assertEnvironmentsFilter(t, environments,
    43  		[]v1.EnvironmentFilter{{
    44  			Kind: v1.EnvironmentKindTypePermanent,
    45  		}, {
    46  			Kind: v1.EnvironmentKindTypePreview,
    47  		}},
    48  		"staging", "production", "jstrachan-demo96-pr-1", "jstrachan-another-pr-3")
    49  
    50  	assertEnvironmentsFilter(t, environments,
    51  		[]v1.EnvironmentFilter{{
    52  			Kind:     v1.EnvironmentKindTypePermanent,
    53  			Excludes: []string{"prod*"},
    54  		}},
    55  		"staging")
    56  }
    57  
    58  func assertEnvironmentsFilter(t *testing.T, environments []*v1.Environment, filters []v1.EnvironmentFilter, expectedNames ...string) {
    59  	actual := []string{}
    60  	for _, env := range environments {
    61  		if kube.EnvironmentMatchesAny(env, filters) {
    62  			actual = append(actual, env.Name)
    63  		}
    64  	}
    65  	assert.Equal(t, expectedNames, actual, "for filters %#v", filters)
    66  }