github.com/jenkins-x/jx/v2@v2.1.155/pkg/cmd/step/step_link_services_test.go (about)

     1  // +build unit
     2  
     3  package step_test
     4  
     5  import (
     6  	"testing"
     7  
     8  	step2 "github.com/jenkins-x/jx/v2/pkg/cmd/opts/step"
     9  	"github.com/jenkins-x/jx/v2/pkg/cmd/step"
    10  	"github.com/jenkins-x/jx/v2/pkg/cmd/testhelpers"
    11  
    12  	"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
    13  	"github.com/jenkins-x/jx/v2/pkg/gits"
    14  	"github.com/jenkins-x/jx/v2/pkg/helm"
    15  	resources_test "github.com/jenkins-x/jx/v2/pkg/kube/resources/mocks"
    16  	"github.com/stretchr/testify/assert"
    17  	corev1 "k8s.io/api/core/v1"
    18  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    19  	"k8s.io/apimachinery/pkg/runtime"
    20  )
    21  
    22  const (
    23  	fromNs                   = "from-namespace-powdered-water"
    24  	toNs                     = "to-namespace-journal-entry"
    25  	serviceNameInFromNs      = "service-a-is-for-angry"
    26  	serviceNameDummyInFromNs = "service-p-is-polluted"
    27  	serviceNameInToNs        = "service-b-is-for-berserk"
    28  )
    29  
    30  func TestServiceLinking(t *testing.T) {
    31  	t.Parallel()
    32  	o := step.StepLinkServicesOptions{
    33  		StepOptions: step2.StepOptions{
    34  			CommonOptions: &opts.CommonOptions{},
    35  		},
    36  		FromNamespace: fromNs,
    37  		Includes:      []string{serviceNameInFromNs},
    38  		Excludes:      []string{serviceNameDummyInFromNs},
    39  		ToNamespace:   toNs}
    40  	fromNspc := &corev1.Namespace{
    41  		ObjectMeta: metav1.ObjectMeta{
    42  			Name: fromNs,
    43  		},
    44  	}
    45  	svcInFromNs := &corev1.Service{
    46  		ObjectMeta: metav1.ObjectMeta{
    47  			Name:      serviceNameInFromNs,
    48  			Namespace: fromNs,
    49  		},
    50  	}
    51  	svcDummyInFromNs := &corev1.Service{
    52  		ObjectMeta: metav1.ObjectMeta{
    53  			Name:      serviceNameDummyInFromNs,
    54  			Namespace: fromNs,
    55  		},
    56  	}
    57  	toNspc := &corev1.Namespace{
    58  		ObjectMeta: metav1.ObjectMeta{
    59  			Name: toNs,
    60  		},
    61  	}
    62  	svcInToNs := &corev1.Service{
    63  		ObjectMeta: metav1.ObjectMeta{
    64  			Name:      serviceNameInToNs,
    65  			Namespace: toNs,
    66  		},
    67  	}
    68  
    69  	testhelpers.ConfigureTestOptionsWithResources(o.CommonOptions,
    70  		[]runtime.Object{fromNspc, toNspc, svcInFromNs, svcInToNs, svcDummyInFromNs},
    71  		nil,
    72  		gits.NewGitCLI(),
    73  		nil,
    74  		helm.NewHelmCLI("helm", helm.V2, "", true),
    75  		resources_test.NewMockInstaller())
    76  	client, err := o.KubeClient()
    77  	assert.NoError(t, err)
    78  	serviceListFromNsBeforeStepLink, _ := client.CoreV1().Services(fromNs).List(metav1.ListOptions{})
    79  	assert.EqualValues(t, len(serviceListFromNsBeforeStepLink.Items), 2)
    80  	serviceListToNsBeforeStepLink, _ := client.CoreV1().Services(toNs).List(metav1.ListOptions{})
    81  	assert.EqualValues(t, len(serviceListToNsBeforeStepLink.Items), 1)
    82  	err = o.Run()
    83  	serviceList, _ := client.CoreV1().Services(toNs).List(metav1.ListOptions{})
    84  	serviceNames := []string{""}
    85  	for _, service := range serviceList.Items {
    86  		serviceNames = append(serviceNames, service.Name)
    87  	}
    88  	serviceListToNsAfterStepLink, _ := client.CoreV1().Services(toNs).List(metav1.ListOptions{})
    89  	assert.EqualValues(t, len(serviceListToNsAfterStepLink.Items), 2)
    90  
    91  	assert.Contains(t, serviceNames, serviceNameInFromNs) //Check if service that was in include list got added
    92  	assert.EqualValues(t, len(serviceNames), 3)           //Check if service that was in exclude list didn't get added
    93  	assert.NoError(t, err)
    94  }