Skip to content
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
193 changes: 193 additions & 0 deletions pkg/util/helm/helm_operation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
package helm

import (
"testing"
"time"

helmclient "github.com/mittwald/go-helm-client"
"helm.sh/helm/v3/pkg/repo"
)

func TestInstallOrUpgradeChart(t *testing.T) {
atomic := true
if !helmParam.Chart.Wait {
atomic = false
}
tmout, err := time.ParseDuration(helmParam.Chart.Timeout)
if err != nil {
t.Log(err)
}
chartSpec := &helmclient.ChartSpec{
ReleaseName: helmParam.Chart.ReleaseName,
ChartName: helmParam.Chart.ChartName,
Namespace: helmParam.Chart.Namespace,
ValuesYaml: helmParam.Chart.ValuesYaml,
Version: helmParam.Chart.Version,
CreateNamespace: false,
DisableHooks: false,
Replace: true,
Wait: helmParam.Chart.Wait,
DependencyUpdate: false,
Timeout: tmout,
GenerateName: false,
NameTemplate: "",
Atomic: atomic,
SkipCRDs: false,
UpgradeCRDs: helmParam.Chart.UpgradeCRDs,
SubNotes: false,
Force: false,
ResetValues: false,
ReuseValues: false,
Recreate: false,
MaxHistory: 0,
CleanupOnFail: false,
DryRun: false,
}

// ctrl := gomock.NewController(t)
// defer ctrl.Finish()

// mockClient := mockhelmclient.NewMockClient(ctrl)
// if mockClient == nil {
// t.Fail()
// }
h, err := NewHelm(helmParam, WithChartSpec(chartSpec), WithClient(&DefaultMockClient{}))

if err != nil {
t.Errorf("error: %v\n", err)
}
// mockClient.EXPECT().InstallOrUpgradeChart(context.TODO(), chartSpec).Return(&mockedRelease, nil)

err = h.InstallOrUpgradeChart()
if err != nil {
t.Error(err)
}
}

func TestAddOrUpdateChartRepo(t *testing.T) {
entry := &repo.Entry{
Name: helmParam.Repo.Name,
URL: helmParam.Repo.URL,
Username: "",
Password: "",
CertFile: "",
KeyFile: "",
CAFile: "",
InsecureSkipTLSverify: false,
PassCredentialsAll: false,
}
atomic := true
if !helmParam.Chart.Wait {
atomic = false
}
tmout, err := time.ParseDuration(helmParam.Chart.Timeout)
if err != nil {
t.Log(err)
}
chartSpec := &helmclient.ChartSpec{
ReleaseName: helmParam.Chart.ReleaseName,
ChartName: helmParam.Chart.ChartName,
Namespace: helmParam.Chart.Namespace,
ValuesYaml: helmParam.Chart.ValuesYaml,
Version: helmParam.Chart.Version,
CreateNamespace: false,
DisableHooks: false,
Replace: true,
Wait: helmParam.Chart.Wait,
DependencyUpdate: false,
Timeout: tmout,
GenerateName: false,
NameTemplate: "",
Atomic: atomic,
SkipCRDs: false,
UpgradeCRDs: helmParam.Chart.UpgradeCRDs,
SubNotes: false,
Force: false,
ResetValues: false,
ReuseValues: false,
Recreate: false,
MaxHistory: 0,
CleanupOnFail: false,
DryRun: false,
}

h, err := NewHelm(helmParam, WithEntry(entry), WithChartSpec(chartSpec), WithClient(&DefaultMockClient{}))
if err != nil {
t.Errorf("error: %v\n", err)
}

err = h.AddOrUpdateChartRepo(*entry)
if err != nil {
t.Error(err)
}
}

func TestHelm_UninstallHelmChartRelease(t *testing.T) {
atomic := true
if !helmParam.Chart.Wait {
atomic = false
}
tmout, err := time.ParseDuration(helmParam.Chart.Timeout)
if err != nil {
t.Log(err)
}
chartSpec := &helmclient.ChartSpec{
ReleaseName: helmParam.Chart.ReleaseName,
ChartName: helmParam.Chart.ChartName,
Namespace: helmParam.Chart.Namespace,
ValuesYaml: helmParam.Chart.ValuesYaml,
Version: helmParam.Chart.Version,
CreateNamespace: false,
DisableHooks: false,
Replace: true,
Wait: helmParam.Chart.Wait,
DependencyUpdate: false,
Timeout: tmout,
GenerateName: false,
NameTemplate: "",
Atomic: atomic,
SkipCRDs: false,
UpgradeCRDs: helmParam.Chart.UpgradeCRDs,
SubNotes: false,
Force: false,
ResetValues: false,
ReuseValues: false,
Recreate: false,
MaxHistory: 0,
CleanupOnFail: false,
DryRun: false,
}
// base
h, err := NewHelm(helmParam, WithChartSpec(chartSpec), WithClient(&DefaultMockClient3{}))
if err != nil {
t.Errorf("error: %v\n", err)
}

err = h.UninstallHelmChartRelease()
if err != nil {
t.Error(err)
}

// mock error not found
h, err = NewHelm(helmParam, WithChartSpec(chartSpec), WithClient(&DefaultMockClient{}))
if err != nil {
t.Errorf("error: %v\n", err)
}
err = h.UninstallHelmChartRelease()
if err != nil {
t.Error(err)
}

// mock error
h, err = NewHelm(helmParam, WithChartSpec(chartSpec), WithClient(&DefaultMockClient2{}))
if err != nil {
t.Errorf("error: %v\n", err)
}
err = h.UninstallHelmChartRelease()
if err == nil {
t.Error("error not found")
}
if err != NormalError {
t.Errorf("got: %+v\n, want %+v\n", err, NormalError)
}
}
184 changes: 0 additions & 184 deletions pkg/util/helm/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,187 +151,3 @@ func TestNewHelmWithOption(t *testing.T) {
t.Errorf("NewHelm() = \n%+v\n, want \n%+v\n", got, want)
}
}

func TestInstallOrUpgradeChart(t *testing.T) {
atomic := true
if !helmParam.Chart.Wait {
atomic = false
}
tmout, err := time.ParseDuration(helmParam.Chart.Timeout)
if err != nil {
t.Log(err)
}
chartSpec := &helmclient.ChartSpec{
ReleaseName: helmParam.Chart.ReleaseName,
ChartName: helmParam.Chart.ChartName,
Namespace: helmParam.Chart.Namespace,
ValuesYaml: helmParam.Chart.ValuesYaml,
Version: helmParam.Chart.Version,
CreateNamespace: false,
DisableHooks: false,
Replace: true,
Wait: helmParam.Chart.Wait,
DependencyUpdate: false,
Timeout: tmout,
GenerateName: false,
NameTemplate: "",
Atomic: atomic,
SkipCRDs: false,
UpgradeCRDs: helmParam.Chart.UpgradeCRDs,
SubNotes: false,
Force: false,
ResetValues: false,
ReuseValues: false,
Recreate: false,
MaxHistory: 0,
CleanupOnFail: false,
DryRun: false,
}

// ctrl := gomock.NewController(t)
// defer ctrl.Finish()

// mockClient := mockhelmclient.NewMockClient(ctrl)
// if mockClient == nil {
// t.Fail()
// }
h, err := NewHelm(helmParam, WithChartSpec(chartSpec), WithClient(&DefaultMockClient{}))

if err != nil {
t.Errorf("error: %v\n", err)
}
// mockClient.EXPECT().InstallOrUpgradeChart(context.TODO(), chartSpec).Return(&mockedRelease, nil)

err = h.InstallOrUpgradeChart()
if err != nil {
t.Error(err)
}
}

func TestAddOrUpdateChartRepo(t *testing.T) {
entry := &repo.Entry{
Name: helmParam.Repo.Name,
URL: helmParam.Repo.URL,
Username: "",
Password: "",
CertFile: "",
KeyFile: "",
CAFile: "",
InsecureSkipTLSverify: false,
PassCredentialsAll: false,
}
atomic := true
if !helmParam.Chart.Wait {
atomic = false
}
tmout, err := time.ParseDuration(helmParam.Chart.Timeout)
if err != nil {
t.Log(err)
}
chartSpec := &helmclient.ChartSpec{
ReleaseName: helmParam.Chart.ReleaseName,
ChartName: helmParam.Chart.ChartName,
Namespace: helmParam.Chart.Namespace,
ValuesYaml: helmParam.Chart.ValuesYaml,
Version: helmParam.Chart.Version,
CreateNamespace: false,
DisableHooks: false,
Replace: true,
Wait: helmParam.Chart.Wait,
DependencyUpdate: false,
Timeout: tmout,
GenerateName: false,
NameTemplate: "",
Atomic: atomic,
SkipCRDs: false,
UpgradeCRDs: helmParam.Chart.UpgradeCRDs,
SubNotes: false,
Force: false,
ResetValues: false,
ReuseValues: false,
Recreate: false,
MaxHistory: 0,
CleanupOnFail: false,
DryRun: false,
}

h, err := NewHelm(helmParam, WithEntry(entry), WithChartSpec(chartSpec), WithClient(&DefaultMockClient{}))
if err != nil {
t.Errorf("error: %v\n", err)
}

err = h.AddOrUpdateChartRepo(*entry)
if err != nil {
t.Error(err)
}
}

func TestHelm_UninstallHelmChartRelease(t *testing.T) {
atomic := true
if !helmParam.Chart.Wait {
atomic = false
}
tmout, err := time.ParseDuration(helmParam.Chart.Timeout)
if err != nil {
t.Log(err)
}
chartSpec := &helmclient.ChartSpec{
ReleaseName: helmParam.Chart.ReleaseName,
ChartName: helmParam.Chart.ChartName,
Namespace: helmParam.Chart.Namespace,
ValuesYaml: helmParam.Chart.ValuesYaml,
Version: helmParam.Chart.Version,
CreateNamespace: false,
DisableHooks: false,
Replace: true,
Wait: helmParam.Chart.Wait,
DependencyUpdate: false,
Timeout: tmout,
GenerateName: false,
NameTemplate: "",
Atomic: atomic,
SkipCRDs: false,
UpgradeCRDs: helmParam.Chart.UpgradeCRDs,
SubNotes: false,
Force: false,
ResetValues: false,
ReuseValues: false,
Recreate: false,
MaxHistory: 0,
CleanupOnFail: false,
DryRun: false,
}
// base
h, err := NewHelm(helmParam, WithChartSpec(chartSpec), WithClient(&DefaultMockClient3{}))
if err != nil {
t.Errorf("error: %v\n", err)
}

err = h.UninstallHelmChartRelease()
if err != nil {
t.Error(err)
}

// mock error not found
h, err = NewHelm(helmParam, WithChartSpec(chartSpec), WithClient(&DefaultMockClient{}))
if err != nil {
t.Errorf("error: %v\n", err)
}
err = h.UninstallHelmChartRelease()
if err != nil {
t.Error(err)
}

// mock error
h, err = NewHelm(helmParam, WithChartSpec(chartSpec), WithClient(&DefaultMockClient2{}))
if err != nil {
t.Errorf("error: %v\n", err)
}
err = h.UninstallHelmChartRelease()
if err == nil {
t.Error("error not found")
}
if err != NormalError {
t.Errorf("got: %+v\n, want %+v\n", err, NormalError)
}
}
2 changes: 1 addition & 1 deletion pkg/util/mapz/map.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package mapz

func FillMapWithStrAndError(keys []string, value error) map[string]error {
retMap := make(map[string]error)
retMap := make(map[string]error, len(keys))
if len(keys) == 0 {
return retMap
}
Expand Down
Loading