Skip to content

Commit 59e97a4

Browse files
authored
chore: add back cli unit tests (#1623)
1 parent cefa45b commit 59e97a4

19 files changed

+1036
-0
lines changed

cmd/installer/cli/cidr_test.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package cli
2+
3+
import (
4+
"testing"
5+
6+
"github.com/k0sproject/k0s/pkg/apis/k0s/v1beta1"
7+
"github.com/spf13/cobra"
8+
"github.com/spf13/pflag"
9+
"github.com/stretchr/testify/require"
10+
"k8s.io/utils/ptr"
11+
)
12+
13+
func Test_getCIDRConfig(t *testing.T) {
14+
tests := []struct {
15+
name string
16+
setFlags func(flagSet *pflag.FlagSet)
17+
expected *CIDRConfig
18+
}{
19+
{
20+
name: "with pod and service flags",
21+
expected: &CIDRConfig{
22+
PodCIDR: "10.0.0.0/24",
23+
ServiceCIDR: "10.1.0.0/24",
24+
GlobalCIDR: nil,
25+
},
26+
setFlags: func(flagSet *pflag.FlagSet) {
27+
flagSet.Set("pod-cidr", "10.0.0.0/24")
28+
flagSet.Set("service-cidr", "10.1.0.0/24")
29+
},
30+
},
31+
{
32+
name: "with pod flag",
33+
expected: &CIDRConfig{
34+
PodCIDR: "10.0.0.0/24",
35+
ServiceCIDR: v1beta1.DefaultNetwork().ServiceCIDR,
36+
GlobalCIDR: nil,
37+
},
38+
setFlags: func(flagSet *pflag.FlagSet) {
39+
flagSet.Set("pod-cidr", "10.0.0.0/24")
40+
},
41+
},
42+
{
43+
name: "with pod, service and cidr flags",
44+
expected: &CIDRConfig{
45+
PodCIDR: "10.0.0.0/24",
46+
ServiceCIDR: "10.1.0.0/24",
47+
GlobalCIDR: nil,
48+
},
49+
setFlags: func(flagSet *pflag.FlagSet) {
50+
flagSet.Set("pod-cidr", "10.0.0.0/24")
51+
flagSet.Set("service-cidr", "10.1.0.0/24")
52+
flagSet.Set("cidr", "10.2.0.0/24")
53+
},
54+
},
55+
{
56+
name: "with pod and cidr flags",
57+
expected: &CIDRConfig{
58+
PodCIDR: "10.0.0.0/24",
59+
ServiceCIDR: v1beta1.DefaultNetwork().ServiceCIDR,
60+
GlobalCIDR: nil,
61+
},
62+
setFlags: func(flagSet *pflag.FlagSet) {
63+
flagSet.Set("pod-cidr", "10.0.0.0/24")
64+
flagSet.Set("cidr", "10.2.0.0/24")
65+
},
66+
},
67+
{
68+
name: "with cidr flag",
69+
expected: &CIDRConfig{
70+
PodCIDR: "10.2.0.0/25",
71+
ServiceCIDR: "10.2.0.128/25",
72+
GlobalCIDR: ptr.To("10.2.0.0/24"),
73+
},
74+
setFlags: func(flagSet *pflag.FlagSet) {
75+
flagSet.Set("cidr", "10.2.0.0/24")
76+
},
77+
},
78+
}
79+
80+
for _, test := range tests {
81+
t.Run(test.name, func(t *testing.T) {
82+
req := require.New(t)
83+
84+
cmd := &cobra.Command{}
85+
addCIDRFlags(cmd)
86+
87+
test.setFlags(cmd.Flags())
88+
89+
got, err := getCIDRConfig(cmd)
90+
req.NoError(err)
91+
req.Equal(test.expected, got)
92+
})
93+
}
94+
}
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
package cli
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"testing"
7+
8+
"github.com/replicatedhq/embedded-cluster/pkg/release"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func Test_getLicenseFromFilepath(t *testing.T) {
13+
tests := []struct {
14+
name string
15+
licenseContents string
16+
wantErr string
17+
useRelease bool
18+
}{
19+
{
20+
name: "no license, no release",
21+
wantErr: "",
22+
},
23+
{
24+
name: "no license, with release",
25+
useRelease: true,
26+
wantErr: `no license was provided for embedded-cluster-smoke-test-staging-app and one is required, please rerun with '--license <path to license file>'`,
27+
},
28+
{
29+
name: "valid license, no release",
30+
licenseContents: `
31+
spec:
32+
appSlug: embedded-cluster-smoke-test-staging-app
33+
channelID: "2cHXb1RCttzpR0xvnNWyaZCgDBP"
34+
isEmbeddedClusterDownloadEnabled: true
35+
`,
36+
wantErr: "a license was provided but no release was found in binary, please rerun without the license flag",
37+
},
38+
{
39+
name: "valid license, with release",
40+
useRelease: true,
41+
licenseContents: `
42+
spec:
43+
appSlug: embedded-cluster-smoke-test-staging-app
44+
channelID: "2cHXb1RCttzpR0xvnNWyaZCgDBP"
45+
isEmbeddedClusterDownloadEnabled: true
46+
`,
47+
},
48+
{
49+
name: "valid multi-channel license, with release",
50+
useRelease: true,
51+
licenseContents: `
52+
spec:
53+
appSlug: embedded-cluster-smoke-test-staging-app
54+
channelID: "OtherChannelID"
55+
isEmbeddedClusterDownloadEnabled: true
56+
channels:
57+
- channelID: OtherChannelID
58+
channelName: OtherChannel
59+
channelSlug: other-channel
60+
isDefault: true
61+
- channelID: 2cHXb1RCttzpR0xvnNWyaZCgDBP
62+
channelName: ExpectedChannel
63+
channelSlug: expected-channel
64+
isDefault: false
65+
`,
66+
},
67+
{
68+
name: "expired license, with release",
69+
useRelease: true,
70+
licenseContents: `
71+
spec:
72+
appSlug: embedded-cluster-smoke-test-staging-app
73+
channelID: "2cHXb1RCttzpR0xvnNWyaZCgDBP"
74+
isEmbeddedClusterDownloadEnabled: true
75+
entitlements:
76+
expires_at:
77+
description: License Expiration
78+
signature: {}
79+
title: Expiration
80+
value: "2024-06-03T00:00:00Z"
81+
valueType: String
82+
`,
83+
wantErr: "license expired on 2024-06-03 00:00:00 +0000 UTC, please provide a valid license",
84+
},
85+
{
86+
name: "license with no expiration, with release",
87+
useRelease: true,
88+
licenseContents: `
89+
spec:
90+
appSlug: embedded-cluster-smoke-test-staging-app
91+
channelID: "2cHXb1RCttzpR0xvnNWyaZCgDBP"
92+
isEmbeddedClusterDownloadEnabled: true
93+
entitlements:
94+
expires_at:
95+
description: License Expiration
96+
signature: {}
97+
title: Expiration
98+
value: ""
99+
valueType: String
100+
`,
101+
},
102+
{
103+
name: "license with 100 year expiration, with release",
104+
useRelease: true,
105+
licenseContents: `
106+
spec:
107+
appSlug: embedded-cluster-smoke-test-staging-app
108+
channelID: "2cHXb1RCttzpR0xvnNWyaZCgDBP"
109+
isEmbeddedClusterDownloadEnabled: true
110+
entitlements:
111+
expires_at:
112+
description: License Expiration
113+
signature: {}
114+
title: Expiration
115+
value: "2124-06-03T00:00:00Z"
116+
valueType: String
117+
`,
118+
},
119+
{
120+
name: "embedded cluster not enabled, with release",
121+
useRelease: true,
122+
licenseContents: `
123+
spec:
124+
appSlug: embedded-cluster-smoke-test-staging-app
125+
channelID: "2cHXb1RCttzpR0xvnNWyaZCgDBP"
126+
isEmbeddedClusterDownloadEnabled: false
127+
`,
128+
wantErr: "license does not have embedded cluster enabled, please provide a valid license",
129+
},
130+
{
131+
name: "incorrect license (multichan license)",
132+
useRelease: true,
133+
licenseContents: `
134+
spec:
135+
appSlug: embedded-cluster-smoke-test-staging-app
136+
channelID: "2i9fCbxTNIhuAOaC6MoKMVeGzuK"
137+
isEmbeddedClusterDownloadEnabled: false
138+
channels:
139+
- channelID: 2i9fCbxTNIhuAOaC6MoKMVeGzuK
140+
channelName: Stable
141+
channelSlug: stable
142+
isDefault: true
143+
- channelID: 4l9fCbxTNIhuAOaC6MoKMVeV3K
144+
channelName: Alternate
145+
channelSlug: alternate
146+
isDefault: false
147+
`,
148+
wantErr: "binary channel 2cHXb1RCttzpR0xvnNWyaZCgDBP (CI) not present in license, channels allowed by license are: stable (2i9fCbxTNIhuAOaC6MoKMVeGzuK), alternate (4l9fCbxTNIhuAOaC6MoKMVeV3K)",
149+
},
150+
{
151+
name: "incorrect license (pre-multichan license)",
152+
useRelease: true,
153+
licenseContents: `
154+
spec:
155+
appSlug: embedded-cluster-smoke-test-staging-app
156+
channelID: "2i9fCbxTNIhuAOaC6MoKMVeGzuK"
157+
channelName: "Stable"
158+
isEmbeddedClusterDownloadEnabled: false
159+
`,
160+
wantErr: "binary channel 2cHXb1RCttzpR0xvnNWyaZCgDBP (CI) not present in license, channels allowed by license are: Stable (2i9fCbxTNIhuAOaC6MoKMVeGzuK)",
161+
},
162+
}
163+
for _, tt := range tests {
164+
t.Run(tt.name, func(t *testing.T) {
165+
req := require.New(t)
166+
167+
tmpdir, err := os.MkdirTemp("", "license")
168+
defer os.RemoveAll(tmpdir)
169+
req.NoError(err)
170+
171+
licenseFile, err := os.Create(tmpdir + "/license.yaml")
172+
req.NoError(err)
173+
_, err = licenseFile.Write([]byte(tt.licenseContents))
174+
req.NoError(err)
175+
176+
dataMap := map[string][]byte{}
177+
if tt.useRelease {
178+
dataMap["release.yaml"] = []byte(`
179+
# channel release object
180+
channelID: "2cHXb1RCttzpR0xvnNWyaZCgDBP"
181+
channelSlug: "CI"
182+
appSlug: "embedded-cluster-smoke-test-staging-app"
183+
versionLabel: testversion
184+
`)
185+
}
186+
err = release.SetReleaseDataForTests(dataMap)
187+
req.NoError(err)
188+
189+
if tt.licenseContents != "" {
190+
_, err = getLicenseFromFilepath(filepath.Join(tmpdir, "license.yaml"))
191+
} else {
192+
_, err = getLicenseFromFilepath("")
193+
}
194+
195+
if tt.wantErr != "" {
196+
req.EqualError(err, tt.wantErr)
197+
} else {
198+
req.NoError(err)
199+
}
200+
})
201+
}
202+
}

0 commit comments

Comments
 (0)