From 96cfacb58b2999178f169ceebf3209eb8e8d1412 Mon Sep 17 00:00:00 2001 From: Aravind K P Date: Wed, 15 Dec 2021 23:24:47 +0530 Subject: [PATCH] cli: wrap errors PR-URL: https://github.com/hasura/graphql-engine-mono/pull/3146 GitOrigin-RevId: 5dd78bf9145d3bfc9ff4281239d849e796a36246 --- cli/cli.go | 39 +++++---- cli/commands/actions_create.go | 2 +- cli/commands/metadata_handlers.go | 5 +- cli/commands/metadata_inconsistency_list.go | 3 +- cli/commands/metadata_reload.go | 7 +- cli/commands/migrate_apply.go | 2 +- cli/internal/diff/diff.go | 4 +- cli/internal/metadataobject/metadataobject.go | 2 +- cli/migrate/cmd/commands.go | 2 +- cli/migrate/database/hasuradb/types.go | 4 +- cli/migrate/util.go | 3 +- cli/pkg/metadata/project_metadata_test.go | 63 ++++++++++++--- cli/pkg/migrate/project_migrate_test.go | 12 ++- cli/util/server.go | 79 ++++++++++--------- cli/util/server_test.go | 58 ++++++++++++++ 15 files changed, 195 insertions(+), 90 deletions(-) create mode 100644 cli/util/server_test.go diff --git a/cli/cli.go b/cli/cli.go index 89c356fe01c..592e12dd024 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -53,7 +53,6 @@ import ( "github.com/hasura/graphql-engine/cli/v2/telemetry" "github.com/hasura/graphql-engine/cli/v2/util" "github.com/hasura/graphql-engine/cli/v2/version" - "github.com/pkg/errors" "github.com/sirupsen/logrus" "github.com/spf13/viper" "github.com/subosito/gotenv" @@ -204,7 +203,7 @@ func (c *ServerConfig) GetHasuraInternalServerConfig() error { client := http.Client{Timeout: 30 * time.Second} req, err := http.NewRequest("GET", url, nil) if err != nil { - return errors.Wrap(err, "error fetching config from server") + return fmt.Errorf("error fetching config from server: %w", err) } if c.AdminSecret != "" { @@ -307,10 +306,10 @@ func (s *ServerConfig) SetTLSConfig() error { certPath, _ := filepath.Abs(s.CAPath) cert, err := ioutil.ReadFile(certPath) if err != nil { - return errors.Errorf("error reading CA %s", s.CAPath) + return fmt.Errorf("error reading CA %s: %w", s.CAPath, err) } if ok := rootCAs.AppendCertsFromPEM(cert); !ok { - return errors.Errorf("Unable to append given CA cert.") + return fmt.Errorf("unable to append given CA cert") } s.TLSConfig = &tls.Config{ RootCAs: rootCAs, @@ -516,7 +515,7 @@ func (ec *ExecutionContext) Prepare() error { // setup global config err := ec.setupGlobalConfig() if err != nil { - return errors.Wrap(err, "setting up global config failed") + return fmt.Errorf("setting up global config failed: %w", err) } if !ec.proPluginVersionValidated { @@ -554,7 +553,7 @@ func (ec *ExecutionContext) SetupPlugins() error { base := filepath.Join(ec.GlobalConfigDir, "plugins") base, err := filepath.Abs(base) if err != nil { - return errors.Wrap(err, "cannot get absolute path") + return fmt.Errorf("cannot get absolute path: %w", err) } ec.PluginsConfig = plugins.New(base) ec.PluginsConfig.Logger = ec.Logger @@ -594,7 +593,7 @@ func (ec *ExecutionContext) SetupCodegenAssetsRepo() error { base := filepath.Join(ec.GlobalConfigDir, util.ActionsCodegenDirName) base, err := filepath.Abs(base) if err != nil { - return errors.Wrap(err, "cannot get absolute path") + return fmt.Errorf("cannot get absolute path: %w", err) } ec.CodegenAssetsRepo = util.NewGitUtil(util.ActionsCodegenRepoURI, base, "") ec.CodegenAssetsRepo.Logger = ec.Logger @@ -621,7 +620,7 @@ func (ec *ExecutionContext) Validate() error { // load .env file err = ec.loadEnvfile() if err != nil { - return errors.Wrap(err, "loading .env file failed") + return fmt.Errorf("loading .env file failed: %w", err) } // set names of config file @@ -630,7 +629,7 @@ func (ec *ExecutionContext) Validate() error { // read config and parse the values into Config err = ec.readConfig() if err != nil { - return errors.Wrap(err, "cannot read config") + return fmt.Errorf("cannot read config: %w", err) } // set name of migration directory @@ -638,7 +637,7 @@ func (ec *ExecutionContext) Validate() error { if _, err := os.Stat(ec.MigrationDir); os.IsNotExist(err) { err = os.MkdirAll(ec.MigrationDir, os.ModePerm) if err != nil { - return errors.Wrap(err, "cannot create migrations directory") + return fmt.Errorf("cannot create migrations directory: %w", err) } } @@ -646,7 +645,7 @@ func (ec *ExecutionContext) Validate() error { if _, err := os.Stat(ec.SeedsDirectory); os.IsNotExist(err) { err = os.MkdirAll(ec.SeedsDirectory, os.ModePerm) if err != nil { - return errors.Wrap(err, "cannot create seeds directory") + return fmt.Errorf("cannot create seeds directory: %w", err) } } @@ -672,7 +671,7 @@ func (ec *ExecutionContext) Validate() error { if _, err := os.Stat(ec.MetadataDir); os.IsNotExist(err) && !(len(ec.MetadataFile) > 0) { err = os.MkdirAll(ec.MetadataDir, os.ModePerm) if err != nil { - return errors.Wrap(err, "cannot create metadata directory") + return fmt.Errorf("cannot create metadata directory: %w", err) } } } @@ -700,13 +699,13 @@ func (ec *ExecutionContext) Validate() error { // get version from the server and match with the cli version err = ec.checkServerVersion() if err != nil { - return errors.Wrap(err, "version check") + return fmt.Errorf("version check: %w", err) } // get the server feature flags err = ec.Version.GetServerFeatureFlags() if err != nil { - return errors.Wrap(err, "error in getting server feature flags") + return fmt.Errorf("error in getting server feature flags %w", err) } var headers map[string]string if ec.Config.AdminSecret != "" { @@ -762,9 +761,9 @@ func (ec *ExecutionContext) Validate() error { } var state *util.ServerState if ec.HasMetadataV3 { - state = util.GetServerState(ec.Config.GetV1MetadataEndpoint(), ec.Config.ServerConfig.AdminSecret, ec.Config.ServerConfig.TLSConfig, ec.HasMetadataV3, ec.Logger) + state = util.GetServerState(httpClient, ec.Config.GetV1MetadataEndpoint(), ec.HasMetadataV3, ec.Logger) } else { - state = util.GetServerState(ec.Config.GetV1QueryEndpoint(), ec.Config.ServerConfig.AdminSecret, ec.Config.ServerConfig.TLSConfig, ec.HasMetadataV3, ec.Logger) + state = util.GetServerState(httpClient, ec.Config.GetV1QueryEndpoint(), ec.HasMetadataV3, ec.Logger) } ec.ServerUUID = state.UUID ec.Telemetry.ServerUUID = ec.ServerUUID @@ -776,7 +775,7 @@ func (ec *ExecutionContext) Validate() error { func (ec *ExecutionContext) checkServerVersion() error { v, err := version.FetchServerVersion(ec.Config.ServerConfig.GetVersionEndpoint(), ec.Config.ServerConfig.HTTPClient) if err != nil { - return errors.Wrap(err, "failed to get version from server") + return fmt.Errorf("failed to get version from server: %w", err) } ec.Version.SetServerVersion(v) ec.Telemetry.ServerVersion = ec.Version.GetServerVersion() @@ -837,7 +836,7 @@ func (ec *ExecutionContext) readConfig() error { v.AddConfigPath(ec.ExecutionDirectory) err := v.ReadInConfig() if err != nil { - return errors.Wrap(err, "cannot read config from file/env") + return fmt.Errorf("cannot read config from file/env: %w", err) } adminSecret := v.GetString("admin_secret") if adminSecret == "" { @@ -881,7 +880,7 @@ func (ec *ExecutionContext) readConfig() error { } err = ec.Config.ServerConfig.ParseEndpoint() if err != nil { - return errors.Wrap(err, "unable to parse server endpoint") + return fmt.Errorf("unable to parse server endpoint: %w", err) } // this populates the ec.Config.ServerConfig.HasuraServerInternalConfig @@ -893,7 +892,7 @@ func (ec *ExecutionContext) readConfig() error { err = ec.Config.ServerConfig.SetTLSConfig() if err != nil { - return errors.Wrap(err, "setting up TLS config failed") + return fmt.Errorf("setting up TLS config failed: %w", err) } return ec.Config.ServerConfig.SetHTTPClient() } diff --git a/cli/commands/actions_create.go b/cli/commands/actions_create.go index f52afb76698..a0fd011aaeb 100644 --- a/cli/commands/actions_create.go +++ b/cli/commands/actions_create.go @@ -71,7 +71,7 @@ func (o *actionsCreateOptions) run() error { o.EC.Spin("Deriving a Hasura operation...") introSchema, err = o.EC.APIClient.V1Graphql.GetIntrospectionSchema() if err != nil { - return errors.Wrap(err, "error in fetching introspection schema") + return fmt.Errorf("error in fetching introspection schema: %w", err) } o.EC.Spinner.Stop() } diff --git a/cli/commands/metadata_handlers.go b/cli/commands/metadata_handlers.go index 5fa9cfbbfca..c2edf046f86 100644 --- a/cli/commands/metadata_handlers.go +++ b/cli/commands/metadata_handlers.go @@ -12,7 +12,6 @@ import ( goyaml "github.com/goccy/go-yaml" "github.com/hasura/graphql-engine/cli/v2" "github.com/hasura/graphql-engine/cli/v2/internal/projectmetadata" - "github.com/pkg/errors" ) type MetadataModeHandler interface { @@ -39,11 +38,11 @@ func (m *metadataModeDirectoryHandler) Export(o *MetadataExportOptions) error { files, err := metadataHandler.ExportMetadata() o.EC.Spinner.Stop() if err != nil { - return errors.Wrap(err, "failed to export metadata") + return fmt.Errorf("failed to export metadata: %w", err) } err = metadataHandler.WriteMetadata(files) if err != nil { - return errors.Wrap(err, "cannot write metadata to project") + return fmt.Errorf("cannot write metadata to project: %w", err) } return nil } diff --git a/cli/commands/metadata_inconsistency_list.go b/cli/commands/metadata_inconsistency_list.go index 158b7993e30..cf395019b98 100644 --- a/cli/commands/metadata_inconsistency_list.go +++ b/cli/commands/metadata_inconsistency_list.go @@ -6,7 +6,6 @@ import ( "github.com/hasura/graphql-engine/cli/v2/internal/projectmetadata" - "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/hasura/graphql-engine/cli/v2" @@ -27,7 +26,7 @@ func newMetadataInconsistencyListCmd(ec *cli.ExecutionContext) *cobra.Command { err := opts.run() opts.EC.Spinner.Stop() if err != nil { - return errors.Wrap(err, "failed to list inconsistent metadata") + return fmt.Errorf("failed to list inconsistent metadata: %w", err) } if opts.isConsistent { opts.EC.Logger.Println("metadata is consistent") diff --git a/cli/commands/metadata_reload.go b/cli/commands/metadata_reload.go index 04203517dac..7a9a64c5dbf 100644 --- a/cli/commands/metadata_reload.go +++ b/cli/commands/metadata_reload.go @@ -6,7 +6,6 @@ import ( "github.com/hasura/graphql-engine/cli/v2" "github.com/hasura/graphql-engine/cli/v2/internal/projectmetadata" - "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -44,7 +43,7 @@ func (o *MetadataReloadOptions) runWithInfo() error { err := o.run() o.EC.Spinner.Stop() if err != nil { - return errors.Wrap(err, "failed to reload metadata") + return fmt.Errorf("failed to reload metadata: %w", err) } o.EC.Logger.Info("Metadata reloaded") icListOpts := &metadataInconsistencyListOptions{ @@ -52,7 +51,7 @@ func (o *MetadataReloadOptions) runWithInfo() error { } err = icListOpts.read(projectmetadata.NewHandlerFromEC(icListOpts.EC)) if err != nil { - return fmt.Errorf("failed to read metadata status: %v", err) + return fmt.Errorf("failed to read metadata status: %w", err) } if icListOpts.isConsistent { icListOpts.EC.Logger.Infoln("Metadata is consistent") @@ -67,7 +66,7 @@ func (o *MetadataReloadOptions) run() error { metadataHandler := projectmetadata.NewHandlerFromEC(o.EC) _, err = metadataHandler.ReloadMetadata() if err != nil { - return errors.Wrap(err, "Cannot reload metadata") + return fmt.Errorf("cannot reload metadata: %w", err) } return nil } diff --git a/cli/commands/migrate_apply.go b/cli/commands/migrate_apply.go index 8257a442f56..b931aea43bf 100644 --- a/cli/commands/migrate_apply.go +++ b/cli/commands/migrate_apply.go @@ -198,7 +198,7 @@ func (o *MigrateApplyOptions) Apply() (chan MigrateApplyResult, error) { // if so skip this return "", fmt.Errorf("skipping applying migrations on database %s, encountered: \n%s", o.Source.Name, e.Error()) } else if err != nil { - return "", fmt.Errorf("skipping applying migrations on database %s, encountered: \n%v", o.Source.Name, err) + return "", fmt.Errorf("skipping applying migrations on database %s, encountered: \n%w", o.Source.Name, err) } return "", nil } diff --git a/cli/internal/diff/diff.go b/cli/internal/diff/diff.go index 32f3f6d69eb..1f8e92ca8b8 100644 --- a/cli/internal/diff/diff.go +++ b/cli/internal/diff/diff.go @@ -19,7 +19,7 @@ import ( func YamlDiff(fromFile, toFile ytbx.InputFile, writer io.Writer, fileName string) (int, error) { report, err := dyff.CompareInputFiles(fromFile, toFile, dyff.IgnoreOrderChanges(true)) if err != nil { - return -1, fmt.Errorf("error while getting diff: %v", err) + return -1, fmt.Errorf("error while getting diff: %w", err) } reportWriter := &dyff.HumanReport{ Report: report, @@ -32,7 +32,7 @@ func YamlDiff(fromFile, toFile ytbx.InputFile, writer io.Writer, fileName string fmt.Fprintf(writer, "%s\n", fileName) err = reportWriter.WriteReport(writer) if err != nil { - return -1, fmt.Errorf("error while printing diff: %v", err) + return -1, fmt.Errorf("error while printing diff: %w", err) } } return len(report.Diffs), nil diff --git a/cli/internal/metadataobject/metadataobject.go b/cli/internal/metadataobject/metadataobject.go index fad8b94fe07..5c00ef05978 100644 --- a/cli/internal/metadataobject/metadataobject.go +++ b/cli/internal/metadataobject/metadataobject.go @@ -135,7 +135,7 @@ func DefaultGetFiles(yamlFile string) ([]string, error) { files = append(files, nodeFiles...) } } else { - return nil, fmt.Errorf("finding child files in failed: %v", err) + return nil, fmt.Errorf("finding child files in failed: %w", err) } } diff --git a/cli/migrate/cmd/commands.go b/cli/migrate/cmd/commands.go index 959e4b6262a..316de496835 100644 --- a/cli/migrate/cmd/commands.go +++ b/cli/migrate/cmd/commands.go @@ -182,7 +182,7 @@ func SquashCmd(m *migrate.Migrate, from uint64, to int64, version int64, name, d err = createOptions.Create() if err != nil { - return versions, errors.Wrap(err, "cannot create migration") + return versions, fmt.Errorf("cannot create migration: %w", err) } return diff --git a/cli/migrate/database/hasuradb/types.go b/cli/migrate/database/hasuradb/types.go index 6202f24a971..d09880e38c1 100644 --- a/cli/migrate/database/hasuradb/types.go +++ b/cli/migrate/database/hasuradb/types.go @@ -142,10 +142,10 @@ func NewHasuraError(data []byte, isCmd bool) error { var herror HasuraError err := json.Unmarshal(data, &herror) if err != nil { - return fmt.Errorf("failed parsing json: %v; response from API: %s", err, string(data)) + return fmt.Errorf("failed parsing json: %w; response from API: %s", err, string(data)) } return herror default: return fmt.Errorf("Data Error: %s", string(data)) } -} \ No newline at end of file +} diff --git a/cli/migrate/util.go b/cli/migrate/util.go index e619bcf903b..633b902544b 100644 --- a/cli/migrate/util.go +++ b/cli/migrate/util.go @@ -17,7 +17,6 @@ import ( migratedb "github.com/hasura/graphql-engine/cli/v2/migrate/database" "github.com/hasura/graphql-engine/cli/v2" - "github.com/pkg/errors" ) // MultiError holds multiple errors. @@ -166,7 +165,7 @@ func NewMigrate(ec *cli.ExecutionContext, isCmd bool, sourceName string, sourceK t, err := New(opts) if err != nil { - return nil, errors.Wrap(err, "cannot create migrate instance") + return nil, fmt.Errorf("cannot create migrate instance: %w", err) } if ec.Config.Version >= cli.V2 { t.databaseDrv.EnableCheckMetadataConsistency(true) diff --git a/cli/pkg/metadata/project_metadata_test.go b/cli/pkg/metadata/project_metadata_test.go index 99b71db23b5..cc7fe496e47 100644 --- a/cli/pkg/metadata/project_metadata_test.go +++ b/cli/pkg/metadata/project_metadata_test.go @@ -10,7 +10,6 @@ import ( "testing" "github.com/hasura/graphql-engine/cli/v2/internal/hasura" - "github.com/hasura/graphql-engine/cli/v2/pkg/migrate" "github.com/stretchr/testify/require" @@ -247,7 +246,7 @@ func TestProjectMetadata_Reload(t *testing.T) { name string fields fields want string - wantErr bool + wantErr require.ErrorAssertionFunc }{ { "can reload metadata", @@ -256,21 +255,61 @@ func TestProjectMetadata_Reload(t *testing.T) { endpointString: hgeEndpoint, }, `{"message": "success"}`, - false, + require.NoError, }, + // TODO: automate the following tests + // following tests are currently ran manually by making use of https://github.com/Shopify/toxiproxy + //{ + // "can return expected error type when graphql engine connections fail", + // fields{ + // projectDirectory: "testdata/projectv3", + // endpointString: "http://localhost:12345/something", + // }, + // `{"message": "success"}`, + // func(t require.TestingT, err error, i ...interface{}) { + // var e *url.Error + // require.Truef(t, errors.As(err, &e), "expected err to be an instance of %v but got %v", reflect.TypeOf(&url.Error{}), reflect.TypeOf(err)) + // }, + //}, + // config.json + // [ + // { + // "name": "hasura", + // "listen": "[::]:18080", + // "upstream": "localhost:8080", + // "enabled": true + // } + // ] + // + // 1. Simulate reset peer + // $ toxiproxy-cli toxic add -t reset_peer hasura + // 2. Simulate timeout + // $ toxiproxy-cli toxic add -t timeout -a timeout=1 hasura + //{ + // "return expect error type when graphql engine connections reset", + // fields{ + // projectDirectory: "testdata/projectv3", + // endpointString: "http://localhost:18080", + // }, + // `{"message": "success"}`, + // func(t require.TestingT, err error, i ...interface{}) { + // var e *url.Error + // require.Truef(t, errors.As(err, &e), "expected err to be an instance of %v but got %v", reflect.TypeOf(&url.Error{}), reflect.TypeOf(err)) + // }, + //}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - p, err := NewProjectMetadata(tt.fields.projectDirectory, WithEndpoint(hgeEndpoint), WithAdminSecret(testutil.TestAdminSecret)) - require.NoError(t, err) - got, err := p.Reload() - if tt.wantErr { - require.Error(t, err) + p, err := NewProjectMetadata(tt.fields.projectDirectory, WithEndpoint(tt.fields.endpointString), WithAdminSecret(testutil.TestAdminSecret)) + tt.wantErr(t, err) + if p != nil { + got, err := p.Reload() + + tt.wantErr(t, err) + gotb, err := ioutil.ReadAll(got) + require.NoError(t, err) + require.JSONEq(t, tt.want, string(gotb)) } - require.NoError(t, err) - gotb, err := ioutil.ReadAll(got) - require.NoError(t, err) - require.JSONEq(t, tt.want, string(gotb)) }) } } diff --git a/cli/pkg/migrate/project_migrate_test.go b/cli/pkg/migrate/project_migrate_test.go index 28cb13f79d5..5ca6075fa17 100644 --- a/cli/pkg/migrate/project_migrate_test.go +++ b/cli/pkg/migrate/project_migrate_test.go @@ -212,7 +212,17 @@ func TestProjectMigrate_Apply_Configv2(t *testing.T) { require.Error(t, err) } else { require.NoError(t, err) - require.Equal(t, tt.want, got) + for idx, want := range tt.want { + if idx >= len(got) { + t.Errorf("expected to got to have equal number of elements: want %v got %v", len(tt.want), len(got)) + } + if len(want.Message) > 0 { + assert.Equal(t, want.Message, got[idx].Message) + } + if want.Error != nil { + assert.Equal(t, want.Error.Error(), got[idx].Error.Error()) + } + } } }) } diff --git a/cli/util/server.go b/cli/util/server.go index e56816f806e..df1c32b1e22 100644 --- a/cli/util/server.go +++ b/cli/util/server.go @@ -1,13 +1,15 @@ package util import ( - "crypto/tls" + "context" + "encoding/json" "fmt" "net/http" "net/url" "path" - "github.com/parnurzeal/gorequest" + "github.com/hasura/graphql-engine/cli/v2/internal/httpc" + "github.com/sirupsen/logrus" ) @@ -23,40 +25,42 @@ type hdbVersion struct { } // GetServerState queries a server for the state. -func GetServerState(endpoint string, adminSecret string, config *tls.Config, hasMetadataV3 bool, log *logrus.Logger) *ServerState { +func GetServerState(client *httpc.Client, endpoint string, hasMetadataV3 bool, log *logrus.Logger) *ServerState { state := &ServerState{ UUID: "00000000-0000-0000-0000-000000000000", } if hasMetadataV3 { - payload := ` + payload := []byte(` { "type": "get_catalog_state", "args": {} } -` - req := gorequest.New() - if config != nil { - req.TLSClientConfig(config) - } - req.Post(endpoint).Send(payload) - req.Set("X-Hasura-Admin-Secret", adminSecret) - +`) var r struct { ID string `json:"id"` } - _, _, errs := req.EndStruct(&r) - if len(errs) != 0 { - log.Debugf("server state: errors: %v", errs) + var body interface{} + err := json.Unmarshal(payload, &body) + if err != nil { + log.Debugf("unmarshalling json request to construct server state failed: %v", err) + return state + } + req, err := client.NewRequest(http.MethodPost, endpoint, body) + if err != nil { + log.Debugf("constructing http request to construct server state failed: %v", err) + return state + } + + _, err = client.Do(context.Background(), req, &r) + if err != nil { + log.Debugf("http request to construct server state failed: %v", err) return state } state.UUID = r.ID } else { - state := &ServerState{ - UUID: "00000000-0000-0000-0000-000000000000", - } - payload := `{ + payload := []byte(`{ "type": "select", "args": { "table": { @@ -68,29 +72,28 @@ func GetServerState(endpoint string, adminSecret string, config *tls.Config, has "cli_state" ] } - }` - - req := gorequest.New() - if config != nil { - req.TLSClientConfig(config) + }`) + var body interface{} + err := json.Unmarshal(payload, &body) + if err != nil { + log.Debugf("unmarshalling json request to construct server state failed: %v", err) + return state + } + req, err := client.NewRequest(http.MethodPost, endpoint, body) + if err != nil { + log.Debugf("constructing http request to construct server state failed: %v", err) + return state } - req.Post(endpoint).Send(payload) - req.Set("X-Hasura-Admin-Secret", adminSecret) - var r []hdbVersion - _, _, errs := req.EndStruct(&r) - if len(errs) != 0 { - log.Debugf("server state: errors: %v", errs) + _, err = client.Do(context.Background(), req, &r) + if err != nil { + log.Debugf("http request to construct server state failed: %v", err) return state } - - if len(r) != 1 { - log.Debugf("invalid response: %v", r) - return state + if len(r) >= 1 { + state.UUID = r[0].UUID + state.CLIState = r[0].CLIState } - - state.UUID = r[0].UUID - state.CLIState = r[0].CLIState } return state @@ -104,7 +107,7 @@ func GetServerStatus(endpoint string) (err error) { uri.Path = path.Join(uri.Path, "healthz") resp, err := http.Get(uri.String()) if err != nil { - return fmt.Errorf("making http request failed: %s", err.Error()) + return fmt.Errorf("making http request failed: %w", err) } if resp.StatusCode != http.StatusOK { return fmt.Errorf("request failed: url: %s status code: %v status: %s", uri.String(), resp.StatusCode, resp.Status) diff --git a/cli/util/server_test.go b/cli/util/server_test.go new file mode 100644 index 00000000000..d9a83ce52be --- /dev/null +++ b/cli/util/server_test.go @@ -0,0 +1,58 @@ +package util + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/hasura/graphql-engine/cli/v2/internal/httpc" + "github.com/hasura/graphql-engine/cli/v2/internal/testutil" + "github.com/sirupsen/logrus" +) + +func TestGetServerState(t *testing.T) { + portV13, teardownV133 := testutil.StartHasura(t, "hasura/graphql-engine:v1.3.3") + hgeEndpointV133 := fmt.Sprintf("http://localhost:%s", portV13) + defer teardownV133() + port, teardown := testutil.StartHasura(t, testutil.HasuraDockerImage) + hgeEndpoint := fmt.Sprintf("http://localhost:%s", port) + defer teardown() + type args struct { + client *httpc.Client + endpoint string + hasMetadataV3 bool + log *logrus.Logger + } + tests := []struct { + name string + args args + }{ + { + "can generate server state for v1.x", + args{ + + testutil.NewHttpcClient(t, portV13, nil), + fmt.Sprintf("%v/v1/query", hgeEndpointV133), + false, + logrus.New(), + }, + }, + { + "can generate server state for latest", + args{ + + testutil.NewHttpcClient(t, port, nil), + fmt.Sprintf("%v/v1/metadata", hgeEndpoint), + true, + logrus.New(), + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := GetServerState(tt.args.client, tt.args.endpoint, tt.args.hasMetadataV3, tt.args.log) + assert.Truef(t, got.UUID != "00000000-0000-0000-0000-000000000000" && got.UUID != "", "expected server UUID to be set got: ", got.UUID) + }) + } +}