sigs.k8s.io/cluster-api@v1.7.1/cmd/clusterctl/client/repository/client_test.go (about)

     1  /*
     2  Copyright 2020 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package repository
    18  
    19  import (
    20  	"context"
    21  	"os"
    22  	"testing"
    23  
    24  	. "github.com/onsi/gomega"
    25  
    26  	clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
    27  	"sigs.k8s.io/cluster-api/cmd/clusterctl/client/config"
    28  	yaml "sigs.k8s.io/cluster-api/cmd/clusterctl/client/yamlprocessor"
    29  	"sigs.k8s.io/cluster-api/cmd/clusterctl/internal/test"
    30  )
    31  
    32  func Test_newRepositoryClient_LocalFileSystemRepository(t *testing.T) {
    33  	g := NewWithT(t)
    34  
    35  	ctx := context.Background()
    36  
    37  	tmpDir := createTempDir(t)
    38  	defer os.RemoveAll(tmpDir)
    39  
    40  	dst1 := createLocalTestProviderFile(t, tmpDir, "bootstrap-foo/v1.0.0/bootstrap-components.yaml", "")
    41  	dst2 := createLocalTestProviderFile(t, tmpDir, "bootstrap-bar/v2.0.0/bootstrap-components.yaml", "")
    42  
    43  	configClient, err := config.New(ctx, "", config.InjectReader(test.NewFakeReader()))
    44  	g.Expect(err).ToNot(HaveOccurred())
    45  
    46  	type fields struct {
    47  		provider config.Provider
    48  	}
    49  	tests := []struct {
    50  		name     string
    51  		fields   fields
    52  		expected Repository
    53  	}{
    54  		{
    55  			name: "successfully creates repository client with local filesystem backend and scheme == \"\"",
    56  			fields: fields{
    57  				provider: config.NewProvider("foo", dst1, clusterctlv1.BootstrapProviderType),
    58  			},
    59  			expected: &localRepository{},
    60  		},
    61  		{
    62  			name: "successfully creates repository client with local filesystem backend and scheme == \"file\"",
    63  			fields: fields{
    64  				provider: config.NewProvider("bar", "file://"+dst2, clusterctlv1.BootstrapProviderType),
    65  			},
    66  			expected: &localRepository{},
    67  		},
    68  		{
    69  			name: "successfully creates repository client with GitHub backend",
    70  			fields: fields{
    71  				provider: config.NewProvider("bar", "https://github.com/o/r/releases/v0.4.1/file.yaml", clusterctlv1.BootstrapProviderType),
    72  			},
    73  			expected: &gitHubRepository{},
    74  		},
    75  		{
    76  			name: "successfully creates repository client with GitLab backend",
    77  			fields: fields{
    78  				provider: config.NewProvider("bar", "https://gitlab.example.org/api/v4/projects/group%2Fproject/packages/generic/my-package/v1.0/path", clusterctlv1.BootstrapProviderType),
    79  			},
    80  			expected: &gitLabRepository{},
    81  		},
    82  	}
    83  	for _, tt := range tests {
    84  		t.Run(tt.name, func(t *testing.T) {
    85  			gs := NewWithT(t)
    86  
    87  			ctx := context.Background()
    88  
    89  			repoClient, err := newRepositoryClient(ctx, tt.fields.provider, configClient)
    90  			gs.Expect(err).ToNot(HaveOccurred())
    91  
    92  			gs.Expect(repoClient.repository).To(BeAssignableToTypeOf(tt.expected))
    93  		})
    94  	}
    95  }
    96  
    97  func Test_newRepositoryClient_YamlProcessor(t *testing.T) {
    98  	tests := []struct {
    99  		name   string
   100  		opts   []Option
   101  		assert func(*WithT, yaml.Processor)
   102  	}{
   103  		{
   104  			name: "it creates a repository client with simple yaml processor by default",
   105  			assert: func(g *WithT, p yaml.Processor) {
   106  				_, ok := (p).(*yaml.SimpleProcessor)
   107  				g.Expect(ok).To(BeTrue())
   108  			},
   109  		},
   110  		{
   111  			name: "it creates a repository client with specified yaml processor",
   112  			opts: []Option{InjectYamlProcessor(test.NewFakeProcessor())},
   113  			assert: func(g *WithT, p yaml.Processor) {
   114  				_, ok := (p).(*yaml.SimpleProcessor)
   115  				g.Expect(ok).To(BeFalse())
   116  				_, ok = (p).(*test.FakeProcessor)
   117  				g.Expect(ok).To(BeTrue())
   118  			},
   119  		},
   120  		{
   121  			name: "it creates a repository with simple yaml processor even if injected with nil processor",
   122  			opts: []Option{InjectYamlProcessor(nil)},
   123  			assert: func(g *WithT, p yaml.Processor) {
   124  				g.Expect(p).ToNot(BeNil())
   125  				_, ok := (p).(*yaml.SimpleProcessor)
   126  				g.Expect(ok).To(BeTrue())
   127  			},
   128  		},
   129  	}
   130  
   131  	for _, tt := range tests {
   132  		t.Run(tt.name, func(t *testing.T) {
   133  			g := NewWithT(t)
   134  
   135  			ctx := context.Background()
   136  
   137  			configProvider := config.NewProvider("fakeProvider", "", clusterctlv1.CoreProviderType)
   138  			configClient, err := config.New(ctx, "", config.InjectReader(test.NewFakeReader()))
   139  			g.Expect(err).ToNot(HaveOccurred())
   140  
   141  			tt.opts = append(tt.opts, InjectRepository(NewMemoryRepository()))
   142  
   143  			repoClient, err := newRepositoryClient(
   144  				ctx,
   145  				configProvider,
   146  				configClient,
   147  				tt.opts...,
   148  			)
   149  			g.Expect(err).ToNot(HaveOccurred())
   150  			tt.assert(g, repoClient.processor)
   151  		})
   152  	}
   153  }