Skip to content

Commit

Permalink
test: fixed existing tests
Browse files Browse the repository at this point in the history
Signed-off-by: Tobias Cudnik <[email protected]>
  • Loading branch information
TobiaszCudnik committed Sep 15, 2023
1 parent d88d3d1 commit bc7826d
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 54 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<a href="https://www.instill.tech/?utm_source=github&utm_medium=banner&utm_campaign=cli_readme">Website</a> |
<a href="https://discord.gg/sevxWsqpGh">Community</a> |
<a href="https://blog.instill.tech/?utm_source=github&utm_medium=banner&utm_campaign=cli_readme">Blog</a><br/><br/>
<a href="https://docs.instill.tech/?utm_source=github&utm_medium=banner&utm_campaign=cli_readme">User Manual</a> |
<a href="https://www.instill.tech/docs/?utm_source=github&utm_medium=banner&utm_campaign=cli_readme">User Manual</a> |
<a href="https://discord.gg/sevxWsqpGh">API Reference</a><br/><br/>
<a href="https://www.instill.tech/get-access/?utm_source=github&utm_medium=banner&utm_campaign=cli_readme"><strong>Get Early Access</strong></a>
</h4>
Expand Down Expand Up @@ -49,6 +49,12 @@ To upgrade:
brew upgrade instill-ai/tap/instill
```

## Usage

```
```

## Issues and discussions
Please directly report any issues in [Issues](https://github.com/instill-ai/cli/issues) or [Pull requests](https://github.com/instill-ai/cli/pulls), or raise a topic in [Discussions](https://github.com/instill-ai/cli/discussions).

Expand Down
29 changes: 29 additions & 0 deletions internal/config/stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"errors"
"github.com/instill-ai/cli/internal/instance"
)

type ConfigStub map[string]string
Expand Down Expand Up @@ -53,3 +54,31 @@ func (c ConfigStub) DefaultHost() (string, error) {
func (c ConfigStub) DefaultHostWithSource() (string, string, error) {
return "", "", nil
}

func (c ConfigStub) DefaultHostname() string {
return instance.FallbackHostname()
}

func (c ConfigStub) HostsTyped() ([]HostConfigTyped, error) {
ins := []HostConfigTyped{
{
APIHostname: "api.instill.tech",
IsDefault: true,
APIVersion: "v1alpha",
Oauth2Hostname: "auth.instill.tech",
Oauth2Audience: "https://api.instill.tech",
Oauth2Issuer: "https://auth.instill.tech/",
Oauth2Secret: "foobar",
Oauth2ClientID: "barfoo",
},
}
return ins, nil
}

func (c ConfigStub) SaveTyped(*HostConfigTyped) error {
return nil
}

func ConfigStubFactory() (Config, error) {
return ConfigStub{}, nil
}
56 changes: 33 additions & 23 deletions pkg/cmd/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,39 @@ func apiRun(opts *ApiOptions) error {
return err
}

// get the host config
cfg, err := opts.Config()
if err != nil {
return err
}
var host *config.HostConfigTyped
if err != nil {
return err
}
hosts, err := cfg.HostsTyped()
if err != nil {
return err
}
hostname := opts.Hostname
if hostname == "" {
hostname = cfg.DefaultHostname()
}
for i := range hosts {
if hosts[i].APIHostname == hostname {
host = &hosts[i]
break
}
}
if host == nil {
return fmt.Errorf(heredoc.Docf(
`ERROR: instance '%s' does not exist
You can add it with:
$ inst instances add %s`,
hostname, hostname))
}

// set up the http client
method := opts.RequestMethod
requestPath := opts.RequestPath
requestHeaders := opts.RequestHeaders
Expand Down Expand Up @@ -201,29 +234,6 @@ func apiRun(opts *ApiOptions) error {
defer opts.IO.StopPager()
}

// get the host config
cfg, err := opts.Config()
if err != nil {
return err
}
var host *config.HostConfigTyped
if err != nil {
return err
}
hosts, err := cfg.HostsTyped()
if err != nil {
return err
}
for i := range hosts {
if hosts[i].APIHostname == opts.Hostname {
host = &hosts[i]
break
}
}
if host == nil {
return fmt.Errorf("instance '%s' doesn't exist", opts.Hostname)
}

// set up the request
// TODO support other services than VDP
requestPath = "vdp/" + host.APIVersion + "/" + strings.TrimPrefix(requestPath, "/")
Expand Down
30 changes: 14 additions & 16 deletions pkg/cmd/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import (
)

func Test_NewCmdApi(t *testing.T) {
f := &cmdutil.Factory{}
f := &cmdutil.Factory{
Config: config.ConfigStubFactory,
}

tests := []struct {
name string
Expand All @@ -35,7 +37,7 @@ func Test_NewCmdApi(t *testing.T) {
name: "override method",
cli: "pipelines -XDELETE",
wants: ApiOptions{
Hostname: "",
Hostname: "api.instill.tech",
RequestMethod: "DELETE",
RequestMethodPassed: true,
RequestPath: "pipelines",
Expand All @@ -55,7 +57,7 @@ func Test_NewCmdApi(t *testing.T) {
name: "with headers",
cli: "user -H 'accept: text/plain' -i",
wants: ApiOptions{
Hostname: "",
Hostname: "api.instill.tech",
RequestMethod: "GET",
RequestMethodPassed: false,
RequestPath: "user",
Expand All @@ -75,7 +77,7 @@ func Test_NewCmdApi(t *testing.T) {
name: "with silenced output",
cli: "models --silent",
wants: ApiOptions{
Hostname: "",
Hostname: "api.instill.tech",
RequestMethod: "GET",
RequestMethodPassed: false,
RequestPath: "models",
Expand All @@ -95,7 +97,7 @@ func Test_NewCmdApi(t *testing.T) {
name: "with request body from file",
cli: "user --input myfile",
wants: ApiOptions{
Hostname: "",
Hostname: "api.instill.tech",
RequestMethod: "GET",
RequestMethodPassed: false,
RequestPath: "user",
Expand All @@ -120,7 +122,7 @@ func Test_NewCmdApi(t *testing.T) {
name: "with cache",
cli: "user --cache 5m",
wants: ApiOptions{
Hostname: "",
Hostname: "api.instill.tech",
RequestMethod: "GET",
RequestMethodPassed: false,
RequestPath: "user",
Expand All @@ -140,7 +142,7 @@ func Test_NewCmdApi(t *testing.T) {
name: "with template",
cli: "user -t 'hello {{.name}}'",
wants: ApiOptions{
Hostname: "",
Hostname: "api.instill.tech",
RequestMethod: "GET",
RequestMethodPassed: false,
RequestPath: "user",
Expand All @@ -160,7 +162,7 @@ func Test_NewCmdApi(t *testing.T) {
name: "with jq filter",
cli: "user -q .name",
wants: ApiOptions{
Hostname: "",
Hostname: "api.instill.tech",
RequestMethod: "GET",
RequestMethodPassed: false,
RequestPath: "user",
Expand Down Expand Up @@ -372,7 +374,7 @@ func Test_apiRun(t *testing.T) {
stream, _, stdout, stderr := iostreams.Test()

tt.options.IO = stream
tt.options.Config = func() (config.Config, error) { return config.NewBlankConfig(), nil }
tt.options.Config = config.ConfigStubFactory
tt.options.HTTPClient = func() (*http.Client, error) {
var tr roundTripper = func(req *http.Request) (*http.Response, error) {
resp := tt.httpResponse
Expand Down Expand Up @@ -458,9 +460,7 @@ func Test_apiRun_inputFile(t *testing.T) {
}
return &http.Client{Transport: tr}, nil
},
Config: func() (config.Config, error) {
return config.NewBlankConfig(), nil
},
Config: config.ConfigStubFactory,
}

err := apiRun(&options)
Expand All @@ -469,7 +469,7 @@ func Test_apiRun_inputFile(t *testing.T) {
}

assert.Equal(t, "POST", resp.Request.Method)
assert.Equal(t, "/hello?a=b&c=d", resp.Request.URL.RequestURI())
assert.Equal(t, "/vdp/v1alpha/hello?a=b&c=d", resp.Request.URL.RequestURI())
assert.Equal(t, tt.contentLength, resp.Request.ContentLength)
assert.Equal(t, "", resp.Request.Header.Get("Content-Type"))
assert.Equal(t, tt.inputContents, bodyBytes)
Expand All @@ -493,9 +493,7 @@ func Test_apiRun_cache(t *testing.T) {
}
return &http.Client{Transport: tr}, nil
},
Config: func() (config.Config, error) {
return config.NewBlankConfig(), nil
},
Config: config.ConfigStubFactory,

RequestPath: "pipelines",
CacheTTL: time.Minute,
Expand Down
10 changes: 5 additions & 5 deletions pkg/cmd/api/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func Test_httpRequest(t *testing.T) {
name: "simple GET",
args: args{
client: &httpClient,
host: "instill.tech",
host: "api.instill.tech",
method: "GET",
p: "/models",
params: nil,
Expand All @@ -61,7 +61,7 @@ func Test_httpRequest(t *testing.T) {
name: "GET with leading slash",
args: args{
client: &httpClient,
host: "instill.tech",
host: "api.instill.tech",
method: "GET",
p: "/models",
params: nil,
Expand All @@ -79,7 +79,7 @@ func Test_httpRequest(t *testing.T) {
name: "GET with params",
args: args{
client: &httpClient,
host: "instill.tech",
host: "api.instill.tech",
method: "GET",
p: "models",
params: map[string]interface{}{
Expand All @@ -99,7 +99,7 @@ func Test_httpRequest(t *testing.T) {
name: "POST with params",
args: args{
client: &httpClient,
host: "github.com",
host: "api.github.com",
method: "POST",
p: "repos",
params: map[string]interface{}{
Expand All @@ -119,7 +119,7 @@ func Test_httpRequest(t *testing.T) {
name: "POST with body and type",
args: args{
client: &httpClient,
host: "github.com",
host: "api.github.com",
method: "POST",
p: "repos",
params: bytes.NewBufferString("CUSTOM"),
Expand Down
5 changes: 4 additions & 1 deletion pkg/cmd/auth/login/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package login

import (
"bytes"
"github.com/instill-ai/cli/internal/config"
"github.com/instill-ai/cli/internal/instance"
"testing"

"github.com/google/shlex"
Expand Down Expand Up @@ -34,7 +36,7 @@ func Test_NewCmdLogin(t *testing.T) {
stdinTTY: true,
cli: "",
wants: LoginOptions{
Hostname: "",
Hostname: instance.FallbackHostname(),
Interactive: true,
},
},
Expand All @@ -46,6 +48,7 @@ func Test_NewCmdLogin(t *testing.T) {
f := &cmdutil.Factory{
IOStreams: io,
Executable: func() string { return "/path/to/instill" },
Config: config.ConfigStubFactory,
}

io.SetStdoutTTY(true)
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/completion/completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestNewCmdCompletion(t *testing.T) {
{
name: "zsh completion",
args: "completion -s zsh",
wantOut: "#compdef _instill instill",
wantOut: "compdef _instill instill",
},
{
name: "fish completion",
Expand Down
18 changes: 11 additions & 7 deletions pkg/cmd/factory/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package factory

import (
"errors"
"fmt"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -115,17 +116,20 @@ func executable(fallbackName string) string {

func configFunc() func() (config.Config, error) {
var cachedConfig config.Config
var configError error
var err error
return func() (config.Config, error) {
if cachedConfig != nil || configError != nil {
return cachedConfig, configError
if cachedConfig != nil || err != nil {
if err != nil {
fmt.Printf("ERROR: cant read the config\n%s", err)
}
return cachedConfig, err
}
cachedConfig, configError = config.ParseDefaultConfig()
if errors.Is(configError, os.ErrNotExist) {
cachedConfig, err = config.ParseDefaultConfig()
if errors.Is(err, os.ErrNotExist) {
cachedConfig = config.NewBlankConfig()
configError = nil
err = nil
}
return cachedConfig, configError
return cachedConfig, err
}
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/cmd/factory/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package factory

import (
"fmt"
"github.com/instill-ai/cli/internal/config"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -140,6 +141,10 @@ func TestNewHTTPClient(t *testing.T) {

type tinyConfig map[string]string

func (c tinyConfig) SaveTyped(typed *config.HostConfigTyped) error {
return nil
}

func (c tinyConfig) Get(host, key string) (string, error) {
return c[fmt.Sprintf("%s:%s", host, key)], nil
}
Expand Down

0 comments on commit bc7826d

Please sign in to comment.