github.com/leg100/ots@v0.0.7-0.20210919080622-034055ced4bd/e2e/e2e_test.go (about)

     1  package e2e
     2  
     3  import (
     4  	"os"
     5  	"os/exec"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  const (
    13  	daemon = "../_build/otsd"
    14  	client = "../_build/ots"
    15  	config = `
    16  terraform {
    17    backend "remote" {
    18      hostname = "localhost:8080"
    19      organization = "automatize"
    20  
    21      workspaces {
    22        name = "dev"
    23      }
    24    }
    25  }
    26  
    27  resource "null_resource" "e2e" {}
    28  `
    29  )
    30  
    31  func TestOTS(t *testing.T) {
    32  	// Create TF config
    33  	root := t.TempDir()
    34  	require.NoError(t, os.WriteFile(filepath.Join(root, "main.tf"), []byte(config), 0600))
    35  
    36  	t.Run("login", func(t *testing.T) {
    37  		cmd := exec.Command(client, "login")
    38  		out, err := cmd.CombinedOutput()
    39  		t.Log(string(out))
    40  		require.NoError(t, err)
    41  	})
    42  
    43  	t.Run("create organization", func(t *testing.T) {
    44  		cmd := exec.Command(client, "organizations", "new", "automatize", "--email", "e2e@automatize.co")
    45  		out, err := cmd.CombinedOutput()
    46  		t.Log(string(out))
    47  		require.NoError(t, err)
    48  	})
    49  
    50  	t.Run("terraform init", func(t *testing.T) {
    51  		chdir(t, root)
    52  		cmd := exec.Command("terraform", "init", "-no-color")
    53  		out, err := cmd.CombinedOutput()
    54  		t.Log(string(out))
    55  		require.NoError(t, err)
    56  	})
    57  
    58  	t.Run("terraform plan", func(t *testing.T) {
    59  		chdir(t, root)
    60  		cmd := exec.Command("terraform", "plan", "-no-color")
    61  		out, err := cmd.CombinedOutput()
    62  		t.Log(string(out))
    63  		require.NoError(t, err)
    64  	})
    65  
    66  	t.Run("terraform apply", func(t *testing.T) {
    67  		chdir(t, root)
    68  		cmd := exec.Command("terraform", "apply", "-no-color", "-auto-approve")
    69  		out, err := cmd.CombinedOutput()
    70  		t.Log(string(out))
    71  		require.NoError(t, err)
    72  	})
    73  
    74  	t.Run("lock workspace", func(t *testing.T) {
    75  		cmd := exec.Command(client, "workspaces", "lock", "dev", "--organization", "automatize")
    76  		out, err := cmd.CombinedOutput()
    77  		t.Log(string(out))
    78  		require.NoError(t, err)
    79  	})
    80  
    81  	t.Run("unlock workspace", func(t *testing.T) {
    82  		cmd := exec.Command(client, "workspaces", "unlock", "dev", "--organization", "automatize")
    83  		out, err := cmd.CombinedOutput()
    84  		t.Log(string(out))
    85  		require.NoError(t, err)
    86  	})
    87  }
    88  
    89  // Chdir changes current directory to this temp directory.
    90  func chdir(t *testing.T, dir string) {
    91  	pwd, err := os.Getwd()
    92  	if err != nil {
    93  		t.Fatal("unable to get current directory")
    94  	}
    95  
    96  	t.Cleanup(func() {
    97  		if err := os.Chdir(pwd); err != nil {
    98  			t.Fatal("unable to reset current directory")
    99  		}
   100  	})
   101  
   102  	if err := os.Chdir(dir); err != nil {
   103  		t.Fatal("unable to change current directory")
   104  	}
   105  }