Skip to content

Commit 36e9d72

Browse files
chore: implementing some unit tests (#55)
* chore: covering defaults package with tests this commits covers defaults package with unit tests. * chore: covering ssh package with unit tests * chore: replacing panic() with logrus.Fatalf * chore: fixes due to rebase
1 parent ba5dfae commit 36e9d72

File tree

6 files changed

+409
-151
lines changed

6 files changed

+409
-151
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ require (
1616
github.com/k0sproject/rig v0.10.0
1717
github.com/replicatedhq/troubleshoot v0.71.1
1818
github.com/sirupsen/logrus v1.9.3
19+
github.com/stretchr/testify v1.8.4
1920
github.com/urfave/cli/v2 v2.25.1
2021
golang.org/x/crypto v0.11.0
2122
golang.org/x/mod v0.12.0
@@ -164,6 +165,7 @@ require (
164165
github.com/pkg/errors v0.9.1 // indirect
165166
github.com/pkg/sftp v1.13.5 // indirect
166167
github.com/pkg/xattr v0.4.9 // indirect
168+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
167169
github.com/prometheus/client_golang v1.15.1 // indirect
168170
github.com/prometheus/client_model v0.4.0 // indirect
169171
github.com/prometheus/common v0.42.0 // indirect

pkg/defaults/defaults.go

Lines changed: 47 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -1,199 +1,106 @@
11
// Package defaults holds default values for the helmvm binary. For sake of
2-
// keeping everything simple this packages panics if some error occurs as
2+
// keeping everything simple this packages exits(1) if some error occurs as
33
// these should not happen in the first place.
44
package defaults
55

6-
import (
7-
"fmt"
8-
"net"
9-
"os"
10-
"path/filepath"
11-
"runtime"
12-
"strings"
13-
14-
"github.com/gosimple/slug"
6+
var (
7+
// K0sVersion holds the version of k0s binary we are embedding. this is
8+
// set at compile time via ldflags.
9+
K0sVersion = "0.0.0"
10+
// provider holds a global reference to the default provider.
11+
provider *DefaultsProvider
1512
)
1613

17-
// K0sVersion holds the version of k0s binary we are embedding. this is set
18-
// at compile time via ldflags.
19-
var K0sVersion = "0.0.0"
20-
21-
func init() {
22-
if err := os.MkdirAll(K0sctlBinsSubDir(), 0755); err != nil {
23-
panic(fmt.Errorf("unable to create basedir: %w", err))
24-
}
25-
if err := os.MkdirAll(ConfigSubDir(), 0755); err != nil {
26-
panic(fmt.Errorf("unable to create config dir: %w", err))
27-
}
28-
if err := os.MkdirAll(HelmVMBinsSubDir(), 0755); err != nil {
29-
panic(fmt.Errorf("unable to create helmvm bin dir: %w", err))
30-
}
31-
if err := os.MkdirAll(HelmVMLogsSubDir(), 0755); err != nil {
32-
panic(fmt.Errorf("unable to create helmvm logs dir: %w", err))
14+
// def returns a global reference to the default provider. creates one if not
15+
// already created.
16+
func def() *DefaultsProvider {
17+
if provider == nil {
18+
provider = NewProvider("")
3319
}
20+
return provider
3421
}
3522

36-
const (
37-
k0sBinsSubDirDarwin = "Library/Caches/k0sctl/k0s/linux/amd64"
38-
k0sBinsSubDirLinux = ".cache/k0sctl/k0s/linux/amd64"
39-
)
40-
41-
// BinaryName returns the binary name, this is useful for places where we
42-
// need to present the name of the binary to the user (the name may vary if
43-
// the binary is renamed). We make sure the name does not contain invalid
44-
// characters for a filename.
23+
// BinaryName calls BinaryName on the default provider.
4524
func BinaryName() string {
46-
exe, err := os.Executable()
47-
if err != nil {
48-
panic(err)
49-
}
50-
base := filepath.Base(exe)
51-
return slug.Make(base)
25+
return def().BinaryName()
5226
}
5327

54-
// K0sctlBinsSubDir returns the path to the directory where k0sctl binaries
55-
// are stored. This is a subdirectory of the user's home directory. Follows
56-
// the k0sctl directory convention.
28+
// K0sctlBinsSubDir calls K0sctlBinsSubDir on the default provider.
5729
func K0sctlBinsSubDir() string {
58-
home, err := os.UserHomeDir()
59-
if err != nil {
60-
panic(err)
61-
}
62-
if runtime.GOOS == "darwin" {
63-
return filepath.Join(home, k0sBinsSubDirDarwin)
64-
}
65-
return filepath.Join(home, k0sBinsSubDirLinux)
30+
return def().K0sctlBinsSubDir()
6631
}
6732

68-
// HelmVMBinsSubDir returns the path to the directory where helmvm binaries
69-
// are stored. This is a subdirectory of the user's home directory.
33+
// HelmVMBinsSubDir calls HelmVMBinsSubDir on the default provider.
7034
func HelmVMBinsSubDir() string {
71-
home, err := os.UserHomeDir()
72-
if err != nil {
73-
panic(err)
74-
}
75-
hidden := fmt.Sprintf(".%s", BinaryName())
76-
return filepath.Join(home, hidden, "bin")
35+
return def().HelmVMBinsSubDir()
7736
}
7837

79-
// HelmVMLogsSubDir returns the path to the directory where helmvm logs are
80-
// stored. This is a subdirectory of the user's home directory.
38+
// HelmVMLogsSubDir calls HelmVMLogsSubDir on the default provider.
8139
func HelmVMLogsSubDir() string {
82-
home, err := os.UserHomeDir()
83-
if err != nil {
84-
panic(err)
85-
}
86-
hidden := fmt.Sprintf(".%s", BinaryName())
87-
return filepath.Join(home, hidden, "logs")
40+
return def().HelmVMLogsSubDir()
8841
}
8942

90-
// K0sctlApplyLogPath returns the path to the k0sctl apply log file.
43+
// K0sctlApplyLogPath calls K0sctlApplyLogPath on the default provider.
9144
func K0sctlApplyLogPath() string {
92-
home, err := os.UserHomeDir()
93-
if err != nil {
94-
panic(err)
95-
}
96-
return filepath.Join(home, ".cache", "k0sctl", "k0sctl.log")
45+
return def().K0sctlApplyLogPath()
9746
}
9847

99-
// SSHKeyPath returns the path to the SSH managed by helmvm installation.
48+
// SSHKeyPath calls SSHKeyPath on the default provider.
10049
func SSHKeyPath() string {
101-
home, err := os.UserHomeDir()
102-
if err != nil {
103-
panic(err)
104-
}
105-
return filepath.Join(home, ".ssh", "helmvm_rsa")
50+
return def().SSHKeyPath()
10651
}
10752

108-
// SSHAuthorizedKeysPath returns the path to the authorized_hosts file.
53+
// SSHAuthorizedKeysPath calls SSHAuthorizedKeysPath on the default provider.
10954
func SSHAuthorizedKeysPath() string {
110-
home, err := os.UserHomeDir()
111-
if err != nil {
112-
panic(err)
113-
}
114-
return filepath.Join(home, ".ssh", "authorized_keys")
55+
return def().SSHAuthorizedKeysPath()
11556
}
11657

117-
// ConfigSubDir returns the path to the directory where k0sctl configuration
118-
// files are stored. This is a subdirectory of the user's home directory.
58+
// ConfigSubDir calls ConfigSubDir on the default provider.
11959
func ConfigSubDir() string {
120-
home, err := os.UserHomeDir()
121-
if err != nil {
122-
panic(err)
123-
}
124-
hidden := fmt.Sprintf(".%s", BinaryName())
125-
return filepath.Join(home, hidden, "etc")
60+
return def().ConfigSubDir()
12661
}
12762

128-
// K0sBinaryPath returns the path to the k0s binary.
63+
// K0sBinaryPath calls K0sBinaryPath on the default provider.
12964
func K0sBinaryPath() string {
130-
return PathToK0sctlBinary(fmt.Sprintf("k0s-%s", K0sVersion))
65+
return def().K0sBinaryPath()
13166
}
13267

133-
// PathToK0sctlBinary is an utility function that returns the full path to
134-
// a materialized binary that belongs to k0sctl. This function does not check
135-
// if the file exists.
68+
// PathToK0sctlBinary calls PathToK0sctlBinary on the default provider.
13669
func PathToK0sctlBinary(name string) string {
137-
return filepath.Join(K0sctlBinsSubDir(), name)
70+
return def().PathToK0sctlBinary(name)
13871
}
13972

140-
// PathToHelmVMBinary is an utility function that returns the full path to a
141-
// materialized binary that belongs to helmvm (do not confuse with binaries
142-
// belonging to k0sctl). This function does not check if the file exists.
73+
// PathToHelmVMBinary calls PathToHelmVMBinary on the default provider.
14374
func PathToHelmVMBinary(name string) string {
144-
return filepath.Join(HelmVMBinsSubDir(), name)
75+
return def().PathToHelmVMBinary(name)
14576
}
14677

147-
// PathToLog returns the full path to a log file. This function does not check
148-
// if the file exists.
78+
// PathToLog calls PathToLog on the default provider.
14979
func PathToLog(name string) string {
150-
return filepath.Join(HelmVMLogsSubDir(), name)
80+
return def().PathToLog(name)
15181
}
15282

153-
// PathToConfig returns the full path to a configuration file. This function
154-
// does not check if the file exists.
83+
// PathToConfig calls PathToConfig on the default provider.
15584
func PathToConfig(name string) string {
156-
return filepath.Join(ConfigSubDir(), name)
85+
return def().PathToConfig(name)
15786
}
15887

159-
// FileNameForImage returns an appropriate .tar name for a given image.
160-
// e.g. quay.io/test/test:v1 would return quay.io-test-test-v1.tar.
88+
// FileNameForImage calls FileNameForImage on the default provider.
16189
func FileNameForImage(img string) string {
162-
prefix := strings.ReplaceAll(img, "/", "-")
163-
prefix = strings.ReplaceAll(prefix, ":", "-")
164-
return fmt.Sprintf("%s.tar", prefix)
90+
return def().FileNameForImage(img)
16591
}
16692

167-
// PreferredNodeIPAddress returns the ip address the node uses when reaching
168-
// the internet. This is useful when the node has multiple interfaces and we
169-
// want to bind to one of the interfaces.
93+
// PreferredNodeIPAddress calls PreferredNodeIPAddress on the default provider.
17094
func PreferredNodeIPAddress() (string, error) {
171-
conn, err := net.Dial("udp", "8.8.8.8:80")
172-
if err != nil {
173-
return "", fmt.Errorf("unable to get local IP: %w", err)
174-
}
175-
defer conn.Close()
176-
addr := conn.LocalAddr().(*net.UDPAddr)
177-
return addr.IP.String(), nil
95+
return def().PreferredNodeIPAddress()
17896
}
17997

180-
// DecentralizedInstall returns true if the cluster installation has been
181-
// executed in a decentralized way (installing the first node then generating
182-
// a join token and installing the others).
98+
// DecentralizedInstall calls DecentralizedInstall on the default provider.
18399
func DecentralizedInstall() bool {
184-
fpath := PathToConfig(".decentralized")
185-
_, err := os.Stat(fpath)
186-
return err == nil
100+
return def().DecentralizedInstall()
187101
}
188102

189-
// SetInstallAsDecentralized sets the decentralized install flag inside the
190-
// configuration directory.
103+
// SetInstallAsDecentralized calls SetInstallAsDecentralized on the default provider.
191104
func SetInstallAsDecentralized() error {
192-
fpath := PathToConfig(".decentralized")
193-
fp, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE, 0666)
194-
if err != nil {
195-
return fmt.Errorf("unable to set installation mode: %w", err)
196-
}
197-
defer fp.Close()
198-
return nil
105+
return def().SetInstallAsDecentralized()
199106
}

0 commit comments

Comments
 (0)