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

     1  /*
     2  Copyright 2019 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  	"testing"
    22  
    23  	. "github.com/onsi/gomega"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  
    26  	clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
    27  	"sigs.k8s.io/cluster-api/cmd/clusterctl/client/config"
    28  	"sigs.k8s.io/cluster-api/cmd/clusterctl/internal/test"
    29  )
    30  
    31  func Test_metadataClient_Get(t *testing.T) {
    32  	type fields struct {
    33  		provider   config.Provider
    34  		version    string
    35  		repository Repository
    36  	}
    37  	tests := []struct {
    38  		name    string
    39  		fields  fields
    40  		want    *clusterctlv1.Metadata
    41  		wantErr bool
    42  	}{
    43  		{
    44  			name: "Pass",
    45  			fields: fields{
    46  				provider: config.NewProvider("p1", "", clusterctlv1.CoreProviderType),
    47  				version:  "v1.0.0",
    48  				repository: NewMemoryRepository().
    49  					WithPaths("root", "").
    50  					WithDefaultVersion("v1.0.0").
    51  					WithMetadata("v1.0.0", &clusterctlv1.Metadata{
    52  						ReleaseSeries: []clusterctlv1.ReleaseSeries{
    53  							{Major: 1, Minor: 2, Contract: test.CurrentCAPIContract},
    54  						},
    55  					}),
    56  			},
    57  			want: &clusterctlv1.Metadata{
    58  				TypeMeta: metav1.TypeMeta{
    59  					APIVersion: clusterctlv1.GroupVersion.String(),
    60  					Kind:       "Metadata",
    61  				},
    62  				ReleaseSeries: []clusterctlv1.ReleaseSeries{
    63  					{
    64  						Major:    1,
    65  						Minor:    2,
    66  						Contract: test.CurrentCAPIContract,
    67  					},
    68  				},
    69  			},
    70  			wantErr: false,
    71  		},
    72  		{
    73  			name: "Fails if the file does not exists",
    74  			fields: fields{
    75  				provider: config.NewProvider("p1", "", clusterctlv1.CoreProviderType),
    76  				version:  "v1.0.0",
    77  				repository: NewMemoryRepository(). // repository without a metadata file
    78  									WithPaths("root", "").
    79  									WithDefaultVersion("v1.0.0"),
    80  			},
    81  			want:    nil,
    82  			wantErr: true,
    83  		},
    84  		{
    85  			name: "Fails if the file does not exists for the current version",
    86  			fields: fields{
    87  				provider: config.NewProvider("p1", "", clusterctlv1.CoreProviderType),
    88  				version:  "v1.0.0",
    89  				repository: NewMemoryRepository().
    90  					WithPaths("root", "").
    91  					WithDefaultVersion("v2.0.0").
    92  					WithMetadata("v2.0.0", &clusterctlv1.Metadata{ // metadata file exists for version 2.0.0, while we are checking metadata for v1.0.0
    93  						ReleaseSeries: []clusterctlv1.ReleaseSeries{
    94  							{Major: 1, Minor: 2, Contract: test.CurrentCAPIContract},
    95  						},
    96  					}),
    97  			},
    98  			want:    nil,
    99  			wantErr: true,
   100  		},
   101  		{
   102  			name: "Fails if the file isn't a valid metadata",
   103  			fields: fields{
   104  				provider: config.NewProvider("p1", "", clusterctlv1.CoreProviderType),
   105  				version:  "v1.0.0",
   106  				repository: NewMemoryRepository().
   107  					WithPaths("root", "").
   108  					WithDefaultVersion("v2.0.0").
   109  					WithFile("v2.0.0", "metadata.yaml", []byte("not a valid metadata file!")), // metadata file exists but is invalid
   110  			},
   111  			want:    nil,
   112  			wantErr: true,
   113  		},
   114  	}
   115  	for _, tt := range tests {
   116  		t.Run(tt.name, func(t *testing.T) {
   117  			g := NewWithT(t)
   118  
   119  			f := &metadataClient{
   120  				configVarClient: test.NewFakeVariableClient(),
   121  				provider:        tt.fields.provider,
   122  				version:         tt.fields.version,
   123  				repository:      tt.fields.repository,
   124  			}
   125  			got, err := f.Get(context.Background())
   126  			if tt.wantErr {
   127  				g.Expect(err).To(HaveOccurred())
   128  				return
   129  			}
   130  			g.Expect(err).ToNot(HaveOccurred())
   131  
   132  			g.Expect(got).To(BeComparableTo(tt.want))
   133  		})
   134  	}
   135  }