github.com/evanlouie/fabrikate@v0.17.4/cmd/install_test.go (about) 1 package cmd 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path" 7 "testing" 8 9 "github.com/evanlouie/fabrikate/util" 10 "github.com/evanlouie/fabrikate/yaml" 11 "github.com/google/uuid" 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func TestInstallJSON(t *testing.T) { 16 componentDir := "../testdata/install" 17 cwd, err := os.Getwd() 18 assert.Nil(t, err) 19 defer func() { 20 assert.Nil(t, os.Chdir(cwd)) 21 assert.Nil(t, util.UninstallComponents(componentDir)) 22 }() 23 24 // Change cwd to component directory 25 assert.Nil(t, os.Chdir(componentDir)) 26 assert.Nil(t, Install("./")) 27 28 // Installing again should not cause errors 29 assert.Nil(t, Install("./")) 30 } 31 32 func TestInstallYAML(t *testing.T) { 33 componentDir := "../testdata/install-yaml" 34 cwd, err := os.Getwd() 35 assert.Nil(t, err) 36 defer func() { 37 assert.Nil(t, os.Chdir(cwd)) 38 assert.Nil(t, util.UninstallComponents(componentDir)) 39 }() 40 41 // Change cwd to component directory 42 assert.Nil(t, os.Chdir(componentDir)) 43 assert.Nil(t, Install("./")) 44 45 // Installing again should not cause errors 46 assert.Nil(t, Install("./")) 47 } 48 49 func TestInstallWithHooks(t *testing.T) { 50 componentDir := "../testdata/install-hooks" 51 cwd, err := os.Getwd() 52 assert.Nil(t, err) 53 defer func() { 54 assert.Nil(t, os.Chdir(cwd)) 55 assert.Nil(t, util.UninstallComponents(componentDir)) 56 }() 57 58 // Change cwd to component directory 59 assert.Nil(t, os.Chdir(componentDir)) 60 assert.Nil(t, Install("./")) 61 } 62 63 func TestInstallPrivateComponent(t *testing.T) { 64 componentDir := "../testdata/install-private" 65 cwd, err := os.Getwd() 66 assert.Nil(t, err) 67 defer func() { 68 assert.Nil(t, os.Chdir(cwd)) 69 assert.Nil(t, util.UninstallComponents(componentDir)) 70 }() 71 72 // Change cwd to component directory 73 assert.Nil(t, os.Chdir(componentDir)) 74 75 // Should fail with no environment var set to personal_access_token 76 assert.NotNil(t, Install("./")) 77 assert.Nil(t, os.Chdir("./")) 78 79 // If a personal access token exists, assume its correct and Install should succeed 80 if _, exists := os.LookupEnv("personal_access_token"); exists { 81 assert.Nil(t, Install("./")) 82 } else { 83 assert.NotNil(t, Install("./")) 84 } 85 } 86 87 func TestInstallHelmMethod(t *testing.T) { 88 componentDir := "../testdata/install-helm" 89 cwd, err := os.Getwd() 90 assert.Nil(t, err) 91 defer func() { 92 assert.Nil(t, os.Chdir(cwd)) 93 assert.Nil(t, util.UninstallComponents(componentDir)) 94 }() 95 96 // Change cwd to component directory 97 assert.Nil(t, os.Chdir(componentDir)) 98 assert.Nil(t, Install("./")) 99 100 // Installing again should not cause errors 101 assert.Nil(t, Install("./")) 102 103 // Grafana chart should be version 3.7.0 104 grafanaChartYaml := path.Join("helm_repos", "grafana", "Chart.yaml") 105 grafanaChartBytes, err := ioutil.ReadFile(grafanaChartYaml) 106 assert.Nil(t, err) 107 type helmChart struct { 108 Version string 109 Name string 110 } 111 grafanaChart := helmChart{} 112 assert.Nil(t, yaml.Unmarshal(grafanaChartBytes, &grafanaChart)) 113 assert.EqualValues(t, "grafana", grafanaChart.Name) 114 assert.EqualValues(t, "3.7.0", grafanaChart.Version) 115 } 116 117 // Test to cover https://github.com/evanlouie/fabrikate/issues/261 118 // Tests the calling of Install when the helm client isn't initialized and 119 // attempts to get updates from `http://127.0.0.1:8879/charts` which is fails 120 // in most cases as people do not typically run helm servers locally. 121 func TestInstallWithoutHelmInitialized(t *testing.T) { 122 homeDir, err := os.UserHomeDir() 123 assert.Nil(t, err) 124 helmDir := path.Join(homeDir, ".helm") 125 126 if _, err := os.Stat(helmDir); !os.IsNotExist(err) { 127 // Move helm dir to a temporary location to simulate uninitialized helm client 128 randomTmpName, err := uuid.NewRandom() 129 assert.Nil(t, err) 130 tmpDir := path.Join(homeDir, randomTmpName.String()) 131 assert.Nil(t, os.Rename(helmDir, tmpDir)) 132 133 // Ensure it is moved back to normal 134 defer func() { 135 assert.Nil(t, os.RemoveAll(helmDir)) 136 assert.Nil(t, os.Rename(tmpDir, helmDir)) 137 }() 138 } 139 140 componentDir := "../testdata/install-helm-fix-261-dep-update-bug" 141 cwd, err := os.Getwd() 142 assert.Nil(t, err) 143 defer func() { 144 assert.Nil(t, os.Chdir(cwd)) 145 assert.Nil(t, util.UninstallComponents(componentDir)) 146 }() 147 148 // Change cwd to component directory and install 149 assert.Nil(t, os.Chdir(componentDir)) 150 assert.Nil(t, Install("./")) 151 } 152 153 func TestGenerateHelmRepoAlias(t *testing.T) { 154 componentDir := "../testdata/repo-alias" 155 cwd, err := os.Getwd() 156 assert.Nil(t, err) 157 defer func() { 158 assert.Nil(t, os.Chdir(cwd)) 159 assert.Nil(t, util.UninstallComponents(componentDir)) 160 }() 161 162 // Change cwd to component directory 163 assert.Nil(t, os.Chdir(componentDir)) 164 assert.Nil(t, Install("./")) 165 166 assert.Nil(t, err) 167 }