Skip to content
This repository has been archived by the owner on Feb 15, 2022. It is now read-only.

Commit

Permalink
Fixed linting error (#323)
Browse files Browse the repository at this point in the history
  • Loading branch information
evanlouie authored Nov 3, 2020
1 parent a9d2832 commit fd735b4
Showing 1 changed file with 51 additions and 152 deletions.
203 changes: 51 additions & 152 deletions cmd/install_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}

0 comments on commit fd735b4

Please sign in to comment.