Skip to content

Commit 30f8bda

Browse files
committed
return tests + mock api call
1 parent 92d9eb8 commit 30f8bda

File tree

2 files changed

+89
-71
lines changed

2 files changed

+89
-71
lines changed
Lines changed: 63 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,65 @@
11
package commands
22

3-
// import (
4-
// "errors"
5-
// "kool-dev/kool/core/environment"
6-
// "kool-dev/kool/core/shell"
7-
// "kool-dev/kool/services/cloud/api"
8-
// "strings"
9-
// "testing"
10-
// )
11-
12-
// func TestNewDeployDestroyCommand(t *testing.T) {
13-
// destroy := NewKoolDeployDestroy()
14-
// cmd := NewDeployDestroyCommand(destroy)
15-
// if cmd.Use != "destroy" {
16-
// t.Errorf("bad command use: %s", cmd.Use)
17-
// }
18-
19-
// if _, ok := destroy.env.(*environment.DefaultEnvStorage); !ok {
20-
// t.Error("unexpected default env on destroy")
21-
// }
22-
// }
23-
24-
// type fakeDestroyCall struct {
25-
// api.DefaultEndpoint
26-
27-
// err error
28-
// resp *api.DestroyResponse
29-
// }
30-
31-
// func (d *fakeDestroyCall) Call() (*api.DestroyResponse, error) {
32-
// return d.resp, d.err
33-
// }
34-
35-
// func TestDeployDestroyExec(t *testing.T) {
36-
// destroy := &KoolDeployDestroy{
37-
// *(newDefaultKoolService().Fake()),
38-
// environment.NewFakeEnvStorage(),
39-
// &fakeDestroyCall{
40-
// DefaultEndpoint: *api.NewDefaultEndpoint(""),
41-
// },
42-
// }
43-
44-
// destroy.env.Set("KOOL_API_TOKEN", "fake token")
45-
// destroy.env.Set("KOOL_API_URL", "fake-url")
46-
47-
// args := []string{}
48-
49-
// if err := destroy.Execute(args); !strings.Contains(err.Error(), "missing deploy domain") {
50-
// t.Errorf("unexpected error - expected missing deploy domain, got: %v", err)
51-
// }
52-
53-
// destroy.env.Set("KOOL_DEPLOY_DOMAIN", "domain.com")
54-
55-
// destroy.apiDestroy.(*fakeDestroyCall).err = errors.New("failed call")
56-
57-
// if err := destroy.Execute(args); !strings.Contains(err.Error(), "failed call") {
58-
// t.Errorf("unexpected error - expected failed call, got: %v", err)
59-
// }
60-
61-
// destroy.apiDestroy.(*fakeDestroyCall).err = nil
62-
// resp := new(api.DestroyResponse)
63-
// resp.Environment.ID = 100
64-
// destroy.apiDestroy.(*fakeDestroyCall).resp = resp
65-
66-
// if err := destroy.Execute(args); err != nil {
67-
// t.Errorf("unexpected error, got: %v", err)
68-
// }
69-
70-
// if !strings.Contains(destroy.shell.(*shell.FakeShell).SuccessOutput[0].(string), "ID: 100") {
71-
// t.Errorf("did not get success message")
72-
// }
73-
// }
3+
import (
4+
"errors"
5+
"kool-dev/kool/core/environment"
6+
"kool-dev/kool/core/shell"
7+
"kool-dev/kool/services/cloud/api"
8+
"strings"
9+
"testing"
10+
)
11+
12+
func TestNewDeployDestroyCommand(t *testing.T) {
13+
destroy := NewKoolDeployDestroy()
14+
cmd := NewDeployDestroyCommand(destroy)
15+
if cmd.Use != "destroy" {
16+
t.Errorf("bad command use: %s", cmd.Use)
17+
}
18+
19+
if _, ok := destroy.env.(*environment.DefaultEnvStorage); !ok {
20+
t.Error("unexpected default env on destroy")
21+
}
22+
}
23+
24+
func TestDeployDestroyExec(t *testing.T) {
25+
destroy := &KoolDeployDestroy{
26+
*(newDefaultKoolService().Fake()),
27+
environment.NewFakeEnvStorage(),
28+
*api.NewDeployDestroy(),
29+
}
30+
31+
destroy.env.Set("KOOL_API_TOKEN", "fake token")
32+
destroy.env.Set("KOOL_API_URL", "fake-url")
33+
34+
args := []string{}
35+
36+
if err := destroy.Execute(args); !strings.Contains(err.Error(), "missing deploy domain") {
37+
t.Errorf("unexpected error - expected missing deploy domain, got: %v", err)
38+
}
39+
40+
destroy.env.Set("KOOL_DEPLOY_DOMAIN", "domain.com")
41+
42+
destroy.apiDestroy.Endpoint.(*api.DefaultEndpoint).Fake()
43+
destroy.apiDestroy.Endpoint.(*api.DefaultEndpoint).MockErr(errors.New("failed call"))
44+
45+
if err := destroy.Execute(args); !strings.Contains(err.Error(), "failed call") {
46+
t.Errorf("unexpected error - expected failed call, got: %v", err)
47+
}
48+
49+
destroy.apiDestroy.Endpoint.(*api.DefaultEndpoint).MockErr(nil)
50+
destroy.apiDestroy.Endpoint.(*api.DefaultEndpoint).MockResp(&api.DeployDestroyResponse{
51+
Environment: struct {
52+
ID int `json:"id"`
53+
}{
54+
ID: 100,
55+
},
56+
})
57+
58+
if err := destroy.Execute(args); err != nil {
59+
t.Errorf("unexpected error, got: %v", err)
60+
}
61+
62+
if !strings.Contains(destroy.shell.(*shell.FakeShell).SuccessOutput[0].(string), "ID: 100") {
63+
t.Errorf("did not get success message")
64+
}
65+
}

services/cloud/api/endpoint.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ type DefaultEndpoint struct {
4848

4949
postBodyBuff bytes.Buffer
5050
postBodyFmtr *multipart.Writer
51+
52+
fake bool
53+
mockErr error
54+
mockResp any
5155
}
5256

5357
// NewDefaultEndpoint creates an Endpoint with given method
@@ -150,6 +154,16 @@ func (e *DefaultEndpoint) DoCall() (err error) {
150154
verbose = e.env.IsTrue("KOOL_VERBOSE")
151155
)
152156

157+
if e.fake {
158+
// fake call
159+
err = e.mockErr
160+
if e.mockResp != nil {
161+
raw, _ := json.Marshal(e.mockResp)
162+
json.Unmarshal(raw, e.response)
163+
}
164+
return
165+
}
166+
153167
if e.postBodyFmtr != nil {
154168
e.SetContentType(e.postBodyFmtr.FormDataContentType())
155169
e.postBodyFmtr.Close()
@@ -254,3 +268,15 @@ func (e *DefaultEndpoint) initPostBody() {
254268
e.postBodyFmtr = multipart.NewWriter(&e.postBodyBuff)
255269
}
256270
}
271+
272+
func (e *DefaultEndpoint) Fake() {
273+
e.fake = true
274+
}
275+
276+
func (e *DefaultEndpoint) MockErr(err error) {
277+
e.mockErr = err
278+
}
279+
280+
func (e *DefaultEndpoint) MockResp(i any) {
281+
e.mockResp = i
282+
}

0 commit comments

Comments
 (0)