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

     1  /*
     2  Copyright 2021 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 config
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	. "github.com/onsi/gomega"
    24  
    25  	"sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
    26  )
    27  
    28  func TestMemoryReader(t *testing.T) {
    29  	tests := []struct {
    30  		name       string
    31  		variables  map[string]string
    32  		providers  []configProvider
    33  		imageMetas map[string]imageMeta
    34  		wantErr    bool
    35  	}{
    36  		{
    37  			name: "providers",
    38  			providers: []configProvider{
    39  				{
    40  					Name: "foo",
    41  					Type: v1alpha3.BootstrapProviderType,
    42  					URL:  "url",
    43  				},
    44  			},
    45  			imageMetas: map[string]imageMeta{},
    46  			variables: map[string]string{
    47  				"one":   "1",
    48  				"two":   "2",
    49  				"three": "3",
    50  			},
    51  		},
    52  	}
    53  	for _, tt := range tests {
    54  		t.Run(tt.name, func(t *testing.T) {
    55  			g := NewWithT(t)
    56  
    57  			ctx := context.Background()
    58  
    59  			f := NewMemoryReader()
    60  			g.Expect(f.Init(ctx, "")).To(Succeed())
    61  			for _, p := range tt.providers {
    62  				_, err := f.AddProvider(p.Name, p.Type, p.URL)
    63  				g.Expect(err).ToNot(HaveOccurred())
    64  			}
    65  			for n, v := range tt.variables {
    66  				f.Set(n, v)
    67  			}
    68  
    69  			providersOut := []configProvider{}
    70  			g.Expect(f.UnmarshalKey("providers", &providersOut)).To(Succeed())
    71  			g.Expect(providersOut).To(BeComparableTo(tt.providers))
    72  
    73  			imagesOut := map[string]imageMeta{}
    74  			g.Expect(f.UnmarshalKey("images", &imagesOut)).To(Succeed())
    75  			g.Expect(imagesOut).To(BeComparableTo(tt.imageMetas))
    76  
    77  			for n, v := range tt.variables {
    78  				outV, err := f.Get(n)
    79  				g.Expect(err).ToNot(HaveOccurred())
    80  				g.Expect(outV).To(Equal(v))
    81  			}
    82  			val, err := f.Get("notfound")
    83  			g.Expect(err).To(HaveOccurred())
    84  			g.Expect(val).To(Equal(""))
    85  		})
    86  	}
    87  }