From f73d39ecc73ad5978b3c1e69b041bd4e20b45622 Mon Sep 17 00:00:00 2001 From: devdinu Date: Mon, 22 Jun 2020 15:15:19 +0530 Subject: [PATCH] moving http/api pkg to api package, adding unit tests for service install --- cmd/service/service.go | 8 +- pkg/{http => }/api/install.go | 2 +- pkg/{http => }/api/install_api_test.go | 4 +- pkg/{http => }/api/installer.go | 0 pkg/{http => }/api/list/listcontract.go | 0 pkg/{http => }/api/list/listhandler.go | 0 pkg/{http => }/api/logger/logger.go | 0 pkg/{http => }/api/ping/pingcontract.go | 0 pkg/{http => }/api/ping/pinghandler.go | 0 pkg/{http => }/api/service.go | 2 +- pkg/api/service_test.go | 121 ++++++++++++++++++ .../api/testdata/albatross/Chart.yaml | 0 .../api/testdata/albatross/values.yaml | 0 .../api/testdata/helm/repositories.yaml | 0 pkg/{http => }/api/upgrade.go | 0 15 files changed, 129 insertions(+), 8 deletions(-) rename pkg/{http => }/api/install.go (97%) rename pkg/{http => }/api/install_api_test.go (97%) rename pkg/{http => }/api/installer.go (100%) rename pkg/{http => }/api/list/listcontract.go (100%) rename pkg/{http => }/api/list/listhandler.go (100%) rename pkg/{http => }/api/logger/logger.go (100%) rename pkg/{http => }/api/ping/pingcontract.go (100%) rename pkg/{http => }/api/ping/pinghandler.go (100%) rename pkg/{http => }/api/service.go (98%) create mode 100644 pkg/api/service_test.go rename pkg/{http => }/api/testdata/albatross/Chart.yaml (100%) rename pkg/{http => }/api/testdata/albatross/values.yaml (100%) rename pkg/{http => }/api/testdata/helm/repositories.yaml (100%) rename pkg/{http => }/api/upgrade.go (100%) diff --git a/cmd/service/service.go b/cmd/service/service.go index 879ee449b..375f9c4be 100644 --- a/cmd/service/service.go +++ b/cmd/service/service.go @@ -5,10 +5,10 @@ import ( "net/http" "helm.sh/helm/v3/pkg/action" - "helm.sh/helm/v3/pkg/http/api" - "helm.sh/helm/v3/pkg/http/api/list" - "helm.sh/helm/v3/pkg/http/api/logger" - "helm.sh/helm/v3/pkg/http/api/ping" + "helm.sh/helm/v3/pkg/api" + "helm.sh/helm/v3/pkg/api/list" + "helm.sh/helm/v3/pkg/api/logger" + "helm.sh/helm/v3/pkg/api/ping" "helm.sh/helm/v3/pkg/servercontext" ) diff --git a/pkg/http/api/install.go b/pkg/api/install.go similarity index 97% rename from pkg/http/api/install.go rename to pkg/api/install.go index b9945aac4..5d465383a 100644 --- a/pkg/http/api/install.go +++ b/pkg/api/install.go @@ -4,7 +4,7 @@ import ( "encoding/json" "net/http" - "helm.sh/helm/v3/pkg/http/api/logger" + "helm.sh/helm/v3/pkg/api/logger" ) type InstallRequest struct { diff --git a/pkg/http/api/install_api_test.go b/pkg/api/install_api_test.go similarity index 97% rename from pkg/http/api/install_api_test.go rename to pkg/api/install_api_test.go index d9d9b4f7c..72961e8c2 100644 --- a/pkg/http/api/install_api_test.go +++ b/pkg/api/install_api_test.go @@ -12,10 +12,10 @@ import ( "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" "gotest.tools/assert" + "helm.sh/helm/v3/pkg/api" + "helm.sh/helm/v3/pkg/api/logger" "helm.sh/helm/v3/pkg/chart" "helm.sh/helm/v3/pkg/cli" - "helm.sh/helm/v3/pkg/http/api" - "helm.sh/helm/v3/pkg/http/api/logger" "helm.sh/helm/v3/pkg/release" ) diff --git a/pkg/http/api/installer.go b/pkg/api/installer.go similarity index 100% rename from pkg/http/api/installer.go rename to pkg/api/installer.go diff --git a/pkg/http/api/list/listcontract.go b/pkg/api/list/listcontract.go similarity index 100% rename from pkg/http/api/list/listcontract.go rename to pkg/api/list/listcontract.go diff --git a/pkg/http/api/list/listhandler.go b/pkg/api/list/listhandler.go similarity index 100% rename from pkg/http/api/list/listhandler.go rename to pkg/api/list/listhandler.go diff --git a/pkg/http/api/logger/logger.go b/pkg/api/logger/logger.go similarity index 100% rename from pkg/http/api/logger/logger.go rename to pkg/api/logger/logger.go diff --git a/pkg/http/api/ping/pingcontract.go b/pkg/api/ping/pingcontract.go similarity index 100% rename from pkg/http/api/ping/pingcontract.go rename to pkg/api/ping/pingcontract.go diff --git a/pkg/http/api/ping/pinghandler.go b/pkg/api/ping/pinghandler.go similarity index 100% rename from pkg/http/api/ping/pinghandler.go rename to pkg/api/ping/pinghandler.go diff --git a/pkg/http/api/service.go b/pkg/api/service.go similarity index 98% rename from pkg/http/api/service.go rename to pkg/api/service.go index af3b069d7..3b58b6cd8 100644 --- a/pkg/http/api/service.go +++ b/pkg/api/service.go @@ -4,10 +4,10 @@ import ( "context" "fmt" + "helm.sh/helm/v3/pkg/api/logger" "helm.sh/helm/v3/pkg/chart" "helm.sh/helm/v3/pkg/chart/loader" "helm.sh/helm/v3/pkg/cli" - "helm.sh/helm/v3/pkg/http/api/logger" "helm.sh/helm/v3/pkg/release" _ "k8s.io/client-go/plugin/pkg/client/auth" ) diff --git a/pkg/api/service_test.go b/pkg/api/service_test.go new file mode 100644 index 000000000..728f94287 --- /dev/null +++ b/pkg/api/service_test.go @@ -0,0 +1,121 @@ +package api + +import ( + "context" + "errors" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" + "helm.sh/helm/v3/pkg/api/logger" + "helm.sh/helm/v3/pkg/chart" + "helm.sh/helm/v3/pkg/cli" + "helm.sh/helm/v3/pkg/release" +) + +type ServiceTestSuite struct { + suite.Suite + ctx context.Context + installer *mockInstaller + chartloader *mockChartLoader + svc Service + settings *cli.EnvSettings +} + +func (s *ServiceTestSuite) SetupTest() { + logger.Setup("") + s.ctx = context.Background() + s.installer = new(mockInstaller) + s.chartloader = new(mockChartLoader) + s.settings = &cli.EnvSettings{} + s.svc = NewService(s.settings, s.chartloader, s.installer) +} + +func (s *ServiceTestSuite) TestInstallShouldReturnErrorOnInvalidChart() { + chartName := "stable/invalid-chart" + cfg := InstallConfig{ + Name: "some-component", + Namespace: "hermes", + ChartName: chartName, + } + var vals chartValues + s.chartloader.On("LocateChart", chartName, s.settings).Return("", errors.New("Unable to find chart")) + + res, err := s.svc.Install(s.ctx, cfg, vals) + + t := s.T() + assert.Nil(t, res) + assert.EqualError(t, err, "error in locating chart: Unable to find chart") + s.chartloader.AssertExpectations(t) + s.installer.AssertNotCalled(t, "SetConfig") + s.installer.AssertNotCalled(t, "Run") +} + +func (s *ServiceTestSuite) TestInstallShouldReturnErrorOnFailedIntallRun() { + chartName := "stable/valid-chart" + cfg := InstallConfig{ + Name: "some-component", + Namespace: "hermes", + ChartName: chartName, + } + var vals map[string]interface{} + s.chartloader.On("LocateChart", chartName, s.settings).Return("testdata/albatross", nil) + var release *release.Release + s.installer.On("SetConfig", cfg) + s.installer.On("Run", mock.AnythingOfType("*chart.Chart"), vals).Return(release, errors.New("cluster issue")) + + res, err := s.svc.Install(s.ctx, cfg, vals) + + t := s.T() + assert.Nil(t, res) + assert.EqualError(t, err, "error in installing chart: cluster issue") + s.chartloader.AssertExpectations(t) + s.installer.AssertExpectations(t) +} + +func (s *ServiceTestSuite) TestInstallShouldReturnResultOnSuccess() { + chartName := "stable/valid-chart" + cfg := InstallConfig{ + Name: "some-component", + Namespace: "hermes", + ChartName: chartName, + } + var vals map[string]interface{} + s.chartloader.On("LocateChart", chartName, s.settings).Return("testdata/albatross", nil) + s.installer.On("SetConfig", cfg) + release := &release.Release{Name: "some-comp-release", Info: &release.Info{Status: release.StatusDeployed}} + s.installer.On("Run", mock.AnythingOfType("*chart.Chart"), vals).Return(release, nil) + + res, err := s.svc.Install(s.ctx, cfg, vals) + + t := s.T() + assert.NoError(t, err) + require.NotNil(t, res) + assert.Equal(t, res.status, "deployed") + s.chartloader.AssertExpectations(t) + s.installer.AssertExpectations(t) +} + +func TestServiceSuite(t *testing.T) { + suite.Run(t, new(ServiceTestSuite)) +} + +type mockChartLoader struct{ mock.Mock } + +func (m *mockChartLoader) LocateChart(name string, settings *cli.EnvSettings) (string, error) { + args := m.Called(name, settings) + return args.String(0), args.Error(1) +} + +type mockInstaller struct{ mock.Mock } + +func (m *mockInstaller) SetConfig(cfg InstallConfig) { + m.Called(cfg) +} + +func (m *mockInstaller) Run(c *chart.Chart, vals map[string]interface{}) (*release.Release, error) { + args := m.Called(c, vals) + return args.Get(0).(*release.Release), args.Error(1) +} diff --git a/pkg/http/api/testdata/albatross/Chart.yaml b/pkg/api/testdata/albatross/Chart.yaml similarity index 100% rename from pkg/http/api/testdata/albatross/Chart.yaml rename to pkg/api/testdata/albatross/Chart.yaml diff --git a/pkg/http/api/testdata/albatross/values.yaml b/pkg/api/testdata/albatross/values.yaml similarity index 100% rename from pkg/http/api/testdata/albatross/values.yaml rename to pkg/api/testdata/albatross/values.yaml diff --git a/pkg/http/api/testdata/helm/repositories.yaml b/pkg/api/testdata/helm/repositories.yaml similarity index 100% rename from pkg/http/api/testdata/helm/repositories.yaml rename to pkg/api/testdata/helm/repositories.yaml diff --git a/pkg/http/api/upgrade.go b/pkg/api/upgrade.go similarity index 100% rename from pkg/http/api/upgrade.go rename to pkg/api/upgrade.go