2020-02-03 19:22:47 +00:00
|
|
|
package pkger_test
|
2019-11-04 23:15:53 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-11-05 01:40:42 +00:00
|
|
|
"context"
|
2019-11-04 23:15:53 +00:00
|
|
|
"encoding/json"
|
2020-01-13 19:13:37 +00:00
|
|
|
"fmt"
|
2019-11-04 23:15:53 +00:00
|
|
|
"io"
|
2019-11-05 01:40:42 +00:00
|
|
|
"net/http"
|
2019-11-04 23:15:53 +00:00
|
|
|
"testing"
|
|
|
|
|
2019-11-06 18:16:52 +00:00
|
|
|
"github.com/go-chi/chi"
|
2019-11-05 01:40:42 +00:00
|
|
|
"github.com/influxdata/influxdb"
|
2019-12-12 19:09:32 +00:00
|
|
|
pcontext "github.com/influxdata/influxdb/context"
|
2020-02-03 19:22:47 +00:00
|
|
|
kithttp "github.com/influxdata/influxdb/kit/transport/http"
|
2019-11-08 19:33:41 +00:00
|
|
|
"github.com/influxdata/influxdb/mock"
|
2019-11-12 21:48:58 +00:00
|
|
|
"github.com/influxdata/influxdb/pkg/testttp"
|
2019-11-04 23:15:53 +00:00
|
|
|
"github.com/influxdata/influxdb/pkger"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2019-12-12 19:09:32 +00:00
|
|
|
"go.uber.org/zap"
|
2019-11-05 01:40:42 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
2019-11-04 23:15:53 +00:00
|
|
|
)
|
|
|
|
|
2019-11-06 18:02:45 +00:00
|
|
|
func TestPkgerHTTPServer(t *testing.T) {
|
2019-11-04 23:15:53 +00:00
|
|
|
t.Run("create pkg", func(t *testing.T) {
|
|
|
|
t.Run("should successfully return with valid req body", func(t *testing.T) {
|
2019-11-08 19:33:41 +00:00
|
|
|
fakeLabelSVC := mock.NewLabelService()
|
|
|
|
fakeLabelSVC.FindLabelByIDFn = func(ctx context.Context, id influxdb.ID) (*influxdb.Label, error) {
|
|
|
|
return &influxdb.Label{
|
|
|
|
ID: id,
|
|
|
|
}, nil
|
|
|
|
}
|
2019-12-06 00:53:00 +00:00
|
|
|
svc := pkger.NewService(pkger.WithLabelSVC(fakeLabelSVC))
|
2020-02-04 17:53:22 +00:00
|
|
|
pkgHandler := pkger.NewHTTPServer(zap.NewNop(), svc)
|
2019-12-12 19:09:32 +00:00
|
|
|
svr := newMountedHandler(pkgHandler, 1)
|
2019-11-04 23:15:53 +00:00
|
|
|
|
2019-12-13 20:12:03 +00:00
|
|
|
testttp.
|
2020-02-03 19:22:47 +00:00
|
|
|
PostJSON(t, "/api/v2/packages", pkger.ReqCreatePkg{
|
2019-12-13 20:12:03 +00:00
|
|
|
Resources: []pkger.ResourceToClone{
|
|
|
|
{
|
|
|
|
Kind: pkger.KindLabel,
|
|
|
|
ID: 1,
|
|
|
|
Name: "new name",
|
|
|
|
},
|
2019-11-08 19:33:41 +00:00
|
|
|
},
|
2019-12-13 20:12:03 +00:00
|
|
|
}).
|
2019-11-05 01:40:42 +00:00
|
|
|
Headers("Content-Type", "application/json").
|
|
|
|
Do(svr).
|
2019-12-13 20:12:03 +00:00
|
|
|
ExpectStatus(http.StatusOK).
|
2019-11-05 01:40:42 +00:00
|
|
|
ExpectBody(func(buf *bytes.Buffer) {
|
2020-01-13 19:13:37 +00:00
|
|
|
pkg, err := pkger.Parse(pkger.EncodingJSON, pkger.FromReader(buf))
|
|
|
|
require.NoError(t, err)
|
2019-11-04 23:15:53 +00:00
|
|
|
|
2020-01-13 19:13:37 +00:00
|
|
|
require.NotNil(t, pkg)
|
2019-11-08 19:33:41 +00:00
|
|
|
require.NoError(t, pkg.Validate())
|
2019-11-04 23:15:53 +00:00
|
|
|
|
2020-01-13 19:13:37 +00:00
|
|
|
assert.Len(t, pkg.Objects, 1)
|
2019-11-08 19:33:41 +00:00
|
|
|
assert.Len(t, pkg.Summary().Labels, 1)
|
2019-11-05 01:40:42 +00:00
|
|
|
})
|
|
|
|
|
2019-11-04 23:15:53 +00:00
|
|
|
})
|
2020-01-13 19:13:37 +00:00
|
|
|
|
|
|
|
t.Run("should be invalid if not org ids or resources provided", func(t *testing.T) {
|
2020-02-04 17:53:22 +00:00
|
|
|
pkgHandler := pkger.NewHTTPServer(zap.NewNop(), nil)
|
2020-01-13 19:13:37 +00:00
|
|
|
svr := newMountedHandler(pkgHandler, 1)
|
|
|
|
|
|
|
|
testttp.
|
2020-02-03 19:22:47 +00:00
|
|
|
PostJSON(t, "/api/v2/packages", pkger.ReqCreatePkg{}).
|
2020-01-13 19:13:37 +00:00
|
|
|
Headers("Content-Type", "application/json").
|
|
|
|
Do(svr).
|
|
|
|
ExpectStatus(http.StatusUnprocessableEntity)
|
|
|
|
|
|
|
|
})
|
2019-11-04 23:15:53 +00:00
|
|
|
})
|
2019-11-05 01:40:42 +00:00
|
|
|
|
|
|
|
t.Run("dry run pkg", func(t *testing.T) {
|
|
|
|
t.Run("json", func(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
contentType string
|
2020-02-03 19:22:47 +00:00
|
|
|
reqBody pkger.ReqApplyPkg
|
2019-11-05 01:40:42 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "app json",
|
|
|
|
contentType: "application/json",
|
2020-02-03 19:22:47 +00:00
|
|
|
reqBody: pkger.ReqApplyPkg{
|
2020-01-12 02:25:19 +00:00
|
|
|
DryRun: true,
|
|
|
|
OrgID: influxdb.ID(9000).String(),
|
2020-01-13 19:13:37 +00:00
|
|
|
RawPkg: bucketPkgKinds(t, pkger.EncodingJSON),
|
2020-01-12 02:25:19 +00:00
|
|
|
},
|
2019-11-05 01:40:42 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "defaults json when no content type",
|
2020-02-03 19:22:47 +00:00
|
|
|
reqBody: pkger.ReqApplyPkg{
|
2020-01-12 02:25:19 +00:00
|
|
|
DryRun: true,
|
|
|
|
OrgID: influxdb.ID(9000).String(),
|
2020-01-13 19:13:37 +00:00
|
|
|
RawPkg: bucketPkgKinds(t, pkger.EncodingJSON),
|
2020-01-12 02:25:19 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "retrieves package from a URL",
|
2020-02-03 19:22:47 +00:00
|
|
|
reqBody: pkger.ReqApplyPkg{
|
2020-01-12 02:25:19 +00:00
|
|
|
DryRun: true,
|
|
|
|
OrgID: influxdb.ID(9000).String(),
|
2020-02-07 04:57:24 +00:00
|
|
|
Remotes: []pkger.PkgRemote{{
|
2020-01-23 21:45:32 +00:00
|
|
|
URL: "https://gist.githubusercontent.com/jsteenb2/3a3b2b5fcbd6179b2494c2b54aa2feb0/raw/989d361db7a851a3c388eaed0b59dce7fca7fdf3/bucket_pkg.json",
|
2020-02-07 04:57:24 +00:00
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "app jsonnet",
|
|
|
|
contentType: "application/x-jsonnet",
|
|
|
|
reqBody: pkger.ReqApplyPkg{
|
|
|
|
DryRun: true,
|
|
|
|
OrgID: influxdb.ID(9000).String(),
|
|
|
|
RawPkg: bucketPkgKinds(t, pkger.EncodingJsonnet),
|
2020-01-12 02:25:19 +00:00
|
|
|
},
|
2019-11-05 01:40:42 +00:00
|
|
|
},
|
2020-01-12 02:49:55 +00:00
|
|
|
{
|
|
|
|
name: "app jsonnet",
|
|
|
|
contentType: "application/x-jsonnet",
|
2020-02-03 19:22:47 +00:00
|
|
|
reqBody: pkger.ReqApplyPkg{
|
2020-01-12 02:49:55 +00:00
|
|
|
DryRun: true,
|
|
|
|
OrgID: influxdb.ID(9000).String(),
|
2020-01-13 19:13:37 +00:00
|
|
|
RawPkg: bucketPkgKinds(t, pkger.EncodingJsonnet),
|
2020-01-12 02:49:55 +00:00
|
|
|
},
|
|
|
|
},
|
2019-11-05 01:40:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
fn := func(t *testing.T) {
|
|
|
|
svc := &fakeSVC{
|
2020-02-06 05:42:01 +00:00
|
|
|
DryRunFn: func(ctx context.Context, orgID, userID influxdb.ID, pkg *pkger.Pkg, opts ...pkger.ApplyOptFn) (pkger.Summary, pkger.Diff, error) {
|
2019-11-06 18:02:45 +00:00
|
|
|
if err := pkg.Validate(); err != nil {
|
|
|
|
return pkger.Summary{}, pkger.Diff{}, err
|
|
|
|
}
|
2019-11-05 01:40:42 +00:00
|
|
|
sum := pkg.Summary()
|
|
|
|
var diff pkger.Diff
|
|
|
|
for _, b := range sum.Buckets {
|
|
|
|
diff.Buckets = append(diff.Buckets, pkger.DiffBucket{
|
|
|
|
Name: b.Name,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return sum, diff, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-02-04 17:53:22 +00:00
|
|
|
pkgHandler := pkger.NewHTTPServer(zap.NewNop(), svc)
|
2019-12-12 19:09:32 +00:00
|
|
|
svr := newMountedHandler(pkgHandler, 1)
|
2019-11-05 01:40:42 +00:00
|
|
|
|
2019-12-13 20:12:03 +00:00
|
|
|
testttp.
|
2020-01-12 02:25:19 +00:00
|
|
|
PostJSON(t, "/api/v2/packages/apply", tt.reqBody).
|
2019-11-05 01:40:42 +00:00
|
|
|
Headers("Content-Type", tt.contentType).
|
|
|
|
Do(svr).
|
2019-12-13 20:12:03 +00:00
|
|
|
ExpectStatus(http.StatusOK).
|
2019-11-05 01:40:42 +00:00
|
|
|
ExpectBody(func(buf *bytes.Buffer) {
|
2020-02-03 19:22:47 +00:00
|
|
|
var resp pkger.RespApplyPkg
|
2019-11-05 01:40:42 +00:00
|
|
|
decodeBody(t, buf, &resp)
|
|
|
|
|
|
|
|
assert.Len(t, resp.Summary.Buckets, 1)
|
|
|
|
assert.Len(t, resp.Diff.Buckets, 1)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run(tt.name, fn)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("yml", func(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
contentType string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "app yml",
|
|
|
|
contentType: "application/x-yaml",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "text yml",
|
|
|
|
contentType: "text/yml",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
fn := func(t *testing.T) {
|
|
|
|
svc := &fakeSVC{
|
2020-02-06 05:42:01 +00:00
|
|
|
DryRunFn: func(ctx context.Context, orgID, userID influxdb.ID, pkg *pkger.Pkg, opts ...pkger.ApplyOptFn) (pkger.Summary, pkger.Diff, error) {
|
2019-11-06 18:02:45 +00:00
|
|
|
if err := pkg.Validate(); err != nil {
|
|
|
|
return pkger.Summary{}, pkger.Diff{}, err
|
|
|
|
}
|
2019-11-05 01:40:42 +00:00
|
|
|
sum := pkg.Summary()
|
|
|
|
var diff pkger.Diff
|
|
|
|
for _, b := range sum.Buckets {
|
|
|
|
diff.Buckets = append(diff.Buckets, pkger.DiffBucket{
|
|
|
|
Name: b.Name,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return sum, diff, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-02-04 17:53:22 +00:00
|
|
|
pkgHandler := pkger.NewHTTPServer(zap.NewNop(), svc)
|
2019-12-12 19:09:32 +00:00
|
|
|
svr := newMountedHandler(pkgHandler, 1)
|
2019-11-05 01:40:42 +00:00
|
|
|
|
|
|
|
body := newReqApplyYMLBody(t, influxdb.ID(9000), true)
|
|
|
|
|
2019-12-13 20:12:03 +00:00
|
|
|
testttp.
|
|
|
|
Post(t, "/api/v2/packages/apply", body).
|
2019-11-05 01:40:42 +00:00
|
|
|
Headers("Content-Type", tt.contentType).
|
|
|
|
Do(svr).
|
2019-12-13 20:12:03 +00:00
|
|
|
ExpectStatus(http.StatusOK).
|
2019-11-05 01:40:42 +00:00
|
|
|
ExpectBody(func(buf *bytes.Buffer) {
|
2020-02-03 19:22:47 +00:00
|
|
|
var resp pkger.RespApplyPkg
|
2019-11-05 01:40:42 +00:00
|
|
|
decodeBody(t, buf, &resp)
|
|
|
|
|
|
|
|
assert.Len(t, resp.Summary.Buckets, 1)
|
|
|
|
assert.Len(t, resp.Diff.Buckets, 1)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run(tt.name, fn)
|
|
|
|
}
|
|
|
|
})
|
2020-02-07 04:57:24 +00:00
|
|
|
|
|
|
|
t.Run("with multiple pkgs", func(t *testing.T) {
|
|
|
|
newBktPkg := func(t *testing.T, bktName string) json.RawMessage {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
pkgStr := fmt.Sprintf(`[
|
|
|
|
{
|
|
|
|
"apiVersion": "%[1]s",
|
|
|
|
"kind": "Bucket",
|
|
|
|
"metadata": {
|
|
|
|
"name": %q
|
|
|
|
},
|
|
|
|
"spec": {}
|
|
|
|
}
|
|
|
|
]`, pkger.APIVersion, bktName)
|
|
|
|
|
|
|
|
pkg, err := pkger.Parse(pkger.EncodingJSON, pkger.FromString(pkgStr))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
pkgBytes, err := pkg.Encode(pkger.EncodingJSON)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return pkgBytes
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
reqBody pkger.ReqApplyPkg
|
|
|
|
expectedBkts []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "retrieves package from a URL and raw pkgs",
|
|
|
|
reqBody: pkger.ReqApplyPkg{
|
|
|
|
DryRun: true,
|
|
|
|
OrgID: influxdb.ID(9000).String(),
|
|
|
|
Remotes: []pkger.PkgRemote{{
|
|
|
|
ContentType: "json",
|
|
|
|
URL: "https://gist.githubusercontent.com/jsteenb2/3a3b2b5fcbd6179b2494c2b54aa2feb0/raw/989d361db7a851a3c388eaed0b59dce7fca7fdf3/bucket_pkg.json",
|
|
|
|
}},
|
|
|
|
RawPkgs: []json.RawMessage{
|
|
|
|
newBktPkg(t, "bkt1"),
|
|
|
|
newBktPkg(t, "bkt2"),
|
|
|
|
newBktPkg(t, "bkt3"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedBkts: []string{"bkt1", "bkt2", "bkt3", "rucket_11"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "retrieves packages from raw single and list",
|
|
|
|
reqBody: pkger.ReqApplyPkg{
|
|
|
|
DryRun: true,
|
|
|
|
OrgID: influxdb.ID(9000).String(),
|
|
|
|
RawPkg: newBktPkg(t, "bkt4"),
|
|
|
|
RawPkgs: []json.RawMessage{
|
|
|
|
newBktPkg(t, "bkt1"),
|
|
|
|
newBktPkg(t, "bkt2"),
|
|
|
|
newBktPkg(t, "bkt3"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedBkts: []string{"bkt1", "bkt2", "bkt3", "bkt4"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
fn := func(t *testing.T) {
|
|
|
|
svc := &fakeSVC{
|
|
|
|
DryRunFn: func(ctx context.Context, orgID, userID influxdb.ID, pkg *pkger.Pkg, opts ...pkger.ApplyOptFn) (pkger.Summary, pkger.Diff, error) {
|
|
|
|
if err := pkg.Validate(); err != nil {
|
|
|
|
return pkger.Summary{}, pkger.Diff{}, err
|
|
|
|
}
|
|
|
|
sum := pkg.Summary()
|
|
|
|
var diff pkger.Diff
|
|
|
|
for _, b := range sum.Buckets {
|
|
|
|
diff.Buckets = append(diff.Buckets, pkger.DiffBucket{
|
|
|
|
Name: b.Name,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return sum, diff, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
pkgHandler := pkger.NewHTTPServer(zap.NewNop(), svc)
|
|
|
|
svr := newMountedHandler(pkgHandler, 1)
|
|
|
|
|
|
|
|
testttp.
|
|
|
|
PostJSON(t, "/api/v2/packages/apply", tt.reqBody).
|
|
|
|
Do(svr).
|
|
|
|
ExpectStatus(http.StatusOK).
|
|
|
|
ExpectBody(func(buf *bytes.Buffer) {
|
|
|
|
var resp pkger.RespApplyPkg
|
|
|
|
decodeBody(t, buf, &resp)
|
|
|
|
|
|
|
|
require.Len(t, resp.Summary.Buckets, len(tt.expectedBkts))
|
|
|
|
for i, expected := range tt.expectedBkts {
|
|
|
|
assert.Equal(t, expected, resp.Summary.Buckets[i].Name)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run(tt.name, fn)
|
|
|
|
}
|
|
|
|
})
|
2019-11-05 01:40:42 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("apply a pkg", func(t *testing.T) {
|
|
|
|
svc := &fakeSVC{
|
2020-02-06 05:42:01 +00:00
|
|
|
DryRunFn: func(ctx context.Context, orgID, userID influxdb.ID, pkg *pkger.Pkg, opts ...pkger.ApplyOptFn) (pkger.Summary, pkger.Diff, error) {
|
2019-11-06 18:02:45 +00:00
|
|
|
if err := pkg.Validate(); err != nil {
|
|
|
|
return pkger.Summary{}, pkger.Diff{}, err
|
|
|
|
}
|
2019-11-05 01:40:42 +00:00
|
|
|
sum := pkg.Summary()
|
|
|
|
var diff pkger.Diff
|
|
|
|
for _, b := range sum.Buckets {
|
|
|
|
diff.Buckets = append(diff.Buckets, pkger.DiffBucket{
|
|
|
|
Name: b.Name,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return sum, diff, nil
|
|
|
|
},
|
2019-12-27 19:22:05 +00:00
|
|
|
ApplyFn: func(ctx context.Context, orgID, userID influxdb.ID, pkg *pkger.Pkg, opts ...pkger.ApplyOptFn) (pkger.Summary, error) {
|
|
|
|
var opt pkger.ApplyOpt
|
|
|
|
for _, o := range opts {
|
|
|
|
require.NoError(t, o(&opt))
|
|
|
|
}
|
|
|
|
sum := pkg.Summary()
|
|
|
|
for key := range opt.MissingSecrets {
|
|
|
|
sum.MissingSecrets = append(sum.MissingSecrets, key)
|
|
|
|
}
|
|
|
|
return sum, nil
|
2019-11-05 01:40:42 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-02-04 17:53:22 +00:00
|
|
|
pkgHandler := pkger.NewHTTPServer(zap.NewNop(), svc)
|
2019-12-12 19:09:32 +00:00
|
|
|
svr := newMountedHandler(pkgHandler, 1)
|
2019-11-05 01:40:42 +00:00
|
|
|
|
2019-12-13 20:12:03 +00:00
|
|
|
testttp.
|
2020-02-03 19:22:47 +00:00
|
|
|
PostJSON(t, "/api/v2/packages/apply", pkger.ReqApplyPkg{
|
2019-12-27 19:22:05 +00:00
|
|
|
OrgID: influxdb.ID(9000).String(),
|
|
|
|
Secrets: map[string]string{"secret1": "val1"},
|
2020-01-13 19:13:37 +00:00
|
|
|
RawPkg: bucketPkgKinds(t, pkger.EncodingJSON),
|
2019-12-13 20:12:03 +00:00
|
|
|
}).
|
2019-11-05 01:40:42 +00:00
|
|
|
Do(svr).
|
2019-12-13 20:12:03 +00:00
|
|
|
ExpectStatus(http.StatusCreated).
|
2019-11-05 01:40:42 +00:00
|
|
|
ExpectBody(func(buf *bytes.Buffer) {
|
2020-02-03 19:22:47 +00:00
|
|
|
var resp pkger.RespApplyPkg
|
2019-11-05 01:40:42 +00:00
|
|
|
decodeBody(t, buf, &resp)
|
|
|
|
|
|
|
|
assert.Len(t, resp.Summary.Buckets, 1)
|
|
|
|
assert.Len(t, resp.Diff.Buckets, 1)
|
2019-12-27 19:22:05 +00:00
|
|
|
assert.Equal(t, []string{"secret1"}, resp.Summary.MissingSecrets)
|
2019-11-14 00:43:28 +00:00
|
|
|
assert.Nil(t, resp.Errors)
|
2019-11-05 01:40:42 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-13 19:13:37 +00:00
|
|
|
func bucketPkgKinds(t *testing.T, encoding pkger.Encoding) []byte {
|
2019-11-05 01:40:42 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2019-11-06 18:02:45 +00:00
|
|
|
var pkgStr string
|
2019-11-05 01:40:42 +00:00
|
|
|
switch encoding {
|
2020-01-12 02:49:55 +00:00
|
|
|
case pkger.EncodingJsonnet:
|
|
|
|
pkgStr = `
|
|
|
|
local Bucket(name, desc) = {
|
2020-01-13 19:13:37 +00:00
|
|
|
apiVersion: '%[1]s',
|
2020-01-12 02:49:55 +00:00
|
|
|
kind: 'Bucket',
|
2020-01-13 19:13:37 +00:00
|
|
|
metadata: {
|
|
|
|
name: name
|
|
|
|
},
|
|
|
|
spec: {
|
|
|
|
description: desc
|
|
|
|
}
|
2020-01-12 02:49:55 +00:00
|
|
|
};
|
|
|
|
|
2020-01-13 19:13:37 +00:00
|
|
|
[
|
|
|
|
Bucket(name="rucket_1", desc="bucket 1 description"),
|
|
|
|
]
|
2020-01-12 02:49:55 +00:00
|
|
|
`
|
2019-11-05 01:40:42 +00:00
|
|
|
case pkger.EncodingJSON:
|
2020-01-13 19:13:37 +00:00
|
|
|
pkgStr = `[
|
|
|
|
{
|
|
|
|
"apiVersion": "%[1]s",
|
|
|
|
"kind": "Bucket",
|
|
|
|
"metadata": {
|
|
|
|
"name": "rucket_11"
|
|
|
|
},
|
|
|
|
"spec": {
|
|
|
|
"description": "bucket 1 description"
|
|
|
|
}
|
2019-11-06 18:02:45 +00:00
|
|
|
}
|
2020-01-13 19:13:37 +00:00
|
|
|
]
|
2019-11-06 18:02:45 +00:00
|
|
|
`
|
2019-11-05 01:40:42 +00:00
|
|
|
case pkger.EncodingYAML:
|
2020-01-13 19:13:37 +00:00
|
|
|
pkgStr = `apiVersion: %[1]s
|
|
|
|
kind: Bucket
|
|
|
|
metadata:
|
|
|
|
name: rucket_11
|
2019-11-06 18:02:45 +00:00
|
|
|
spec:
|
2020-01-13 19:13:37 +00:00
|
|
|
description: bucket 1 description
|
2019-11-06 18:02:45 +00:00
|
|
|
`
|
2019-11-05 01:40:42 +00:00
|
|
|
default:
|
|
|
|
require.FailNow(t, "invalid encoding provided: "+encoding.String())
|
|
|
|
}
|
|
|
|
|
2020-01-13 19:13:37 +00:00
|
|
|
pkg, err := pkger.Parse(encoding, pkger.FromString(fmt.Sprintf(pkgStr, pkger.APIVersion)))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
b, err := pkg.Encode(encoding)
|
2019-11-05 01:40:42 +00:00
|
|
|
require.NoError(t, err)
|
2020-01-13 19:13:37 +00:00
|
|
|
return b
|
2019-11-05 01:40:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newReqApplyYMLBody(t *testing.T, orgID influxdb.ID, dryRun bool) *bytes.Buffer {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
2020-02-03 19:22:47 +00:00
|
|
|
err := yaml.NewEncoder(&buf).Encode(pkger.ReqApplyPkg{
|
2019-11-05 01:40:42 +00:00
|
|
|
DryRun: dryRun,
|
|
|
|
OrgID: orgID.String(),
|
2020-01-13 19:13:37 +00:00
|
|
|
RawPkg: bucketPkgKinds(t, pkger.EncodingYAML),
|
2019-11-05 01:40:42 +00:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
return &buf
|
2019-11-04 23:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func decodeBody(t *testing.T, r io.Reader, v interface{}) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
if err := json.NewDecoder(r).Decode(v); err != nil {
|
|
|
|
require.FailNow(t, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-05 01:40:42 +00:00
|
|
|
type fakeSVC struct {
|
2020-02-06 05:42:01 +00:00
|
|
|
DryRunFn func(ctx context.Context, orgID, userID influxdb.ID, pkg *pkger.Pkg, opts ...pkger.ApplyOptFn) (pkger.Summary, pkger.Diff, error)
|
2019-12-27 19:22:05 +00:00
|
|
|
ApplyFn func(ctx context.Context, orgID, userID influxdb.ID, pkg *pkger.Pkg, opts ...pkger.ApplyOptFn) (pkger.Summary, error)
|
2019-11-05 01:40:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fakeSVC) CreatePkg(ctx context.Context, setters ...pkger.CreatePkgSetFn) (*pkger.Pkg, error) {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
2020-02-06 05:42:01 +00:00
|
|
|
func (f *fakeSVC) DryRun(ctx context.Context, orgID, userID influxdb.ID, pkg *pkger.Pkg, opts ...pkger.ApplyOptFn) (pkger.Summary, pkger.Diff, error) {
|
2019-11-05 01:40:42 +00:00
|
|
|
if f.DryRunFn == nil {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
2020-02-06 05:42:01 +00:00
|
|
|
return f.DryRunFn(ctx, orgID, userID, pkg, opts...)
|
2019-11-05 01:40:42 +00:00
|
|
|
}
|
|
|
|
|
2019-12-27 19:22:05 +00:00
|
|
|
func (f *fakeSVC) Apply(ctx context.Context, orgID, userID influxdb.ID, pkg *pkger.Pkg, opts ...pkger.ApplyOptFn) (pkger.Summary, error) {
|
2019-11-05 01:40:42 +00:00
|
|
|
if f.ApplyFn == nil {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
2019-12-27 19:22:05 +00:00
|
|
|
return f.ApplyFn(ctx, orgID, userID, pkg, opts...)
|
2019-11-05 01:40:42 +00:00
|
|
|
}
|
2019-11-06 18:16:52 +00:00
|
|
|
|
2020-02-03 19:22:47 +00:00
|
|
|
func newMountedHandler(rh kithttp.ResourceHandler, userID influxdb.ID) chi.Router {
|
2019-11-06 18:16:52 +00:00
|
|
|
r := chi.NewRouter()
|
2019-12-12 19:09:32 +00:00
|
|
|
r.Mount(rh.Prefix(), authMW(userID)(rh))
|
2019-11-06 18:16:52 +00:00
|
|
|
return r
|
|
|
|
}
|
2019-12-12 19:09:32 +00:00
|
|
|
|
|
|
|
func authMW(userID influxdb.ID) func(http.Handler) http.Handler {
|
|
|
|
return func(next http.Handler) http.Handler {
|
|
|
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r = r.WithContext(pcontext.SetAuthorizer(r.Context(), &influxdb.Session{UserID: userID}))
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
return http.HandlerFunc(fn)
|
|
|
|
}
|
|
|
|
}
|