Skip to content

Commit bc9800c

Browse files
committed
test(cli): add product-cli unit tests for HCP create cluster
The four platforms — AWS, KubeVirt, OpenStack, and Agent — have no product-cli cluster creation tests at all. Signed-off-by: mehabhalodiya <mehabhalodiya@gmail.com>
1 parent 144cca9 commit bc9800c

10 files changed

Lines changed: 1602 additions & 0 deletions
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package agent
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"sort"
7+
"testing"
8+
9+
. "github.com/onsi/gomega"
10+
11+
"github.com/openshift/hypershift/cmd/cluster/agent"
12+
"github.com/openshift/hypershift/cmd/cluster/core"
13+
"github.com/openshift/hypershift/support/certs"
14+
"github.com/openshift/hypershift/support/testutil"
15+
"github.com/openshift/hypershift/test/integration/framework"
16+
17+
utilrand "k8s.io/apimachinery/pkg/util/rand"
18+
19+
"github.com/spf13/cobra"
20+
"github.com/spf13/pflag"
21+
)
22+
23+
func TestNewCreateCommand(t *testing.T) {
24+
t.Parallel()
25+
26+
tests := []struct {
27+
name string
28+
test func(t *testing.T)
29+
}{
30+
{
31+
name: "When Agent create command is created, it should have 'agent' as use",
32+
test: func(t *testing.T) {
33+
g := NewWithT(t)
34+
opts := core.DefaultOptions()
35+
cmd := NewCreateCommand(opts)
36+
g.Expect(cmd.Use).To(Equal("agent"))
37+
},
38+
},
39+
{
40+
name: "When Agent create command is created, it should mark agent-namespace as required",
41+
test: func(t *testing.T) {
42+
g := NewWithT(t)
43+
opts := core.DefaultOptions()
44+
cmd := NewCreateCommand(opts)
45+
46+
agentNSFlag := cmd.Flag("agent-namespace")
47+
g.Expect(agentNSFlag).ToNot(BeNil())
48+
g.Expect(agentNSFlag.Annotations).To(HaveKey(cobra.BashCompOneRequiredFlag))
49+
g.Expect(agentNSFlag.Annotations[cobra.BashCompOneRequiredFlag]).To(ContainElement("true"))
50+
},
51+
},
52+
{
53+
name: "When Agent create command is added to a parent, pull-secret should be inherited",
54+
test: func(t *testing.T) {
55+
g := NewWithT(t)
56+
opts := core.DefaultOptions()
57+
58+
parent := &cobra.Command{Use: "cluster"}
59+
core.BindOptions(opts, parent.PersistentFlags())
60+
61+
cmd := NewCreateCommand(opts)
62+
parent.AddCommand(cmd)
63+
64+
pullSecretFlag := cmd.InheritedFlags().Lookup("pull-secret")
65+
g.Expect(pullSecretFlag).ToNot(BeNil(), "pull-secret should be inherited from parent command")
66+
},
67+
},
68+
{
69+
name: "When Agent create command is created, it should register exactly the expected flags",
70+
test: func(t *testing.T) {
71+
g := NewWithT(t)
72+
opts := core.DefaultOptions()
73+
cmd := NewCreateCommand(opts)
74+
75+
expectedFlags := []string{
76+
"agent-namespace",
77+
"agentLabelSelector",
78+
"api-server-address",
79+
}
80+
var actualFlags []string
81+
cmd.Flags().VisitAll(func(f *pflag.Flag) {
82+
actualFlags = append(actualFlags, f.Name)
83+
})
84+
sort.Strings(actualFlags)
85+
g.Expect(actualFlags).To(Equal(expectedFlags))
86+
},
87+
},
88+
}
89+
90+
for _, tt := range tests {
91+
t.Run(tt.name, func(t *testing.T) {
92+
t.Parallel()
93+
tt.test(t)
94+
})
95+
}
96+
}
97+
98+
func TestCreateCluster(t *testing.T) {
99+
utilrand.Seed(1234567890)
100+
certs.UnsafeSeed(1234567890)
101+
ctx := framework.InterruptableContext(t.Context())
102+
103+
tempDir := t.TempDir()
104+
105+
pullSecretFile := filepath.Join(tempDir, "pull-secret.json")
106+
if err := os.WriteFile(pullSecretFile, []byte(`fake`), 0600); err != nil {
107+
t.Fatalf("failed to write pullSecret: %v", err)
108+
}
109+
110+
for _, testCase := range []struct {
111+
name string
112+
args []string
113+
}{
114+
{
115+
name: "When minimal flags are provided, it should render correctly",
116+
args: []string{
117+
"--api-server-address=fakeAddress",
118+
"--render-sensitive",
119+
"--name=example",
120+
"--pull-secret=" + pullSecretFile,
121+
},
122+
},
123+
} {
124+
t.Run(testCase.name, func(t *testing.T) {
125+
flags := pflag.NewFlagSet(testCase.name, pflag.ContinueOnError)
126+
coreOpts := core.DefaultOptions()
127+
core.BindOptions(coreOpts, flags)
128+
agentOpts := agent.DefaultOptions()
129+
agent.BindOptions(agentOpts, flags)
130+
if err := flags.Parse(testCase.args); err != nil {
131+
t.Fatalf("failed to parse flags: %v", err)
132+
}
133+
134+
tempDir := t.TempDir()
135+
manifestsFile := filepath.Join(tempDir, "manifests.yaml")
136+
coreOpts.Render = true
137+
coreOpts.RenderInto = manifestsFile
138+
139+
if err := core.CreateCluster(ctx, coreOpts, agentOpts); err != nil {
140+
t.Fatalf("failed to create cluster: %v", err)
141+
}
142+
143+
manifests, err := os.ReadFile(manifestsFile)
144+
if err != nil {
145+
t.Fatalf("failed to read manifests file: %v", err)
146+
}
147+
testutil.CompareWithFixture(t, manifests)
148+
})
149+
}
150+
}

product-cli/cmd/cluster/agent/testdata/zz_fixture_TestCreateCluster_When_minimal_flags_are_provided__it_should_render_correctly.yaml

Lines changed: 135 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)