From ab0f2d63a25797c06277129fcd88ef7037298305 Mon Sep 17 00:00:00 2001 From: psihachina Date: Mon, 19 Sep 2022 16:17:13 +0300 Subject: [PATCH] added unit tests for the `terraform` package --- internal/terraform/local.go | 20 +-- internal/terraform/terraform_test.go | 232 +++++++++++++++++++++++++++ 2 files changed, 243 insertions(+), 9 deletions(-) create mode 100644 internal/terraform/terraform_test.go diff --git a/internal/terraform/local.go b/internal/terraform/local.go index 3f9b431a..68b41d96 100644 --- a/internal/terraform/local.go +++ b/internal/terraform/local.go @@ -25,6 +25,7 @@ import ( const ( versionPrefix = "terraform_" + defaultMirror = "https://releases.hashicorp.com/terraform" ) type local struct { @@ -64,11 +65,11 @@ func (l *local) Run() error { func (l *local) Prepare() error { var ( - defaultMirror = "https://releases.hashicorp.com/terraform" - path = "" + mirror = defaultMirror + path = "" ) - path, err := installVersion(l.version, &defaultMirror) + path, err := installVersion(l.version, &mirror) if err != nil { return err } @@ -195,7 +196,10 @@ func installVersion(version string, mirrorURL *string) (string, error) { exist := tfswitcher.VersionExist(requestedVersion, tflist) //check if version exist before downloading it if exist { - Install(requestedVersion, *mirrorURL) + err := Install(requestedVersion, *mirrorURL) + if err != nil { + return "", err + } } else { return "", fmt.Errorf("provided terraform version does not exist") } @@ -227,7 +231,7 @@ func Install(tfversion string, mirrorURL string) error { /* if selected version already exists */ if fileExist { - os.Exit(0) + return nil } // if it does not have a slash - append it @@ -243,16 +247,14 @@ func Install(tfversion string, mirrorURL string) error { /* If unable to download file from url, exit(1) immediately */ if errDownload != nil { - fmt.Println(errDownload) - os.Exit(1) + return errDownload } /* unzip the downloaded zipfile */ _, errUnzip := tfswitcher.Unzip(zipFile, installLocation) if errUnzip != nil { fmt.Println("[Error] : Unable to unzip downloaded zip file") - log.Fatal(errUnzip) - os.Exit(1) + return errUnzip } /* rename unzipped file to terraform version name - terraform_x.x.x */ diff --git a/internal/terraform/terraform_test.go b/internal/terraform/terraform_test.go new file mode 100644 index 00000000..da65de5b --- /dev/null +++ b/internal/terraform/terraform_test.go @@ -0,0 +1,232 @@ +package terraform + +import ( + "context" + "fmt" + "github.com/hazelops/ize/internal/config" + "github.com/hazelops/ize/pkg/terminal" + "io" + "os" + "testing" +) + +func TestInstall(t *testing.T) { + type args struct { + tfversion string + mirrorURL string + } + tests := []struct { + name string + args args + wantErr bool + }{ + {name: "success", args: args{ + tfversion: "1.1.9", + mirrorURL: defaultMirror, + }, wantErr: false}, + {name: "success exist", args: args{ + tfversion: "1.1.9", + mirrorURL: defaultMirror, + }, wantErr: false}, + {name: "invalid mirror", args: args{ + tfversion: "1.2.9", + mirrorURL: "invalid.com/mirror", + }, wantErr: true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := Install(tt.args.tfversion, tt.args.mirrorURL); (err != nil) != tt.wantErr { + t.Errorf("Install() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func Test_installVersion(t *testing.T) { + mirror := defaultMirror + hd := home() + + type args struct { + version string + mirrorURL *string + } + tests := []struct { + name string + args args + want string + wantErr bool + }{ + {name: "success", args: args{ + version: "1.1.3", + mirrorURL: &mirror, + }, want: fmt.Sprintf("%s/%s", hd, ".ize/versions/terraform/terraform_1.1.3"), wantErr: false}, + {name: "success exist", args: args{ + version: "1.1.3", + mirrorURL: &mirror, + }, want: fmt.Sprintf("%s/%s", hd, ".ize/versions/terraform/terraform_1.1.3"), wantErr: false}, + {name: "invalid tf version", args: args{ + version: "1.1.X", + mirrorURL: &mirror, + }, wantErr: true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := installVersion(tt.args.version, tt.args.mirrorURL) + if (err != nil) != tt.wantErr { + t.Errorf("installVersion() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("installVersion() got = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_local_Prepare(t *testing.T) { + type fields struct { + version string + command []string + env []string + output io.Writer + tfpath string + project *config.Project + } + tests := []struct { + name string + fields fields + wantErr bool + }{ + {name: "success", fields: fields{ + version: "1.1.3", + command: []string{"version"}, + env: nil, + output: os.Stdout, + tfpath: "", + project: &config.Project{}, + }, wantErr: false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + l := &local{ + version: tt.fields.version, + command: tt.fields.command, + env: tt.fields.env, + output: tt.fields.output, + tfpath: tt.fields.tfpath, + project: tt.fields.project, + } + if err := l.Prepare(); (err != nil) != tt.wantErr { + t.Errorf("Prepare() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func Test_local_Run(t *testing.T) { + mirror := defaultMirror + version, err := installVersion("1.1.3", &mirror) + if err != nil { + t.Error(err) + return + } + + type fields struct { + version string + command []string + env []string + output io.Writer + tfpath string + project *config.Project + } + tests := []struct { + name string + fields fields + wantErr bool + }{ + {name: "success", fields: fields{ + version: "1.1.3", + command: []string{"version"}, + env: nil, + output: os.Stdout, + tfpath: version, + project: &config.Project{}, + }, wantErr: false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + l := &local{ + version: tt.fields.version, + command: tt.fields.command, + env: tt.fields.env, + output: tt.fields.output, + tfpath: tt.fields.tfpath, + project: tt.fields.project, + } + if err := l.Run(); (err != nil) != tt.wantErr { + t.Errorf("Run() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func Test_local_RunUI(t *testing.T) { + mirror := defaultMirror + version, err := installVersion("1.1.3", &mirror) + if err != nil { + t.Error(err) + return + } + + type fields struct { + version string + command []string + env []string + output io.Writer + tfpath string + project *config.Project + } + type args struct { + ui terminal.UI + } + tests := []struct { + name string + fields fields + args args + wantErr bool + }{ + { + name: "success", + fields: fields{ + version: "1.1.3", + command: []string{"version"}, + env: nil, + output: os.Stdout, + tfpath: version, + project: &config.Project{}, + }, + args: args{ui: terminal.ConsoleUI(context.TODO(), true)}, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + l := &local{ + version: tt.fields.version, + command: tt.fields.command, + env: tt.fields.env, + output: tt.fields.output, + tfpath: tt.fields.tfpath, + project: tt.fields.project, + } + if err := l.RunUI(tt.args.ui); (err != nil) != tt.wantErr { + t.Errorf("RunUI() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func home() string { + h, _ := os.UserHomeDir() + return h +}