Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New integration tests #203

Merged
merged 12 commits into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ jobs:
- name: Coverage
id: coverage
run: |
go test -v -coverprofile=cov.out ./...
go test -v -coverpkg=./... -coverprofile=cov.out ./...
go tool cover -html=cov.out -o coverage.html
COV=$(go tool cover -func=cov.out | awk '/total/ { gsub("%", "", $3); print $3 }')
(( $(echo "${COV} > ${MIN_COV}" | bc -l) )) && STATE=success || STATE=failure
echo "::set-output name=COV::${COV}% (Required ${MIN_COV}%)"
echo "::set-output name=STATE::${STATE}"
env:
MIN_COV: 70
MIN_COV: 60

- uses: Sibz/github-status-action@v1
with:
Expand Down
14 changes: 4 additions & 10 deletions cmd/gmailctl/cmd/upstream.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
package cmd

import (
"fmt"

"github.com/mbrt/gmailctl/pkg/api"
papply "github.com/mbrt/gmailctl/pkg/apply"
)

func upstreamConfig(gmailapi *api.GmailAPI) (papply.GmailConfig, error) {
f, err := gmailapi.ListFilters()
cfg, err := papply.FromAPI(gmailapi)
if err != nil {
if len(f) == 0 {
return papply.GmailConfig{}, fmt.Errorf("getting filters from Gmail: %w", err)
if len(cfg.Filters) == 0 {
return papply.GmailConfig{}, err
}
// We have some filters, let's work with what we have and issue a warning.
stderrPrintf("Warning: Error getting one or more filters from Gmail: %sThey will be ignored in the diff.\n", err)
}
l, err := gmailapi.ListLabels()
return papply.GmailConfig{
Labels: l,
Filters: f,
}, err
return cfg, nil
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.15

require (
github.com/google/go-jsonnet v0.17.0
github.com/gorilla/mux v1.8.0
github.com/hashicorp/go-multierror v1.1.1
github.com/pmezard/go-difflib v1.0.0
github.com/spf13/cobra v1.2.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ github.com/googleapis/gax-go/v2 v2.1.0/go.mod h1:Q3nei7sK6ybPYH7twZdmQpAd1MKb7pf
github.com/googleapis/gax-go/v2 v2.1.1 h1:dp3bWCh+PPO1zjRRiCSczJav13sBvG4UhNyVTa1KqdU=
github.com/googleapis/gax-go/v2 v2.1.1/go.mod h1:hddJymUZASv3XPyGkUpKj8pPO47Rmb0eJc8R6ouapiM=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/hashicorp/consul/api v1.1.0/go.mod h1:VmuI/Lkw1nC05EYQWNKwWGbkg+FbDBtguAZLlVdkD9Q=
github.com/hashicorp/consul/sdk v0.1.1/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8=
Expand Down
138 changes: 86 additions & 52 deletions integration_test.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
package integration_test

import (
"bufio"
"bytes"
"context"
"encoding/json"
"flag"
"io/ioutil"
"path/filepath"
"sort"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/mbrt/gmailctl/internal/fakegmail"
"github.com/mbrt/gmailctl/pkg/api"
"github.com/mbrt/gmailctl/pkg/apply"
cfg "github.com/mbrt/gmailctl/pkg/config"
cfgv3 "github.com/mbrt/gmailctl/pkg/config/v1alpha3"
"github.com/mbrt/gmailctl/pkg/config"
"github.com/mbrt/gmailctl/pkg/config/v1alpha2"
"github.com/mbrt/gmailctl/pkg/export/xml"
"github.com/mbrt/gmailctl/pkg/rimport"
)

func readConfig(t *testing.T, path string) cfgv3.Config {
t.Helper()
res, err := cfg.ReadFile(path, filepath.Join("testdata", path))
if err != nil {
t.Fatal(err)
}
return res
}
// update is useful to regenerate the golden files
// Make sure the new version makes sense!!
var update = flag.Bool("update", false, "update golden files")

type testPaths struct {
locals []string
diffs []string
}
var fixedTime = mustParseTime("2006-01-02 15:04", "2018-03-08 17:00")

func globTestdataPaths(t *testing.T, pattern string) []string {
t.Helper()
Expand All @@ -37,49 +41,79 @@ func globTestdataPaths(t *testing.T, pattern string) []string {
return fs
}

func allTestPaths(t *testing.T) testPaths {
t.Helper()
local := globTestdataPaths(t, "local.*.yaml")
local = append(local, globTestdataPaths(t, "local.*.jsonnet")...)
tp := testPaths{
locals: local,
diffs: globTestdataPaths(t, "local.*.diff"),
}
if len(tp.locals) != len(tp.diffs) {
t.Fatal("expected both yaml and diff to be present")
}
return tp
}
func TestIntegration(t *testing.T) {
cfgPaths := globTestdataPaths(t, "valid/*.jsonnet")
gapi := api.NewFromService(fakegmail.NewService(context.Background(), t))

func parseConfig(t *testing.T, path string) apply.ConfigParseRes {
t.Helper()
config := readConfig(t, path)
r, err := apply.FromConfig(config)
if err != nil {
t.Fatal(err)
}
return r
}
for _, cfgPath := range cfgPaths {
name := strings.TrimSuffix(cfgPath, ".jsonnet")
t.Run(name, func(t *testing.T) {
// Parse the config.
cfg, err := config.ReadFile(cfgPath, filepath.Join("testdata", cfgPath))
require.Nil(t, err)
pres, err := apply.FromConfig(cfg)
require.Nil(t, err)

func TestIntegrationImport(t *testing.T) {
tps := allTestPaths(t)
// Export.
xmlexp := xml.NewWithTime(func() time.Time { return fixedTime })
var cfgxml bytes.Buffer
bw := bufio.NewWriter(&cfgxml)
author := v1alpha2.Author{Name: "Me", Email: "me@gmail.com"}
err = xmlexp.Export(author, pres.Filters, bw)
bw.Flush() // Make sure everything is written out.
require.Nil(t, err)

for i := 0; i < len(tps.locals); i++ {
localPath := tps.locals[i]
// Fetch the upstream filters.
upres, err := apply.FromAPI(gapi)
require.Nil(t, err)

t.Run(localPath, func(t *testing.T) {
local := parseConfig(t, localPath).GmailConfig
// Apply the diff.
d, err := apply.Diff(pres.GmailConfig, upres)
require.Nil(t, err)
err = apply.Apply(d, gapi, true)
require.Nil(t, err)

// Import
config, err := rimport.Import(local.Filters, local.Labels)
assert.Nil(t, err)
// Generate
pres, err := apply.FromConfig(config)
assert.Nil(t, err)
diff, err := apply.Diff(pres.GmailConfig, local)
// Re-generating imported filters should not cause any diff
assert.Nil(t, err)
assert.Equal(t, "", diff.String())
// Import.
upres, err = apply.FromAPI(gapi)
require.Nil(t, err)
icfg, err := rimport.Import(upres.Filters, upres.Labels)
require.Nil(t, err)
icfgJSON, err := json.MarshalIndent(icfg, "", " ")
require.Nil(t, err)

// Compare the results with the golden files (or update the golden files).
if *update {
// Import.
err := ioutil.WriteFile(name+".json", icfgJSON, 0o600)
require.Nil(t, err)
// Diff.
err = ioutil.WriteFile(name+".diff", []byte(d.String()), 0o600)
require.Nil(t, err)
// Export.
err = ioutil.WriteFile(name+".xml", cfgxml.Bytes(), 0o600)
require.Nil(t, err)
return
}
// Import.
b, err := ioutil.ReadFile(name + ".json")
require.Nil(t, err)
assert.Equal(t, string(b), string(icfgJSON))
// Diff.
b, err = ioutil.ReadFile(name + ".diff")
require.Nil(t, err)
assert.Equal(t, string(b), d.String())
// Export
b, err = ioutil.ReadFile(name + ".xml")
require.Nil(t, err)
assert.Equal(t, string(b), cfgxml.String())
})
}
}

func mustParseTime(layout, value string) time.Time {
t, err := time.Parse(layout, value)
if err != nil {
panic(err)
}
return t
}
Loading