From fd735b4ab9234f4df14649e17b80293191142116 Mon Sep 17 00:00:00 2001 From: Evan Louie Date: Tue, 3 Nov 2020 10:36:52 -0800 Subject: [PATCH] Fixed linting error (#323) --- cmd/install_test.go | 203 +++++++++++--------------------------------- 1 file changed, 51 insertions(+), 152 deletions(-) diff --git a/cmd/install_test.go b/cmd/install_test.go index f9023fa..c10f264 100644 --- a/cmd/install_test.go +++ b/cmd/install_test.go @@ -1,167 +1,66 @@ package cmd import ( - "io/ioutil" - "os" - "path" "testing" - "github.com/google/uuid" "github.com/microsoft/fabrikate/util" - "github.com/stretchr/testify/assert" - "github.com/timfpark/yaml" ) -func TestInstallJSON(t *testing.T) { - componentDir := "../testdata/install" - cwd, err := os.Getwd() - assert.Nil(t, err) - defer func() { - assert.Nil(t, os.Chdir(cwd)) - assert.Nil(t, util.UninstallComponents(componentDir)) - }() - - // Change cwd to component directory - assert.Nil(t, os.Chdir(componentDir)) - assert.Nil(t, Install("./")) - - // Installing again should not cause errors - assert.Nil(t, Install("./")) -} - -func TestInstallYAML(t *testing.T) { - componentDir := "../testdata/install-yaml" - cwd, err := os.Getwd() - assert.Nil(t, err) - defer func() { - assert.Nil(t, os.Chdir(cwd)) - assert.Nil(t, util.UninstallComponents(componentDir)) - }() - - // Change cwd to component directory - assert.Nil(t, os.Chdir(componentDir)) - assert.Nil(t, Install("./")) - - // Installing again should not cause errors - assert.Nil(t, Install("./")) -} - -func TestInstallWithHooks(t *testing.T) { - componentDir := "../testdata/install-hooks" - cwd, err := os.Getwd() - assert.Nil(t, err) - defer func() { - assert.Nil(t, os.Chdir(cwd)) - assert.Nil(t, util.UninstallComponents(componentDir)) - }() - - // Change cwd to component directory - assert.Nil(t, os.Chdir(componentDir)) - assert.Nil(t, Install("./")) -} - -func TestInstallPrivateComponent(t *testing.T) { - componentDir := "../testdata/install-private" - cwd, err := os.Getwd() - assert.Nil(t, err) - defer func() { - assert.Nil(t, os.Chdir(cwd)) - assert.Nil(t, util.UninstallComponents(componentDir)) - }() - - // Change cwd to component directory - assert.Nil(t, os.Chdir(componentDir)) - - // Should fail with no environment var set to personal_access_token - assert.NotNil(t, Install("./")) - assert.Nil(t, os.Chdir("./")) - - // If a personal access token exists, assume its correct and Install should succeed - if _, exists := os.LookupEnv("personal_access_token"); exists { - assert.Nil(t, Install("./")) - } else { - assert.NotNil(t, Install("./")) +func TestInstall(t *testing.T) { + type args struct { + path string } -} - -func TestInstallHelmMethod(t *testing.T) { - componentDir := "../testdata/install-helm" - cwd, err := os.Getwd() - assert.Nil(t, err) - defer func() { - assert.Nil(t, os.Chdir(cwd)) - assert.Nil(t, util.UninstallComponents(componentDir)) - }() - - // Change cwd to component directory - assert.Nil(t, os.Chdir(componentDir)) - assert.Nil(t, Install("./")) - - // Installing again should not cause errors - assert.Nil(t, Install("./")) - - // Grafana chart should be version 3.7.0 - grafanaChartYaml := path.Join("helm_repos", "grafana", "Chart.yaml") - grafanaChartBytes, err := ioutil.ReadFile(grafanaChartYaml) - assert.Nil(t, err) - type helmChart struct { - Version string - Name string + tests := []struct { + name string + args args + wantErr bool + }{ + { + "json", + args{"../testdata/install"}, + false, + }, + + { + "yaml", + args{"../testdata/install-yaml"}, + false, + }, + + { + "hooks", + args{"../testdata/install-hooks"}, + false, + }, + + { + "private git", + args{"../testdata/install-private"}, + true, + }, + + { + "helm", + args{"../testdata/install-helm"}, + false, + }, + + { + "repo-alias", + args{"../testdata/repo-alias"}, + false, + }, } - grafanaChart := helmChart{} - assert.Nil(t, yaml.Unmarshal(grafanaChartBytes, &grafanaChart)) - assert.EqualValues(t, "grafana", grafanaChart.Name) - assert.EqualValues(t, "3.7.0", grafanaChart.Version) -} - -// Test to cover https://github.com/microsoft/fabrikate/issues/261 -// Tests the calling of Install when the helm client isn't initialized and -// attempts to get updates from `http://127.0.0.1:8879/charts` which is fails -// in most cases as people do not typically run helm servers locally. -func TestInstallWithoutHelmInitialized(t *testing.T) { - homeDir, err := os.UserHomeDir() - assert.Nil(t, err) - helmDir := path.Join(homeDir, ".helm") - - if _, err := os.Stat(helmDir); !os.IsNotExist(err) { - // Move helm dir to a temporary location to simulate uninitialized helm client - randomTmpName, err := uuid.NewRandom() - assert.Nil(t, err) - tmpDir := path.Join(homeDir, randomTmpName.String()) - assert.Nil(t, os.Rename(helmDir, tmpDir)) - // Ensure it is moved back to normal + for _, tt := range tests { defer func() { - assert.Nil(t, os.RemoveAll(helmDir)) - assert.Nil(t, os.Rename(tmpDir, helmDir)) + _ = util.UninstallComponents(tt.args.path) }() - } - - componentDir := "../testdata/install-helm-fix-261-dep-update-bug" - cwd, err := os.Getwd() - assert.Nil(t, err) - defer func() { - assert.Nil(t, os.Chdir(cwd)) - assert.Nil(t, util.UninstallComponents(componentDir)) - }() - - // Change cwd to component directory and install - assert.Nil(t, os.Chdir(componentDir)) - assert.Nil(t, Install("./")) -} -func TestGenerateHelmRepoAlias(t *testing.T) { - componentDir := "../testdata/repo-alias" - cwd, err := os.Getwd() - assert.Nil(t, err) - defer func() { - assert.Nil(t, os.Chdir(cwd)) - assert.Nil(t, util.UninstallComponents(componentDir)) - }() - - // Change cwd to component directory - assert.Nil(t, os.Chdir(componentDir)) - assert.Nil(t, Install("./")) - - assert.Nil(t, err) + t.Run(tt.name, func(t *testing.T) { + if err := Install(tt.args.path); (err != nil) != tt.wantErr { + t.Errorf("Install() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } }