github.com/aleksanderaleksic/tgmigrate@v0.1.7/config/config_test.go (about)

     1  package config
     2  
     3  import (
     4  	"flag"
     5  	"github.com/aleksanderaleksic/tgmigrate/test"
     6  	"github.com/stretchr/testify/assert"
     7  	"github.com/urfave/cli/v2"
     8  	"os"
     9  	"path/filepath"
    10  	"testing"
    11  )
    12  
    13  func getContextWithConfigFIle(path string) *cli.Context {
    14  	app := cli.NewApp()
    15  
    16  	set := flag.NewFlagSet("apply", 0)
    17  	set.String("config", path, "")
    18  
    19  	return cli.NewContext(app, set, nil)
    20  }
    21  
    22  func TestReadNonExistentConfigFile(t *testing.T) {
    23  	ass := assert.New(t)
    24  	testDir := t.TempDir()
    25  	defer os.RemoveAll(testDir)
    26  	ctx := getContextWithConfigFIle(filepath.Join(testDir, ".tgmigrate.hcl"))
    27  
    28  	c, err := GetConfigFile(ctx)
    29  	ass.Nil(c)
    30  	ass.NotNil(err)
    31  }
    32  
    33  func TestHandleErrorParsingConfig(t *testing.T) {
    34  	ass := assert.New(t)
    35  	testDir := t.TempDir()
    36  	defer os.RemoveAll(testDir)
    37  
    38  	confFilePath := filepath.Join(testDir, ".tgmigrate.hcl")
    39  	test.TestFile(t, confFilePath, `
    40  migration {}
    41  `)
    42  	ctx := getContextWithConfigFIle(confFilePath)
    43  
    44  	c, err := GetConfigFile(ctx)
    45  	ass.Nil(c)
    46  	ass.NotNil(err)
    47  }
    48  
    49  func TestHandleErrorParsingInvalidHistoryStorageConfig(t *testing.T) {
    50  	ass := assert.New(t)
    51  	testDir := t.TempDir()
    52  	defer os.RemoveAll(testDir)
    53  
    54  	confFilePath := filepath.Join(testDir, ".tgmigrate.hcl")
    55  	test.TestFile(t, confFilePath, `
    56  migration {
    57    migration = "./migrations"
    58  
    59    history {
    60      storage "s3" {
    61        region = "us-east-1"
    62        assume_role = "arn:aws:iam::12345678:role/history"
    63        key = "history.json"
    64      }
    65    }
    66  
    67    state "s3" {
    68      bucket = "test-state-bucket"
    69      region = "us-east-2"
    70      assume_role = "arn:aws:iam::12345678:role/state"
    71    }
    72  }
    73  `)
    74  	ctx := getContextWithConfigFIle(confFilePath)
    75  
    76  	c, err := GetConfigFile(ctx)
    77  	ass.Nil(c)
    78  	ass.NotNil(err)
    79  }
    80  
    81  func TestHandleErrorParsingInvalidStateStorageConfig(t *testing.T) {
    82  	ass := assert.New(t)
    83  	testDir := t.TempDir()
    84  	defer os.RemoveAll(testDir)
    85  
    86  	confFilePath := filepath.Join(testDir, ".tgmigrate.hcl")
    87  	test.TestFile(t, confFilePath, `
    88  migration {
    89    migration = "./migrations"
    90  
    91    history {
    92      storage "s3" {
    93        bucket = "test-history-bucket"
    94        region = "us-east-1"
    95        assume_role = "arn:aws:iam::12345678:role/history"
    96        key = "history.json"
    97      }
    98    }
    99  
   100    state "s3" {
   101      region = "us-east-2"
   102      assume_role = "arn:aws:iam::12345678:role/state"
   103    }
   104  }
   105  `)
   106  	ctx := getContextWithConfigFIle(confFilePath)
   107  
   108  	c, err := GetConfigFile(ctx)
   109  	ass.Nil(c)
   110  	ass.NotNil(err)
   111  }
   112  
   113  func TestHandleErrorFindingConfigInParentFolder(t *testing.T) {
   114  	ass := assert.New(t)
   115  	testDir := t.TempDir()
   116  	defer os.RemoveAll(testDir)
   117  
   118  	app := cli.NewApp()
   119  	set := flag.NewFlagSet("apply", 0)
   120  	ctx := cli.NewContext(app, set, nil)
   121  
   122  	c, err := GetConfigFile(ctx)
   123  	ass.Nil(c)
   124  	ass.NotNil(err)
   125  }
   126  
   127  func TestFindingConfigInParentFolder(t *testing.T) {
   128  	ass := assert.New(t)
   129  	testDir := t.TempDir()
   130  	confFilePath := filepath.Join("../", ".tgmigrate.hcl")
   131  	defer os.RemoveAll(testDir)
   132  	defer os.RemoveAll(confFilePath)
   133  
   134  	err := os.Chdir(testDir)
   135  	if err != nil {
   136  		t.Fatal("Failed to change directory for test")
   137  	}
   138  
   139  	test.TestFile(t, confFilePath, `
   140  migration {
   141    migration = "./migrations"
   142  
   143    history {
   144      storage "s3" {
   145        bucket = "test-history-bucket"
   146        region = "us-east-1"
   147        assume_role = "arn:aws:iam::12345678:role/history"
   148        key = "history.json"
   149      }
   150    }
   151  
   152    state "s3" {
   153      bucket = "test-state-bucket"
   154      region = "us-east-2"
   155      assume_role = "arn:aws:iam::12345678:role/state"
   156    }
   157  }
   158  `)
   159  
   160  	app := cli.NewApp()
   161  	set := flag.NewFlagSet("apply", 0)
   162  	ctx := cli.NewContext(app, set, nil)
   163  
   164  	c, err := GetConfigFile(ctx)
   165  	ass.Nil(err)
   166  	ass.NotNil(c)
   167  }
   168  
   169  func TestReadConfig(t *testing.T) {
   170  	ass := assert.New(t)
   171  	testDir := t.TempDir()
   172  	defer os.RemoveAll(testDir)
   173  
   174  	confFilePath := filepath.Join(testDir, ".tgmigrate.hcl")
   175  	test.TestFile(t, confFilePath, `
   176  migration {
   177    migration = "./migrations"
   178  
   179    history {
   180      storage "s3" {
   181        bucket = "test-history-bucket"
   182        region = "us-east-1"
   183        assume_role = "arn:aws:iam::12345678:role/history"
   184        key = "history.json"
   185      }
   186    }
   187  
   188    state "s3" {
   189      bucket = "test-state-bucket"
   190      region = "us-east-2"
   191      assume_role = "arn:aws:iam::12345678:role/state"
   192    }
   193  }
   194  `)
   195  
   196  	ctx := getContextWithConfigFIle(confFilePath)
   197  
   198  	c, err := GetConfigFile(ctx)
   199  	if err != nil {
   200  		t.Errorf("error getting config file %s", err)
   201  	}
   202  
   203  	ass.Equal(c.Path, confFilePath)
   204  	ass.Equal(c.MigrationDir, "./migrations")
   205  	ass.Equal(c.AbsoluteMigrationDir, filepath.Join(testDir, "migrations"))
   206  
   207  	stateAssumeRoleArn := "arn:aws:iam::12345678:role/state"
   208  	ass.Equal(c.State, State{
   209  		Type: "s3",
   210  		Config: &S3StateConfig{
   211  			Bucket:        "test-state-bucket",
   212  			Region:        "us-east-2",
   213  			StateFileName: nil,
   214  			AssumeRole:    &stateAssumeRoleArn,
   215  		},
   216  	})
   217  
   218  	historyAssumeRoleArn := "arn:aws:iam::12345678:role/history"
   219  	ass.Equal(c.History, History{
   220  		Storage: HistoryStorage{
   221  			Type: "s3",
   222  			Config: &S3HistoryStorageConfig{
   223  				Bucket:     "test-history-bucket",
   224  				Region:     "us-east-1",
   225  				Key:        "history.json",
   226  				AssumeRole: &historyAssumeRoleArn,
   227  			},
   228  		}})
   229  }
   230  
   231  func TestReadConfigWithVariables(t *testing.T) {
   232  	ass := assert.New(t)
   233  	testDir := t.TempDir()
   234  	defer os.RemoveAll(testDir)
   235  
   236  	confFilePath := filepath.Join(testDir, ".tgmigrate.hcl")
   237  	test.TestFile(t, confFilePath, `
   238  migration {
   239    migration = "./migrations"
   240  
   241    history {
   242      storage "s3" {
   243        bucket = "${BUCKET}"
   244        region = "${REGION}"
   245        assume_role = "${ASSUME_ROLE}"
   246        key = "history.json"
   247      }
   248    }
   249  
   250    state "s3" {
   251      bucket = "${BUCKET}"
   252      region = "${REGION}"
   253      assume_role = "${ASSUME_ROLE}"
   254    }
   255  }
   256  `)
   257  	app := cli.NewApp()
   258  
   259  	set := flag.NewFlagSet("apply", 0)
   260  	set.String("config", confFilePath, "")
   261  	set.String("cv", "BUCKET=test-bucket;REGION=us-east-1;ASSUME_ROLE=arn:aws:iam::12345678:role/test", "")
   262  
   263  	ctx := cli.NewContext(app, set, nil)
   264  
   265  	c, err := GetConfigFile(ctx)
   266  	if err != nil {
   267  		t.Errorf("error getting config file %s", err)
   268  		t.FailNow()
   269  	}
   270  
   271  	ass.Equal(c.Path, confFilePath)
   272  	ass.Equal(c.MigrationDir, "./migrations")
   273  	ass.Equal(c.AbsoluteMigrationDir, filepath.Join(testDir, "migrations"))
   274  
   275  	assumeRoleArn := "arn:aws:iam::12345678:role/test"
   276  	ass.Equal(c.State, State{
   277  		Type: "s3",
   278  		Config: &S3StateConfig{
   279  			Bucket:        "test-bucket",
   280  			Region:        "us-east-1",
   281  			StateFileName: nil,
   282  			AssumeRole:    &assumeRoleArn,
   283  		},
   284  	})
   285  
   286  	ass.Equal(c.History, History{
   287  		Storage: HistoryStorage{
   288  			Type: "s3",
   289  			Config: &S3HistoryStorageConfig{
   290  				Bucket:     "test-bucket",
   291  				Region:     "us-east-1",
   292  				Key:        "history.json",
   293  				AssumeRole: &assumeRoleArn,
   294  			},
   295  		}})
   296  }