|
| 1 | +package verify |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/hashicorp/go-version" |
| 13 | + "github.com/hashicorp/hc-install/product" |
| 14 | + "github.com/hashicorp/hc-install/releases" |
| 15 | + "github.com/hashicorp/hc-install/src" |
| 16 | + "github.com/hashicorp/terraform-exec/tfexec" |
| 17 | + "github.com/stretchr/testify/require" |
| 18 | +) |
| 19 | + |
| 20 | +type WorkingExecutable struct { |
| 21 | + Executable |
| 22 | + WorkingDir string |
| 23 | + TF *tfexec.Terraform |
| 24 | +} |
| 25 | + |
| 26 | +type Executable struct { |
| 27 | + ExecPath string |
| 28 | + Version string |
| 29 | + DirPath string |
| 30 | + |
| 31 | + ins src.Installable |
| 32 | +} |
| 33 | + |
| 34 | +func (e Executable) WorkingDir(dir string) (WorkingExecutable, error) { |
| 35 | + tf, err := tfexec.NewTerraform(dir, e.ExecPath) |
| 36 | + if err != nil { |
| 37 | + return WorkingExecutable{}, fmt.Errorf("create terraform exec: %w", err) |
| 38 | + } |
| 39 | + |
| 40 | + return WorkingExecutable{ |
| 41 | + Executable: e, |
| 42 | + WorkingDir: dir, |
| 43 | + TF: tf, |
| 44 | + }, nil |
| 45 | +} |
| 46 | + |
| 47 | +//func (e WorkingExecutable) Init(ctx context.Context) error { |
| 48 | +// e.TF.Init(ctx, tfexec.Upgrade(true)) |
| 49 | +//} |
| 50 | + |
| 51 | +// TerraformTestVersions returns a list of Terraform versions to test. |
| 52 | +func TerraformTestVersions(ctx context.Context) []src.Installable { |
| 53 | + lv := LatestTerraformVersion(ctx) |
| 54 | + return []src.Installable{ |
| 55 | + lv, |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func InstallTerraforms(ctx context.Context, t *testing.T, installables ...src.Installable) []Executable { |
| 60 | + // All terraform versions are installed in the same root directory |
| 61 | + root := t.TempDir() |
| 62 | + execPaths := make([]Executable, 0, len(installables)) |
| 63 | + |
| 64 | + for _, installable := range installables { |
| 65 | + ex := Executable{ |
| 66 | + ins: installable, |
| 67 | + } |
| 68 | + switch tfi := installable.(type) { |
| 69 | + case *releases.ExactVersion: |
| 70 | + ver := tfi.Version.String() |
| 71 | + t.Logf("Installing Terraform %s", ver) |
| 72 | + tfi.InstallDir = filepath.Join(root, ver) |
| 73 | + |
| 74 | + err := os.Mkdir(tfi.InstallDir, 0o755) |
| 75 | + require.NoErrorf(t, err, "tf install %q", ver) |
| 76 | + |
| 77 | + ex.Version = ver |
| 78 | + ex.DirPath = tfi.InstallDir |
| 79 | + case *releases.LatestVersion: |
| 80 | + t.Logf("Installing latest Terraform") |
| 81 | + ver := "latest" |
| 82 | + tfi.InstallDir = filepath.Join(root, ver) |
| 83 | + |
| 84 | + err := os.Mkdir(tfi.InstallDir, 0o755) |
| 85 | + require.NoErrorf(t, err, "tf install %q", ver) |
| 86 | + |
| 87 | + ex.Version = ver |
| 88 | + ex.DirPath = tfi.InstallDir |
| 89 | + default: |
| 90 | + // We only support the types we know about |
| 91 | + t.Fatalf("unknown installable type %T", tfi) |
| 92 | + } |
| 93 | + |
| 94 | + execPath, err := installable.Install(ctx) |
| 95 | + require.NoErrorf(t, err, "tf install") |
| 96 | + ex.ExecPath = execPath |
| 97 | + |
| 98 | + execPaths = append(execPaths, ex) |
| 99 | + } |
| 100 | + |
| 101 | + return execPaths |
| 102 | +} |
| 103 | + |
| 104 | +func LatestTerraformVersion(ctx context.Context) *releases.LatestVersion { |
| 105 | + return &releases.LatestVersion{ |
| 106 | + Product: product.Terraform, |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +// TerraformVersions will return all versions that match the constraints plus the |
| 111 | +// current latest version. |
| 112 | +func TerraformVersions(ctx context.Context, constraints version.Constraints) ([]*releases.ExactVersion, error) { |
| 113 | + if len(constraints) == 0 { |
| 114 | + return nil, fmt.Errorf("no constraints provided, don't fetch everything") |
| 115 | + } |
| 116 | + |
| 117 | + srcs, err := (&releases.Versions{ |
| 118 | + Product: product.Terraform, |
| 119 | + Enterprise: nil, |
| 120 | + Constraints: constraints, |
| 121 | + ListTimeout: time.Second * 60, |
| 122 | + Install: releases.InstallationOptions{ |
| 123 | + Timeout: 0, |
| 124 | + Dir: "", |
| 125 | + SkipChecksumVerification: false, |
| 126 | + }, |
| 127 | + }).List(ctx) |
| 128 | + if err != nil { |
| 129 | + return nil, fmt.Errorf("failed to list Terraform versions: %w", err) |
| 130 | + } |
| 131 | + |
| 132 | + include := make([]*releases.ExactVersion, 0) |
| 133 | + for _, src := range srcs { |
| 134 | + ev, ok := src.(*releases.ExactVersion) |
| 135 | + if !ok { |
| 136 | + return nil, fmt.Errorf("failed to cast src to ExactVersion, type was %T", src) |
| 137 | + } |
| 138 | + |
| 139 | + include = append(include, ev) |
| 140 | + } |
| 141 | + |
| 142 | + return include, nil |
| 143 | +} |
| 144 | + |
| 145 | +func Apply() { |
| 146 | + installer := &releases.ExactVersion{ |
| 147 | + Product: product.Terraform, |
| 148 | + Version: version.Must(version.NewVersion("1.0.6")), |
| 149 | + } |
| 150 | + |
| 151 | + execPath, err := installer.Install(context.Background()) |
| 152 | + if err != nil { |
| 153 | + log.Fatalf("error installing Terraform: %s", err) |
| 154 | + } |
| 155 | + |
| 156 | + workingDir := "/path/to/working/dir" |
| 157 | + tf, err := tfexec.NewTerraform(workingDir, execPath) |
| 158 | + if err != nil { |
| 159 | + log.Fatalf("error running NewTerraform: %s", err) |
| 160 | + } |
| 161 | + |
| 162 | + err = tf.Init(context.Background(), tfexec.Upgrade(true)) |
| 163 | + if err != nil { |
| 164 | + log.Fatalf("error running Init: %s", err) |
| 165 | + } |
| 166 | + |
| 167 | + state, err := tf.Show(context.Background()) |
| 168 | + if err != nil { |
| 169 | + log.Fatalf("error running Show: %s", err) |
| 170 | + } |
| 171 | + |
| 172 | + fmt.Println(state.FormatVersion) // "0.1" |
| 173 | +} |
0 commit comments