This repository has been archived by the owner on Jun 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
cli/cmd: rename --kubeconfig flag to --kubeconfig-file #602
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b9294aa
cli/cmd: rename --kubeconfig flag to --kubeconfig-file
invidian 97469ab
cli/cmd: add back support for using KUBECONFIG env variable
invidian 5024bf7
cli/cmd/root.go: handle binding flag errors
invidian c61ba20
cli/cmd: getKubeconfig: refactor into smaller functions
invidian 7470a24
cli/cmd: add tests for getKubeconfig
invidian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
// Copyright 2020 The Lokomotive Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cmd | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/spf13/viper" | ||
) | ||
|
||
type kubeconfigSources struct { | ||
flag string | ||
env string | ||
configFile string | ||
} | ||
|
||
func prepareKubeconfigSource(t *testing.T, k *kubeconfigSources) { | ||
// Ensure viper flag is NOT empty. | ||
viper.Set(kubeconfigFlag, k.flag) | ||
|
||
if k.env == "" { | ||
// Ensure KUBECONFIG is not set. | ||
if err := os.Unsetenv(kubeconfigEnvVariable); err != nil { | ||
t.Fatalf("unsetting %q environment variable: %v", kubeconfigEnvVariable, err) | ||
} | ||
} | ||
|
||
if k.env != "" { | ||
// Ensure KUBECONFIG IS set. | ||
if err := os.Setenv(kubeconfigEnvVariable, k.env); err != nil { | ||
t.Fatalf("setting %q environment variable: %v", kubeconfigEnvVariable, err) | ||
} | ||
} | ||
|
||
// Ensure there is no lokocfg configuration in working directory. | ||
tmpDir, err := ioutil.TempDir("", "lokoctl-tests-") | ||
if err != nil { | ||
t.Fatalf("creating tmp dir: %v", err) | ||
} | ||
|
||
t.Cleanup(func() { | ||
if err := os.RemoveAll(tmpDir); err != nil { | ||
t.Logf("removing temp dir %q: %v", tmpDir, err) | ||
} | ||
}) | ||
|
||
if err := os.Chdir(tmpDir); err != nil { | ||
t.Fatalf("changing working directory to %q: %v", tmpDir, err) | ||
} | ||
|
||
if k.configFile != "" { | ||
path := filepath.Join(tmpDir, "cluster.lokocfg") | ||
if err := ioutil.WriteFile(path, []byte(k.configFile), 0600); err != nil { | ||
t.Fatalf("writing file %q: %v", path, err) | ||
} | ||
} | ||
} | ||
|
||
func TestGetKubeconfigFlag(t *testing.T) { | ||
expectedPath := "/foo" | ||
|
||
k := &kubeconfigSources{ | ||
configFile: `cluster "packet" { | ||
asset_dir = "/bad" | ||
|
||
cluster_name = "" | ||
controller_count = 0 | ||
facility = "" | ||
management_cidrs = [] | ||
node_private_cidr = "" | ||
project_id = "" | ||
ssh_pubkeys = [] | ||
dns { | ||
provider = "" | ||
zone = "" | ||
} | ||
worker_pool "foo" { | ||
count = 0 | ||
} | ||
}`, | ||
flag: expectedPath, | ||
iaguis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
env: "/badpath", | ||
} | ||
|
||
prepareKubeconfigSource(t, k) | ||
|
||
kubeconfig, err := getKubeconfig() | ||
if err != nil { | ||
t.Fatalf("getting kubeconfig: %v", err) | ||
} | ||
|
||
if kubeconfig != expectedPath { | ||
t.Fatalf("expected %q, got %q", expectedPath, kubeconfig) | ||
} | ||
} | ||
|
||
func TestGetKubeconfigConfigFile(t *testing.T) { | ||
expectedPath := assetsKubeconfig("/foo") | ||
|
||
k := &kubeconfigSources{ | ||
configFile: `cluster "packet" { | ||
iaguis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
asset_dir = "/foo" | ||
|
||
cluster_name = "" | ||
controller_count = 0 | ||
facility = "" | ||
management_cidrs = [] | ||
node_private_cidr = "" | ||
project_id = "" | ||
ssh_pubkeys = [] | ||
dns { | ||
provider = "" | ||
zone = "" | ||
} | ||
worker_pool "foo" { | ||
count = 0 | ||
} | ||
}`, | ||
env: "/badpath", | ||
} | ||
|
||
prepareKubeconfigSource(t, k) | ||
|
||
kubeconfig, err := getKubeconfig() | ||
if err != nil { | ||
t.Fatalf("getting kubeconfig: %v", err) | ||
} | ||
|
||
if kubeconfig != expectedPath { | ||
t.Fatalf("expected %q, got %q", expectedPath, kubeconfig) | ||
} | ||
} | ||
|
||
func TestGetKubeconfigBadConfigFile(t *testing.T) { | ||
expectedPath := "" | ||
|
||
k := &kubeconfigSources{ | ||
configFile: `cluster "packet" { | ||
asset_dir = "/foo" | ||
}`, | ||
} | ||
|
||
prepareKubeconfigSource(t, k) | ||
|
||
kubeconfig, err := getKubeconfig() | ||
if err == nil { | ||
t.Errorf("getting kubeconfig with bad configuration should fail") | ||
} | ||
|
||
if kubeconfig != expectedPath { | ||
t.Fatalf("if getting kubeconfig fails, empty path should be returned") | ||
} | ||
} | ||
|
||
func TestGetKubeconfigEnvVariable(t *testing.T) { | ||
expectedPath := "/foo" | ||
|
||
k := &kubeconfigSources{ | ||
env: expectedPath, | ||
} | ||
|
||
prepareKubeconfigSource(t, k) | ||
|
||
kubeconfig, err := getKubeconfig() | ||
if err != nil { | ||
t.Fatalf("getting kubeconfig: %v", err) | ||
} | ||
|
||
if kubeconfig != expectedPath { | ||
t.Fatalf("expected %q, got %q", expectedPath, kubeconfig) | ||
} | ||
} | ||
|
||
func TestGetKubeconfigDefault(t *testing.T) { | ||
expectedPath := expandKubeconfigPath(defaultKubeconfigPath) | ||
|
||
k := &kubeconfigSources{} | ||
|
||
prepareKubeconfigSource(t, k) | ||
|
||
kubeconfig, err := getKubeconfig() | ||
if err != nil { | ||
t.Fatalf("getting kubeconfig: %v", err) | ||
} | ||
|
||
if kubeconfig != expectedPath { | ||
t.Fatalf("expected %q, got %q", expectedPath, kubeconfig) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like a test where both the KUBECONFIG flag is set and the lokocfg file is present to make sure it takes the file defined in the lokocfg. This is the most dangerous case IMO.