Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 25 additions & 25 deletions pkg/minikube/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"context"
"fmt"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -712,50 +713,49 @@ preferences: {}
users:
- name: minikube
`
var tests = []struct {
description string
kubeconfigPath string
config string
err bool
tests := []struct {
description string
config string
err bool
}{
{
description: "ok",
kubeconfigPath: "/tmp/kube_config",
config: mockK8sConfig,
err: false,
description: "ok",
config: mockK8sConfig,
err: false,
},
{
description: "empty config",
kubeconfigPath: "/tmp/kube_config",
config: "",
err: true,
description: "empty config",
config: "",
err: true,
},
{
description: "broken config",
kubeconfigPath: "/tmp/kube_config",
config: "this**is&&not: yaml::valid: file",
err: true,
description: "broken config",
config: "this**is&&not: yaml::valid: file",
err: true,
},
}

for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
mockK8sConfigByte := []byte(test.config)
mockK8sConfigPath := test.kubeconfigPath
err := os.WriteFile(mockK8sConfigPath, mockK8sConfigByte, 0644)
defer os.Remove(mockK8sConfigPath)
if err != nil {
t.Fatalf("Unexpected error when writing to file %v. Error: %v", test.kubeconfigPath, err)
// Each sub-test gets its own temp dir & file for isolation and portability.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is standard practice, I don't think we ned to justify it. All tests should work like this.

tmpDir := t.TempDir()
mockK8sConfigPath := filepath.Join(tmpDir, "kube_config")

// Only write the file if we have any content; an empty string still creates a 0-byte file,
// which is what we want to simulate "empty config".
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment contradicts the code, we always write a file, even if we don't have any content.

if err := os.WriteFile(mockK8sConfigPath, []byte(test.config), 0644); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no reason to create the kubeconfig world readable. This is the default kubeconfig permissions on macOS:

% ls -lh ~/.kube/config
-rw-------  1 nir  staff   106B Oct 10 11:37 /Users/nir/.kube/config

It will be better to use real world permissions. This will break the code if it has bad assumptions about being able to read the config from another user in the same group.

t.Fatalf("failed to write kubeconfig: %v", err)
}
t.Setenv("KUBECONFIG", mockK8sConfigPath)

k8s := K8sClientGetter{}
_, err = k8s.GetCoreClient("minikube")
_, err := k8s.GetCoreClient("minikube")

if err != nil && !test.err {
t.Fatalf("GetCoreClient returned unexpected error: %v", err)
}
if err == nil && test.err {
t.Fatal("GetCoreClient expected to return error but got nil")
t.Fatalf("GetCoreClient expected to return error but got nil")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why t.Fatalf() if we don't format anything?

}
})
}
Expand Down
Loading