Skip to content
This repository was archived by the owner on Mar 16, 2024. It is now read-only.

Commit e8a480d

Browse files
authored
validate -j/--project inputs for acorn cli (#1218)
Signed-off-by: Joshua Silverio <joshua@acorn.io>
1 parent 0f53ce0 commit e8a480d

File tree

3 files changed

+142
-6
lines changed

3 files changed

+142
-6
lines changed

pkg/cli/acorn.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88

99
cli "github.com/acorn-io/acorn/pkg/cli/builder"
1010
"github.com/acorn-io/acorn/pkg/client/term"
11+
"github.com/acorn-io/acorn/pkg/config"
12+
"github.com/acorn-io/acorn/pkg/project"
1113
"github.com/google/go-containerregistry/pkg/logs"
1214
"github.com/pterm/pterm"
1315
"github.com/sirupsen/logrus"
@@ -117,6 +119,23 @@ func (a *Acorn) PersistentPre(cmd *cobra.Command, args []string) error {
117119
return err
118120
}
119121
}
122+
123+
if a.Project != "" {
124+
clientFactory := CommandClientFactory{
125+
cmd: cmd,
126+
acorn: a,
127+
}
128+
cfg, err := config.ReadCLIConfig()
129+
if err != nil {
130+
return err
131+
}
132+
133+
_, err = project.Get(cmd.Context(), clientFactory.Options().WithCLIConfig(cfg), a.Project)
134+
if err != nil {
135+
return err
136+
}
137+
}
138+
120139
return nil
121140
}
122141

pkg/cli/acorn_test.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,105 @@ func TestAcorn(t *testing.T) {
7272
})
7373
}
7474
}
75+
76+
func TestAcornProject(t *testing.T) {
77+
type fields struct {
78+
Quiet bool
79+
Output string
80+
All bool
81+
}
82+
type args struct {
83+
cmd *cobra.Command
84+
args []string
85+
client *testdata.MockClient
86+
}
87+
var _, w, _ = os.Pipe()
88+
tests := []struct {
89+
name string
90+
fields fields
91+
args args
92+
wantErr bool
93+
wantOut string
94+
commandContext CommandContext
95+
}{
96+
{
97+
name: "acorn image -j noproject",
98+
fields: fields{
99+
All: false,
100+
Quiet: false,
101+
Output: "",
102+
},
103+
commandContext: CommandContext{
104+
ClientFactory: &testdata.MockClientFactory{},
105+
StdOut: w,
106+
StdErr: w,
107+
StdIn: strings.NewReader(""),
108+
},
109+
args: args{
110+
args: []string{"image", "-j", "noproject"},
111+
client: &testdata.MockClient{},
112+
},
113+
wantErr: true,
114+
wantOut: "projects.api.acorn.io \"noproject\" not found",
115+
},
116+
{
117+
name: "acorn image -j 12.badname",
118+
fields: fields{
119+
All: false,
120+
Quiet: false,
121+
Output: "",
122+
},
123+
commandContext: CommandContext{
124+
ClientFactory: &testdata.MockClientFactory{},
125+
StdOut: w,
126+
StdErr: w,
127+
StdIn: strings.NewReader(""),
128+
},
129+
args: args{
130+
args: []string{"image", "-j", "12.badname"},
131+
client: &testdata.MockClient{},
132+
},
133+
wantErr: true,
134+
wantOut: "invalid project name [12.badname]: can not contain \".\"",
135+
},
136+
{
137+
name: "acorn image -j badname/projectname/here",
138+
fields: fields{
139+
All: false,
140+
Quiet: false,
141+
Output: "",
142+
},
143+
commandContext: CommandContext{
144+
ClientFactory: &testdata.MockClientFactory{},
145+
StdOut: w,
146+
StdErr: w,
147+
StdIn: strings.NewReader(""),
148+
},
149+
args: args{
150+
args: []string{"image", "-j", "badname/projectname/here"},
151+
client: &testdata.MockClient{},
152+
},
153+
wantErr: true,
154+
wantOut: "failed to find authentication token for server badname, please run 'acorn login badname' first",
155+
},
156+
}
157+
for _, tt := range tests {
158+
t.Run(tt.name, func(t *testing.T) {
159+
r, w, _ := os.Pipe()
160+
os.Stdout = w
161+
tt.args.cmd = New()
162+
tt.args.cmd.SetArgs(tt.args.args)
163+
err := tt.args.cmd.Execute()
164+
if err != nil && !tt.wantErr {
165+
assert.Failf(t, "got err when err not expected", "got err: %s", err.Error())
166+
} else if err != nil && tt.wantErr {
167+
assert.Equal(t, tt.wantOut, err.Error())
168+
} else {
169+
w.Close()
170+
out, _ := io.ReadAll(r)
171+
testOut, _ := os.ReadFile(tt.wantOut)
172+
assert.Equal(t, string(testOut), string(out))
173+
}
174+
})
175+
}
176+
}

pkg/cli/testdata/MockClient.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ type MockClientFactory struct {
2727
SecretItem *apiv1.Secret
2828
ImageList []apiv1.Image
2929
ImageItem *apiv1.Image
30+
ProjectList []apiv1.Project
31+
ProjectItem *apiv1.Project
3032
}
3133

3234
func (dc *MockClientFactory) Options() project.Options {
@@ -41,12 +43,14 @@ func (dc *MockClientFactory) CreateDefault() (client.Client, error) {
4143
Volumes: dc.VolumeList,
4244
Secrets: dc.SecretList,
4345
Images: dc.ImageList,
46+
Projects: dc.ProjectList,
4447
AppItem: dc.AppItem,
4548
ContainerItem: dc.ContainerItem,
4649
CredentialItem: dc.CredentialItem,
4750
VolumeItem: dc.VolumeItem,
4851
SecretItem: dc.SecretItem,
4952
ImageItem: dc.ImageItem,
53+
ProjectItem: dc.ProjectItem,
5054
}, nil
5155
}
5256

@@ -63,6 +67,8 @@ type MockClient struct {
6367
SecretItem *apiv1.Secret
6468
Images []apiv1.Image
6569
ImageItem *apiv1.Image
70+
Projects []apiv1.Project
71+
ProjectItem *apiv1.Project
6672
}
6773

6874
func (m *MockClient) AppPullImage(ctx context.Context, name string) error {
@@ -612,18 +618,27 @@ func (m *MockClient) AcornImageBuild(ctx context.Context, file string, opts *cli
612618
}
613619

614620
func (m *MockClient) ProjectList(ctx context.Context) ([]apiv1.Project, error) {
615-
//TODO implement me
616-
panic("implement me")
621+
if m.Projects != nil {
622+
return m.Projects, nil
623+
}
624+
return []apiv1.Project{{
625+
TypeMeta: metav1.TypeMeta{},
626+
ObjectMeta: metav1.ObjectMeta{Name: "project"},
627+
}}, nil
617628
}
618629

619630
func (m *MockClient) GetProject() string {
620-
//TODO implement me
621-
panic("implement me")
631+
if m.ProjectItem != nil {
632+
return m.ProjectItem.Name
633+
}
634+
return ""
622635
}
623636

624637
func (m *MockClient) ProjectGet(ctx context.Context, name string) (*apiv1.Project, error) {
625-
//TODO implement me
626-
panic("implement me")
638+
if m.ProjectItem != nil {
639+
return m.ProjectItem, nil
640+
}
641+
return nil, nil
627642
}
628643

629644
func (m *MockClient) ProjectCreate(ctx context.Context, name string) (*apiv1.Project, error) {

0 commit comments

Comments
 (0)