sigs.k8s.io/cluster-api@v1.7.1/cmd/clusterctl/client/cluster/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 cluster
    18  
    19  import (
    20  	"testing"
    21  
    22  	. "github.com/onsi/gomega"
    23  
    24  	yaml "sigs.k8s.io/cluster-api/cmd/clusterctl/client/yamlprocessor"
    25  	"sigs.k8s.io/cluster-api/cmd/clusterctl/internal/test"
    26  )
    27  
    28  func Test_newClusterClient_YamlProcessor(t *testing.T) {
    29  	tests := []struct {
    30  		name   string
    31  		opts   []Option
    32  		assert func(*WithT, yaml.Processor)
    33  	}{
    34  		{
    35  			name: "it creates a cluster client with simple yaml processor by default",
    36  			assert: func(g *WithT, p yaml.Processor) {
    37  				_, ok := (p).(*yaml.SimpleProcessor)
    38  				g.Expect(ok).To(BeTrue())
    39  			},
    40  		},
    41  		{
    42  			name: "it creates a cluster client with specified yaml processor",
    43  			opts: []Option{InjectYamlProcessor(test.NewFakeProcessor())},
    44  			assert: func(g *WithT, p yaml.Processor) {
    45  				_, ok := (p).(*yaml.SimpleProcessor)
    46  				g.Expect(ok).To(BeFalse())
    47  				_, ok = (p).(*test.FakeProcessor)
    48  				g.Expect(ok).To(BeTrue())
    49  			},
    50  		},
    51  		{
    52  			name: "it creates a cluster client with simple yaml processor even if injected with nil processor",
    53  			opts: []Option{InjectYamlProcessor(nil)},
    54  			assert: func(g *WithT, p yaml.Processor) {
    55  				g.Expect(p).ToNot(BeNil())
    56  				_, ok := (p).(*yaml.SimpleProcessor)
    57  				g.Expect(ok).To(BeTrue())
    58  			},
    59  		},
    60  	}
    61  
    62  	for _, tt := range tests {
    63  		t.Run(tt.name, func(t *testing.T) {
    64  			g := NewWithT(t)
    65  
    66  			client := newClusterClient(Kubeconfig{}, &fakeConfigClient{}, tt.opts...)
    67  			g.Expect(client).ToNot(BeNil())
    68  			tt.assert(g, client.processor)
    69  		})
    70  	}
    71  }