Unverified Commit 200abf0f authored by Oren Shomron's avatar Oren Shomron Committed by GitHub
Browse files

Merge pull request #835 from shomron/issue-702-validate-env

Validate environment prior to fetching package
parents 70935911 abe8e31d
......@@ -19,12 +19,15 @@ import (
"github.com/ksonnet/ksonnet/pkg/app"
"github.com/ksonnet/ksonnet/pkg/pkg"
"github.com/ksonnet/ksonnet/pkg/registry"
"github.com/pkg/errors"
)
type libCacher func(app.App, registry.InstalledChecker, pkg.Descriptor, string, bool) (*app.LibraryConfig, error)
type libUpdater func(name string, env string, spec *app.LibraryConfig) (*app.LibraryConfig, error)
type envChecker func(name string) (bool, error)
// RunPkgInstall runs `pkg install`
func RunPkgInstall(m map[string]interface{}) error {
pi, err := NewPkgInstall(m)
......@@ -45,6 +48,7 @@ type PkgInstall struct {
checker registry.InstalledChecker
libCacherFn libCacher
libUpdateFn libUpdater
envCheckerFn envChecker
}
// NewPkgInstall creates an instance of PkgInstall.
......@@ -70,6 +74,14 @@ func NewPkgInstall(m map[string]interface{}) (*PkgInstall, error) {
return registry.CacheDependency(a, checker, d, customName, force, httpClient)
},
libUpdateFn: a.UpdateLib,
envCheckerFn: func(name string) (bool, error) {
env, err := a.Environment(name)
if err != nil {
return false, err
}
exists := (env != nil)
return exists, nil
},
}
if ol.err != nil {
......@@ -86,6 +98,17 @@ func (pi *PkgInstall) Run() error {
return err
}
// Environment validation
if pi.envName != "" {
ok, err := pi.envCheckerFn(pi.envName)
if err != nil {
return errors.Wrap(err, "checking environment")
}
if !ok {
return errors.Errorf("invalid environment: %s", pi.envName)
}
}
libCfg, err := pi.libCacherFn(pi.app, pi.checker, d, customName, pi.force)
if err != nil {
return err
......
......@@ -22,6 +22,7 @@ import (
amocks "github.com/ksonnet/ksonnet/pkg/app/mocks"
"github.com/ksonnet/ksonnet/pkg/pkg"
"github.com/ksonnet/ksonnet/pkg/registry"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
......@@ -97,3 +98,45 @@ func TestPkgInstall_requires_app(t *testing.T) {
_, err := NewPkgInstall(in)
require.Error(t, err)
}
func TestPkgInstall_invalid_env(t *testing.T) {
withApp(t, func(appMock *amocks.App) {
libName := "incubator/apache"
customName := "customName"
in := map[string]interface{}{
OptionApp: appMock,
OptionPkgName: libName,
OptionName: customName,
OptionEnvName: "invalid-env",
OptionForce: false,
OptionTLSSkipVerify: false,
}
a, err := NewPkgInstall(in)
require.NoError(t, err)
var cacherCalled bool
fakeCacher := func(a app.App, checker registry.InstalledChecker, d pkg.Descriptor, cn string, force bool) (*app.LibraryConfig, error) {
cacherCalled = true
return nil, errors.New("not implemented")
}
var updaterCalled bool
fakeUpdater := func(name string, env string, spec *app.LibraryConfig) (*app.LibraryConfig, error) {
updaterCalled = true
return nil, errors.New("not implemented")
}
a.libCacherFn = fakeCacher
a.libUpdateFn = fakeUpdater
a.envCheckerFn = func(string) (bool, error) {
return false, nil
}
err = a.Run()
require.Error(t, err)
assert.False(t, cacherCalled, "dependency cacher called unexpectedly")
assert.False(t, updaterCalled, "library reference updater called unexpectedly")
})
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment