|
| 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 client |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3" |
| 23 | + "sigs.k8s.io/cluster-api/cmd/clusterctl/pkg/client/config" |
| 24 | + "sigs.k8s.io/cluster-api/cmd/clusterctl/pkg/internal/test" |
| 25 | +) |
| 26 | + |
| 27 | +// dummy test to document fakeClient usage |
| 28 | +func TestNewFakeClient(t *testing.T) { |
| 29 | + // create a fake config with a provider named P1 and a variable named var |
| 30 | + repository1Config := config.NewProvider("p1", "url", clusterctlv1.CoreProviderType) |
| 31 | + |
| 32 | + config1 := newFakeConfig(). |
| 33 | + WithVar("var", "value"). |
| 34 | + WithProvider(repository1Config) |
| 35 | + |
| 36 | + // create a new fakeClient using the fake config |
| 37 | + newFakeClient(config1) |
| 38 | +} |
| 39 | + |
| 40 | +type fakeClient struct { |
| 41 | + configClient config.Client |
| 42 | + internalclient *clusterctlClient |
| 43 | +} |
| 44 | + |
| 45 | +var _ Client = &fakeClient{} |
| 46 | + |
| 47 | +func (f fakeClient) GetProvidersConfig() ([]Provider, error) { |
| 48 | + return f.internalclient.GetProvidersConfig() |
| 49 | +} |
| 50 | + |
| 51 | +// newFakeClient return a fake implementation of the client for high-level clusterctl library, based on th given config. |
| 52 | +func newFakeClient(configClient config.Client) *fakeClient { |
| 53 | + |
| 54 | + fake := &fakeClient{} |
| 55 | + |
| 56 | + fake.configClient = configClient |
| 57 | + if fake.configClient == nil { |
| 58 | + fake.configClient = newFakeConfig() |
| 59 | + } |
| 60 | + |
| 61 | + fake.internalclient, _ = newClusterctlClient("fake-config", |
| 62 | + InjectConfig(fake.configClient), |
| 63 | + ) |
| 64 | + |
| 65 | + return fake |
| 66 | +} |
| 67 | + |
| 68 | +// newFakeConfig return a fake implementation of the client for low-level config library. |
| 69 | +// The implementation uses a FakeReader that stores configuration settings in a config map; you can use |
| 70 | +// the WithVar or WithProvider methods to set the config map values. |
| 71 | +func newFakeConfig() *fakeConfigClient { |
| 72 | + fakeReader := test.NewFakeReader() |
| 73 | + |
| 74 | + client, _ := config.New("fake-config", config.InjectReader(fakeReader)) |
| 75 | + |
| 76 | + return &fakeConfigClient{ |
| 77 | + fakeReader: fakeReader, |
| 78 | + internalclient: client, |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +type fakeConfigClient struct { |
| 83 | + fakeReader *test.FakeReader |
| 84 | + internalclient config.Client |
| 85 | +} |
| 86 | + |
| 87 | +var _ config.Client = &fakeConfigClient{} |
| 88 | + |
| 89 | +func (f fakeConfigClient) Providers() config.ProvidersClient { |
| 90 | + return f.internalclient.Providers() |
| 91 | +} |
| 92 | + |
| 93 | +func (f fakeConfigClient) Variables() config.VariablesClient { |
| 94 | + return f.internalclient.Variables() |
| 95 | +} |
| 96 | + |
| 97 | +func (f *fakeConfigClient) WithVar(key, value string) *fakeConfigClient { |
| 98 | + f.fakeReader.WithVar(key, value) |
| 99 | + return f |
| 100 | +} |
| 101 | + |
| 102 | +func (f *fakeConfigClient) WithProvider(provider config.Provider) *fakeConfigClient { |
| 103 | + f.fakeReader.WithProvider(provider.Name(), provider.Type(), provider.URL()) |
| 104 | + return f |
| 105 | +} |
| 106 | + |
| 107 | +var ( |
| 108 | + bootstrapProviderConfig = config.NewProvider("bootstrap", "url", clusterctlv1.BootstrapProviderType) |
| 109 | +) |
0 commit comments