graphql-engine/cli/commands/metadata_diff_test.go
Jeff Sieu 2ee7f7d76e cli(metadata): add diff command and dry-run flag (#3157)
### Description
Adds a `metadata diff` command to show comparisons between two different sets of Hasura metadata.
```
# Show changes between server metadata and the exported metadata file:
hasura metadata diff

# Show changes between server metadata and that in local_metadata.yaml:
hasura metadata diff local_metadata.yaml

# Show changes between metadata from metadata.yaml and metadata_old.yaml:
hasura metadata diff metadata.yaml metadata_old.yaml
```

Also adds a `--dry-run` flag to `metadata apply` command which will print the diff and exit rather than actually applying the metadata.

### Affected components 
- CLI
- Docs

### Related Issues
Close #3126, Close #3127

### Solution and Design
- Added `metadata_diff.go` and `metadata_diff_test.go`


### Steps to test and verify
```
hasura metadata export
# Make changes to migrations/metadata.yaml
hasura metadata diff
```

### Limitations, known bugs & workarounds
This is just a general-purpose diff. 

A more contextual diff with the understanding of metadata can be added once #3072  is merged.
2019-10-30 19:24:22 +05:30

126 lines
2.8 KiB
Go

package commands
import (
"bytes"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"testing"
"time"
"github.com/briandowns/spinner"
"github.com/hasura/graphql-engine/cli"
"github.com/hasura/graphql-engine/cli/version"
"github.com/sirupsen/logrus/hooks/test"
)
var testMetadata1 = `allowlist: []
functions: []
query_collections: []
remote_schemas: []
tables:
- array_relationships: []
delete_permissions: []
event_triggers: []
insert_permissions: []
is_enum: false
object_relationships: []
select_permissions: []
table: test
update_permissions: []
`
var testMetadata2 = `allowlist: []
functions: []
query_collections: []
remote_schemas: []
tables:
- array_relationships: []
configuration:
custom_column_names: {}
custom_root_fields:
delete: null
insert: null
select: null
select_aggregate: null
select_by_pk: null
update: null
delete_permissions: []
event_triggers: []
insert_permissions: []
is_enum: false
object_relationships: []
select_permissions: []
table: test
update_permissions: []
`
func TestMetadataDiffCmd(t *testing.T) {
endpointURL, err := url.Parse(os.Getenv("HASURA_GRAPHQL_TEST_ENDPOINT"))
if err != nil {
t.Fatal(err)
}
// Create migration Dir
migrationsDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(migrationsDir)
metadataFile := filepath.Join(migrationsDir, "metadata.yaml")
testMetadataFile1 := filepath.Join(migrationsDir, "testmetadata1.yaml")
testMetadataFile2 := filepath.Join(migrationsDir, "testmetadata2.yaml")
mustWriteFile(t, "", metadataFile, testMetadata1)
mustWriteFile(t, "", testMetadataFile1, testMetadata1)
mustWriteFile(t, "", testMetadataFile2, testMetadata2)
logger, _ := test.NewNullLogger()
outputFile := new(bytes.Buffer)
opts := &metadataDiffOptions{
EC: &cli.ExecutionContext{
Logger: logger,
Spinner: spinner.New(spinner.CharSets[7], 100*time.Millisecond),
MetadataFile: []string{metadataFile},
ServerConfig: &cli.ServerConfig{
Endpoint: endpointURL.String(),
AdminSecret: os.Getenv("HASURA_GRAPHQL_TEST_ADMIN_SECRET"),
ParsedEndpoint: endpointURL,
},
},
output: outputFile,
}
opts.EC.Version = version.New()
v, err := version.FetchServerVersion(opts.EC.ServerConfig.Endpoint)
if err != nil {
t.Fatalf("getting server version failed: %v", err)
}
opts.EC.Version.SetServerVersion(v)
// Run without args
opts.metadata[0] = metadataFile
err = opts.run()
if err != nil {
t.Fatalf("failed diffing metadata: %v", err)
}
// Run with one arg
opts.metadata = [2]string{testMetadataFile1, ""}
err = opts.run()
if err != nil {
t.Fatalf("failed diffing metadata: %v", err)
}
// Run with two args
opts.metadata = [2]string{testMetadataFile1, testMetadataFile2}
err = opts.run()
if err != nil {
t.Fatalf("failed diffing metadata: %v", err)
}
}