github.com/emicklei/gmig@v1.18.3-0.20240405210147-57c940a1c6cf/migration_test.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"io/ioutil"
     6  	"log"
     7  	"os"
     8  	"os/exec"
     9  	"path/filepath"
    10  	"testing"
    11  
    12  	"github.com/urfave/cli"
    13  	"gopkg.in/yaml.v2"
    14  )
    15  
    16  var one = `
    17  do:
    18  - going up
    19  # comment for down
    20  undo:
    21  - going down
    22  `
    23  
    24  func TestParseMigration(t *testing.T) {
    25  	var m Migration
    26  	if err := yaml.Unmarshal([]byte(one), &m); err != nil {
    27  		t.Error(err)
    28  	}
    29  	if got, want := m.DoSection[0], "going up"; got != want {
    30  		t.Errorf("got [%v] want [%v]", got, want)
    31  	}
    32  	if got, want := m.UndoSection[0], "going down"; got != want {
    33  		t.Errorf("got [%v] want [%v]", got, want)
    34  	}
    35  }
    36  
    37  // gcloud config list --format json
    38  func readConfig() Config {
    39  	type gcloudconfig struct {
    40  		Compute struct {
    41  			Region, Zone string
    42  		}
    43  		Core struct {
    44  			Project string
    45  		}
    46  	}
    47  	var gc gcloudconfig
    48  	cmd := exec.Command("gcloud", "config", "list", "--format", "json")
    49  	out, _ := runCommand(cmd)
    50  	json.Unmarshal(out, &gc)
    51  	return Config{
    52  		Project: gc.Core.Project,
    53  		Region:  gc.Compute.Region,
    54  		Zone:    gc.Compute.Zone,
    55  	}
    56  }
    57  
    58  func TestSetupShellScriptNotVerbose(t *testing.T) {
    59  
    60  	want := `#!/bin/bash
    61  # temporary gmig execution script
    62  set -e -v`
    63  
    64  	if got := setupShellScript(false); got != want {
    65  		t.Errorf("got [%v] want [%v]", got, want)
    66  	}
    67  }
    68  
    69  func TestSetupShellScriptVerbose(t *testing.T) {
    70  
    71  	want := `#!/bin/bash
    72  # temporary gmig execution script
    73  set -e -x`
    74  
    75  	if got := setupShellScript(true); got != want {
    76  		t.Errorf("got [%v] want [%v]", got, want)
    77  	}
    78  }
    79  
    80  func TestNewFilenameWithIndex(t *testing.T) {
    81  	wd, _ := os.Getwd()
    82  	dir, err := ioutil.TempDir("", "testing")
    83  	if err != nil {
    84  		log.Fatal(err)
    85  	}
    86  	defer os.RemoveAll(dir) // clean up
    87  	// change and restore finally
    88  	if err := os.Chdir(dir); err != nil {
    89  		return
    90  	}
    91  	defer os.Chdir(wd)
    92  	desc := "first migration"
    93  	want := "010_first_migration.yaml"
    94  	if got := NewFilenameWithIndex(desc); got != want {
    95  		t.Errorf("NewFilenameWithIndex(%s) = %v, want %v", desc, got, want)
    96  	}
    97  	tmpfn := filepath.Join(dir, "20181026t183700_starts_with_timestamp.yaml")
    98  	if err := ioutil.WriteFile(tmpfn, []byte(""), 0444); err != nil {
    99  		log.Fatal(err)
   100  	}
   101  	desc = "first after timestamp"
   102  	want = "300_first_after_timestamp.yaml"
   103  	if got := NewFilenameWithIndex(desc); got != want {
   104  		t.Errorf("NewFilenameWithIndex(%s) = %v, want %v", desc, got, want)
   105  	}
   106  	tmpfn = filepath.Join(dir, "400_starts_with_high_index.yaml")
   107  	if err := ioutil.WriteFile(tmpfn, []byte(""), 0444); err != nil {
   108  		log.Fatal(err)
   109  	}
   110  	desc = "first after high index"
   111  	want = "405_first_after_high_index.yaml"
   112  	if got := NewFilenameWithIndex(desc); got != want {
   113  		t.Errorf("NewFilenameWithIndex(%s) = %v, want %v", desc, got, want)
   114  	}
   115  	tmpfn = filepath.Join(dir, "unexpected_yaml_in_directory.yaml")
   116  	if err := ioutil.WriteFile(tmpfn, []byte(""), 0444); err != nil {
   117  		log.Fatal(err)
   118  	}
   119  	desc = "potentially unexpected naming"
   120  	want = "010_potentially_unexpected_naming.yaml"
   121  	if got := NewFilenameWithIndex(desc); got != want {
   122  		t.Errorf("NewFilenameWithIndex(%s) = %v, want %v", desc, got, want)
   123  	}
   124  }
   125  
   126  func TestEvaluateCondition(t *testing.T) {
   127  	envs := []string{"ZONE=A", "PROJECT=B"}
   128  	ok, err := evaluateCondition(`PROJECT == "B"`, envs)
   129  	if err != nil {
   130  		log.Fatal(err)
   131  	}
   132  	if got, want := ok, true; got != want {
   133  		t.Errorf("got [%v] want [%v]", got, want)
   134  	}
   135  }
   136  
   137  func TestMigrationContextWithMigrationsOverride(t *testing.T) {
   138  	t.Skip()
   139  	c := new(cli.Context)
   140  	c.Set("migrations", "/tmp/here")
   141  	ctx, err := getMigrationContext(c)
   142  	if err != nil {
   143  		t.Fatal(err)
   144  	}
   145  	t.Log(ctx)
   146  }