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

     1  // +build unit
     2  
     3  package kube_test
     4  
     5  import (
     6  	"testing"
     7  
     8  	jenkinsio "github.com/jenkins-x/jx-api/pkg/apis/jenkins.io"
     9  	v1 "github.com/jenkins-x/jx-api/pkg/apis/jenkins.io/v1"
    10  	"github.com/jenkins-x/jx-api/pkg/client/clientset/versioned/fake"
    11  	"github.com/olli-ai/jx/v2/pkg/kube"
    12  	"github.com/olli-ai/jx/v2/pkg/kube/naming"
    13  	"github.com/stretchr/testify/assert"
    14  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    15  )
    16  
    17  const (
    18  	ns = "jx"
    19  )
    20  
    21  func TestFindSourceRepository(t *testing.T) {
    22  	existingRepos := &v1.SourceRepositoryList{
    23  		Items: []v1.SourceRepository{
    24  			// Standard auto-created source repository
    25  			createSourceRepository("first-org-first-repo", "first-org", "first-repo", "https://github.com", false),
    26  			// Arbitrary name
    27  			createSourceRepository("random-name", "second-org", "second-repo", "https://github.com", false),
    28  			// Unlabeled, to verify proper behavior with legacy autocreated source repositories without labels
    29  			createSourceRepository("third-org-third-repo", "third-org", "third-repo", "https://github.com", true),
    30  		},
    31  	}
    32  	jxClient := fake.NewSimpleClientset(existingRepos)
    33  
    34  	// Test the standard auto-created
    35  	firstSr, err := kube.FindSourceRepository(jxClient, ns, "first-org", "first-repo", "github")
    36  	assert.NoError(t, err)
    37  	assert.NotNil(t, firstSr)
    38  	assert.Equal(t, "first-org-first-repo", firstSr.Name)
    39  
    40  	// Test the arbitrary name
    41  	secondSr, err := kube.FindSourceRepository(jxClient, ns, "second-org", "second-repo", "github")
    42  	assert.NoError(t, err)
    43  	assert.NotNil(t, secondSr)
    44  	assert.Equal(t, "random-name", secondSr.Name)
    45  
    46  	// Test the unlabeled case
    47  	thirdSr, err := kube.FindSourceRepository(jxClient, ns, "third-org", "third-repo", "github")
    48  	assert.NoError(t, err)
    49  	assert.NotNil(t, thirdSr)
    50  	assert.Equal(t, "third-org-third-repo", thirdSr.Name)
    51  	assert.Equal(t, "", thirdSr.Labels[v1.LabelOwner])
    52  }
    53  
    54  func TestGetOrCreateSourceRepositories(t *testing.T) {
    55  	tests := []struct {
    56  		name         string
    57  		org          string
    58  		repo         string
    59  		providerURL  string
    60  		existingName string
    61  	}{
    62  		{
    63  			name:        "no existing repo",
    64  			org:         "some-org",
    65  			repo:        "some-repo",
    66  			providerURL: "https://github.com",
    67  		},
    68  		{
    69  			name:         "existing repo, standard name",
    70  			org:          "some-org",
    71  			repo:         "some-repo",
    72  			providerURL:  "https://github.com",
    73  			existingName: "some-org-some-repo",
    74  		},
    75  		{
    76  			name:         "existing repo, different name",
    77  			org:          "some-org",
    78  			repo:         "some-repo",
    79  			providerURL:  "https://github.com",
    80  			existingName: "some-arbitrary-name",
    81  		},
    82  	}
    83  
    84  	for _, tt := range tests {
    85  		t.Run(tt.name, func(t *testing.T) {
    86  			var existingRepo v1.SourceRepository
    87  			if tt.existingName != "" {
    88  				existingRepo = createSourceRepository(tt.existingName, tt.org, tt.repo, tt.providerURL, false)
    89  			}
    90  			var jxClient *fake.Clientset
    91  			if tt.existingName != "" {
    92  				jxClient = fake.NewSimpleClientset(&existingRepo)
    93  			} else {
    94  				jxClient = fake.NewSimpleClientset()
    95  			}
    96  
    97  			createdOrExisting, err := kube.GetOrCreateSourceRepository(jxClient, ns, tt.repo, tt.org, tt.providerURL)
    98  			assert.NoError(t, err)
    99  			assert.NotNil(t, createdOrExisting)
   100  
   101  			if tt.existingName == "" {
   102  				createdRepoName := naming.ToValidName(tt.org + "-" + tt.repo)
   103  				assert.Equal(t, createdRepoName, createdOrExisting.Name, "new SourceRepository name should be %s but is %s", createdRepoName, createdOrExisting.Name)
   104  			} else {
   105  				assert.Equal(t, existingRepo.Name, createdOrExisting.Name, "existing or updated repository name should be %s but is %s", existingRepo.Name, createdOrExisting.Name)
   106  			}
   107  
   108  			assert.Equal(t, tt.org, createdOrExisting.Spec.Org)
   109  			assert.Equal(t, tt.repo, createdOrExisting.Spec.Repo)
   110  			assert.Equal(t, kube.ToProviderName(tt.providerURL), createdOrExisting.Spec.ProviderName)
   111  		})
   112  	}
   113  }
   114  
   115  func createSourceRepository(name, org, repo, providerURL string, skipLabels bool) v1.SourceRepository {
   116  	labels := make(map[string]string)
   117  	if !skipLabels {
   118  		labels[v1.LabelOwner] = org
   119  		labels[v1.LabelRepository] = repo
   120  		labels[v1.LabelProvider] = kube.ToProviderName(providerURL)
   121  	}
   122  
   123  	return v1.SourceRepository{
   124  		TypeMeta: metav1.TypeMeta{
   125  			Kind:       "SourceRepository",
   126  			APIVersion: jenkinsio.GroupName + "/" + jenkinsio.Version,
   127  		},
   128  		ObjectMeta: metav1.ObjectMeta{
   129  			Name:      name,
   130  			Namespace: ns,
   131  			Labels:    labels,
   132  		},
   133  		Spec: v1.SourceRepositorySpec{
   134  			Org:          org,
   135  			Provider:     providerURL,
   136  			ProviderName: kube.ToProviderName(providerURL),
   137  			Repo:         repo,
   138  		},
   139  	}
   140  
   141  }